gemcap-be-common 1.2.140 → 1.3.0

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.
Files changed (303) hide show
  1. package/classes/bank-transaction-item.d.ts +17 -0
  2. package/classes/bank-transaction-item.js +64 -0
  3. package/classes/bank-transaction-item.ts +66 -0
  4. package/classes/bank-uploaded-transaction.d.ts +17 -0
  5. package/classes/bank-uploaded-transaction.js +35 -0
  6. package/classes/bank-uploaded-transaction.ts +35 -0
  7. package/classes/inventory-item.d.ts +41 -0
  8. package/classes/inventory-item.js +44 -0
  9. package/classes/inventory-item.ts +63 -0
  10. package/classes/payable-account-item.d.ts +22 -0
  11. package/classes/payable-account-item.js +27 -0
  12. package/classes/payable-account-item.ts +35 -0
  13. package/classes/quickbook-item.d.ts +37 -0
  14. package/classes/quickbook-item.js +51 -0
  15. package/classes/quickbook-item.ts +59 -0
  16. package/classes/receivable-item.d.ts +26 -0
  17. package/classes/receivable-item.js +28 -0
  18. package/classes/receivable-item.ts +38 -0
  19. package/constants/date-formats.contsants.d.ts +1 -0
  20. package/constants/date-formats.contsants.js +4 -0
  21. package/constants/date-formats.contsants.ts +1 -0
  22. package/db/brokers.db.d.ts +185 -0
  23. package/db/brokers.db.js +35 -2
  24. package/db/brokers.db.ts +34 -1
  25. package/db/collateral-adjustments.db.d.ts +34 -0
  26. package/db/collateral-adjustments.db.js +52 -0
  27. package/db/collateral-adjustments.db.ts +54 -0
  28. package/db/collaterals.db.d.ts +1 -1
  29. package/db/equipment.db.d.ts +40 -0
  30. package/db/equipment.db.js +55 -0
  31. package/db/equipment.db.ts +56 -0
  32. package/db/financial-spreading.db.ts +2 -1
  33. package/db/groups.d.ts +5 -0
  34. package/db/groups.js +57 -0
  35. package/db/groups.ts +52 -0
  36. package/db/inventories.d.ts +91 -0
  37. package/db/inventories.js +449 -0
  38. package/db/inventories.ts +481 -0
  39. package/db/inventory-availability.d.ts +3 -0
  40. package/db/inventory-availability.js +103 -0
  41. package/db/inventory-availability.ts +113 -0
  42. package/db/new-summary.d.ts +31 -0
  43. package/db/new-summary.js +1295 -0
  44. package/db/new-summary.ts +1509 -0
  45. package/db/payable-accounts.d.ts +30 -0
  46. package/db/payable-accounts.js +55 -0
  47. package/db/payable-accounts.ts +50 -0
  48. package/db/reserve.db.d.ts +34 -0
  49. package/db/reserve.db.js +52 -0
  50. package/db/reserve.db.ts +48 -0
  51. package/db/uploads.db.d.ts +2 -0
  52. package/db/uploads.db.js +29 -0
  53. package/db/uploads.db.ts +24 -0
  54. package/helpers/main.helper.d.ts +31 -0
  55. package/helpers/main.helper.js +63 -0
  56. package/helpers/main.helper.ts +63 -0
  57. package/models/AccountPayableItem.model.d.ts +6 -6
  58. package/models/AllocatedBankTransaction.model.d.ts +54 -0
  59. package/models/AllocatedBankTransaction.model.js +70 -0
  60. package/models/AllocatedBankTransaction.model.ts +94 -0
  61. package/models/AllocatedData.model.d.ts +33 -0
  62. package/models/AllocatedData.model.js +19 -0
  63. package/models/AllocatedData.model.ts +24 -0
  64. package/models/BBCDate.model.d.ts +3 -3
  65. package/models/BBCSheet.model.d.ts +3 -3
  66. package/models/Banks.model.d.ts +3 -3
  67. package/models/Borrower.model.d.ts +3 -3
  68. package/models/BorrowerData.model.d.ts +3 -3
  69. package/models/BorrowerDataInsurance.model.d.ts +3 -3
  70. package/models/BorrowerDataTerm.model.d.ts +3 -3
  71. package/models/BorrowerSummary.model.js +1 -1
  72. package/models/BorrowerSummary.model.ts +1 -1
  73. package/models/CalandarDay.model.d.ts +40 -0
  74. package/models/CalandarDay.model.js +47 -0
  75. package/models/CalandarDay.model.ts +61 -0
  76. package/models/CashAllocationProduct.model.d.ts +119 -0
  77. package/models/CashAllocationProduct.model.js +102 -0
  78. package/models/CashAllocationProduct.model.ts +112 -0
  79. package/models/CashAllocationReference.model.d.ts +37 -0
  80. package/models/CashAllocationReference.model.js +27 -0
  81. package/models/CashAllocationReference.model.ts +40 -0
  82. package/models/CollateralAdjustment.model.d.ts +51 -0
  83. package/models/CollateralAdjustment.model.js +61 -0
  84. package/models/CollateralAdjustment.model.ts +98 -0
  85. package/models/Company.model.d.ts +35 -0
  86. package/models/Company.model.js +18 -0
  87. package/models/Company.model.ts +29 -0
  88. package/models/CustomerAPGroup.model.d.ts +32 -0
  89. package/models/CustomerAPGroup.model.js +24 -0
  90. package/models/CustomerAPGroup.model.ts +31 -0
  91. package/models/Equipment.model.d.ts +53 -0
  92. package/models/Equipment.model.js +140 -0
  93. package/models/Equipment.model.ts +172 -0
  94. package/models/FinancialCompliance.model.d.ts +39 -0
  95. package/models/FinancialCompliance.model.js +64 -0
  96. package/models/FinancialCompliance.model.ts +78 -0
  97. package/models/FinancialComplianceBorrower.model.d.ts +58 -0
  98. package/models/FinancialComplianceBorrower.model.js +82 -0
  99. package/models/FinancialComplianceBorrower.model.ts +118 -0
  100. package/models/FinancialIndexes.model.d.ts +36 -0
  101. package/models/FinancialIndexes.model.js +27 -0
  102. package/models/FinancialIndexes.model.ts +37 -0
  103. package/models/Inventory.model.d.ts +18 -18
  104. package/models/InventoryAvailability.model.d.ts +21 -21
  105. package/models/InventoryAvailabilityItem.model.d.ts +6 -6
  106. package/models/InventoryItem.model.d.ts +24 -24
  107. package/models/InventoryManualEntry.model.d.ts +9 -9
  108. package/models/InventorySeasonalRates.model.d.ts +3 -3
  109. package/models/LoanBroker.model.d.ts +3 -3
  110. package/models/LoanCharges.model.d.ts +12 -12
  111. package/models/LoanProducts.model.d.ts +9 -9
  112. package/models/LoanStatementStatus.model.d.ts +35 -0
  113. package/models/LoanStatementStatus.model.js +34 -0
  114. package/models/LoanStatementStatus.model.ts +45 -0
  115. package/models/LoanStatementTransaction.model.d.ts +9 -9
  116. package/models/LoanTransactionFile.model.d.ts +41 -0
  117. package/models/LoanTransactionFile.model.js +44 -0
  118. package/models/LoanTransactionFile.model.ts +61 -0
  119. package/models/MappedGroup.model.d.ts +37 -0
  120. package/models/MappedGroup.model.js +33 -0
  121. package/models/MappedGroup.model.ts +46 -0
  122. package/models/MonthEndData.Model.d.ts +41 -0
  123. package/models/MonthEndData.Model.js +42 -0
  124. package/models/MonthEndData.Model.ts +53 -0
  125. package/models/OrganizationEmails.model.d.ts +44 -0
  126. package/models/OrganizationEmails.model.js +40 -0
  127. package/models/OrganizationEmails.model.ts +54 -0
  128. package/models/ProductBroker.model.d.ts +9 -9
  129. package/models/QuickbooksAccount.model.d.ts +39 -0
  130. package/models/QuickbooksAccount.model.js +43 -0
  131. package/models/QuickbooksAccount.model.ts +57 -0
  132. package/models/Receivable.model.d.ts +12 -12
  133. package/models/ReceivableAvailability.model.d.ts +54 -54
  134. package/models/ReceivableAvailabilityItem.model.d.ts +57 -57
  135. package/models/ReceivableItem.model.d.ts +6 -6
  136. package/models/Reserve.model.d.ts +51 -0
  137. package/models/Reserve.model.js +96 -0
  138. package/models/Reserve.model.ts +125 -0
  139. package/models/TermLoan.model.d.ts +3 -3
  140. package/models/TermLoanCalculated.model.d.ts +6 -6
  141. package/models/TransactionAttachedFile.Model.d.ts +35 -0
  142. package/models/TransactionAttachedFile.Model.js +37 -0
  143. package/models/TransactionAttachedFile.Model.ts +48 -0
  144. package/models/UploadedBankTransaction.model.d.ts +56 -0
  145. package/models/UploadedBankTransaction.model.js +78 -0
  146. package/models/UploadedBankTransaction.model.ts +110 -0
  147. package/models/UploadedData.model.d.ts +36 -0
  148. package/models/UploadedData.model.js +23 -0
  149. package/models/UploadedData.model.ts +35 -0
  150. package/models/UploadedFile.model.d.ts +40 -0
  151. package/models/UploadedFile.model.js +41 -0
  152. package/models/UploadedFile.model.ts +57 -0
  153. package/models/UploadedSheet.model.d.ts +46 -0
  154. package/models/UploadedSheet.model.js +27 -0
  155. package/models/UploadedSheet.model.ts +51 -0
  156. package/package.json +10 -1
  157. package/repositories/globals.repository.d.ts +8 -0
  158. package/repositories/globals.repository.js +24 -0
  159. package/repositories/globals.repository.ts +21 -0
  160. package/services/attached-files.service.d.ts +57 -0
  161. package/services/attached-files.service.js +103 -0
  162. package/services/attached-files.service.ts +123 -0
  163. package/services/availability.service.d.ts +77 -0
  164. package/services/availability.service.js +897 -0
  165. package/services/availability.service.ts +1034 -0
  166. package/services/bank-uploaded-transactions.service.d.ts +33 -0
  167. package/services/bank-uploaded-transactions.service.js +430 -0
  168. package/services/bank-uploaded-transactions.service.ts +475 -0
  169. package/services/banks.service.d.ts +36 -0
  170. package/services/banks.service.js +91 -0
  171. package/services/banks.service.ts +95 -0
  172. package/services/borrower-summary.service.d.ts +35 -0
  173. package/services/borrower-summary.service.js +310 -0
  174. package/services/borrower-summary.service.ts +334 -0
  175. package/services/borrowers.service.d.ts +103 -0
  176. package/services/borrowers.service.js +268 -0
  177. package/services/borrowers.service.ts +302 -0
  178. package/services/brokers.service.d.ts +212 -0
  179. package/services/brokers.service.js +160 -0
  180. package/services/brokers.service.ts +200 -0
  181. package/services/calendar.service.d.ts +53 -0
  182. package/services/calendar.service.js +108 -0
  183. package/services/calendar.service.ts +128 -0
  184. package/services/cash-allocation.service.d.ts +40 -0
  185. package/services/cash-allocation.service.js +92 -0
  186. package/services/cash-allocation.service.ts +105 -0
  187. package/services/collateral-adjustments.service.d.ts +38 -0
  188. package/services/collateral-adjustments.service.js +82 -0
  189. package/services/collateral-adjustments.service.ts +95 -0
  190. package/services/collaterals.service.d.ts +69 -0
  191. package/services/collaterals.service.js +279 -0
  192. package/services/collaterals.service.ts +319 -0
  193. package/services/companies.service.d.ts +5 -0
  194. package/services/companies.service.js +21 -0
  195. package/services/companies.service.ts +23 -0
  196. package/services/compliance-borrowers.service.d.ts +152 -0
  197. package/services/compliance-borrowers.service.js +569 -0
  198. package/services/compliance-borrowers.service.ts +617 -0
  199. package/services/equipment.service.d.ts +42 -0
  200. package/services/equipment.service.js +120 -0
  201. package/services/equipment.service.ts +149 -0
  202. package/services/file-manager.service.d.ts +44 -0
  203. package/services/file-manager.service.js +120 -0
  204. package/services/file-manager.service.ts +146 -0
  205. package/services/financial-compliance.service.d.ts +58 -0
  206. package/services/financial-compliance.service.js +281 -0
  207. package/services/financial-compliance.service.ts +309 -0
  208. package/services/financial-indexes.service.d.ts +20 -0
  209. package/services/financial-indexes.service.js +241 -0
  210. package/services/financial-indexes.service.ts +257 -0
  211. package/services/financial-spreading.service.d.ts +74 -0
  212. package/services/financial-spreading.service.js +450 -0
  213. package/services/financial-spreading.service.ts +517 -0
  214. package/services/globals.service.d.ts +5 -0
  215. package/services/globals.service.js +11 -0
  216. package/services/globals.service.ts +8 -0
  217. package/services/groups.service.d.ts +39 -0
  218. package/services/groups.service.js +65 -0
  219. package/services/groups.service.ts +64 -0
  220. package/services/inventory-availability.service.d.ts +13 -0
  221. package/services/inventory-availability.service.js +170 -0
  222. package/services/inventory-availability.service.ts +187 -0
  223. package/services/inventory.service.d.ts +118 -0
  224. package/services/inventory.service.js +239 -0
  225. package/services/inventory.service.ts +276 -0
  226. package/services/loan-charges.service.d.ts +83 -0
  227. package/services/loan-charges.service.js +343 -0
  228. package/services/loan-charges.service.ts +396 -0
  229. package/services/loan-payments.service.d.ts +94 -0
  230. package/services/loan-payments.service.js +485 -0
  231. package/services/loan-payments.service.ts +541 -0
  232. package/services/loan-products.service.d.ts +12 -0
  233. package/services/loan-products.service.js +55 -0
  234. package/services/loan-products.service.ts +58 -0
  235. package/services/loan-statement-balance.service.d.ts +16 -0
  236. package/services/loan-statement-balance.service.js +106 -0
  237. package/services/loan-statement-balance.service.ts +113 -0
  238. package/services/loan-statement-effects.service.d.ts +8 -0
  239. package/services/loan-statement-effects.service.js +42 -0
  240. package/services/loan-statement-effects.service.ts +41 -0
  241. package/services/loan-statement-status.service.d.ts +208 -0
  242. package/services/loan-statement-status.service.js +159 -0
  243. package/services/loan-statement-status.service.ts +177 -0
  244. package/services/loan-statement.service.d.ts +186 -0
  245. package/services/loan-statement.service.js +935 -0
  246. package/services/loan-statement.service.ts +1040 -0
  247. package/services/loan-transactions.service.d.ts +169 -0
  248. package/services/loan-transactions.service.js +941 -0
  249. package/services/loan-transactions.service.ts +1042 -0
  250. package/services/lock.service.d.ts +6 -0
  251. package/services/lock.service.js +45 -0
  252. package/services/lock.service.ts +45 -0
  253. package/services/manual-entry.service.d.ts +20 -0
  254. package/services/manual-entry.service.js +186 -0
  255. package/services/manual-entry.service.ts +201 -0
  256. package/services/month-end-data.service.d.ts +34 -0
  257. package/services/month-end-data.service.js +30 -0
  258. package/services/month-end-data.service.ts +35 -0
  259. package/services/nodemailer.service.d.ts +96 -0
  260. package/services/nodemailer.service.js +689 -0
  261. package/services/nodemailer.service.ts +774 -0
  262. package/services/organization-emails.service.d.ts +31 -0
  263. package/services/organization-emails.service.js +10 -0
  264. package/services/organization-emails.service.ts +7 -0
  265. package/services/organizations.service.d.ts +34 -0
  266. package/services/organizations.service.js +74 -0
  267. package/services/organizations.service.ts +84 -0
  268. package/services/pdf.service.d.ts +61 -0
  269. package/services/pdf.service.js +547 -0
  270. package/services/pdf.service.ts +642 -0
  271. package/services/quickbooks.service.d.ts +99 -0
  272. package/services/quickbooks.service.js +640 -0
  273. package/services/quickbooks.service.ts +734 -0
  274. package/services/reports/investor-summary.service.d.ts +28 -0
  275. package/services/reports/investor-summary.service.js +136 -0
  276. package/services/reports/investor-summary.service.ts +159 -0
  277. package/services/reports.service.d.ts +126 -0
  278. package/services/reports.service.js +584 -0
  279. package/services/reports.service.ts +702 -0
  280. package/services/reserve.service.d.ts +37 -0
  281. package/services/reserve.service.js +76 -0
  282. package/services/reserve.service.ts +79 -0
  283. package/services/sentry.service.d.ts +11 -0
  284. package/services/sentry.service.js +49 -0
  285. package/services/sentry.service.ts +33 -0
  286. package/services/signs.service.d.ts +69 -0
  287. package/services/signs.service.js +230 -0
  288. package/services/signs.service.ts +260 -0
  289. package/services/term-loan.service.d.ts +30 -0
  290. package/services/term-loan.service.js +614 -0
  291. package/services/term-loan.service.ts +696 -0
  292. package/services/uploads.service.d.ts +134 -0
  293. package/services/uploads.service.js +587 -0
  294. package/services/uploads.service.ts +643 -0
  295. package/services/user-logs.service.d.ts +23 -0
  296. package/services/user-logs.service.js +160 -0
  297. package/services/user-logs.service.ts +177 -0
  298. package/services/users.service.d.ts +4 -4
  299. package/services/yield.service.d.ts +46 -0
  300. package/services/yield.service.js +42 -12
  301. package/services/yield.service.ts +38 -8
  302. package/tsconfig.json +5 -5
  303. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,343 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.LoanChargesService = void 0;
7
+ const mongoose_1 = __importDefault(require("mongoose"));
8
+ const dayjs_1 = __importDefault(require("dayjs"));
9
+ const loan_charge_type_enum_1 = require("../enums/loan-charge-type.enum");
10
+ const _models_1 = require("../models/_models");
11
+ const loan_products_db_1 = require("../db/loan-products.db");
12
+ const loan_types_enum_1 = require("../enums/loan-types.enum");
13
+ const loan_charges_db_1 = require("../db/loan-charges.db");
14
+ const LoanProducts_model_1 = require("../models/LoanProducts.model");
15
+ const LoanCharges_model_1 = require("../models/LoanCharges.model");
16
+ const LoanStatementTransaction_model_1 = require("../models/LoanStatementTransaction.model");
17
+ const collaterals_db_1 = require("../db/collaterals.db");
18
+ const updateChargesForProduct = false;
19
+ class LoanChargesService {
20
+ borrowerService;
21
+ constructor(borrowerService) {
22
+ this.borrowerService = borrowerService;
23
+ }
24
+ validateCharge = (charge) => {
25
+ const chargePLCode = charge.PLCode ? charge.PLCode.trim() : '';
26
+ const chargeBorrowerCode = charge.code.trim().slice(3, 6);
27
+ return chargePLCode.length > 0 && chargeBorrowerCode.length > 0;
28
+ };
29
+ async getProductByChargeId(chargeId) {
30
+ const charge = await LoanCharges_model_1.LoanCharge.findById(chargeId).lean();
31
+ if (!charge) {
32
+ return null;
33
+ }
34
+ return LoanProducts_model_1.LoanProduct.findById(charge.productId).lean();
35
+ }
36
+ async getBorrowersLoanProducts(borrowerId) {
37
+ return LoanProducts_model_1.LoanProduct.find({ borrowerId }).lean();
38
+ }
39
+ async getAllLoanProducts() {
40
+ const borrowers = await this.borrowerService.getActiveBorrowers();
41
+ const borrowerIds = borrowers.map((borrower) => borrower._id);
42
+ return LoanProducts_model_1.LoanProduct.aggregate([
43
+ { $match: { borrowerId: { $in: borrowerIds } } },
44
+ {
45
+ $lookup: {
46
+ from: _models_1.MODEL_NAMES.borrowers,
47
+ localField: 'borrowerId',
48
+ foreignField: '_id',
49
+ as: 'borrower',
50
+ },
51
+ },
52
+ { $unwind: '$borrower' },
53
+ { $sort: { 'borrower.name': 1 } },
54
+ { $project: { borrower: 0 } },
55
+ ]);
56
+ }
57
+ async getLoanProducts(borrowerId) {
58
+ return (0, loan_products_db_1.getLoanProducts)(borrowerId);
59
+ }
60
+ async getLoanProductsByIds(productIds) {
61
+ const ids = productIds.map((productId) => new mongoose_1.default.Types.ObjectId(productId));
62
+ return LoanProducts_model_1.LoanProduct.aggregate([
63
+ {
64
+ $match: {
65
+ '_id': { $in: ids },
66
+ },
67
+ }, {
68
+ $sort: {
69
+ 'order': 1,
70
+ },
71
+ }, {
72
+ $unset: loan_products_db_1.fieldsToUnset,
73
+ }, {
74
+ $addFields: {
75
+ valid: true,
76
+ },
77
+ },
78
+ ]);
79
+ }
80
+ async getLoanProductBalance(productId, forDate = new Date()) {
81
+ return (0, loan_products_db_1.getLoanProductBalance)(productId, forDate);
82
+ }
83
+ async getParticipationBalance(code, end) {
84
+ const masterProduct = await LoanProducts_model_1.LoanProduct.findOne({ masterCode: code }).lean();
85
+ if (!masterProduct) {
86
+ return 0;
87
+ }
88
+ const { balance } = await this.getLoanProductBalance(masterProduct._id.toString(), end);
89
+ return balance;
90
+ }
91
+ ;
92
+ async getLoanProductById(productId) {
93
+ return (0, loan_products_db_1.getLoanProductById)(productId);
94
+ }
95
+ async getLoanProductIdByCode(code) {
96
+ const foundProduct = await LoanProducts_model_1.LoanProduct.findOne({ code }).lean();
97
+ if (foundProduct) {
98
+ return foundProduct._id.toString();
99
+ }
100
+ return null;
101
+ }
102
+ async getLoanChargeIdByCode(code) {
103
+ const foundCharge = await LoanCharges_model_1.LoanCharge.findOne({ code }).lean();
104
+ if (foundCharge) {
105
+ return foundCharge._id.toString();
106
+ }
107
+ return null;
108
+ }
109
+ async saveLoanProducts(borrowerId, loanProducts) {
110
+ const mappedProducts = loanProducts.map((product, order) => {
111
+ return { ...product, borrowerId: new mongoose_1.default.Types.ObjectId(borrowerId), order };
112
+ });
113
+ const allProducts = await this.getLoanProducts(borrowerId);
114
+ const productIdsToRemove = allProducts.map((product) => product._id.toString());
115
+ await Promise.all(mappedProducts.map(async (product) => {
116
+ if (!product._id) {
117
+ const newProduct = new LoanProducts_model_1.LoanProduct(product);
118
+ const savedProduct = await newProduct.save();
119
+ if (product.type === loan_types_enum_1.ELoanTypes.TERM) {
120
+ const chargeTypes = [
121
+ {
122
+ chargeType: loan_charge_type_enum_1.ELoanChargeType.INTEREST_FEE,
123
+ calculationBasis: loan_types_enum_1.EChargeCalculationBasis.DAILY_BALANCE,
124
+ frequency: loan_types_enum_1.EChargeFrequencies.DAILY,
125
+ },
126
+ {
127
+ chargeType: loan_charge_type_enum_1.ELoanChargeType.ADMIN_FEE,
128
+ calculationBasis: loan_types_enum_1.EChargeCalculationBasis.AVERAGE_MONTHLY_BALANCE,
129
+ frequency: loan_types_enum_1.EChargeFrequencies.MONTHLY,
130
+ },
131
+ ];
132
+ const charges = chargeTypes.map((charge) => {
133
+ const newCharge = {
134
+ PLCode: 'XXXXXX',
135
+ _id: null,
136
+ active: true,
137
+ applyFrom: product.startDate,
138
+ calculationBasis: charge.calculationBasis,
139
+ chargeType: charge.chargeType,
140
+ code: 'XXXXXX',
141
+ frequency: charge.frequency,
142
+ includeInYield: true,
143
+ minimumAmount: 0,
144
+ name: charge.chargeType,
145
+ percent: 0,
146
+ productId: new mongoose_1.default.Types.ObjectId(String(savedProduct._id)),
147
+ };
148
+ return newCharge;
149
+ });
150
+ await this.saveLoanCharges(borrowerId, charges, true);
151
+ }
152
+ }
153
+ else {
154
+ await LoanProducts_model_1.LoanProduct.findByIdAndUpdate(product._id, product);
155
+ if (updateChargesForProduct) {
156
+ await this.updatedChargeCodes(product._id.toString());
157
+ }
158
+ const index = productIdsToRemove.indexOf(product._id.toString());
159
+ if (index > -1) {
160
+ productIdsToRemove.splice(index, 1);
161
+ }
162
+ }
163
+ }));
164
+ await Promise.all(productIdsToRemove.map(async (id) => {
165
+ await LoanProducts_model_1.LoanProduct.findByIdAndDelete(id);
166
+ const foundLinkedCharges = await LoanCharges_model_1.LoanCharge.find({ productId: new mongoose_1.default.Types.ObjectId(id) });
167
+ await Promise.all(foundLinkedCharges.map(async (foundLinked) => {
168
+ await LoanCharges_model_1.LoanCharge.findByIdAndDelete(foundLinked._id);
169
+ }));
170
+ }));
171
+ }
172
+ async getLoanCharges(borrowerId) {
173
+ return LoanCharges_model_1.LoanCharge.aggregate([
174
+ {
175
+ $match: {
176
+ 'borrowerId': new mongoose_1.default.Types.ObjectId(borrowerId),
177
+ 'deletedAt': { $exists: false },
178
+ },
179
+ }, {
180
+ $sort: {
181
+ 'productId': 1,
182
+ 'order': 1,
183
+ },
184
+ }, {
185
+ $unset: loan_products_db_1.fieldsToUnset,
186
+ }, {
187
+ $addFields: {
188
+ valid: true,
189
+ },
190
+ },
191
+ ]);
192
+ }
193
+ async getLoanChargeForProduct(productId) {
194
+ return (0, loan_charges_db_1.getLoanChargeForProduct)(productId);
195
+ }
196
+ validateLoanCharge(loanCharge) {
197
+ const validationResult = LoanCharges_model_1.chargeViewValidationSchema.validate(loanCharge);
198
+ return !!validationResult.error;
199
+ }
200
+ async saveLoanCharges(borrowerId, loanCharges, quickCreate = false) {
201
+ const mappedCharges = loanCharges.map((charge, order) => {
202
+ return { ...charge, borrowerId: new mongoose_1.default.Types.ObjectId(borrowerId), order };
203
+ });
204
+ const allCharges = await this.getLoanCharges(borrowerId);
205
+ const chargeIdsToRemove = allCharges.map((charge) => charge._id.toString());
206
+ await Promise.all(mappedCharges.map(async (charge) => {
207
+ if (!quickCreate) {
208
+ if (!this.validateCharge(charge)) {
209
+ if (charge._id) {
210
+ const index = chargeIdsToRemove.indexOf(charge._id.toString());
211
+ if (index > -1) {
212
+ chargeIdsToRemove.splice(index, 1);
213
+ }
214
+ }
215
+ return;
216
+ }
217
+ }
218
+ if (!charge._id || charge._id.toString().includes('new_')) {
219
+ const { _id, ...rest } = charge;
220
+ const newCharge = new LoanCharges_model_1.LoanCharge(rest);
221
+ await newCharge.save();
222
+ }
223
+ else {
224
+ await LoanCharges_model_1.LoanCharge.findByIdAndUpdate(charge._id, charge);
225
+ const index = chargeIdsToRemove.indexOf(charge._id.toString());
226
+ if (index > -1) {
227
+ chargeIdsToRemove.splice(index, 1);
228
+ }
229
+ }
230
+ }));
231
+ if (!quickCreate) {
232
+ await Promise.all(chargeIdsToRemove.map(async (id) => {
233
+ await LoanCharges_model_1.LoanCharge.findByIdAndUpdate(id, { deletedAt: new Date() });
234
+ }));
235
+ }
236
+ }
237
+ async getChargeIds(productIds, chargeType = null) {
238
+ const loanChargesGrouped = await Promise.all(productIds.map(async (productId) => {
239
+ return await this.getLoanChargeForProduct(productId);
240
+ }));
241
+ const loanCharges = loanChargesGrouped.reduce((acc, group) => [...acc, ...group], []);
242
+ const filteredCharges = chargeType ? loanCharges.filter((charge) => charge.chargeType === chargeType) : loanCharges;
243
+ return filteredCharges.map((charge) => charge._id);
244
+ }
245
+ async getLoanStatementsForProduct(filter, paginatorOptions = null, chargeType = null) {
246
+ const chargesIds = await this.getChargeIds(filter.productIds, chargeType);
247
+ return LoanStatementTransaction_model_1.LoanStatementTransactionModel.aggregate([
248
+ {
249
+ $match: {
250
+ $and: [
251
+ { 'date': { $gte: filter.start } },
252
+ { 'date': { $lte: (0, dayjs_1.default)(filter.end).endOf('day').toDate() } },
253
+ { 'chargeId': { '$in': chargesIds } },
254
+ ],
255
+ },
256
+ }, {
257
+ $sort: {
258
+ 'date': 1,
259
+ 'order': 1,
260
+ },
261
+ },
262
+ ...(0, collaterals_db_1.ITEMS_PAGINATION)(paginatorOptions),
263
+ ]);
264
+ }
265
+ async getLoanStatementsForIdsAndCharge(filter, paginatorOptions = null, chargeType = null) {
266
+ const chargesIds = await this.getChargeIds(filter.productIds, chargeType);
267
+ return LoanStatementTransaction_model_1.LoanStatementTransactionModel.aggregate([
268
+ {
269
+ $match: {
270
+ $and: [
271
+ { '_id': { '$in': filter.transactionIds } },
272
+ { 'chargeId': { '$in': chargesIds } },
273
+ ],
274
+ },
275
+ }, {
276
+ $sort: {
277
+ 'date': 1,
278
+ 'order': 1,
279
+ },
280
+ },
281
+ ...(0, collaterals_db_1.ITEMS_PAGINATION)(paginatorOptions),
282
+ ]);
283
+ }
284
+ async getLoanStatementsForForProductWithCharges(filter, paginatorOptions) {
285
+ const loanChargesGrouped = await Promise.all(filter.productIds.map(async (productId) => {
286
+ return await this.getLoanChargeForProduct(productId);
287
+ }));
288
+ const loanCharges = loanChargesGrouped.reduce((acc, group) => [...acc, ...group], []);
289
+ const chargesIds = loanCharges.map((charge) => charge._id);
290
+ return LoanStatementTransaction_model_1.LoanStatementTransactionModel.aggregate([
291
+ {
292
+ $match: {
293
+ $and: [
294
+ { 'date': { $gte: filter.start } },
295
+ { 'date': { $lte: filter.end } },
296
+ { 'chargeId': { '$in': chargesIds } },
297
+ ],
298
+ },
299
+ }, {
300
+ $sort: {
301
+ 'date': 1,
302
+ 'order': 1,
303
+ },
304
+ }, {
305
+ $lookup: {
306
+ from: 'loan_charges',
307
+ localField: 'chargeId',
308
+ foreignField: '_id',
309
+ as: 'charge',
310
+ },
311
+ }, {
312
+ $unwind: {
313
+ path: '$charge',
314
+ },
315
+ },
316
+ ...(0, collaterals_db_1.ITEMS_PAGINATION)(paginatorOptions),
317
+ ]);
318
+ }
319
+ async getTotalLoanStatementsForProduct(filter) {
320
+ const loanChargesGrouped = await Promise.all(filter.productIds.map(async (productId) => {
321
+ return await this.getLoanChargeForProduct(productId);
322
+ }));
323
+ const loanCharges = loanChargesGrouped.reduce((acc, group) => [...acc, ...group], []);
324
+ const chargesIds = loanCharges.map((charge) => charge._id);
325
+ return LoanStatementTransaction_model_1.LoanStatementTransactionModel.countDocuments({
326
+ $and: [
327
+ { 'chargeId': { $in: chargesIds } },
328
+ { 'date': { $gte: filter.start } },
329
+ { 'date': { $lte: filter.end } },
330
+ ],
331
+ });
332
+ }
333
+ async updatedChargeCodes(productId) {
334
+ const product = await LoanProducts_model_1.LoanProduct.findById(productId).lean();
335
+ const borrowerProductCode = product.code.trim().slice(3, 6);
336
+ const charges = await this.getLoanChargeForProduct(productId);
337
+ await Promise.all(charges.map(async (charge) => {
338
+ const newCode = charge.code.trim().slice(0, 3) + borrowerProductCode;
339
+ await LoanCharges_model_1.LoanCharge.findByIdAndUpdate(charge._id, { code: newCode });
340
+ }));
341
+ }
342
+ }
343
+ exports.LoanChargesService = LoanChargesService;