feat: Edit make journal entries.

This commit is contained in:
Ahmed Bouhuolia
2020-04-08 17:50:35 +02:00
parent 8ba96e7343
commit 4920d5f419
16 changed files with 225 additions and 75 deletions

View File

@@ -25,6 +25,10 @@ export default {
const router = express.Router();
router.use(JWTAuth);
router.get('/manual-journals/:id',
this.getManualJournal.validation,
asyncMiddleware(this.getManualJournal.handler));
router.get('/manual-journals',
this.manualJournals.validation,
asyncMiddleware(this.manualJournals.handler));
@@ -33,7 +37,7 @@ export default {
this.makeJournalEntries.validation,
asyncMiddleware(this.makeJournalEntries.handler));
router.post('/manual-journal/:id',
router.post('/manual-journals/:id',
this.editManualJournal.validation,
asyncMiddleware(this.editManualJournal.handler));
@@ -375,6 +379,26 @@ export default {
journal.loadEntries(transactions);
journal.removeEntries();
entries.forEach((entry) => {
const account = accounts.find((a) => a.id === entry.account_id);
const jouranlEntry = new JournalEntry({
debit: entry.debit,
credit: entry.credit,
account: account.id,
referenceType: 'Journal',
referenceId: manualJournal.id,
accountNormal: account.type.normal,
note: entry.note,
date: formattedDate,
userId: user.id,
});
if (entry.debit) {
journal.debit(jouranlEntry);
} else {
journal.credit(jouranlEntry);
}
});
await Promise.all([
journal.deleteEntries(),
journal.saveEntries(),
@@ -405,8 +429,19 @@ export default {
return res.status(404).send({
errors: [{ type: 'MANUAL.JOURNAL.NOT.FOUND', code: 100 }],
});
}
}
const transactions = await AccountTransaction.query()
.whereIn('reference_type', ['Journal', 'ManualJournal'])
.where('reference_id', manualJournal.id);
return res.status(200).send({
manual_journal: {
...manualJournal.toJSON(),
entries: [
...transactions,
],
},
});
},
},

View File

@@ -16,7 +16,6 @@ describe('routes: /accounts/', () => {
.post('/api/accounts')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
@@ -191,7 +190,6 @@ describe('routes: /accounts/', () => {
});
describe('GET: `/accounts`', () => {
it('Should retrieve accounts resource not found.', async () => {
const res = await request()
.get('/api/accounts')