gemcap-be-common 1.3.180 → 1.3.182

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.3.180",
3
+ "version": "1.3.182",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -82,6 +82,18 @@ class BankUploadedTransactionsService {
82
82
  cashAllocationProducts.forEach((product) => {
83
83
  cashAllocationProductsMap.set(product._id.toString(), product);
84
84
  });
85
+ const findMatchingCashAllocationReference = (bankTransaction, searchInDetails) => {
86
+ return cashAllocationReferences.find((cashAllocationReference) => {
87
+ const referenceToSearch = cashAllocationReference.reference.toLowerCase();
88
+ const fieldsToSearch = [bankTransaction.reference];
89
+ if (searchInDetails) {
90
+ fieldsToSearch.push(bankTransaction.detail1, bankTransaction.detail2, bankTransaction.detail3);
91
+ }
92
+ return fieldsToSearch
93
+ .filter((field) => !!field)
94
+ .some((field) => field.toLowerCase().includes(referenceToSearch));
95
+ });
96
+ };
85
97
  const transactionHasMatch = (bankTransaction) => {
86
98
  if (bankTransaction.splitTransactions.length > 0) {
87
99
  const totalSplit = bankTransaction.splitTransactions.reduce((acc, split) => new decimal_js_1.Decimal(split.amount).add(acc).toNumber(), 0);
@@ -89,14 +101,14 @@ class BankUploadedTransactionsService {
89
101
  return true;
90
102
  }
91
103
  }
92
- const foundCashAllocationReference = cashAllocationReferences.find((cashAllocationReference) => bankTransaction.reference.toLowerCase().includes(cashAllocationReference.reference.toLowerCase()));
104
+ const foundCashAllocationReference = findMatchingCashAllocationReference(bankTransaction, true);
93
105
  if (!foundCashAllocationReference || !foundCashAllocationReference.cashAllocationProductId) {
94
106
  return false;
95
107
  }
96
108
  return !!cashAllocationProductsMap.get(foundCashAllocationReference.cashAllocationProductId.toString());
97
109
  };
98
110
  const transactionCanSplit = (bankTransaction) => {
99
- const foundCashAllocationReference = cashAllocationReferences.find((cashAllocationReference) => bankTransaction.reference.toLowerCase().includes(cashAllocationReference.reference.toLowerCase()));
111
+ const foundCashAllocationReference = findMatchingCashAllocationReference(bankTransaction, true);
100
112
  if (!foundCashAllocationReference || !foundCashAllocationReference.cashAllocationProductId) {
101
113
  return false;
102
114
  }
@@ -103,6 +103,21 @@ export class BankUploadedTransactionsService {
103
103
  cashAllocationProductsMap.set(product._id.toString(), product);
104
104
  });
105
105
 
106
+ const findMatchingCashAllocationReference = (bankTransaction: IUploadedBankTransaction, searchInDetails: boolean) => {
107
+ return cashAllocationReferences.find((cashAllocationReference) => {
108
+ const referenceToSearch = cashAllocationReference.reference.toLowerCase();
109
+
110
+ const fieldsToSearch = [bankTransaction.reference];
111
+ if (searchInDetails) {
112
+ fieldsToSearch.push(bankTransaction.detail1, bankTransaction.detail2, bankTransaction.detail3);
113
+ }
114
+
115
+ return fieldsToSearch
116
+ .filter((field): field is string => !!field)
117
+ .some((field) => field.toLowerCase().includes(referenceToSearch));
118
+ });
119
+ };
120
+
106
121
  const transactionHasMatch = (bankTransaction: IUploadedBankTransaction): boolean => {
107
122
  if (bankTransaction.splitTransactions.length > 0) {
108
123
  const totalSplit = bankTransaction.splitTransactions.reduce((acc, split) => new Decimal(split.amount).add(acc).toNumber(), 0);
@@ -110,7 +125,7 @@ export class BankUploadedTransactionsService {
110
125
  return true;
111
126
  }
112
127
  }
113
- const foundCashAllocationReference = cashAllocationReferences.find((cashAllocationReference) => bankTransaction.reference.toLowerCase().includes(cashAllocationReference.reference.toLowerCase()));
128
+ const foundCashAllocationReference = findMatchingCashAllocationReference(bankTransaction, true);
114
129
  if (!foundCashAllocationReference || !foundCashAllocationReference.cashAllocationProductId) {
115
130
  return false;
116
131
  }
@@ -118,7 +133,7 @@ export class BankUploadedTransactionsService {
118
133
  };
119
134
 
120
135
  const transactionCanSplit = (bankTransaction: IUploadedBankTransaction): boolean => {
121
- const foundCashAllocationReference = cashAllocationReferences.find((cashAllocationReference) => bankTransaction.reference.toLowerCase().includes(cashAllocationReference.reference.toLowerCase()));
136
+ const foundCashAllocationReference = findMatchingCashAllocationReference(bankTransaction, true);
122
137
  if (!foundCashAllocationReference || !foundCashAllocationReference.cashAllocationProductId) {
123
138
  return false;
124
139
  }
@@ -372,10 +372,10 @@ class TermLoanService {
372
372
  isLastPeriod = actualMonthlyPayment > monthOpeningBalance || (currentDate.format('YYYY-MM-DD') === endTermMonths.format('YYYY-MM-DD'));
373
373
  const isGracePeriod = checkGracePeriodDate(principalDueDay);
374
374
  const monthlyPrincipal = (isGracePeriod) ? 0 : (isLastPeriod ? monthOpeningBalance : actualMonthlyPayment);
375
- const totalPayment = monthPayments.length > 0 ? monthPayments.reduce((acc, p) => acc + Math.abs(p.amount), 0) : lastMonthPrincipal;
376
- const totalDisbursements = monthDisbursements.length > 0 ? monthDisbursements.reduce((acc, p) => acc + Math.abs(p.amount), 0) : 0;
375
+ const totalPayment = monthPayments.length > 0 ? monthPayments.reduce((acc, p) => new decimal_js_1.default(acc).add(Math.abs(p.amount)).toDP(2).toNumber(), 0) : lastMonthPrincipal;
376
+ const totalDisbursements = monthDisbursements.length > 0 ? monthDisbursements.reduce((acc, p) => new decimal_js_1.default(acc).add(p.amount).toDP(2).toNumber(), 0) : 0;
377
377
  const closingBalance = new decimal_js_1.default(monthOpeningBalance).sub(totalPayment).add(totalDisbursements).toDP(2).toNumber();
378
- openingBalance = closingBalance;
378
+ // openingBalance = closingBalance;
379
379
  const monthlyResult = {
380
380
  termLoanId: termLoan._id,
381
381
  openingBalance: monthOpeningBalance,
@@ -390,7 +390,7 @@ class TermLoanService {
390
390
  amortization: true, // TODO calculate
391
391
  paymentDueDate: principalDueDay.date(termLoan.settings.principalDueDay).toDate(),
392
392
  payments: null,
393
- disbursements: totalDisbursements,
393
+ disbursements: Math.abs(totalDisbursements),
394
394
  dailyResults: calculatedDailyResults,
395
395
  };
396
396
  calculatedMonthlyResults.push(monthlyResult);
@@ -404,7 +404,6 @@ class TermLoanService {
404
404
  }
405
405
  const paymentDates = [...new Set(calculatedMonthlyResults.map((res) => (0, dayjs_1.default)(res.paymentDueDate).format('YYYY-MM-DD')))];
406
406
  const mergedResults = paymentDates.reduce((acc, paymentDate) => {
407
- console.log(paymentDate, 'processing results');
408
407
  const filteredDateResults = calculatedMonthlyResults.filter((res) => (0, dayjs_1.default)(res.paymentDueDate).format('YYYY-MM-DD') === paymentDate);
409
408
  if (filteredDateResults.length === 1) {
410
409
  return [...acc, ...filteredDateResults];
@@ -480,11 +479,19 @@ class TermLoanService {
480
479
  }
481
480
  const trDate = typeof res.paymentDueDate === 'string' ? new Date(res.paymentDueDate) : res.paymentDueDate;
482
481
  const lastTransaction = await loanTransactionsService.getLastTransactionForDate(product._id.toString(), trDate);
483
- if (lastTransaction && lastTransaction.balance > 0) {
482
+ if (lastTransaction && lastTransaction.balance >= 0) {
484
483
  return res;
485
484
  }
486
485
  }))).filter((res) => !!res);
487
- await this.saveCalculatedTermLoan(filteredRes, productId);
486
+ let balanceIsZero = false;
487
+ const afterClosingBalance = filteredRes.reduce((acc, res) => {
488
+ if (balanceIsZero) {
489
+ return acc;
490
+ }
491
+ balanceIsZero = res.closingBalance === 0;
492
+ return [...acc, res];
493
+ }, []);
494
+ await this.saveCalculatedTermLoan(afterClosingBalance, productId);
488
495
  await this.updateTermLoanStatus(termLoan._id.toString(), TermLoan_model_1.ETermLoanStatus.CALCULATED, new Date());
489
496
  return await this.getTermLoan(productId, actual);
490
497
  }
@@ -450,10 +450,10 @@ export class TermLoanService {
450
450
  isLastPeriod = actualMonthlyPayment > monthOpeningBalance || (currentDate.format('YYYY-MM-DD') === endTermMonths.format('YYYY-MM-DD'));
451
451
  const isGracePeriod = checkGracePeriodDate(principalDueDay);
452
452
  const monthlyPrincipal = (isGracePeriod) ? 0 : (isLastPeriod ? monthOpeningBalance : actualMonthlyPayment);
453
- const totalPayment = monthPayments.length > 0 ? monthPayments.reduce((acc, p) => acc + Math.abs(p.amount), 0) : lastMonthPrincipal;
454
- const totalDisbursements = monthDisbursements.length > 0 ? monthDisbursements.reduce((acc, p) => acc + Math.abs(p.amount), 0) : 0;
453
+ const totalPayment = monthPayments.length > 0 ? monthPayments.reduce((acc, p) => new Decimal(acc).add(Math.abs(p.amount)).toDP(2).toNumber(), 0) : lastMonthPrincipal;
454
+ const totalDisbursements = monthDisbursements.length > 0 ? monthDisbursements.reduce((acc, p) => new Decimal(acc).add(p.amount).toDP(2).toNumber(), 0) : 0;
455
455
  const closingBalance = new Decimal(monthOpeningBalance).sub(totalPayment).add(totalDisbursements).toDP(2).toNumber();
456
- openingBalance = closingBalance;
456
+ // openingBalance = closingBalance;
457
457
  const monthlyResult: ITermLoanCalculated = {
458
458
  termLoanId: termLoan._id,
459
459
  openingBalance: monthOpeningBalance,
@@ -468,7 +468,7 @@ export class TermLoanService {
468
468
  amortization: true, // TODO calculate
469
469
  paymentDueDate: principalDueDay.date(termLoan.settings.principalDueDay).toDate(),
470
470
  payments: null,
471
- disbursements: totalDisbursements,
471
+ disbursements: Math.abs(totalDisbursements),
472
472
  dailyResults: calculatedDailyResults,
473
473
  };
474
474
  calculatedMonthlyResults.push(monthlyResult);
@@ -482,7 +482,6 @@ export class TermLoanService {
482
482
  }
483
483
  const paymentDates = [...new Set(calculatedMonthlyResults.map((res) => dayjs(res.paymentDueDate).format('YYYY-MM-DD')))];
484
484
  const mergedResults = paymentDates.reduce((acc, paymentDate) => {
485
- console.log(paymentDate, 'processing results');
486
485
  const filteredDateResults = calculatedMonthlyResults.filter((res) => dayjs(res.paymentDueDate).format('YYYY-MM-DD') === paymentDate);
487
486
  if (filteredDateResults.length === 1) {
488
487
  return [...acc, ...filteredDateResults];
@@ -558,11 +557,19 @@ export class TermLoanService {
558
557
  }
559
558
  const trDate = typeof res.paymentDueDate === 'string' ? new Date(res.paymentDueDate) : res.paymentDueDate;
560
559
  const lastTransaction = await loanTransactionsService.getLastTransactionForDate(product._id.toString(), trDate);
561
- if (lastTransaction && lastTransaction.balance > 0) {
560
+ if (lastTransaction && lastTransaction.balance >= 0) {
562
561
  return res;
563
562
  }
564
563
  }))).filter((res) => !!res);
565
- await this.saveCalculatedTermLoan(filteredRes, productId);
564
+ let balanceIsZero = false;
565
+ const afterClosingBalance = filteredRes.reduce((acc, res) => {
566
+ if (balanceIsZero) {
567
+ return acc;
568
+ }
569
+ balanceIsZero = res.closingBalance === 0;
570
+ return [...acc, res];
571
+ }, []);
572
+ await this.saveCalculatedTermLoan(afterClosingBalance, productId);
566
573
  await this.updateTermLoanStatus(termLoan._id.toString(), ETermLoanStatus.CALCULATED, new Date());
567
574
  return await this.getTermLoan(productId, actual);
568
575
  }