fix: validate payment made entries ids exists.

fix: retrieve payment made and receive details.
This commit is contained in:
Ahmed Bouhuolia
2020-11-04 00:23:58 +02:00
parent 083a2dfbc5
commit 1738a333c7
6 changed files with 132 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
import { Inject, Service } from 'typedi';
import { entries, omit, sumBy, difference } from 'lodash';
import { omit, sumBy, difference } from 'lodash';
import {
EventDispatcher,
EventDispatcherInterface,
@@ -488,27 +488,46 @@ export default class BillPaymentsService {
* @param {number} billPaymentId - The bill payment id.
* @return {object}
*/
public async getBillPayment(tenantId: number, billPaymentId: number) {
public async getBillPayment(tenantId: number, billPaymentId: number): Promise<{
billPayment: IBillPayment,
payableBills: IBill[],
paymentMadeBills: IBill[],
}> {
const { BillPayment, Bill } = this.tenancy.models(tenantId);
const billPayment = await BillPayment.query()
.findById(billPaymentId)
.withGraphFetched('entries')
.withGraphFetched('entries.bill')
.withGraphFetched('vendor')
.withGraphFetched('paymentAccount');
if (!billPayment) {
throw new ServiceError(ERRORS.PAYMENT_MADE_NOT_FOUND);
}
const billsIds = billPayment.entries.map((entry) => entry.billId);
const payableBills = await Bill.query().onBuild((builder) => {
const billsIds = billPayment.entries.map((entry) => entry.billId);
// Retrieve all payable bills that assocaited to the payment made transaction.
const payableBills = await Bill.query()
.modify('dueBills')
.whereNotIn('id', billsIds)
.where('vendor_id', billPayment.vendorId)
.orderBy('bill_date', 'ASC');
builder.where('vendor_id', billPayment.vendorId);
builder.orWhereIn('id', billsIds);
builder.orderByRaw(`FIELD(id, ${billsIds.join(', ')}) DESC`);
builder.orderBy('bill_date', 'ASC');
})
return { billPayment, payableBills };
// Retrieve all payment made assocaited bills.
const paymentMadeBills = billPayment.entries.map((entry) => ({
...(entry.bill),
dueAmount: (entry.bill.dueAmount + entry.paymentAmount),
}));
return {
billPayment: {
...billPayment,
entries: billPayment.entries.map((entry) => ({
...omit(entry, ['bill']),
})),
},
payableBills,
paymentMadeBills,
};
}
/**