gemcap-be-common 1.5.84 → 1.5.85

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.5.84",
3
+ "version": "1.5.85",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -60,7 +60,7 @@ export declare class ComplianceBorrowersService {
60
60
  isComplianceBorrowerAllowed(userAccess: IUserAccess, requestedBorrowerId: string): boolean;
61
61
  calculateInstanceStatuses(instance: IBorrowerItemInstance, item: IBorrowerItemLean): IInstanceStatus;
62
62
  getFullComplianceBorrowerById(complianceBorrowerId: string): Promise<IComplianceBorrowerDocumentFullWithStatuses>;
63
- getAllBorrowersShortened(userAccess: IUserAccess): Promise<Pick<IComplianceBorrowerWithBorrower, "items" | "borrower" | "fundingStatus">[]>;
63
+ getAllBorrowersShortened(userAccess: IUserAccess): Promise<Pick<IComplianceBorrowerWithBorrower, "borrower" | "fundingStatus" | "items">[]>;
64
64
  generateInstances(complianceBorrowerId: any): Promise<IComplianceBorrowerDocumentFullWithStatuses>;
65
65
  calculateAndUpdateFundingStatus(complianceBorrowerId: string): Promise<void>;
66
66
  calculateFundingStatus(complianceBorrower: IComplianceBorrowerDocumentFull): string;
@@ -60,6 +60,13 @@ export interface ITransactionsFilter {
60
60
  amount?: number;
61
61
  reference?: string;
62
62
  }
63
+ export interface ITransactionsMultiProductsFilter {
64
+ periodStart: Date;
65
+ periodEnd: Date;
66
+ productIds: string[];
67
+ amount?: number;
68
+ reference?: string;
69
+ }
63
70
  export declare class LoanTransactionsService {
64
71
  private readonly attachedFilesService;
65
72
  private readonly banksService;
@@ -80,7 +87,9 @@ export declare class LoanTransactionsService {
80
87
  getTransactionsById(transactionId: string): Promise<mongoose.FlattenMaps<ILoanTransaction> & {
81
88
  _id: mongoose.Types.ObjectId;
82
89
  }>;
90
+ private getLoanTransactionsInternal;
83
91
  getLoanTransactions<T extends boolean>(transactionsFilter: ITransactionsFilter, paginatorOptions: IPaginatorOptions, withBanks: T, isReverse?: boolean, isMinified?: boolean): Promise<(T extends true ? ILoanTransactionViewWithBank : ILoanTransactionView)[]>;
92
+ getLoanTransactionsForProducts<T extends boolean>(transactionsFilter: ITransactionsMultiProductsFilter, paginatorOptions: IPaginatorOptions, withBanks: T, isReverse?: boolean, isMinified?: boolean): Promise<(T extends true ? ILoanTransactionViewWithBank : ILoanTransactionView)[]>;
84
93
  getLoanTransactionsForFilter(periodStart: Date, periodEnd: Date, productId: string): Promise<ILoanTransactionView[]>;
85
94
  getLoanTransactionsForProduct(productId: string): Promise<ILoanTransactionView[]>;
86
95
  getLoanTransactionFiles(): Promise<{
@@ -61,62 +61,82 @@ class LoanTransactionsService {
61
61
  async getTransactionsById(transactionId) {
62
62
  return LoanTransaction_model_1.LoanTransaction.findById(transactionId).lean();
63
63
  }
64
- async getLoanTransactions(transactionsFilter, paginatorOptions, withBanks, isReverse = false, isMinified = false) {
65
- const borrowerProducts = await this.loanChargesService.getLoanProducts(transactionsFilter.borrowerId);
66
- const productIds = borrowerProducts
67
- .filter((product) => transactionsFilter.productId ? product._id.toString() === transactionsFilter.productId : true)
68
- .map((product) => product._id);
64
+ async getLoanTransactionsInternal(productIds, transactionsFilter, paginatorOptions, withBanks, isReverse = false, isMinified = false) {
69
65
  const optionalFilters = [];
70
66
  if (transactionsFilter.amount) {
71
- optionalFilters.push({ 'amount': transactionsFilter.amount });
67
+ optionalFilters.push({ amount: transactionsFilter.amount });
72
68
  }
73
69
  if (transactionsFilter.reference) {
74
- optionalFilters.push({ 'reference': { $regex: transactionsFilter.reference, $options: 'i' } });
75
- }
76
- const bankLookup = !withBanks
77
- ? []
78
- : [
79
- {
80
- $lookup: {
81
- from: 'banks',
82
- localField: 'bankId',
83
- foreignField: '_id',
84
- as: 'bank',
85
- },
86
- }, {
87
- $unwind: {
88
- path: '$bank',
89
- preserveNullAndEmptyArrays: true,
90
- },
70
+ optionalFilters.push({
71
+ reference: {
72
+ $regex: transactionsFilter.reference,
73
+ $options: 'i',
91
74
  },
92
- ];
93
- const projectedFields = isMinified
94
- ? [{ $project: { _id: 1, date: 1, amount: 1, reference: 1, balance: 1, transactionType: 1 } }]
95
- : [];
96
- return LoanTransaction_model_1.LoanTransaction.aggregate([
75
+ });
76
+ }
77
+ const pipeline = [
97
78
  {
98
79
  $match: {
99
- $and: [
100
- { 'date': { $gte: transactionsFilter.periodStart } },
101
- { 'date': { $lte: transactionsFilter.periodEnd } },
102
- { 'productId': { $in: productIds } },
103
- ...optionalFilters,
104
- ],
80
+ date: {
81
+ $gte: transactionsFilter.periodStart,
82
+ $lte: transactionsFilter.periodEnd,
83
+ },
84
+ productId: { $in: productIds },
85
+ ...Object.assign({}, ...optionalFilters),
105
86
  },
106
- }, {
87
+ },
88
+ {
107
89
  $sort: {
108
- 'date': isReverse ? -1 : 1,
109
- 'order': isReverse ? -1 : 1,
110
- 'createdAt': isReverse ? -1 : 1,
90
+ date: isReverse ? -1 : 1,
91
+ order: isReverse ? -1 : 1,
92
+ createdAt: isReverse ? -1 : 1,
111
93
  },
112
94
  },
113
95
  ...(0, collaterals_db_1.ITEMS_PAGINATION)(paginatorOptions),
114
- ...bankLookup,
115
- {
116
- $unset: loan_products_db_1.fieldsToUnset,
117
- },
118
- ...projectedFields,
119
- ]);
96
+ ];
97
+ if (withBanks) {
98
+ pipeline.push({
99
+ $lookup: {
100
+ from: 'banks',
101
+ localField: 'bankId',
102
+ foreignField: '_id',
103
+ as: 'bank',
104
+ },
105
+ }, {
106
+ $unwind: {
107
+ path: '$bank',
108
+ preserveNullAndEmptyArrays: true,
109
+ },
110
+ });
111
+ }
112
+ pipeline.push({
113
+ $unset: loan_products_db_1.fieldsToUnset,
114
+ });
115
+ if (isMinified) {
116
+ pipeline.push({
117
+ $project: {
118
+ _id: 1,
119
+ date: 1,
120
+ amount: 1,
121
+ reference: 1,
122
+ balance: 1,
123
+ transactionType: 1,
124
+ },
125
+ });
126
+ }
127
+ return LoanTransaction_model_1.LoanTransaction.aggregate(pipeline);
128
+ }
129
+ async getLoanTransactions(transactionsFilter, paginatorOptions, withBanks, isReverse = false, isMinified = false) {
130
+ const borrowerProducts = await this.loanChargesService.getLoanProducts(transactionsFilter.borrowerId);
131
+ const productIds = borrowerProducts
132
+ .filter((product) => transactionsFilter.productId
133
+ ? product._id.toString() === transactionsFilter.productId
134
+ : true)
135
+ .map((product) => product._id);
136
+ return this.getLoanTransactionsInternal(productIds, transactionsFilter, paginatorOptions, withBanks, isReverse, isMinified);
137
+ }
138
+ async getLoanTransactionsForProducts(transactionsFilter, paginatorOptions, withBanks, isReverse = false, isMinified = false) {
139
+ return this.getLoanTransactionsInternal(transactionsFilter.productIds.map((id) => new mongoose_1.default.Types.ObjectId(id)), transactionsFilter, paginatorOptions, withBanks, isReverse, isMinified);
120
140
  }
121
141
  async getLoanTransactionsForFilter(periodStart, periodEnd, productId) {
122
142
  return LoanTransaction_model_1.LoanTransaction.aggregate([
@@ -68,6 +68,14 @@ export interface ITransactionsFilter {
68
68
  reference?: string;
69
69
  }
70
70
 
71
+ export interface ITransactionsMultiProductsFilter {
72
+ periodStart: Date;
73
+ periodEnd: Date;
74
+ productIds: string[];
75
+ amount?: number;
76
+ reference?: string;
77
+ }
78
+
71
79
  export class LoanTransactionsService {
72
80
 
73
81
  private readonly config: ILoanTransactionsServiceConfig;
@@ -96,22 +104,57 @@ export class LoanTransactionsService {
96
104
  return LoanTransaction.findById(transactionId).lean();
97
105
  }
98
106
 
99
- async getLoanTransactions<T extends boolean>(transactionsFilter: ITransactionsFilter, paginatorOptions: IPaginatorOptions, withBanks: T, isReverse = false, isMinified = false) {
100
- const borrowerProducts = await this.loanChargesService.getLoanProducts(transactionsFilter.borrowerId);
101
- const productIds = borrowerProducts
102
- .filter((product) => transactionsFilter.productId ? product._id.toString() === transactionsFilter.productId : true)
103
- .map((product) => product._id);
104
-
107
+ private async getLoanTransactionsInternal<T extends boolean>(
108
+ productIds: mongoose.Types.ObjectId[],
109
+ transactionsFilter: {
110
+ periodStart: Date;
111
+ periodEnd: Date;
112
+ amount?: number;
113
+ reference?: string;
114
+ },
115
+ paginatorOptions: IPaginatorOptions,
116
+ withBanks: T,
117
+ isReverse = false,
118
+ isMinified = false,
119
+ ) {
105
120
  const optionalFilters = [];
121
+
106
122
  if (transactionsFilter.amount) {
107
- optionalFilters.push({ 'amount': transactionsFilter.amount });
123
+ optionalFilters.push({ amount: transactionsFilter.amount });
108
124
  }
125
+
109
126
  if (transactionsFilter.reference) {
110
- optionalFilters.push({ 'reference': { $regex: transactionsFilter.reference, $options: 'i' } });
127
+ optionalFilters.push({
128
+ reference: {
129
+ $regex: transactionsFilter.reference,
130
+ $options: 'i',
131
+ },
132
+ });
111
133
  }
112
- const bankLookup = !withBanks
113
- ? []
114
- : [
134
+
135
+ const pipeline: mongoose.PipelineStage[] = [
136
+ {
137
+ $match: {
138
+ date: {
139
+ $gte: transactionsFilter.periodStart,
140
+ $lte: transactionsFilter.periodEnd,
141
+ },
142
+ productId: { $in: productIds },
143
+ ...Object.assign({}, ...optionalFilters),
144
+ },
145
+ },
146
+ {
147
+ $sort: {
148
+ date: isReverse ? -1 : 1,
149
+ order: isReverse ? -1 : 1,
150
+ createdAt: isReverse ? -1 : 1,
151
+ },
152
+ },
153
+ ...ITEMS_PAGINATION(paginatorOptions),
154
+ ];
155
+
156
+ if (withBanks) {
157
+ pipeline.push(
115
158
  {
116
159
  $lookup: {
117
160
  from: 'banks',
@@ -119,42 +162,86 @@ export class LoanTransactionsService {
119
162
  foreignField: '_id',
120
163
  as: 'bank',
121
164
  },
122
- }, {
165
+ },
166
+ {
123
167
  $unwind: {
124
168
  path: '$bank',
125
169
  preserveNullAndEmptyArrays: true,
126
170
  },
127
171
  },
128
- ];
129
-
130
- const projectedFields = isMinified
131
- ? [{ $project: { _id: 1, date: 1, amount: 1, reference: 1, balance: 1, transactionType: 1 } }]
132
- : [];
172
+ );
173
+ }
133
174
 
134
- return LoanTransaction.aggregate<T extends true ? ILoanTransactionViewWithBank : ILoanTransactionView>([
135
- {
136
- $match: {
137
- $and: [
138
- { 'date': { $gte: transactionsFilter.periodStart } },
139
- { 'date': { $lte: transactionsFilter.periodEnd } },
140
- { 'productId': { $in: productIds } },
141
- ...optionalFilters,
142
- ],
143
- },
144
- }, {
145
- $sort: {
146
- 'date': isReverse ? -1 : 1,
147
- 'order': isReverse ? -1 : 1,
148
- 'createdAt': isReverse ? -1 : 1,
149
- },
150
- },
151
- ...ITEMS_PAGINATION(paginatorOptions),
152
- ...bankLookup,
175
+ pipeline.push(
153
176
  {
154
177
  $unset: fieldsToUnset,
155
178
  },
156
- ...projectedFields,
157
- ]);
179
+ );
180
+
181
+ if (isMinified) {
182
+ pipeline.push({
183
+ $project: {
184
+ _id: 1,
185
+ date: 1,
186
+ amount: 1,
187
+ reference: 1,
188
+ balance: 1,
189
+ transactionType: 1,
190
+ },
191
+ });
192
+ }
193
+
194
+ return LoanTransaction.aggregate<
195
+ T extends true ? ILoanTransactionViewWithBank : ILoanTransactionView
196
+ >(pipeline);
197
+ }
198
+
199
+ async getLoanTransactions<T extends boolean>(
200
+ transactionsFilter: ITransactionsFilter,
201
+ paginatorOptions: IPaginatorOptions,
202
+ withBanks: T,
203
+ isReverse = false,
204
+ isMinified = false,
205
+ ) {
206
+ const borrowerProducts = await this.loanChargesService.getLoanProducts(
207
+ transactionsFilter.borrowerId,
208
+ );
209
+
210
+ const productIds = borrowerProducts
211
+ .filter((product) =>
212
+ transactionsFilter.productId
213
+ ? product._id.toString() === transactionsFilter.productId
214
+ : true,
215
+ )
216
+ .map((product) => product._id);
217
+
218
+ return this.getLoanTransactionsInternal(
219
+ productIds,
220
+ transactionsFilter,
221
+ paginatorOptions,
222
+ withBanks,
223
+ isReverse,
224
+ isMinified,
225
+ );
226
+ }
227
+
228
+ async getLoanTransactionsForProducts<T extends boolean>(
229
+ transactionsFilter: ITransactionsMultiProductsFilter,
230
+ paginatorOptions: IPaginatorOptions,
231
+ withBanks: T,
232
+ isReverse = false,
233
+ isMinified = false,
234
+ ) {
235
+ return this.getLoanTransactionsInternal(
236
+ transactionsFilter.productIds.map(
237
+ (id) => new mongoose.Types.ObjectId(id),
238
+ ),
239
+ transactionsFilter,
240
+ paginatorOptions,
241
+ withBanks,
242
+ isReverse,
243
+ isMinified,
244
+ );
158
245
  }
159
246
 
160
247
  async getLoanTransactionsForFilter(periodStart: Date, periodEnd: Date, productId: string) {
@@ -1,5 +1,5 @@
1
- /// <reference types="mongoose/types/document" />
2
1
  /// <reference types="mongoose/types/types" />
2
+ /// <reference types="mongoose/types/document" />
3
3
  /// <reference types="mongoose/types/aggregate" />
4
4
  /// <reference types="mongoose/types/callback" />
5
5
  /// <reference types="mongoose/types/collection" />
@@ -31,7 +31,7 @@ export declare class YieldService {
31
31
  getCalculatedYieldTotals(productId: string, selectedMonth: ISelectedMonth): Promise<(mongoose.FlattenMaps<import("../models/Yield.model").IYieldData> & {
32
32
  _id: mongoose.Types.ObjectId;
33
33
  })[]>;
34
- getCalculatedYieldTotalsForPeriod(productId: string, periodStart: ISelectedMonth, periodEnd: ISelectedMonth): Promise<(mongoose.FlattenMaps<import("../models/Yield.model").IYieldData> & {
34
+ getCalculatedYieldTotalsForPeriod(productIds: string[], periodStart: ISelectedMonth, periodEnd: ISelectedMonth): Promise<(mongoose.FlattenMaps<import("../models/Yield.model").IYieldData> & {
35
35
  _id: mongoose.Types.ObjectId;
36
36
  })[]>;
37
37
  getYieldData(params: IYieldParams): Promise<{
@@ -33,9 +33,9 @@ class YieldService {
33
33
  month: selectedMonth.month,
34
34
  }).lean();
35
35
  }
36
- async getCalculatedYieldTotalsForPeriod(productId, periodStart, periodEnd) {
36
+ async getCalculatedYieldTotalsForPeriod(productIds, periodStart, periodEnd) {
37
37
  return Yield_model_1.YieldData.find({
38
- productId: new mongoose_1.default.Types.ObjectId(productId),
38
+ productId: productIds.map((productId) => new mongoose_1.default.Types.ObjectId(productId)),
39
39
  $or: [
40
40
  {
41
41
  year: periodStart.year,
@@ -39,9 +39,9 @@ export class YieldService {
39
39
  }).lean();
40
40
  }
41
41
 
42
- async getCalculatedYieldTotalsForPeriod(productId: string, periodStart: ISelectedMonth, periodEnd: ISelectedMonth) {
42
+ async getCalculatedYieldTotalsForPeriod(productIds: string[], periodStart: ISelectedMonth, periodEnd: ISelectedMonth) {
43
43
  return YieldData.find({
44
- productId: new mongoose.Types.ObjectId(productId),
44
+ productId: productIds.map((productId) => new mongoose.Types.ObjectId(productId)),
45
45
  $or: [
46
46
  {
47
47
  year: periodStart.year,