gemcap-be-common 1.5.92 → 1.5.94

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.
@@ -0,0 +1,58 @@
1
+ /// <reference types="mongoose/types/types" />
2
+ /// <reference types="mongoose/types/models" />
3
+ /// <reference types="mongoose/types/schemaoptions" />
4
+ /// <reference types="mongoose/types/utility" />
5
+ /// <reference types="mongoose/types/document" />
6
+ /// <reference types="mongoose/types/aggregate" />
7
+ /// <reference types="mongoose/types/callback" />
8
+ /// <reference types="mongoose/types/collection" />
9
+ /// <reference types="mongoose/types/connection" />
10
+ /// <reference types="mongoose/types/cursor" />
11
+ /// <reference types="mongoose/types/document" />
12
+ /// <reference types="mongoose/types/error" />
13
+ /// <reference types="mongoose/types/expressions" />
14
+ /// <reference types="mongoose/types/helpers" />
15
+ /// <reference types="mongoose/types/middlewares" />
16
+ /// <reference types="mongoose/types/indexes" />
17
+ /// <reference types="mongoose/types/models" />
18
+ /// <reference types="mongoose/types/mongooseoptions" />
19
+ /// <reference types="mongoose/types/pipelinestage" />
20
+ /// <reference types="mongoose/types/populate" />
21
+ /// <reference types="mongoose/types/query" />
22
+ /// <reference types="mongoose/types/schemaoptions" />
23
+ /// <reference types="mongoose/types/schematypes" />
24
+ /// <reference types="mongoose/types/session" />
25
+ /// <reference types="mongoose/types/types" />
26
+ /// <reference types="mongoose/types/utility" />
27
+ /// <reference types="mongoose/types/validation" />
28
+ /// <reference types="mongoose/types/virtuals" />
29
+ /// <reference types="mongoose/types/inferschematype" />
30
+ import mongoose from 'mongoose';
31
+ import { ELoanChargeType } from '../enums/loan-charge-type.enum';
32
+ export interface IFacilityChargeGroup {
33
+ uiId: string;
34
+ facilityId: mongoose.Types.ObjectId;
35
+ name: string;
36
+ groupingType: ELoanChargeType;
37
+ chargeIds: mongoose.Types.ObjectId[];
38
+ order: number;
39
+ deletedAt?: Date;
40
+ }
41
+ export type TFacilityChargeGroupDoc = mongoose.HydratedDocument<IFacilityChargeGroup>;
42
+ export interface IFacilityChargeGroupLean extends IFacilityChargeGroup {
43
+ _id: mongoose.Types.ObjectId;
44
+ createdAt: Date;
45
+ updatedAt: Date;
46
+ }
47
+ export interface IFacilityChargeGroupPlain extends Omit<IFacilityChargeGroup, 'facilityId' | 'chargeIds'> {
48
+ _id: string;
49
+ facilityId: string;
50
+ chargeIds: string[];
51
+ createdAt: Date;
52
+ updatedAt: Date;
53
+ }
54
+ export type TFacilityChargeGroupModel = mongoose.Model<IFacilityChargeGroup>;
55
+ export declare const FacilityChargeGroupSchema: mongoose.Schema<IFacilityChargeGroup, TFacilityChargeGroupModel, {}, {}, {}, {}, mongoose.DefaultSchemaOptions, IFacilityChargeGroup, mongoose.Document<unknown, {}, mongoose.FlatRecord<IFacilityChargeGroup>> & mongoose.FlatRecord<IFacilityChargeGroup> & {
56
+ _id: mongoose.Types.ObjectId;
57
+ }>;
58
+ export declare const FacilityChargeGroup: TFacilityChargeGroupModel;
@@ -0,0 +1,46 @@
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.FacilityChargeGroup = exports.FacilityChargeGroupSchema = void 0;
7
+ const mongoose_1 = __importDefault(require("mongoose"));
8
+ const _models_1 = require("./_models");
9
+ const loan_charge_type_enum_1 = require("../enums/loan-charge-type.enum");
10
+ exports.FacilityChargeGroupSchema = new mongoose_1.default.Schema({
11
+ facilityId: {
12
+ type: mongoose_1.default.Schema.Types.ObjectId,
13
+ ref: _models_1.MODEL_NAMES.facilities,
14
+ required: true,
15
+ },
16
+ chargeIds: [{
17
+ type: mongoose_1.default.Schema.Types.ObjectId,
18
+ ref: _models_1.MODEL_NAMES.loanCharges,
19
+ required: true,
20
+ }],
21
+ groupingType: {
22
+ type: String,
23
+ enum: Object.values(loan_charge_type_enum_1.ELoanChargeType),
24
+ required: true,
25
+ },
26
+ uiId: {
27
+ type: String,
28
+ required: true,
29
+ },
30
+ name: {
31
+ type: String,
32
+ required: true,
33
+ },
34
+ order: {
35
+ type: Number,
36
+ required: true,
37
+ },
38
+ deletedAt: {
39
+ type: Date,
40
+ },
41
+ }, {
42
+ timestamps: { createdAt: true, updatedAt: true },
43
+ versionKey: false,
44
+ });
45
+ exports.FacilityChargeGroupSchema.index({ facilityId: 1, uiId: 1 }, { unique: true });
46
+ exports.FacilityChargeGroup = mongoose_1.default.model(_models_1.MODEL_NAMES.facilityChargeGroups, exports.FacilityChargeGroupSchema);
@@ -0,0 +1,78 @@
1
+ import mongoose from 'mongoose';
2
+
3
+ import { MODEL_NAMES } from './_models';
4
+ import { ELoanChargeType } from '../enums/loan-charge-type.enum';
5
+
6
+ export interface IFacilityChargeGroup {
7
+ uiId: string;
8
+ facilityId: mongoose.Types.ObjectId;
9
+ name: string;
10
+ groupingType: ELoanChargeType;
11
+ chargeIds: mongoose.Types.ObjectId[];
12
+ order: number;
13
+ deletedAt?: Date;
14
+ }
15
+
16
+ export type TFacilityChargeGroupDoc = mongoose.HydratedDocument<IFacilityChargeGroup>;
17
+
18
+ export interface IFacilityChargeGroupLean extends IFacilityChargeGroup {
19
+ _id: mongoose.Types.ObjectId;
20
+ createdAt: Date;
21
+ updatedAt: Date;
22
+ }
23
+
24
+ export interface IFacilityChargeGroupPlain extends Omit<IFacilityChargeGroup, 'facilityId' | 'chargeIds'> {
25
+ _id: string;
26
+ facilityId: string;
27
+ chargeIds: string[];
28
+ createdAt: Date;
29
+ updatedAt: Date;
30
+ }
31
+
32
+ export type TFacilityChargeGroupModel = mongoose.Model<IFacilityChargeGroup>;
33
+
34
+ export const FacilityChargeGroupSchema = new mongoose.Schema<IFacilityChargeGroup, TFacilityChargeGroupModel>(
35
+ {
36
+ facilityId: {
37
+ type: mongoose.Schema.Types.ObjectId,
38
+ ref: MODEL_NAMES.facilities,
39
+ required: true,
40
+ },
41
+ chargeIds: [{
42
+ type: mongoose.Schema.Types.ObjectId,
43
+ ref: MODEL_NAMES.loanCharges,
44
+ required: true,
45
+ }],
46
+ groupingType: {
47
+ type: String,
48
+ enum: Object.values(ELoanChargeType),
49
+ required: true,
50
+ },
51
+ uiId: {
52
+ type: String,
53
+ required: true,
54
+ },
55
+ name: {
56
+ type: String,
57
+ required: true,
58
+ },
59
+ order: {
60
+ type: Number,
61
+ required: true,
62
+ },
63
+ deletedAt: {
64
+ type: Date,
65
+ },
66
+ },
67
+ {
68
+ timestamps: { createdAt: true, updatedAt: true },
69
+ versionKey: false,
70
+ },
71
+ );
72
+
73
+ FacilityChargeGroupSchema.index(
74
+ { facilityId: 1, uiId: 1 },
75
+ { unique: true },
76
+ );
77
+
78
+ export const FacilityChargeGroup = mongoose.model<IFacilityChargeGroup, TFacilityChargeGroupModel>(MODEL_NAMES.facilityChargeGroups, FacilityChargeGroupSchema);
@@ -90,18 +90,19 @@ export declare const LoanProductSchema: mongoose.Schema<any, mongoose.Model<any,
90
90
  updatedAt: NativeDate;
91
91
  } & {
92
92
  borrowerId: mongoose.Types.ObjectId;
93
- masterLoanProductId: mongoose.Types.ObjectId;
93
+ type: string;
94
+ name: string;
94
95
  order: number;
95
96
  active: boolean;
96
97
  code: string;
97
- name: string;
98
- type: string;
99
98
  startDate: Date;
100
99
  maturityDate: Date;
101
100
  commitment: number;
102
101
  isBalanceActual: boolean;
103
102
  isFloatedBalanceActual: boolean;
103
+ masterLoanProductId: mongoose.Types.ObjectId;
104
104
  isDefaultPaymentOrder: boolean;
105
+ __v?: number;
105
106
  facilityId?: mongoose.Types.ObjectId;
106
107
  masterCode?: string;
107
108
  payoffDate?: Date;
@@ -112,24 +113,24 @@ export declare const LoanProductSchema: mongoose.Schema<any, mongoose.Model<any,
112
113
  prepaymentTerms?: string;
113
114
  settlementCode?: string;
114
115
  deactivationDate?: Date;
115
- __v?: number;
116
116
  }, mongoose.Document<unknown, {}, mongoose.FlatRecord<{
117
117
  createdAt: NativeDate;
118
118
  updatedAt: NativeDate;
119
119
  } & {
120
120
  borrowerId: mongoose.Types.ObjectId;
121
- masterLoanProductId: mongoose.Types.ObjectId;
121
+ type: string;
122
+ name: string;
122
123
  order: number;
123
124
  active: boolean;
124
125
  code: string;
125
- name: string;
126
- type: string;
127
126
  startDate: Date;
128
127
  maturityDate: Date;
129
128
  commitment: number;
130
129
  isBalanceActual: boolean;
131
130
  isFloatedBalanceActual: boolean;
131
+ masterLoanProductId: mongoose.Types.ObjectId;
132
132
  isDefaultPaymentOrder: boolean;
133
+ __v?: number;
133
134
  facilityId?: mongoose.Types.ObjectId;
134
135
  masterCode?: string;
135
136
  payoffDate?: Date;
@@ -140,24 +141,24 @@ export declare const LoanProductSchema: mongoose.Schema<any, mongoose.Model<any,
140
141
  prepaymentTerms?: string;
141
142
  settlementCode?: string;
142
143
  deactivationDate?: Date;
143
- __v?: number;
144
144
  }>> & mongoose.FlatRecord<{
145
145
  createdAt: NativeDate;
146
146
  updatedAt: NativeDate;
147
147
  } & {
148
148
  borrowerId: mongoose.Types.ObjectId;
149
- masterLoanProductId: mongoose.Types.ObjectId;
149
+ type: string;
150
+ name: string;
150
151
  order: number;
151
152
  active: boolean;
152
153
  code: string;
153
- name: string;
154
- type: string;
155
154
  startDate: Date;
156
155
  maturityDate: Date;
157
156
  commitment: number;
158
157
  isBalanceActual: boolean;
159
158
  isFloatedBalanceActual: boolean;
159
+ masterLoanProductId: mongoose.Types.ObjectId;
160
160
  isDefaultPaymentOrder: boolean;
161
+ __v?: number;
161
162
  facilityId?: mongoose.Types.ObjectId;
162
163
  masterCode?: string;
163
164
  payoffDate?: Date;
@@ -168,7 +169,6 @@ export declare const LoanProductSchema: mongoose.Schema<any, mongoose.Model<any,
168
169
  prepaymentTerms?: string;
169
170
  settlementCode?: string;
170
171
  deactivationDate?: Date;
171
- __v?: number;
172
172
  }> & {
173
173
  _id: mongoose.Types.ObjectId;
174
174
  }>;
@@ -56,18 +56,18 @@ export declare const PostponedTransactionSchema: mongoose.Schema<any, mongoose.M
56
56
  timestamps: false;
57
57
  }, {
58
58
  productId: mongoose.Types.ObjectId;
59
- postponedToDate: string;
60
59
  transactionId: mongoose.Types.ObjectId;
60
+ postponedToDate: string;
61
61
  __v?: number;
62
62
  }, mongoose.Document<unknown, {}, mongoose.FlatRecord<{
63
63
  productId: mongoose.Types.ObjectId;
64
- postponedToDate: string;
65
64
  transactionId: mongoose.Types.ObjectId;
65
+ postponedToDate: string;
66
66
  __v?: number;
67
67
  }>> & mongoose.FlatRecord<{
68
68
  productId: mongoose.Types.ObjectId;
69
- postponedToDate: string;
70
69
  transactionId: mongoose.Types.ObjectId;
70
+ postponedToDate: string;
71
71
  __v?: number;
72
72
  }> & {
73
73
  _id: mongoose.Types.ObjectId;
@@ -32,6 +32,7 @@ import mongoose from 'mongoose';
32
32
  import { ISelectedMonth } from '../helpers/date.helper';
33
33
  import { ILoanChargePlain } from './LoanCharges.model';
34
34
  import { ILoanProductPlain } from './LoanProducts.model';
35
+ import { IFacilityPlain } from './Facility.model';
35
36
  export declare const yieldParamsValidationSchema: Joi.ObjectSchema<IYieldParams>;
36
37
  export declare enum ETotalType {
37
38
  AVERAGE_BALANCE = "AVERAGE_BALANCE",
@@ -57,6 +58,8 @@ export interface IYieldParams {
57
58
  export interface IYieldData {
58
59
  productId?: mongoose.Types.ObjectId;
59
60
  chargeId?: mongoose.Types.ObjectId;
61
+ facilityId?: mongoose.Types.ObjectId;
62
+ facilityChargeGroupId?: mongoose.Types.ObjectId;
60
63
  totalType: string;
61
64
  year: number;
62
65
  month: number;
@@ -67,6 +70,7 @@ export interface IYieldViewData {
67
70
  order: number;
68
71
  charge: ILoanChargePlain;
69
72
  product: ILoanProductPlain;
73
+ facility?: IFacilityPlain;
70
74
  title: string;
71
75
  class?: string;
72
76
  [key: `value-${string}`]: number | string | ILoanChargePlain | ILoanProductPlain;
@@ -82,10 +86,12 @@ export interface IYieldDataLean extends IYieldData {
82
86
  createdAt: Date;
83
87
  updatedAt: Date;
84
88
  }
85
- export interface IYieldDataPlain extends Omit<IYieldData, 'productId' | 'chargeId'> {
89
+ export interface IYieldDataPlain extends Omit<IYieldData, 'productId' | 'chargeId' | 'facilityId' | 'facilityChargeGroupId'> {
86
90
  _id: string;
87
- productId: string;
88
- chargeId: string;
91
+ productId?: string;
92
+ chargeId?: string;
93
+ facilityId?: string;
94
+ facilityChargeGroupId?: string;
89
95
  }
90
96
  export type TYieldDataModel = mongoose.Model<IYieldData>;
91
97
  export declare const YieldDataSchema: mongoose.Schema<IYieldData, TYieldDataModel, {}, {}, {}, {}, mongoose.DefaultSchemaOptions, IYieldData, mongoose.Document<unknown, {}, mongoose.FlatRecord<IYieldData>> & mongoose.FlatRecord<IYieldData> & {
@@ -51,6 +51,16 @@ exports.YieldDataSchema = new mongoose_1.default.Schema({
51
51
  ref: _models_1.MODEL_NAMES.loanCharges,
52
52
  required: false,
53
53
  },
54
+ facilityId: {
55
+ type: mongoose_1.default.Schema.Types.ObjectId,
56
+ ref: _models_1.MODEL_NAMES.facilities,
57
+ required: false,
58
+ },
59
+ facilityChargeGroupId: {
60
+ type: mongoose_1.default.Schema.Types.ObjectId,
61
+ ref: _models_1.MODEL_NAMES.facilityChargeGroups,
62
+ required: false,
63
+ },
54
64
  totalType: {
55
65
  type: String,
56
66
  },
@@ -5,6 +5,7 @@ import { ISelectedMonth } from '../helpers/date.helper';
5
5
  import { MODEL_NAMES } from './_models';
6
6
  import { ILoanChargePlain } from './LoanCharges.model';
7
7
  import { ILoanProductPlain } from './LoanProducts.model';
8
+ import { IFacilityPlain } from './Facility.model';
8
9
 
9
10
  export const yieldParamsValidationSchema = Joi.object<IYieldParams>({
10
11
  periodStart: Joi.object({
@@ -58,6 +59,8 @@ export interface IYieldParams {
58
59
  export interface IYieldData {
59
60
  productId?: mongoose.Types.ObjectId;
60
61
  chargeId?: mongoose.Types.ObjectId;
62
+ facilityId?: mongoose.Types.ObjectId;
63
+ facilityChargeGroupId?: mongoose.Types.ObjectId;
61
64
  totalType: string;
62
65
  year: number;
63
66
  month: number;
@@ -69,6 +72,7 @@ export interface IYieldViewData {
69
72
  order: number;
70
73
  charge: ILoanChargePlain;
71
74
  product: ILoanProductPlain;
75
+ facility?: IFacilityPlain;
72
76
  title: string;
73
77
  class?: string;
74
78
 
@@ -88,10 +92,12 @@ export interface IYieldDataLean extends IYieldData {
88
92
  updatedAt: Date;
89
93
  }
90
94
 
91
- export interface IYieldDataPlain extends Omit<IYieldData, 'productId' | 'chargeId'> {
95
+ export interface IYieldDataPlain extends Omit<IYieldData, 'productId' | 'chargeId' | 'facilityId' | 'facilityChargeGroupId'> {
92
96
  _id: string;
93
- productId: string;
94
- chargeId: string;
97
+ productId?: string;
98
+ chargeId?: string;
99
+ facilityId?: string;
100
+ facilityChargeGroupId?: string;
95
101
  }
96
102
 
97
103
  export type TYieldDataModel = mongoose.Model<IYieldData>;
@@ -108,6 +114,16 @@ export const YieldDataSchema = new mongoose.Schema<IYieldData, TYieldDataModel>(
108
114
  ref: MODEL_NAMES.loanCharges,
109
115
  required: false,
110
116
  },
117
+ facilityId: {
118
+ type: mongoose.Schema.Types.ObjectId,
119
+ ref: MODEL_NAMES.facilities,
120
+ required: false,
121
+ },
122
+ facilityChargeGroupId: {
123
+ type: mongoose.Schema.Types.ObjectId,
124
+ ref: MODEL_NAMES.facilityChargeGroups,
125
+ required: false,
126
+ },
111
127
  totalType: {
112
128
  type: String,
113
129
  },
@@ -68,6 +68,7 @@ export declare const MODEL_NAMES: {
68
68
  earlyPayments: string;
69
69
  externalUserAccess: string;
70
70
  facilities: string;
71
+ facilityChargeGroups: string;
71
72
  financialComplianceBorrowers: string;
72
73
  financialComplianceSettings: string;
73
74
  financialIndexes: string;
package/models/_models.js CHANGED
@@ -71,6 +71,7 @@ exports.MODEL_NAMES = {
71
71
  earlyPayments: 'early_payments',
72
72
  externalUserAccess: 'external_user_access',
73
73
  facilities: 'facilities',
74
+ facilityChargeGroups: 'facility_charge_groups',
74
75
  financialComplianceBorrowers: 'financialComplianceBorrowers',
75
76
  financialComplianceSettings: 'financialComplianceSettings',
76
77
  financialIndexes: 'financial_indexes',
package/models/_models.ts CHANGED
@@ -68,6 +68,7 @@ export const MODEL_NAMES = {
68
68
  earlyPayments : 'early_payments',
69
69
  externalUserAccess: 'external_user_access',
70
70
  facilities: 'facilities',
71
+ facilityChargeGroups: 'facility_charge_groups',
71
72
  financialComplianceBorrowers: 'financialComplianceBorrowers',
72
73
  financialComplianceSettings: 'financialComplianceSettings',
73
74
  financialIndexes: 'financial_indexes',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gemcap-be-common",
3
- "version": "1.5.92",
3
+ "version": "1.5.94",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -27,6 +27,8 @@ import mongoose from 'mongoose';
27
27
  import { ISelectedMonth } from '../helpers/date.helper';
28
28
  import { IYieldParams, IYieldViewData } from '../models/Yield.model';
29
29
  export declare class YieldService {
30
+ private toPlainProduct;
31
+ private toPlainCharge;
30
32
  private getCalculatedYieldData;
31
33
  getCalculatedYieldTotals(productId: string, selectedMonth: ISelectedMonth): Promise<(mongoose.FlattenMaps<import("../models/Yield.model").IYieldData> & {
32
34
  _id: mongoose.Types.ObjectId;
@@ -13,6 +13,23 @@ const LoanProducts_model_1 = require("../models/LoanProducts.model");
13
13
  const microservice_tasks_db_1 = require("../db/microservice-tasks.db");
14
14
  const microservice_task_enum_1 = require("../enums/microservice-task.enum");
15
15
  class YieldService {
16
+ toPlainProduct(product) {
17
+ return {
18
+ ...product,
19
+ _id: product._id.toString(),
20
+ borrowerId: product.borrowerId.toString(),
21
+ facilityId: product.facilityId?.toString() ?? null,
22
+ masterLoanProductId: product.masterLoanProductId?.toString() ?? null,
23
+ };
24
+ }
25
+ toPlainCharge(charge) {
26
+ return {
27
+ ...charge,
28
+ _id: charge._id.toString(),
29
+ borrowerId: charge.borrowerId.toString(),
30
+ productId: charge.productId.toString(),
31
+ };
32
+ }
16
33
  async getCalculatedYieldData(charge, selectedMonth) {
17
34
  const oldData = await Yield_model_1.YieldData.findOne({
18
35
  chargeId: charge._id,
@@ -65,19 +82,21 @@ class YieldService {
65
82
  }
66
83
  const startDate = (0, date_helper_1.convertSelectedMonthToDate)(params.periodStart);
67
84
  const endDate = (0, date_helper_1.convertSelectedMonthToDate)(params.periodEnd);
68
- const charges = await LoanCharges_model_1.LoanCharge.find({ includeInYield: true }).lean();
85
+ const leanCharges = await LoanCharges_model_1.LoanCharge.find({ includeInYield: true }).lean();
86
+ const charges = leanCharges.map((c) => this.toPlainCharge(c));
69
87
  const productIDs = charges.reduce((acc, charge) => [...acc, charge.productId.toString()], []);
70
- const products = await LoanProducts_model_1.LoanProduct
88
+ const leanProducts = await LoanProducts_model_1.LoanProduct
71
89
  .find({ _id: { $in: Object.values(productIDs).map((id) => new mongoose_1.default.Types.ObjectId(id)) } })
72
90
  .populate('borrowerId')
73
91
  .lean();
92
+ const products = leanProducts.map((p) => this.toPlainProduct(p));
74
93
  const productMap = products
75
94
  .reduce((acc, product) => {
76
95
  return {
77
96
  ...acc,
78
- [product._id.toString()]: {
97
+ [product._id]: {
79
98
  product,
80
- charges: charges.filter((charge) => charge.productId.toString() == product._id.toString()),
99
+ charges: charges.filter((charge) => charge.productId == product._id),
81
100
  },
82
101
  };
83
102
  }, {});
@@ -88,9 +107,9 @@ class YieldService {
88
107
  await Promise.all(productMap[productId].charges.map(async (charge) => {
89
108
  const monthlyData = {
90
109
  charge,
91
- product: productMap[charge.productId.toString()].product,
110
+ product: productMap[charge.productId].product,
92
111
  order: 0,
93
- title: productMap[charge.productId.toString()].product.name,
112
+ title: productMap[charge.productId].product.name,
94
113
  };
95
114
  await Promise.all(Object.entries(allDates).map(async ([dateView, date]) => {
96
115
  if (!hasAllData) {
@@ -10,14 +10,33 @@ import {
10
10
  YieldData,
11
11
  yieldParamsValidationSchema,
12
12
  } from '../models/Yield.model';
13
- import { ILoanChargeDoc, LoanCharge } from '../models/LoanCharges.model';
14
- import { ILoanProductDoc, LoanProduct } from '../models/LoanProducts.model';
13
+ import { ILoanChargeLean, ILoanChargePlain, LoanCharge } from '../models/LoanCharges.model';
14
+ import { ILoanProductLean, ILoanProductPlain, LoanProduct } from '../models/LoanProducts.model';
15
15
  import { createMicroserviceTasks } from '../db/microservice-tasks.db';
16
16
  import { EMicroserviceTask } from '../enums/microservice-task.enum';
17
17
 
18
18
  export class YieldService {
19
19
 
20
- private async getCalculatedYieldData(charge: ILoanChargeDoc, selectedMonth: ISelectedMonth) {
20
+ private toPlainProduct(product: ILoanProductLean): ILoanProductPlain {
21
+ return {
22
+ ...product,
23
+ _id: product._id.toString(),
24
+ borrowerId: product.borrowerId.toString(),
25
+ facilityId: product.facilityId?.toString() ?? null,
26
+ masterLoanProductId: product.masterLoanProductId?.toString() ?? null,
27
+ };
28
+ }
29
+
30
+ private toPlainCharge(charge: ILoanChargeLean): ILoanChargePlain {
31
+ return {
32
+ ...charge,
33
+ _id: charge._id.toString(),
34
+ borrowerId: charge.borrowerId.toString(),
35
+ productId: charge.productId.toString(),
36
+ };
37
+ }
38
+
39
+ private async getCalculatedYieldData(charge: ILoanChargePlain, selectedMonth: ISelectedMonth) {
21
40
  const oldData = await YieldData.findOne({
22
41
  chargeId: charge._id,
23
42
  productId: charge.productId,
@@ -74,25 +93,28 @@ export class YieldService {
74
93
 
75
94
  const startDate = convertSelectedMonthToDate(params.periodStart);
76
95
  const endDate = convertSelectedMonthToDate(params.periodEnd);
77
- const charges = await LoanCharge.find({ includeInYield: true }).lean();
96
+ const leanCharges = await LoanCharge.find({ includeInYield: true }).lean();
97
+ const charges = leanCharges.map((c) => this.toPlainCharge(c));
78
98
 
79
99
  const productIDs = charges.reduce((acc, charge) => [...acc, charge.productId.toString()], <string[]>[]);
80
100
 
81
- const products = await LoanProduct
101
+ const leanProducts = await LoanProduct
82
102
  .find({ _id: { $in: Object.values(productIDs).map((id) => new mongoose.Types.ObjectId(id)) } })
83
103
  .populate('borrowerId')
84
104
  .lean();
85
105
 
106
+ const products = leanProducts.map((p) => this.toPlainProduct(p));
107
+
86
108
  const productMap = products
87
109
  .reduce((acc, product) => {
88
110
  return {
89
111
  ...acc,
90
- [product._id.toString()]: {
112
+ [product._id]: {
91
113
  product,
92
- charges: charges.filter((charge) => charge.productId.toString() == product._id.toString()),
114
+ charges: charges.filter((charge) => charge.productId == product._id),
93
115
  },
94
116
  };
95
- }, <{ [productId: string]: { product: ILoanProductDoc, charges: ILoanChargeDoc[] } }>{});
117
+ }, <{ [productId: string]: { product: ILoanProductPlain, charges: ILoanChargePlain[] } }>{});
96
118
 
97
119
  const allDates = getStartOfMonthsBetweenDates(startDate, endDate);
98
120
 
@@ -106,9 +128,9 @@ export class YieldService {
106
128
 
107
129
  const monthlyData = {
108
130
  charge,
109
- product: productMap[charge.productId.toString()].product,
131
+ product: productMap[charge.productId].product,
110
132
  order: 0,
111
- title: productMap[charge.productId.toString()].product.name,
133
+ title: productMap[charge.productId].product.name,
112
134
  };
113
135
 
114
136
  await Promise.all(Object.entries(allDates).map(async ([dateView, date]) => {