gemcap-be-common 1.4.116 → 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.116",
3
+ "version": "1.4.118",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -776,7 +776,12 @@ 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) => {
782
+ if ((0, dayjs_1.default)(product.deactivationDate).isBefore((0, dayjs_1.default)(selectedDate))) {
783
+ return null;
784
+ }
780
785
  const relevantStatementEndMonth = fullMonth
781
786
  ? (0, dayjs_1.default)(selectedDate).utcOffset(0).startOf('month').subtract(1, 'seconds')
782
787
  : (0, dayjs_1.default)(selectedDate).utcOffset(0).endOf('month');
@@ -852,9 +857,8 @@ class LoanStatementService {
852
857
  }));
853
858
  }));
854
859
  return results
855
- .filter((res) => !!res)
856
- .reduce((acc, res) => ([...acc, ...res]), [])
857
- .sort((a, b) => (b.code.toUpperCase() < a.code.toUpperCase() ? 1 : -1));
860
+ .flat()
861
+ .sort((a, b) => a.code.toUpperCase().localeCompare(b.code.toUpperCase()));
858
862
  }
859
863
  async getOutstandingStatementsExcel(borrowerIds, selectedDate, fullMonth) {
860
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,89 +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
- const relevantStatementEndMonth = fullMonth
875
- ? dayjs(selectedDate).utcOffset(0).startOf('month').subtract(1, 'seconds')
876
- : dayjs(selectedDate).utcOffset(0).endOf('month');
877
- const relevantStatementEnd = fullMonth
878
- ? dayjs(selectedDate).utcOffset(0).startOf('month').subtract(1, 'seconds')
879
- : dayjs(selectedDate).utcOffset(0).endOf('day');
880
- const relevantStatementStart = dayjs(selectedDate).utcOffset(0).startOf('month').subtract(1, 'seconds').startOf('month');
881
-
882
- const paymentFilterEnd = dayjs(selectedDate).utcOffset(0).endOf('day');
883
- const paymentFilterStart = relevantStatementStart;
884
-
885
- const lastTransaction = await this.loanTransactionsService.getLastTransactionForDate(product._id.toString(), dayjs(selectedDate).endOf('day').toDate());
886
- let actualBalance = 0;
887
- if (lastTransaction) {
888
- actualBalance = new Decimal(lastTransaction.balance).toDP(2).toNumber();
889
- }
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
+ }
890
895
 
891
- const statementFilter = {
892
- productIds: [product._id.toString()],
893
- start: relevantStatementStart.toDate(),
894
- end: relevantStatementEnd.toDate(),
895
- };
896
- const paymentFilter = {
897
- productIds: [product._id.toString()],
898
- start: paymentFilterStart.toDate(),
899
- end: paymentFilterEnd.toDate(),
900
- };
901
- const statementTransactions = await this.loanChargesService.getLoanStatementsForForProductWithCharges(statementFilter);
902
- const statementPayments = await this.loanPaymentsService.getLoanPayments(paymentFilter, null);
903
- const paidStatementIds = statementPayments.reduce((acc, s) => [...acc, ...s.paid.map((p) => p.statementId.toString())], []);
904
- const fees = {
905
- [ELoanChargeType.INTEREST_FEE]: 0,
906
- [ELoanChargeType.ADMIN_FEE]: 0,
907
- [ELoanChargeType.UNUSED_LINE_FEE]: 0,
908
- [ELoanChargeType.WIRE_FEE]: 0,
909
- [ELoanChargeType.OTHER]: 0,
910
- [ELoanChargeType.RECOVERABLE]: 0,
911
- [ELoanChargeType.ANNUAL_LINE_FEE]: 0,
912
- };
913
- const sumResults = statementTransactions.reduce((acc, t) => {
914
- const amountPaid = paidStatementIds.includes(t._id.toString()) ? t.amountPaid : 0;
915
- return {
916
- ...acc,
917
- [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(),
918
900
  };
919
- }, fees);
920
-
921
- let principal = 0;
922
- let principalExpectedDate = null;
923
- if (product.type === ELoanTypes.TERM) {
924
- const termLoan = await TermLoanModel.findOne({ productId: product._id.toString(), actual: true });
925
- if (termLoan) {
926
- const calculatedTermLoan = await TermLoanCalculatedModel.findOne({
927
- termLoanId: termLoan._id.toString(),
928
- relevantStatement: relevantStatementEndMonth.format('YYYY-MM-DD'),
929
- });
930
- if (calculatedTermLoan) {
931
- if (calculatedTermLoan.payments && calculatedTermLoan.payments.length > 0) {
932
- const totalPayment = calculatedTermLoan.payments.reduce((acc, p) => new Decimal(acc ?? 0).add(p.amount ?? 0).toNumber(), 0);
933
- principal = new Decimal(calculatedTermLoan.monthlyPrincipal).sub(totalPayment).toDP(2).toNumber();
934
- } else {
935
- principal = calculatedTermLoan.monthlyPrincipal;
936
- 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
+ }
937
943
  }
938
944
  }
939
945
  }
940
- }
941
946
 
942
- return {
943
- borrowerCode: borrower.code,
944
- ...product,
945
- actualBalance,
946
- ...sumResults,
947
- principal,
948
- principalExpectedDate,
949
- };
950
- }));
947
+ return {
948
+ borrowerCode: borrower.code,
949
+ ...product,
950
+ actualBalance,
951
+ ...sumResults,
952
+ principal,
953
+ principalExpectedDate,
954
+ };
955
+ }));
951
956
  }));
952
957
  return results
953
- .filter((res) => !!res)
954
- .reduce((acc, res) => ([...acc, ...res]), [])
955
- .sort((a, b) => (b.code.toUpperCase() < a.code.toUpperCase() ? 1 : -1));
958
+ .flat()
959
+ .sort((a, b) => a.code.toUpperCase().localeCompare(b.code.toUpperCase()));
956
960
  }
957
961
 
958
962
  async getOutstandingStatementsExcel(borrowerIds: string[], selectedDate: Date, fullMonth: boolean) {