mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 06:10:31 +00:00
WIP server side.
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import express from 'express';
|
||||
import { check, validationResult, oneOf } from 'express-validator';
|
||||
import { difference } from 'lodash';
|
||||
import knex from 'knex';
|
||||
import asyncMiddleware from '../middleware/asyncMiddleware';
|
||||
import Account from '@/models/Account';
|
||||
import '@/models/AccountBalance';
|
||||
import JournalPoster from '@/services/Accounting/JournalPoster';
|
||||
import JournalEntry from '@/services/Accounting/JournalEntry';
|
||||
|
||||
export default {
|
||||
/**
|
||||
@@ -36,7 +36,6 @@ export default {
|
||||
],
|
||||
async handler(req, res) {
|
||||
const validationErrors = validationResult(req);
|
||||
// const defaultCurrency = 'USD';
|
||||
|
||||
if (!validationErrors.isEmpty()) {
|
||||
return res.boom.badData(null, {
|
||||
@@ -45,16 +44,13 @@ export default {
|
||||
}
|
||||
|
||||
const { accounts } = req.body;
|
||||
|
||||
const accountsIds = accounts.map((account) => account.id);
|
||||
const accountsCollection = await Account.query((query) => {
|
||||
query.select(['id']);
|
||||
query.whereIn('id', accountsIds);
|
||||
}).fetchAll({
|
||||
withRelated: ['balances'],
|
||||
});
|
||||
const accountsCollection = await Account.query()
|
||||
.select(['id'])
|
||||
.whereIn('id', accountsIds);
|
||||
|
||||
const accountsStoredIds = accountsCollection.map((account) => account.attributes.id);
|
||||
// Get the stored accounts Ids and difference with submit accounts.
|
||||
const accountsStoredIds = accountsCollection.map((account) => account.id);
|
||||
const notFoundAccountsIds = difference(accountsIds, accountsStoredIds);
|
||||
const errorReasons = [];
|
||||
|
||||
@@ -62,34 +58,35 @@ export default {
|
||||
const ids = notFoundAccountsIds.map((a) => parseInt(a, 10));
|
||||
errorReasons.push({ type: 'NOT_FOUND_ACCOUNT', code: 100, ids });
|
||||
}
|
||||
|
||||
if (errorReasons.length > 0) {
|
||||
return res.boom.badData(null, { errors: errorReasons });
|
||||
}
|
||||
|
||||
const storedAccountsBalances = accountsCollection.related('balances');
|
||||
const sharedJournalDetails = new JournalEntry({
|
||||
referenceType: 'OpeningBalance',
|
||||
referenceId: 1,
|
||||
});
|
||||
const journalEntries = new JournalPoster(sharedJournalDetails);
|
||||
|
||||
const submitBalancesMap = new Map(accounts.map((account) => [account, account.id]));
|
||||
const storedBalancesMap = new Map(storedAccountsBalances.map((balance) => [
|
||||
balance.attributes, balance.attributes.id,
|
||||
]));
|
||||
accounts.forEach((account) => {
|
||||
const entry = new JournalEntry({
|
||||
account: account.id,
|
||||
accountNormal: account.type.normal,
|
||||
});
|
||||
|
||||
// const updatedStoredBalanced = [];
|
||||
const notStoredBalances = [];
|
||||
|
||||
accountsIds.forEach((id) => {
|
||||
if (!storedBalancesMap.get(id)) {
|
||||
notStoredBalances.push(id);
|
||||
if (account.credit) {
|
||||
entry.credit = account.credit;
|
||||
journalEntries.credit(entry);
|
||||
} else if (account.debit) {
|
||||
entry.debit = account.debit;
|
||||
journalEntries.debit(entry);
|
||||
}
|
||||
});
|
||||
|
||||
await knex('accounts_balances').insert([
|
||||
...notStoredBalances.map((id) => {
|
||||
const account = submitBalancesMap.get(id);
|
||||
return { ...account };
|
||||
}),
|
||||
await Promise.all([
|
||||
journalEntries.saveEntries(),
|
||||
journalEntries.saveBalance(),
|
||||
]);
|
||||
|
||||
return res.status(200).send();
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user