164 lines
4.1 KiB
JavaScript
164 lines
4.1 KiB
JavaScript
import { requestCallback, request } from '../lib/request'
|
|
import { renderDate, renderCategory, renderBankAccount, renderEuro, renderLabelWithSum } from '../lib/renderers'
|
|
|
|
const endpoints = {
|
|
list: '/api/transaction',
|
|
items: '/api/transactions',
|
|
updateCategories: '/api/transactions/update_categories',
|
|
}
|
|
|
|
const getList = async (query, callback) => {
|
|
return requestCallback(`${endpoints.list}?${new URLSearchParams(query)}`, {}, callback ?? (() => true))
|
|
}
|
|
|
|
const getOne = async (query, callback) => {
|
|
return request(`${endpoints.list}?${new URLSearchParams(query)}`, {})
|
|
.then((data) => data.rows[0] ?? null)
|
|
.then(callback ?? (() => true))
|
|
}
|
|
|
|
const importFile = async (data, callback, callbackError) => {
|
|
return requestCallback(
|
|
endpoints.items,
|
|
{
|
|
method: 'POST',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
},
|
|
body: createPayload(data),
|
|
},
|
|
callback ?? (() => true),
|
|
).catch(callbackError ?? (() => true))
|
|
}
|
|
|
|
const createPayload = (data) => {
|
|
const payload = new FormData()
|
|
|
|
payload.append('bank_account_id', data.bank_account_id)
|
|
payload.append('file', data.file)
|
|
payload.append('format', data.format)
|
|
|
|
return payload
|
|
}
|
|
|
|
const createForm = (accounts) => {
|
|
const data = { category_id: null, file: null, format: 'caisse_epargne' }
|
|
|
|
const options = []
|
|
|
|
accounts.forEach((item) => {
|
|
options.push({
|
|
value: item.id,
|
|
text: item.label,
|
|
})
|
|
})
|
|
|
|
return {
|
|
data: data,
|
|
error: null,
|
|
fields: [
|
|
{
|
|
label: 'Compte bancaire',
|
|
widget: 'select',
|
|
options: options,
|
|
required: true,
|
|
key: 'bank_account_id',
|
|
},
|
|
{
|
|
label: 'Format de fichier',
|
|
key: 'format',
|
|
widget: 'select',
|
|
options: [
|
|
{ value: 'caisse_epargne', text: "Caisse d'épargne" },
|
|
{ value: 'banque_populaire', text: "Banque Populaire" },
|
|
{ value: 'revolut', text: 'Revolut' },
|
|
],
|
|
},
|
|
{
|
|
label: 'Fichier',
|
|
description: `Fichier CSV des opérations`,
|
|
widget: 'file',
|
|
required: true,
|
|
key: 'file',
|
|
},
|
|
],
|
|
}
|
|
}
|
|
|
|
const updateCategories = async (callback, callbackError) => {
|
|
return requestCallback(
|
|
`${endpoints.updateCategories}`,
|
|
{
|
|
method: 'POST',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
},
|
|
callback ?? (() => true),
|
|
).catch(callbackError ?? (() => true))
|
|
}
|
|
|
|
const getListFields = () => {
|
|
return [
|
|
{
|
|
key: 'date',
|
|
label: 'Date',
|
|
width: '90px',
|
|
render: (item) => renderDate(item.date),
|
|
},
|
|
{
|
|
key: 'label',
|
|
label: 'Libellé',
|
|
},
|
|
{
|
|
key: 'operation_type',
|
|
width: '200px',
|
|
label: 'Type',
|
|
},
|
|
{
|
|
key: 'category',
|
|
label: 'Catégorie',
|
|
orderKey: 'category_id',
|
|
width: '400px',
|
|
render: (item) => renderCategory(item.category),
|
|
},
|
|
{
|
|
key: 'bank_account',
|
|
label: 'Compte',
|
|
width: '200px',
|
|
orderKey: 'bank_account_id',
|
|
render: (item) => renderBankAccount(item.bank_account),
|
|
},
|
|
{
|
|
key: 'debit',
|
|
renderLabel: (rows) => renderLabelWithSum('Débit', rows, 'debit'),
|
|
width: '120px',
|
|
thClasses: ['text-end'],
|
|
tdClasses: ['text-end'],
|
|
render: (item) => renderEuro(item.debit),
|
|
},
|
|
{
|
|
key: 'credit',
|
|
renderLabel: (rows) => renderLabelWithSum('Crédit', rows, 'credit'),
|
|
width: '120px',
|
|
thClasses: ['text-end'],
|
|
tdClasses: ['text-end'],
|
|
render: (item) => renderEuro(item.credit),
|
|
},
|
|
]
|
|
}
|
|
|
|
const getListFilters = () => {
|
|
return [
|
|
{ key: 'label', type: 'string', label: 'Libellé' },
|
|
{ key: 'debit', type: 'number', label: 'Débit' },
|
|
{ key: 'credit', type: 'number', label: 'Crédit' },
|
|
{ key: 'date', type: 'date', label: 'Date' },
|
|
{ key: 'operation_type', type: 'string', label: 'Type' },
|
|
{ key: 'category_id', type: 'select', label: 'Catégorie', options: [] },
|
|
{ key: 'bank_account_id', type: 'select', label: 'Compte', options: [] },
|
|
]
|
|
}
|
|
|
|
export { endpoints, getList, getOne, createForm, getListFields, getListFilters, importFile, updateCategories }
|