gemcap-be-common 1.2.1 → 1.2.3

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 (71) hide show
  1. package/db/index.d.ts +4 -1
  2. package/db/index.js +4 -1
  3. package/db/index.ts +4 -1
  4. package/db/inventory-availability.db.d.ts +1 -2
  5. package/db/inventory-availability.db.js +2 -3
  6. package/db/inventory-availability.db.ts +7 -2
  7. package/db/inventory-seasonal-rates.db.d.ts +1 -1
  8. package/db/inventory-seasonal-rates.db.js +5 -5
  9. package/db/inventory-seasonal-rates.db.ts +1 -1
  10. package/db/loan-products.db.d.ts +33 -0
  11. package/db/loan-products.db.js +139 -0
  12. package/db/loan-products.db.ts +146 -0
  13. package/db/loan-statement.db.d.ts +2 -0
  14. package/db/loan-statement.db.js +59 -0
  15. package/db/loan-statement.db.ts +53 -0
  16. package/db/microservice-tasks.db.d.ts +1 -1
  17. package/db/microservice-tasks.db.ts +1 -2
  18. package/db/{receivables.ts → receivables.db.ts} +2 -1
  19. package/db/user-logs.db.d.ts +36 -0
  20. package/db/user-logs.db.js +41 -0
  21. package/db/user-logs.db.ts +48 -0
  22. package/enums/index.d.ts +2 -1
  23. package/enums/index.js +2 -1
  24. package/enums/index.ts +2 -1
  25. package/interfaces/collaterals.interface.d.ts +1 -3
  26. package/interfaces/collaterals.interface.ts +3 -5
  27. package/interfaces/index.d.ts +1 -0
  28. package/interfaces/index.js +1 -0
  29. package/interfaces/index.ts +1 -0
  30. package/models/AvailabilitySigns.model.ts +1 -0
  31. package/models/Banks.model.d.ts +45 -0
  32. package/models/Banks.model.js +39 -0
  33. package/models/Banks.model.ts +55 -0
  34. package/models/DeletedRecords.model.d.ts +35 -0
  35. package/models/DeletedRecords.model.js +21 -0
  36. package/models/DeletedRecords.model.ts +28 -0
  37. package/models/InventoryManualEntry.model.d.ts +2 -0
  38. package/models/InventorySeasonalRates.model.d.ts +3 -0
  39. package/models/LoanCharges.model.d.ts +58 -0
  40. package/models/LoanCharges.model.js +207 -0
  41. package/models/LoanCharges.model.ts +238 -0
  42. package/models/LoanProducts.model.d.ts +60 -0
  43. package/models/LoanProducts.model.js +93 -0
  44. package/models/LoanProducts.model.ts +130 -0
  45. package/models/LoanStatementTransaction.model.d.ts +50 -0
  46. package/models/LoanStatementTransaction.model.js +64 -0
  47. package/models/LoanStatementTransaction.model.ts +94 -0
  48. package/models/LoanTransaction.model.d.ts +76 -0
  49. package/models/LoanTransaction.model.js +160 -0
  50. package/models/LoanTransaction.model.ts +218 -0
  51. package/models/MicroserviceTask.model.d.ts +1 -2
  52. package/models/MicroserviceTask.model.ts +1 -2
  53. package/models/PostponedTransactions.model.d.ts +39 -0
  54. package/models/PostponedTransactions.model.js +28 -0
  55. package/models/PostponedTransactions.model.ts +42 -0
  56. package/models/UserLog.model.d.ts +55 -0
  57. package/models/UserLog.model.js +55 -0
  58. package/models/UserLog.model.ts +72 -0
  59. package/models/Yield.model.d.ts +46 -0
  60. package/models/Yield.model.js +48 -0
  61. package/models/Yield.model.ts +76 -0
  62. package/models/_models.d.ts +1 -0
  63. package/models/_models.js +2 -1
  64. package/models/_models.ts +2 -1
  65. package/models/index.d.ts +9 -0
  66. package/models/index.js +9 -0
  67. package/models/index.ts +9 -0
  68. package/package.json +1 -1
  69. package/tsconfig.tsbuildinfo +1 -1
  70. package/db/{receivables.d.ts → receivables.db.d.ts} +0 -0
  71. package/db/{receivables.js → receivables.db.js} +1 -1
package/db/index.d.ts CHANGED
@@ -5,5 +5,8 @@ export * from './collaterals.db';
5
5
  export * from './inventory-availability.db';
6
6
  export * from './inventory-manual-entry.db';
7
7
  export * from './inventory-seasonal-rates.db';
8
+ export * from './loan-products.db';
9
+ export * from './loan-statement.db';
8
10
  export * from './microservice-tasks.db';
9
- export * from './receivables';
11
+ export * from './receivables.db';
12
+ export * from './user-logs.db';
package/db/index.js CHANGED
@@ -21,5 +21,8 @@ __exportStar(require("./collaterals.db"), exports);
21
21
  __exportStar(require("./inventory-availability.db"), exports);
22
22
  __exportStar(require("./inventory-manual-entry.db"), exports);
23
23
  __exportStar(require("./inventory-seasonal-rates.db"), exports);
24
+ __exportStar(require("./loan-products.db"), exports);
25
+ __exportStar(require("./loan-statement.db"), exports);
24
26
  __exportStar(require("./microservice-tasks.db"), exports);
25
- __exportStar(require("./receivables"), exports);
27
+ __exportStar(require("./receivables.db"), exports);
28
+ __exportStar(require("./user-logs.db"), exports);
package/db/index.ts CHANGED
@@ -5,5 +5,8 @@ export * from './collaterals.db';
5
5
  export * from './inventory-availability.db';
6
6
  export * from './inventory-manual-entry.db';
7
7
  export * from './inventory-seasonal-rates.db';
8
+ export * from './loan-products.db';
9
+ export * from './loan-statement.db';
8
10
  export * from './microservice-tasks.db';
9
- export * from './receivables';
11
+ export * from './receivables.db';
12
+ export * from './user-logs.db';
@@ -1,5 +1,4 @@
1
- import { IInventoryAvailabilityDocument, IInventoryAvailabilitySummary } from '../models';
2
- import { IInventoryAvailabilityItem } from '../models';
1
+ import { IInventoryAvailabilityDocument, IInventoryAvailabilityItem, IInventoryAvailabilitySummary } from '../models';
3
2
  export declare const getInventoryAvailabilityByBBCDateId: (bbcDateId: string) => Promise<IInventoryAvailabilityDocument>;
4
3
  export declare const saveInventoryAvailabilityItems: (inventoryAvailabilityId: string, items: IInventoryAvailabilityItem[]) => Promise<void>;
5
4
  export declare const saveInventoryAvailabilitySummary: (inventoryAvailabilityId: string, summary: IInventoryAvailabilitySummary[]) => Promise<void>;
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setManualInputsToggle = exports.updateAvailabilityStatus = exports.saveInventoryAvailabilitySummary = exports.saveInventoryAvailabilityItems = exports.getInventoryAvailabilityByBBCDateId = void 0;
4
4
  const models_1 = require("../models");
5
- const models_2 = require("../models");
6
5
  const getInventoryAvailabilityByBBCDateId = async (bbcDateId) => {
7
6
  return models_1.InventoryAvailabilityModel
8
7
  .findOne({ bbcDateId })
@@ -10,8 +9,8 @@ const getInventoryAvailabilityByBBCDateId = async (bbcDateId) => {
10
9
  };
11
10
  exports.getInventoryAvailabilityByBBCDateId = getInventoryAvailabilityByBBCDateId;
12
11
  const saveInventoryAvailabilityItems = async (inventoryAvailabilityId, items) => {
13
- await models_2.InventoryAvailabilityItemModel.deleteMany({ inventoryAvailabilityId });
14
- await models_2.InventoryAvailabilityItemModel.insertMany(items);
12
+ await models_1.InventoryAvailabilityItemModel.deleteMany({ inventoryAvailabilityId });
13
+ await models_1.InventoryAvailabilityItemModel.insertMany(items);
15
14
  };
16
15
  exports.saveInventoryAvailabilityItems = saveInventoryAvailabilityItems;
17
16
  const saveInventoryAvailabilitySummary = async (inventoryAvailabilityId, summary) => {
@@ -1,5 +1,10 @@
1
- import { IInventoryAvailabilityDocument, IInventoryAvailabilitySummary, InventoryAvailabilityModel } from '../models';
2
- import { IInventoryAvailabilityItem, InventoryAvailabilityItemModel } from '../models';
1
+ import {
2
+ IInventoryAvailabilityDocument,
3
+ IInventoryAvailabilityItem,
4
+ IInventoryAvailabilitySummary,
5
+ InventoryAvailabilityItemModel,
6
+ InventoryAvailabilityModel,
7
+ } from '../models';
3
8
 
4
9
  export const getInventoryAvailabilityByBBCDateId = async (bbcDateId: string): Promise<IInventoryAvailabilityDocument> => {
5
10
  return InventoryAvailabilityModel
@@ -1,4 +1,4 @@
1
- import { IInventorySeasonalRatesDoc, IInventorySeasonalRatesItemDoc } from '../models/InventorySeasonalRates.model';
1
+ import { IInventorySeasonalRatesDoc, IInventorySeasonalRatesItemDoc } from '../models';
2
2
  export declare const getNewSeasonalRates: (borrowerId: string) => Promise<IInventorySeasonalRatesDoc>;
3
3
  export declare const getSeasonalRates: (borrowerId: string) => Promise<IInventorySeasonalRatesDoc>;
4
4
  export declare const saveSeasonalRates: (borrowerId: string, rates: IInventorySeasonalRatesItemDoc[]) => Promise<void>;
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.saveSeasonalRates = exports.getSeasonalRates = exports.getNewSeasonalRates = void 0;
7
7
  const mongoose_1 = __importDefault(require("mongoose"));
8
- const InventorySeasonalRates_model_1 = require("../models/InventorySeasonalRates.model");
8
+ const models_1 = require("../models");
9
9
  const getNewSeasonalRates = async (borrowerId) => {
10
10
  return {
11
11
  borrowerId: new mongoose_1.default.Types.ObjectId(borrowerId),
@@ -14,7 +14,7 @@ const getNewSeasonalRates = async (borrowerId) => {
14
14
  };
15
15
  exports.getNewSeasonalRates = getNewSeasonalRates;
16
16
  const getSeasonalRates = async (borrowerId) => {
17
- const foundRates = await InventorySeasonalRates_model_1.InventorySeasonalEntryModel.findOne({ borrowerId });
17
+ const foundRates = await models_1.InventorySeasonalEntryModel.findOne({ borrowerId });
18
18
  if (foundRates) {
19
19
  return foundRates;
20
20
  }
@@ -22,20 +22,20 @@ const getSeasonalRates = async (borrowerId) => {
22
22
  };
23
23
  exports.getSeasonalRates = getSeasonalRates;
24
24
  const saveSeasonalRates = async (borrowerId, rates) => {
25
- const foundRates = await InventorySeasonalRates_model_1.InventorySeasonalEntryModel.findOne({ borrowerId });
25
+ const foundRates = await models_1.InventorySeasonalEntryModel.findOne({ borrowerId });
26
26
  if (foundRates) {
27
27
  const mappedRates = rates.map((rate) => {
28
28
  const { _id, ...restRate } = rate;
29
29
  return restRate;
30
30
  });
31
- await InventorySeasonalRates_model_1.InventorySeasonalEntryModel.findByIdAndUpdate(foundRates._id, { items: mappedRates });
31
+ await models_1.InventorySeasonalEntryModel.findByIdAndUpdate(foundRates._id, { items: mappedRates });
32
32
  }
33
33
  else {
34
34
  const mappedRates = rates.map((rate) => {
35
35
  const { _id, ...restRate } = rate;
36
36
  return restRate;
37
37
  });
38
- const newRates = await InventorySeasonalRates_model_1.InventorySeasonalEntryModel.create({ borrowerId, items: mappedRates });
38
+ const newRates = await models_1.InventorySeasonalEntryModel.create({ borrowerId, items: mappedRates });
39
39
  await newRates.save();
40
40
  }
41
41
  };
@@ -4,7 +4,7 @@ import {
4
4
  IInventorySeasonalRatesDoc,
5
5
  IInventorySeasonalRatesItemDoc,
6
6
  InventorySeasonalEntryModel,
7
- } from '../models/InventorySeasonalRates.model';
7
+ } from '../models';
8
8
 
9
9
  export const getNewSeasonalRates = async (borrowerId: string) => {
10
10
  return <IInventorySeasonalRatesDoc>{
@@ -0,0 +1,33 @@
1
+ /// <reference types="mongoose/types/aggregate" />
2
+ /// <reference types="mongoose/types/callback" />
3
+ /// <reference types="mongoose/types/collection" />
4
+ /// <reference types="mongoose/types/connection" />
5
+ /// <reference types="mongoose/types/cursor" />
6
+ /// <reference types="mongoose/types/document" />
7
+ /// <reference types="mongoose/types/error" />
8
+ /// <reference types="mongoose/types/expressions" />
9
+ /// <reference types="mongoose/types/helpers" />
10
+ /// <reference types="mongoose/types/middlewares" />
11
+ /// <reference types="mongoose/types/indexes" />
12
+ /// <reference types="mongoose/types/models" />
13
+ /// <reference types="mongoose/types/mongooseoptions" />
14
+ /// <reference types="mongoose/types/pipelinestage" />
15
+ /// <reference types="mongoose/types/populate" />
16
+ /// <reference types="mongoose/types/query" />
17
+ /// <reference types="mongoose/types/schemaoptions" />
18
+ /// <reference types="mongoose/types/schematypes" />
19
+ /// <reference types="mongoose/types/session" />
20
+ /// <reference types="mongoose/types/types" />
21
+ /// <reference types="mongoose/types/utility" />
22
+ /// <reference types="mongoose/types/validation" />
23
+ /// <reference types="mongoose/types/virtuals" />
24
+ /// <reference types="mongoose/types/inferschematype" />
25
+ import mongoose from 'mongoose';
26
+ export declare const getLoanProductById: (productId: string) => Promise<mongoose.FlattenMaps<import("../models").ILoanProductDoc> & {
27
+ _id: mongoose.Types.ObjectId;
28
+ }>;
29
+ export declare const getLoanProductBalance: (productId: string, forDate?: Date) => Promise<{
30
+ balance: number;
31
+ floatedBalance: number;
32
+ }>;
33
+ export declare const getPostponedTransactions: (date: Date, productId: string, removePostponed?: boolean) => Promise<number>;
@@ -0,0 +1,139 @@
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.getPostponedTransactions = exports.getLoanProductBalance = exports.getLoanProductById = void 0;
7
+ const mongoose_1 = __importDefault(require("mongoose"));
8
+ const dayjs_1 = __importDefault(require("dayjs"));
9
+ const helpers_1 = require("../helpers");
10
+ const loan_statement_db_1 = require("./loan-statement.db");
11
+ const models_1 = require("../models");
12
+ const getLoanProductById = async (productId) => {
13
+ return models_1.LoanProduct.findById(productId).lean();
14
+ };
15
+ exports.getLoanProductById = getLoanProductById;
16
+ const getLoanProductBalance = async (productId, forDate = new Date()) => {
17
+ const simpleTransactions = await models_1.LoanTransaction.aggregate([
18
+ {
19
+ $match: {
20
+ 'productId': new mongoose_1.default.Types.ObjectId(productId),
21
+ 'date': { $lte: (0, dayjs_1.default)(forDate).startOf('day').subtract(1, 'seconds').toDate() },
22
+ },
23
+ }, {
24
+ $sort: {
25
+ 'date': -1,
26
+ 'order': -1,
27
+ 'createdAt': -1,
28
+ },
29
+ }, {
30
+ $limit: 1,
31
+ },
32
+ ]);
33
+ const collectionsTransactions = await models_1.LoanTransaction.aggregate([
34
+ {
35
+ $match: {
36
+ 'productId': new mongoose_1.default.Types.ObjectId(productId),
37
+ 'effectiveDate': { $lte: forDate },
38
+ $expr: { $ne: ['$date', '$effectiveDate'] },
39
+ },
40
+ }, {
41
+ $sort: {
42
+ 'effectiveDate': -1,
43
+ 'order': -1,
44
+ 'createdAt': -1,
45
+ },
46
+ }, {
47
+ $limit: 1,
48
+ },
49
+ ]);
50
+ const normalTransactions = await models_1.LoanTransaction.aggregate([
51
+ {
52
+ $match: {
53
+ 'productId': new mongoose_1.default.Types.ObjectId(productId),
54
+ 'effectiveDate': { $lte: forDate },
55
+ $expr: { $eq: ['$date', '$effectiveDate'] },
56
+ },
57
+ }, {
58
+ $sort: {
59
+ 'effectiveDate': -1,
60
+ 'order': -1,
61
+ 'createdAt': -1,
62
+ },
63
+ }, {
64
+ $limit: 1,
65
+ },
66
+ ]);
67
+ const transactions = [...collectionsTransactions, ...normalTransactions].sort((a, b) => {
68
+ if (a.date.valueOf() === b.date.valueOf()) {
69
+ return a.effectiveDate.valueOf() - b.effectiveDate.valueOf();
70
+ }
71
+ return a.date.valueOf() - b.date.valueOf();
72
+ });
73
+ const collectionsTransaction = collectionsTransactions.length === 0 ? null : collectionsTransactions[0];
74
+ const normalTransaction = normalTransactions.length === 0 ? null : normalTransactions[0];
75
+ const balance = simpleTransactions.length === 0 ? 0 : simpleTransactions[0].balance;
76
+ let floatedBalance = 0;
77
+ if (normalTransaction && collectionsTransaction) {
78
+ const totalCollections = await (0, loan_statement_db_1.getTotalForDate)(productId, collectionsTransaction.effectiveDate);
79
+ if (normalTransaction.date.valueOf() === collectionsTransaction.date.valueOf()) {
80
+ floatedBalance = (0, helpers_1.roundToXDigits)(normalTransaction.floatedBalance + totalCollections);
81
+ }
82
+ if (normalTransaction.date.valueOf() > collectionsTransaction.date.valueOf()) {
83
+ floatedBalance = normalTransaction.floatedBalance;
84
+ }
85
+ if (normalTransaction.date.valueOf() < collectionsTransaction.date.valueOf()) {
86
+ floatedBalance = collectionsTransaction.balance;
87
+ }
88
+ }
89
+ if (normalTransaction && !collectionsTransaction) {
90
+ floatedBalance = normalTransaction.floatedBalance;
91
+ }
92
+ const totalPostponed = await (0, exports.getPostponedTransactions)(forDate, productId);
93
+ if (transactions.length === 0) {
94
+ return { balance: 0, floatedBalance: totalPostponed };
95
+ }
96
+ return {
97
+ balance: balance,
98
+ floatedBalance: (0, helpers_1.roundToXDigits)(floatedBalance),
99
+ };
100
+ };
101
+ exports.getLoanProductBalance = getLoanProductBalance;
102
+ const getPostponedTransactions = async (date, productId, removePostponed = false) => {
103
+ const currentDay = (0, dayjs_1.default)(new Date(date)).format('YYYY-MM-DD');
104
+ const postponedTransactions = await models_1.PostponedTransaction.aggregate([
105
+ {
106
+ $match: {
107
+ 'productId': new mongoose_1.default.Types.ObjectId(productId),
108
+ },
109
+ }, {
110
+ $lookup: {
111
+ from: 'loan_transactions',
112
+ localField: 'transactionId',
113
+ foreignField: '_id',
114
+ as: 'transaction',
115
+ },
116
+ }, {
117
+ $unwind: {
118
+ path: '$transaction',
119
+ },
120
+ },
121
+ ]);
122
+ const currentDate = (0, dayjs_1.default)(`${currentDay}T00:00:00.000Z`);
123
+ const mappedTransactions = postponedTransactions.reduce((acc, t) => {
124
+ const transactionDate = (0, dayjs_1.default)(`${t.postponedToDate}T00:00:00.000Z`);
125
+ if (transactionDate <= currentDate) {
126
+ return [...acc, t];
127
+ }
128
+ return acc;
129
+ }, []);
130
+ let totalAmount = 0;
131
+ await Promise.all(mappedTransactions.map(async (t) => {
132
+ totalAmount = (0, helpers_1.roundToXDigits)(totalAmount + t.transaction.amount);
133
+ if (removePostponed) {
134
+ await models_1.PostponedTransaction.findByIdAndDelete(t._id);
135
+ }
136
+ }));
137
+ return totalAmount;
138
+ };
139
+ exports.getPostponedTransactions = getPostponedTransactions;
@@ -0,0 +1,146 @@
1
+ import mongoose from 'mongoose';
2
+ import dayjs from 'dayjs';
3
+
4
+ import { roundToXDigits } from '../helpers';
5
+ import { getTotalForDate } from './loan-statement.db';
6
+ import {
7
+ IPostponedTransactionWithTransaction,
8
+ PostponedTransaction,
9
+ LoanProduct,
10
+ LoanTransaction,
11
+ ILoanTransactionDoc,
12
+ } from '../models';
13
+
14
+ export const getLoanProductById = async (productId: string) => {
15
+ return LoanProduct.findById(productId).lean();
16
+ }
17
+
18
+ export const getLoanProductBalance = async (productId: string, forDate = new Date()) => {
19
+
20
+ const simpleTransactions = await LoanTransaction.aggregate<ILoanTransactionDoc>([
21
+ {
22
+ $match: {
23
+ 'productId': new mongoose.Types.ObjectId(productId),
24
+ 'date': { $lte: dayjs(forDate).startOf('day').subtract(1, 'seconds').toDate() },
25
+ },
26
+ }, {
27
+ $sort: {
28
+ 'date': -1,
29
+ 'order': -1,
30
+ 'createdAt': -1,
31
+ },
32
+ }, {
33
+ $limit: 1,
34
+ },
35
+ ]);
36
+
37
+ const collectionsTransactions = await LoanTransaction.aggregate<ILoanTransactionDoc>([
38
+ {
39
+ $match: {
40
+ 'productId': new mongoose.Types.ObjectId(productId),
41
+ 'effectiveDate': { $lte: forDate },
42
+ $expr: { $ne: ['$date', '$effectiveDate'] },
43
+ },
44
+ }, {
45
+ $sort: {
46
+ 'effectiveDate': -1,
47
+ 'order': -1,
48
+ 'createdAt': -1,
49
+ },
50
+ }, {
51
+ $limit: 1,
52
+ },
53
+ ]);
54
+
55
+ const normalTransactions = await LoanTransaction.aggregate<ILoanTransactionDoc>([
56
+ {
57
+ $match: {
58
+ 'productId': new mongoose.Types.ObjectId(productId),
59
+ 'effectiveDate': { $lte: forDate },
60
+ $expr: { $eq: ['$date', '$effectiveDate'] },
61
+ },
62
+ }, {
63
+ $sort: {
64
+ 'effectiveDate': -1,
65
+ 'order': -1,
66
+ 'createdAt': -1,
67
+ },
68
+ }, {
69
+ $limit: 1,
70
+ },
71
+ ]);
72
+
73
+
74
+ const transactions = [...collectionsTransactions, ...normalTransactions].sort((a, b) => {
75
+ if (a.date.valueOf() === b.date.valueOf()) {
76
+ return a.effectiveDate.valueOf() - b.effectiveDate.valueOf();
77
+ }
78
+ return a.date.valueOf() - b.date.valueOf();
79
+ });
80
+ const collectionsTransaction = collectionsTransactions.length === 0 ? null : collectionsTransactions[0];
81
+ const normalTransaction = normalTransactions.length === 0 ? null : normalTransactions[0];
82
+ const balance = simpleTransactions.length === 0 ? 0 : simpleTransactions[0].balance;
83
+ let floatedBalance = 0;
84
+
85
+ if (normalTransaction && collectionsTransaction) {
86
+ const totalCollections = await getTotalForDate(productId, collectionsTransaction.effectiveDate);
87
+ if (normalTransaction.date.valueOf() === collectionsTransaction.date.valueOf()) {
88
+ floatedBalance = roundToXDigits(normalTransaction.floatedBalance + totalCollections);
89
+ }
90
+ if (normalTransaction.date.valueOf() > collectionsTransaction.date.valueOf()) {
91
+ floatedBalance = normalTransaction.floatedBalance;
92
+ }
93
+ if (normalTransaction.date.valueOf() < collectionsTransaction.date.valueOf()) {
94
+ floatedBalance = collectionsTransaction.balance;
95
+ }
96
+ }
97
+ if (normalTransaction && !collectionsTransaction) {
98
+ floatedBalance = normalTransaction.floatedBalance;
99
+ }
100
+ const totalPostponed = await getPostponedTransactions(forDate, productId);
101
+ if (transactions.length === 0) {
102
+ return { balance: 0, floatedBalance: totalPostponed };
103
+ }
104
+ return {
105
+ balance: balance,
106
+ floatedBalance: roundToXDigits(floatedBalance),
107
+ };
108
+ }
109
+
110
+ export const getPostponedTransactions = async (date: Date, productId: string, removePostponed = false) => {
111
+ const currentDay = dayjs(new Date(date)).format('YYYY-MM-DD');
112
+ const postponedTransactions = await PostponedTransaction.aggregate<IPostponedTransactionWithTransaction>([
113
+ {
114
+ $match: {
115
+ 'productId': new mongoose.Types.ObjectId(productId),
116
+ },
117
+ }, {
118
+ $lookup: {
119
+ from: 'loan_transactions',
120
+ localField: 'transactionId',
121
+ foreignField: '_id',
122
+ as: 'transaction',
123
+ },
124
+ }, {
125
+ $unwind: {
126
+ path: '$transaction',
127
+ },
128
+ },
129
+ ]);
130
+ const currentDate = dayjs(`${currentDay}T00:00:00.000Z`);
131
+ const mappedTransactions = postponedTransactions.reduce((acc, t) => {
132
+ const transactionDate = dayjs(`${t.postponedToDate}T00:00:00.000Z`);
133
+ if (transactionDate <= currentDate) {
134
+ return [...acc, t];
135
+ }
136
+ return acc;
137
+ }, (<IPostponedTransactionWithTransaction[]>[]));
138
+ let totalAmount = 0;
139
+ await Promise.all(mappedTransactions.map(async (t) => {
140
+ totalAmount = roundToXDigits(totalAmount + t.transaction.amount);
141
+ if (removePostponed) {
142
+ await PostponedTransaction.findByIdAndDelete(t._id);
143
+ }
144
+ }));
145
+ return totalAmount;
146
+ }
@@ -0,0 +1,2 @@
1
+ export declare const getTotalTransactionAmountForCharge: (chargeId: string, start: Date, end: Date) => Promise<any>;
2
+ export declare const getTotalForDate: (productId: string, date: Date) => Promise<number>;
@@ -0,0 +1,59 @@
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.getTotalForDate = exports.getTotalTransactionAmountForCharge = void 0;
7
+ const mongoose_1 = __importDefault(require("mongoose"));
8
+ const models_1 = require("../models");
9
+ const getTotalTransactionAmountForCharge = async (chargeId, start, end) => {
10
+ const results = await models_1.LoanStatementTransactionModel.aggregate([
11
+ {
12
+ '$match': {
13
+ $and: [
14
+ { 'date': { $gte: start } },
15
+ { 'date': { $lte: end } },
16
+ { 'chargeId': new mongoose_1.default.Types.ObjectId(chargeId) },
17
+ ],
18
+ },
19
+ }, {
20
+ '$group': {
21
+ '_id': '$chargeId',
22
+ 'totalAmount': { '$sum': '$amount' },
23
+ },
24
+ }, {
25
+ '$project': {
26
+ '_id': 0,
27
+ 'totalAmount': { $round: ['$totalAmount', 2] },
28
+ },
29
+ },
30
+ ]);
31
+ if (results.length === 0) {
32
+ return 0;
33
+ }
34
+ else {
35
+ return results[0].totalAmount;
36
+ }
37
+ };
38
+ exports.getTotalTransactionAmountForCharge = getTotalTransactionAmountForCharge;
39
+ const getTotalForDate = async (productId, date) => {
40
+ const totalAmount = await models_1.LoanTransaction.aggregate([
41
+ {
42
+ $match: {
43
+ 'productId': new mongoose_1.default.Types.ObjectId(productId),
44
+ 'effectiveDate': date,
45
+ $expr: { $ne: ['$date', '$effectiveDate'] },
46
+ },
47
+ }, {
48
+ $group: {
49
+ '_id': '$effectiveDate',
50
+ 'totalAmount': { $sum: '$amount' },
51
+ },
52
+ },
53
+ ]);
54
+ if (totalAmount.length === 0) {
55
+ return 0;
56
+ }
57
+ return totalAmount[0].totalAmount;
58
+ };
59
+ exports.getTotalForDate = getTotalForDate;
@@ -0,0 +1,53 @@
1
+ import mongoose from 'mongoose';
2
+
3
+ import { LoanStatementTransactionModel, LoanTransaction } from '../models';
4
+
5
+ export const getTotalTransactionAmountForCharge = async (chargeId: string, start: Date, end: Date) => {
6
+ const results = await LoanStatementTransactionModel.aggregate([
7
+ {
8
+ '$match': {
9
+ $and: [
10
+ { 'date': { $gte: start } },
11
+ { 'date': { $lte: end } },
12
+ { 'chargeId': new mongoose.Types.ObjectId(chargeId) },
13
+ ],
14
+ },
15
+ }, {
16
+ '$group': {
17
+ '_id': '$chargeId',
18
+ 'totalAmount': { '$sum': '$amount' },
19
+ },
20
+ }, {
21
+ '$project': {
22
+ '_id': 0,
23
+ 'totalAmount': { $round: ['$totalAmount', 2] },
24
+ },
25
+ },
26
+ ]);
27
+ if (results.length === 0) {
28
+ return 0;
29
+ } else {
30
+ return results[0].totalAmount;
31
+ }
32
+ };
33
+
34
+ export const getTotalForDate = async (productId: string, date: Date): Promise<number> => {
35
+ const totalAmount = await LoanTransaction.aggregate<{ _id: Date, totalAmount: number }>([
36
+ {
37
+ $match: {
38
+ 'productId': new mongoose.Types.ObjectId(productId),
39
+ 'effectiveDate': date,
40
+ $expr: { $ne: ['$date', '$effectiveDate'] },
41
+ },
42
+ }, {
43
+ $group: {
44
+ '_id': '$effectiveDate',
45
+ 'totalAmount': { $sum: '$amount' },
46
+ },
47
+ },
48
+ ]);
49
+ if (totalAmount.length === 0) {
50
+ return 0;
51
+ }
52
+ return totalAmount[0].totalAmount;
53
+ }
@@ -1,5 +1,5 @@
1
- import { EMicroserviceTask } from '../enums/microservice-task.enum';
2
1
  import { IMicroserviceTaskDoc } from '../models';
2
+ import { EMicroserviceTask } from '../enums';
3
3
  export declare const createMicroserviceTasks: (cmd: EMicroserviceTask, params: any, executeAt?: Date) => Promise<void>;
4
4
  export declare const findFirstTask: () => Promise<IMicroserviceTaskDoc>;
5
5
  export declare const getAllTasks: () => Promise<IMicroserviceTaskDoc[]>;
@@ -1,6 +1,5 @@
1
- import { EMicroserviceTask } from '../enums/microservice-task.enum';
2
1
  import { IMicroserviceTaskDoc, MicroserviceTaskModel } from '../models';
3
- import { EMicroserviceTaskStatuses } from '../enums';
2
+ import { EMicroserviceTask, EMicroserviceTaskStatuses } from '../enums';
4
3
 
5
4
  export const createMicroserviceTasks = async (cmd: EMicroserviceTask, params: any, executeAt: Date = new Date()): Promise<void> => {
6
5
  const tasks = await MicroserviceTaskModel.find({ cmd, params, status: EMicroserviceTaskStatuses.SCHEDULED });
@@ -1,10 +1,11 @@
1
1
  import dayjs from 'dayjs';
2
2
  import mongoose from 'mongoose';
3
+
3
4
  import { IReceivableItemDocPopulated, ReceivableItemModel, ReceivableModel } from '../models';
4
5
  import { COLLATERALS_LOOKUP, getBBCSheetsForBorrowerAndType } from './collaterals.db';
5
6
  import { ECollaterals } from '../enums';
6
- import { getBBCSheetsByType, getUniqueDates } from './bbcSheets.db';
7
7
  import { ReceivableAvailabilityModel } from '../models';
8
+ import { getBBCSheetsByType, getUniqueDates } from './bbcSheets.db';
8
9
 
9
10
  export const CUSTOMER_NUMBER_SEPARATOR = '~@~';
10
11
 
@@ -0,0 +1,36 @@
1
+ /// <reference types="mongoose/types/aggregate" />
2
+ /// <reference types="mongoose/types/callback" />
3
+ /// <reference types="mongoose/types/collection" />
4
+ /// <reference types="mongoose/types/connection" />
5
+ /// <reference types="mongoose/types/cursor" />
6
+ /// <reference types="mongoose/types/document" />
7
+ /// <reference types="mongoose/types/error" />
8
+ /// <reference types="mongoose/types/expressions" />
9
+ /// <reference types="mongoose/types/helpers" />
10
+ /// <reference types="mongoose/types/middlewares" />
11
+ /// <reference types="mongoose/types/indexes" />
12
+ /// <reference types="mongoose/types/models" />
13
+ /// <reference types="mongoose/types/mongooseoptions" />
14
+ /// <reference types="mongoose/types/pipelinestage" />
15
+ /// <reference types="mongoose/types/populate" />
16
+ /// <reference types="mongoose/types/query" />
17
+ /// <reference types="mongoose/types/schemaoptions" />
18
+ /// <reference types="mongoose/types/schematypes" />
19
+ /// <reference types="mongoose/types/session" />
20
+ /// <reference types="mongoose/types/types" />
21
+ /// <reference types="mongoose/types/utility" />
22
+ /// <reference types="mongoose/types/validation" />
23
+ /// <reference types="mongoose/types/virtuals" />
24
+ /// <reference types="mongoose/types/inferschematype" />
25
+ import mongoose from 'mongoose';
26
+ import { ELogActionType, ELogType } from '../models';
27
+ export interface ICreateLogParams {
28
+ logType: ELogType;
29
+ userId: string;
30
+ action: ELogActionType;
31
+ details: Object;
32
+ recordId?: mongoose.Schema.Types.ObjectId;
33
+ recordCollection?: string;
34
+ }
35
+ export declare const createLog: (params: ICreateLogParams) => Promise<void>;
36
+ export declare const getLogChanges: (obj1: Object, obj2: Object) => {};