gemcap-be-common 1.4.117 → 1.4.118

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gemcap-be-common",
3
- "version": "1.4.117",
3
+ "version": "1.4.118",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -776,7 +776,9 @@ class LoanStatementService {
776
776
  const results = await Promise.all(borrowerIds.map(async (borrowerId) => {
777
777
  const borrower = await Borrower_model_1.BorrowerModel.findById(borrowerId).lean();
778
778
  const products = await this.loanChargesService.getLoanProducts(borrowerId);
779
- return await Promise.all(products.map(async (product) => {
779
+ return await Promise.all(products
780
+ .filter(product => !(0, dayjs_1.default)(product.deactivationDate).isBefore((0, dayjs_1.default)(selectedDate)))
781
+ .map(async (product) => {
780
782
  if ((0, dayjs_1.default)(product.deactivationDate).isBefore((0, dayjs_1.default)(selectedDate))) {
781
783
  return null;
782
784
  }
@@ -855,9 +857,8 @@ class LoanStatementService {
855
857
  }));
856
858
  }));
857
859
  return results
858
- .filter((res) => !!res)
859
- .reduce((acc, res) => ([...acc, ...res]), [])
860
- .sort((a, b) => (b.code.toUpperCase() < a.code.toUpperCase() ? 1 : -1));
860
+ .flat()
861
+ .sort((a, b) => a.code.toUpperCase().localeCompare(b.code.toUpperCase()));
861
862
  }
862
863
  async getOutstandingStatementsExcel(borrowerIds, selectedDate, fullMonth) {
863
864
  const results = await this.getOutstandingStatements(borrowerIds, selectedDate, fullMonth);
@@ -831,7 +831,7 @@ export class LoanStatementService {
831
831
  }
832
832
  case ELoanTypes.TERM: {
833
833
  data.termBalance = lastTransaction.balance ?? 0;
834
- break
834
+ break;
835
835
  }
836
836
  }
837
837
  }
@@ -870,92 +870,93 @@ export class LoanStatementService {
870
870
  const results = await Promise.all(borrowerIds.map(async (borrowerId) => {
871
871
  const borrower = await BorrowerModel.findById(borrowerId).lean();
872
872
  const products = await this.loanChargesService.getLoanProducts(borrowerId);
873
- return await Promise.all(products.map(async (product) => {
874
- if (dayjs(product.deactivationDate).isBefore(dayjs(selectedDate))) {
875
- return null;
876
- }
877
- const relevantStatementEndMonth = fullMonth
878
- ? dayjs(selectedDate).utcOffset(0).startOf('month').subtract(1, 'seconds')
879
- : dayjs(selectedDate).utcOffset(0).endOf('month');
880
- const relevantStatementEnd = fullMonth
881
- ? dayjs(selectedDate).utcOffset(0).startOf('month').subtract(1, 'seconds')
882
- : dayjs(selectedDate).utcOffset(0).endOf('day');
883
- const relevantStatementStart = dayjs(selectedDate).utcOffset(0).startOf('month').subtract(1, 'seconds').startOf('month');
884
-
885
- const paymentFilterEnd = dayjs(selectedDate).utcOffset(0).endOf('day');
886
- const paymentFilterStart = relevantStatementStart;
887
-
888
- const lastTransaction = await this.loanTransactionsService.getLastTransactionForDate(product._id.toString(), dayjs(selectedDate).endOf('day').toDate());
889
- let actualBalance = 0;
890
- if (lastTransaction) {
891
- actualBalance = new Decimal(lastTransaction.balance).toDP(2).toNumber();
892
- }
873
+ return await Promise.all(products
874
+ .filter(product => !dayjs(product.deactivationDate).isBefore(dayjs(selectedDate)))
875
+ .map(async (product) => {
876
+ if (dayjs(product.deactivationDate).isBefore(dayjs(selectedDate))) {
877
+ return null;
878
+ }
879
+ const relevantStatementEndMonth = fullMonth
880
+ ? dayjs(selectedDate).utcOffset(0).startOf('month').subtract(1, 'seconds')
881
+ : dayjs(selectedDate).utcOffset(0).endOf('month');
882
+ const relevantStatementEnd = fullMonth
883
+ ? dayjs(selectedDate).utcOffset(0).startOf('month').subtract(1, 'seconds')
884
+ : dayjs(selectedDate).utcOffset(0).endOf('day');
885
+ const relevantStatementStart = dayjs(selectedDate).utcOffset(0).startOf('month').subtract(1, 'seconds').startOf('month');
886
+
887
+ const paymentFilterEnd = dayjs(selectedDate).utcOffset(0).endOf('day');
888
+ const paymentFilterStart = relevantStatementStart;
889
+
890
+ const lastTransaction = await this.loanTransactionsService.getLastTransactionForDate(product._id.toString(), dayjs(selectedDate).endOf('day').toDate());
891
+ let actualBalance = 0;
892
+ if (lastTransaction) {
893
+ actualBalance = new Decimal(lastTransaction.balance).toDP(2).toNumber();
894
+ }
893
895
 
894
- const statementFilter = {
895
- productIds: [product._id.toString()],
896
- start: relevantStatementStart.toDate(),
897
- end: relevantStatementEnd.toDate(),
898
- };
899
- const paymentFilter = {
900
- productIds: [product._id.toString()],
901
- start: paymentFilterStart.toDate(),
902
- end: paymentFilterEnd.toDate(),
903
- };
904
- const statementTransactions = await this.loanChargesService.getLoanStatementsForForProductWithCharges(statementFilter);
905
- const statementPayments = await this.loanPaymentsService.getLoanPayments(paymentFilter, null);
906
- const paidStatementIds = statementPayments.reduce((acc, s) => [...acc, ...s.paid.map((p) => p.statementId.toString())], []);
907
- const fees = {
908
- [ELoanChargeType.INTEREST_FEE]: 0,
909
- [ELoanChargeType.ADMIN_FEE]: 0,
910
- [ELoanChargeType.UNUSED_LINE_FEE]: 0,
911
- [ELoanChargeType.WIRE_FEE]: 0,
912
- [ELoanChargeType.OTHER]: 0,
913
- [ELoanChargeType.RECOVERABLE]: 0,
914
- [ELoanChargeType.ANNUAL_LINE_FEE]: 0,
915
- };
916
- const sumResults = statementTransactions.reduce((acc, t) => {
917
- const amountPaid = paidStatementIds.includes(t._id.toString()) ? t.amountPaid : 0;
918
- return {
919
- ...acc,
920
- [t.charge.chargeType]: new Decimal(acc[t.charge.chargeType] ?? 0).add(t.amount ?? 0).sub(amountPaid ?? 0).toNumber(),
896
+ const statementFilter = {
897
+ productIds: [product._id.toString()],
898
+ start: relevantStatementStart.toDate(),
899
+ end: relevantStatementEnd.toDate(),
921
900
  };
922
- }, fees);
923
-
924
- let principal = 0;
925
- let principalExpectedDate = null;
926
- if (product.type === ELoanTypes.TERM) {
927
- const termLoan = await TermLoanModel.findOne({ productId: product._id.toString(), actual: true });
928
- if (termLoan) {
929
- const calculatedTermLoan = await TermLoanCalculatedModel.findOne({
930
- termLoanId: termLoan._id.toString(),
931
- relevantStatement: relevantStatementEndMonth.format('YYYY-MM-DD'),
932
- });
933
- if (calculatedTermLoan) {
934
- if (calculatedTermLoan.payments && calculatedTermLoan.payments.length > 0) {
935
- const totalPayment = calculatedTermLoan.payments.reduce((acc, p) => new Decimal(acc ?? 0).add(p.amount ?? 0).toNumber(), 0);
936
- principal = new Decimal(calculatedTermLoan.monthlyPrincipal).sub(totalPayment).toDP(2).toNumber();
937
- } else {
938
- principal = calculatedTermLoan.monthlyPrincipal;
939
- principalExpectedDate = calculatedTermLoan.paymentDueDate;
901
+ const paymentFilter = {
902
+ productIds: [product._id.toString()],
903
+ start: paymentFilterStart.toDate(),
904
+ end: paymentFilterEnd.toDate(),
905
+ };
906
+ const statementTransactions = await this.loanChargesService.getLoanStatementsForForProductWithCharges(statementFilter);
907
+ const statementPayments = await this.loanPaymentsService.getLoanPayments(paymentFilter, null);
908
+ const paidStatementIds = statementPayments.reduce((acc, s) => [...acc, ...s.paid.map((p) => p.statementId.toString())], []);
909
+ const fees = {
910
+ [ELoanChargeType.INTEREST_FEE]: 0,
911
+ [ELoanChargeType.ADMIN_FEE]: 0,
912
+ [ELoanChargeType.UNUSED_LINE_FEE]: 0,
913
+ [ELoanChargeType.WIRE_FEE]: 0,
914
+ [ELoanChargeType.OTHER]: 0,
915
+ [ELoanChargeType.RECOVERABLE]: 0,
916
+ [ELoanChargeType.ANNUAL_LINE_FEE]: 0,
917
+ };
918
+ const sumResults = statementTransactions.reduce((acc, t) => {
919
+ const amountPaid = paidStatementIds.includes(t._id.toString()) ? t.amountPaid : 0;
920
+ return {
921
+ ...acc,
922
+ [t.charge.chargeType]: new Decimal(acc[t.charge.chargeType] ?? 0).add(t.amount ?? 0).sub(amountPaid ?? 0).toNumber(),
923
+ };
924
+ }, fees);
925
+
926
+ let principal = 0;
927
+ let principalExpectedDate = null;
928
+ if (product.type === ELoanTypes.TERM) {
929
+ const termLoan = await TermLoanModel.findOne({ productId: product._id.toString(), actual: true });
930
+ if (termLoan) {
931
+ const calculatedTermLoan = await TermLoanCalculatedModel.findOne({
932
+ termLoanId: termLoan._id.toString(),
933
+ relevantStatement: relevantStatementEndMonth.format('YYYY-MM-DD'),
934
+ });
935
+ if (calculatedTermLoan) {
936
+ if (calculatedTermLoan.payments && calculatedTermLoan.payments.length > 0) {
937
+ const totalPayment = calculatedTermLoan.payments.reduce((acc, p) => new Decimal(acc ?? 0).add(p.amount ?? 0).toNumber(), 0);
938
+ principal = new Decimal(calculatedTermLoan.monthlyPrincipal).sub(totalPayment).toDP(2).toNumber();
939
+ } else {
940
+ principal = calculatedTermLoan.monthlyPrincipal;
941
+ principalExpectedDate = calculatedTermLoan.paymentDueDate;
942
+ }
940
943
  }
941
944
  }
942
945
  }
943
- }
944
946
 
945
- return {
946
- borrowerCode: borrower.code,
947
- ...product,
948
- actualBalance,
949
- ...sumResults,
950
- principal,
951
- principalExpectedDate,
952
- };
953
- }));
947
+ return {
948
+ borrowerCode: borrower.code,
949
+ ...product,
950
+ actualBalance,
951
+ ...sumResults,
952
+ principal,
953
+ principalExpectedDate,
954
+ };
955
+ }));
954
956
  }));
955
957
  return results
956
- .filter((res) => !!res)
957
- .reduce((acc, res) => ([...acc, ...res]), [])
958
- .sort((a, b) => (b.code.toUpperCase() < a.code.toUpperCase() ? 1 : -1));
958
+ .flat()
959
+ .sort((a, b) => a.code.toUpperCase().localeCompare(b.code.toUpperCase()));
959
960
  }
960
961
 
961
962
  async getOutstandingStatementsExcel(borrowerIds: string[], selectedDate: Date, fullMonth: boolean) {