gemcap-be-common 1.2.62 → 1.2.64

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.
@@ -1,3 +1,4 @@
1
+ import _ from 'lodash';
1
2
  import { IStatementPeriod } from '../models/LoanStatementTransaction.model';
2
3
  export declare enum ELedgerReportType {
3
4
  SHORT = 0,
@@ -36,7 +37,18 @@ export declare const ledgerHeadersMap: {
36
37
  export declare const getTotalTransactionAmountForCharge: (chargeId: string, start: Date, end: Date) => Promise<any>;
37
38
  export declare const getTotalForDate: (productId: string, date: Date) => Promise<number>;
38
39
  export declare const getStatementBorrowersTransactions: (start: string, end: string, selectedPeriod: string, selectedBorrowerIds: string[], ledgerType: ELedgerReportType) => Promise<{
39
- transactions: any[];
40
+ transactions: ({
41
+ borrowerCode: string;
42
+ type: string;
43
+ chargeCode: string;
44
+ PLCode: string;
45
+ productCode: string;
46
+ productName: string;
47
+ info: string;
48
+ title: string;
49
+ amount: string;
50
+ statementAmount: string;
51
+ } | _.Omit<import("./reports.db").ILedgerReportRow, "balance" | "floatedBalance">)[];
40
52
  }[]>;
41
53
  export declare const getBorrowerWithPeriods: (borrowerId: string, period: {
42
54
  selectedPeriod: string;
@@ -105,39 +105,35 @@ const getStatementBorrowersTransactions = async (start, end, selectedPeriod, sel
105
105
  if (!selectedBorrowerIds) {
106
106
  return [{ transactions: [header] }];
107
107
  }
108
- const allTransactions = {};
109
- await Promise.all(selectedBorrowerIds.map(async (borrowerId) => {
110
- const products = await (0, loan_products_db_1.getLoanProducts)(borrowerId);
111
- const periodFull = await (0, exports.getBorrowerWithPeriods)(borrowerId, {
108
+ const periodFull = selectedPeriod === 'ENTIRE_LOAN'
109
+ ? [{
110
+ period: {
111
+ selectedPeriod: selectedPeriod,
112
+ start: (0, dayjs_1.default)(start).utc().toDate().toISOString(),
113
+ end: (0, dayjs_1.default)(end).utc().toDate().toISOString(),
114
+ },
115
+ }]
116
+ : await (0, exports.getBorrowerWithPeriods)(selectedBorrowerIds[0], {
112
117
  selectedStart: new Date(start),
113
118
  selectedEnd: new Date(end),
114
119
  selectedPeriod,
115
120
  });
116
- const someMappedResults = products.reduce((acc, product) => {
117
- return [
118
- ...acc,
119
- ...periodFull.map((periodDesc) => ({
120
- productStart: periodDesc.period.start,
121
- productEnd: periodDesc.period.end,
122
- productId: product._id.toString(),
123
- })),
124
- ];
125
- }, []);
126
- await Promise.all(someMappedResults.map(async (productDesc) => {
127
- const borrowerTransactions = await (0, reports_db_1.getLedger)(productDesc.productId, new Date(productDesc.productStart), new Date(productDesc.productEnd), ledgerType, true);
128
- Object.values(borrowerTransactions).forEach((transactions) => {
129
- const [_header, ...onlyData] = transactions;
130
- const mappedData = lodash_1.default.map(onlyData, (obj) => lodash_1.default.omit(obj, ['balance', 'floatedBalance']));
131
- allTransactions[borrowerId] = mappedData;
132
- });
133
- }));
121
+ const allProductIds = [];
122
+ await Promise.all(selectedBorrowerIds.map(async (borrowerId) => {
123
+ const borrowerProducts = await (0, loan_products_db_1.getLoanProducts)(borrowerId);
124
+ allProductIds.push(borrowerProducts.map((product) => product._id.toString()));
134
125
  }));
135
- const sortedTransactions = [];
136
- for (const borrowerId of selectedBorrowerIds) {
137
- console.log(borrowerId);
138
- sortedTransactions.push(...allTransactions[borrowerId]);
139
- }
140
- return [{ transactions: [header, ...sortedTransactions] }];
126
+ const products = await LoanProducts_model_1.LoanProduct.find({ borrowerId: { $in: selectedBorrowerIds.map((id) => new mongoose_1.default.Types.ObjectId(id)) } }).lean();
127
+ const productIds = products.map((product) => product._id.toString());
128
+ const ledgerData = await (0, reports_db_1.getLedger)({
129
+ productIds,
130
+ startDate: new Date((0, dayjs_1.default)(periodFull[0].period.start).utc().toDate().toISOString()),
131
+ endDate: new Date((0, dayjs_1.default)(periodFull[0].period.end).utc().toDate().toISOString()),
132
+ reportType: ledgerType,
133
+ }, { showBalances: false, showFloatedBalance: false });
134
+ const transactions = Object.values(ledgerData).reduce((acc, ledgerTransaction) => [...acc, ...ledgerTransaction], []);
135
+ const transactionsWithoutBalance = lodash_1.default.map(transactions, (obj) => lodash_1.default.omit(obj, ['balance', 'floatedBalance']));
136
+ return [{ transactions: [header, ...transactionsWithoutBalance] }];
141
137
  };
142
138
  exports.getStatementBorrowersTransactions = getStatementBorrowersTransactions;
143
139
  const getBorrowerWithPeriods = async (borrowerId, period, splitPeriods = false) => {
@@ -103,45 +103,39 @@ export const getStatementBorrowersTransactions = async (start: string, end: stri
103
103
  if (!selectedBorrowerIds) {
104
104
  return [{ transactions: [header] }];
105
105
  }
106
- const allTransactions: { [borrowerId: string]: unknown[] } = {};
107
- await Promise.all(selectedBorrowerIds.map(async (borrowerId) => {
108
- const products = await getLoanProducts(borrowerId);
109
- const periodFull = await getBorrowerWithPeriods(borrowerId, {
106
+
107
+ const periodFull = selectedPeriod === 'ENTIRE_LOAN'
108
+ ? [{
109
+ period: {
110
+ selectedPeriod: selectedPeriod,
111
+ start: dayjs(start).utc().toDate().toISOString(),
112
+ end: dayjs(end).utc().toDate().toISOString(),
113
+ },
114
+ }]
115
+ : await getBorrowerWithPeriods(selectedBorrowerIds[0], {
110
116
  selectedStart: new Date(start),
111
117
  selectedEnd: new Date(end),
112
118
  selectedPeriod,
113
119
  });
114
- const someMappedResults = products.reduce((acc, product) => {
115
- return [
116
- ...acc,
117
- ...periodFull.map((periodDesc) => ({
118
- productStart: periodDesc.period.start,
119
- productEnd: periodDesc.period.end,
120
- productId: product._id.toString(),
121
- })),
122
- ];
123
- }, []);
124
- await Promise.all(someMappedResults.map(async (productDesc) => {
125
- const borrowerTransactions = await getLedger(
126
- productDesc.productId,
127
- new Date(productDesc.productStart),
128
- new Date(productDesc.productEnd),
129
- ledgerType,
130
- true,
131
- );
132
- Object.values(borrowerTransactions).forEach((transactions) => {
133
- const [_header, ...onlyData] = transactions;
134
- const mappedData = _.map(onlyData, (obj) => _.omit(obj, ['balance', 'floatedBalance']));
135
- allTransactions[borrowerId] = mappedData;
136
- });
137
- }));
120
+
121
+ const allProductIds = [];
122
+ await Promise.all(selectedBorrowerIds.map(async (borrowerId) => {
123
+ const borrowerProducts = await getLoanProducts(borrowerId);
124
+ allProductIds.push(borrowerProducts.map((product) => product._id.toString()));
138
125
  }));
139
- const sortedTransactions = [];
140
- for (const borrowerId of selectedBorrowerIds) {
141
- console.log(borrowerId);
142
- sortedTransactions.push(...allTransactions[borrowerId]);
143
- }
144
- return [{ transactions: [header, ...sortedTransactions] }];
126
+
127
+ const products = await LoanProduct.find({ borrowerId: { $in: selectedBorrowerIds.map((id) => new mongoose.Types.ObjectId(id)) } }).lean();
128
+ const productIds = products.map((product) => product._id.toString());
129
+ const ledgerData = await getLedger({
130
+ productIds,
131
+ startDate: new Date(dayjs(periodFull[0].period.start).utc().toDate().toISOString()),
132
+ endDate: new Date(dayjs(periodFull[0].period.end).utc().toDate().toISOString()),
133
+ reportType: ledgerType,
134
+ }, { showBalances: false, showFloatedBalance: false },
135
+ );
136
+ const transactions = Object.values(ledgerData).reduce((acc, ledgerTransaction) => [...acc, ...ledgerTransaction], []);
137
+ const transactionsWithoutBalance = _.map(transactions, (obj) => _.omit(obj, ['balance', 'floatedBalance']));
138
+ return [{ transactions: [header, ...transactionsWithoutBalance] }];
145
139
  };
146
140
 
147
141
  export const getBorrowerWithPeriods = async (borrowerId: string, period: {
@@ -1,6 +1,31 @@
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';
1
26
  import { EMicroserviceTask } from '../enums/microservice-task.enum';
2
27
  import { IMicroserviceTaskDoc } from '../models/MicroserviceTask.model';
3
- export declare const createMicroserviceTasks: (cmd: EMicroserviceTask, params: any, executeAt?: Date) => Promise<void>;
28
+ export declare const createMicroserviceTasks: (cmd: EMicroserviceTask, params: any, executeAt?: Date, userId?: mongoose.Types.ObjectId) => Promise<void>;
4
29
  export declare const findFirstTask: () => Promise<IMicroserviceTaskDoc>;
5
30
  export declare const getAllTasks: () => Promise<IMicroserviceTaskDoc[]>;
6
31
  export declare const deleteTask: (taskId: string) => Promise<void>;
@@ -3,14 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.deleteTask = exports.getAllTasks = exports.findFirstTask = exports.createMicroserviceTasks = void 0;
4
4
  const MicroserviceTask_model_1 = require("../models/MicroserviceTask.model");
5
5
  const microservice_task_statuses_enum_1 = require("../enums/microservice-task-statuses.enum");
6
- const createMicroserviceTasks = async (cmd, params, executeAt = new Date()) => {
6
+ const createMicroserviceTasks = async (cmd, params, executeAt = new Date(), userId) => {
7
7
  const tasks = await MicroserviceTask_model_1.MicroserviceTaskModel.find({ cmd, params, status: microservice_task_statuses_enum_1.EMicroserviceTaskStatuses.SCHEDULED });
8
8
  if (!tasks.length) {
9
9
  const newTask = await MicroserviceTask_model_1.MicroserviceTaskModel.create({
10
+ userId,
10
11
  cmd,
11
12
  params,
12
13
  status: microservice_task_statuses_enum_1.EMicroserviceTaskStatuses.SCHEDULED,
13
- executeAt
14
+ executeAt,
14
15
  });
15
16
  await newTask.save();
16
17
  }
@@ -24,16 +25,16 @@ const findFirstTask = async () => {
24
25
  'status': microservice_task_statuses_enum_1.EMicroserviceTaskStatuses.SCHEDULED,
25
26
  $or: [
26
27
  { 'executeAt': { $exists: false } },
27
- { 'executeAt': { $lte: currentDate } }
28
- ]
29
- }
28
+ { 'executeAt': { $lte: currentDate } },
29
+ ],
30
+ },
30
31
  }, {
31
32
  $sort: {
32
- 'createdAt': 1
33
- }
33
+ 'createdAt': 1,
34
+ },
34
35
  }, {
35
- $limit: 1
36
- }
36
+ $limit: 1,
37
+ },
37
38
  ]);
38
39
  if (lastTasks.length) {
39
40
  return lastTasks[0];
@@ -1,19 +1,22 @@
1
+ import mongoose from 'mongoose';
2
+
1
3
  import { EMicroserviceTask } from '../enums/microservice-task.enum';
2
4
  import { IMicroserviceTaskDoc, MicroserviceTaskModel } from '../models/MicroserviceTask.model';
3
5
  import { EMicroserviceTaskStatuses } from '../enums/microservice-task-statuses.enum';
4
6
 
5
- export const createMicroserviceTasks = async (cmd: EMicroserviceTask, params: any, executeAt: Date = new Date()): Promise<void> => {
7
+ export const createMicroserviceTasks = async (cmd: EMicroserviceTask, params: any, executeAt: Date = new Date(), userId?: mongoose.Types.ObjectId): Promise<void> => {
6
8
  const tasks = await MicroserviceTaskModel.find({ cmd, params, status: EMicroserviceTaskStatuses.SCHEDULED });
7
9
  if (!tasks.length) {
8
10
  const newTask = await MicroserviceTaskModel.create({
11
+ userId,
9
12
  cmd,
10
13
  params,
11
14
  status: EMicroserviceTaskStatuses.SCHEDULED,
12
- executeAt
15
+ executeAt,
13
16
  });
14
17
  await newTask.save();
15
18
  }
16
- }
19
+ };
17
20
 
18
21
  export const findFirstTask = async (): Promise<IMicroserviceTaskDoc> => {
19
22
  const currentDate = new Date();
@@ -23,30 +26,30 @@ export const findFirstTask = async (): Promise<IMicroserviceTaskDoc> => {
23
26
  'status': EMicroserviceTaskStatuses.SCHEDULED,
24
27
  $or: [
25
28
  { 'executeAt': { $exists: false } },
26
- { 'executeAt': { $lte: currentDate } }
27
- ]
28
- }
29
+ { 'executeAt': { $lte: currentDate } },
30
+ ],
31
+ },
29
32
  }, {
30
33
  $sort: {
31
- 'createdAt': 1
32
- }
34
+ 'createdAt': 1,
35
+ },
33
36
  }, {
34
- $limit: 1
35
- }
37
+ $limit: 1,
38
+ },
36
39
  ]);
37
40
  if (lastTasks.length) {
38
41
  return lastTasks[0];
39
42
  }
40
43
  return null;
41
- }
44
+ };
42
45
 
43
46
  export const getAllTasks = async (): Promise<IMicroserviceTaskDoc[]> => {
44
47
  return MicroserviceTaskModel
45
48
  .find({})
46
49
  .sort({ 'createdAt': 1 });
47
- }
50
+ };
48
51
 
49
52
  export const deleteTask = async (taskId: string): Promise<void> => {
50
53
  await MicroserviceTaskModel
51
54
  .findByIdAndDelete(taskId);
52
- }
55
+ };
@@ -1,4 +1,31 @@
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 { ILoanProductDoc } from '../models/LoanProducts.model';
1
26
  import { ELedgerReportType } from './loan-statement.db';
27
+ import mongoose from 'mongoose';
28
+ import { ILoanTransactionDoc } from '../models/LoanTransaction.model';
2
29
  export declare enum EChargeType {
3
30
  INTEREST_FEE = "INTEREST",
4
31
  ADMIN_FEE = "ADMIN FEE",
@@ -49,8 +76,21 @@ export interface ILedgerDataRow {
49
76
  paymentDate: Date;
50
77
  isBrokerFee?: boolean;
51
78
  }
52
- export declare const getLedger: (productId: string, startDate: Date, endDate: Date, reportType: ELedgerReportType, showFloatedBalance?: boolean) => Promise<{
53
- [x: string]: string[][];
54
- } | {
55
- [x: string]: Pick<any, string>[];
79
+ export type LedgerTransactions = {
80
+ transactions: ILoanTransactionDoc[];
81
+ product: ILoanProductDoc;
82
+ productId: mongoose.Types.ObjectId;
83
+ };
84
+ export type ReportLedgerParams = {
85
+ productIds: string[];
86
+ startDate: Date;
87
+ endDate: Date;
88
+ reportType: ELedgerReportType;
89
+ };
90
+ export type ReportLedgerOptions = {
91
+ showFloatedBalance: boolean;
92
+ showBalances: boolean;
93
+ };
94
+ export declare const getLedger: (params: ReportLedgerParams, options?: ReportLedgerOptions) => Promise<{
95
+ [productId: string]: ILedgerReportRow[];
56
96
  }>;