Skip to content

Commit bddeb56

Browse files
Merge pull request #2690 from JaySoni1/WEB-117-loan-account-charge-amount-charges-doubling-on-application-modification
WEB-117 Loan Account Charge(Amount% charges) Doubling on Application Modification
2 parents 1bf6665 + 1430416 commit bddeb56

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

src/app/loans/edit-loans-account/edit-loans-account.component.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,29 @@ export class EditLoansAccountComponent {
129129
const locale = this.settingsService.language.code;
130130
const dateFormat = this.settingsService.dateFormat;
131131
const loanType = 'individual';
132+
const uniqueCharges = new Map<number | string, any>();
133+
(this.loansAccount.charges ?? []).forEach((charge: any) => {
134+
const chargeId = charge.chargeId;
135+
if (chargeId == null) {
136+
return;
137+
} // Skip malformed entries
138+
uniqueCharges.set(chargeId, charge);
139+
});
140+
132141
const loansAccountData = {
133142
...this.loansAccount,
134143
clientId: this.loansAccountAndTemplate.clientId,
135-
charges: this.loansAccount.charges.map((charge: any) => ({
136-
chargeId: charge.id,
137-
amount: charge.amount,
138-
dueDate: charge.dueDate && this.dateUtils.formatDate(charge.dueDate, dateFormat)
139-
})),
144+
charges: Array.from(uniqueCharges.values()).map((charge: any) => {
145+
const result: any = {
146+
chargeId: charge.chargeId,
147+
amount: charge.amount,
148+
dueDate: charge.dueDate && this.dateUtils.formatDate(charge.dueDate, dateFormat)
149+
};
150+
if (charge.id && charge.id !== charge.chargeId) {
151+
result.id = charge.id;
152+
}
153+
return result;
154+
}),
140155
collateral: this.loansAccount.collateral.map((collateralEle: any) => ({
141156
type: collateralEle.type,
142157
value: collateralEle.value,

src/app/loans/loans-account-stepper/loans-account-charges-step/loans-account-charges-step.component.ts

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ export class LoansAccountChargesStepComponent implements OnInit, OnChanges {
140140
ngOnInit() {
141141
if (this.loansAccountTemplate && this.loansAccountTemplate.charges) {
142142
this.chargesDataSource =
143-
this.loansAccountTemplate.charges.map((charge: any) => ({ ...charge, id: charge.chargeId })) || [];
143+
this.loansAccountTemplate.charges.map((charge: any) => {
144+
return {
145+
...charge,
146+
id: charge.id,
147+
chargeId: charge.chargeId
148+
};
149+
}) || [];
144150
}
145151
this.dataSource = new MatTableDataSource<any>(this.activeClientMembers);
146152
}
@@ -162,13 +168,26 @@ export class LoansAccountChargesStepComponent implements OnInit, OnChanges {
162168
if (this.loansAccountProductTemplate.overdueCharges) {
163169
this.overDueChargesDataSource = this.loansAccountProductTemplate.overdueCharges;
164170
}
171+
const isModification = this.loanId != null;
165172
if (
166173
this.loansAccountProductTemplate.charges &&
167174
this.loansAccountProductTemplate.charges.length > 0 &&
168175
this.chargesDataSource.length === 0
169176
) {
170177
this.chargesDataSource =
171-
this.loansAccountProductTemplate.charges.map((charge: any) => ({ ...charge, id: charge.chargeId })) || [];
178+
this.loansAccountProductTemplate.charges.map((charge: any) => ({
179+
...charge,
180+
chargeId: charge.chargeId || charge.id
181+
})) || [];
182+
} else if (isModification && this.loansAccountTemplate && this.loansAccountTemplate.charges) {
183+
this.chargesDataSource =
184+
this.loansAccountTemplate.charges.map((charge: any) => {
185+
return {
186+
...charge,
187+
id: charge.id,
188+
chargeId: charge.chargeId
189+
};
190+
}) || [];
172191
}
173192
}
174193
}
@@ -177,7 +196,11 @@ export class LoansAccountChargesStepComponent implements OnInit, OnChanges {
177196
* Add a charge
178197
*/
179198
addCharge(charge: any) {
180-
this.chargesDataSource = this.chargesDataSource.concat([charge.value]);
199+
const newCharge = {
200+
...charge.value,
201+
chargeId: charge.value.id || charge.value.chargeId
202+
};
203+
this.chargesDataSource = this.chargesDataSource.concat([newCharge]);
181204
charge.value = '';
182205
this.pristine = false;
183206
}
@@ -313,10 +336,38 @@ export class LoansAccountChargesStepComponent implements OnInit, OnChanges {
313336
* Returns Loans Account Charges and Collateral Form
314337
*/
315338
get loansAccountCharges() {
339+
const uniqueCharges = this.getUniqueCharges(this.chargesDataSource);
316340
return {
317-
charges: this.chargesDataSource
341+
charges: uniqueCharges.map((charge: any) => {
342+
const result: any = {};
343+
result.chargeId = charge.chargeId;
344+
345+
if (charge.id && charge.id !== charge.chargeId) {
346+
result.id = charge.id;
347+
}
348+
349+
if (charge.amount !== undefined) result.amount = charge.amount;
350+
if (charge.dueDate !== undefined) result.dueDate = charge.dueDate;
351+
if (charge.feeInterval !== undefined) result.feeInterval = charge.feeInterval;
352+
if (charge.feeOnMonthDay !== undefined) result.feeOnMonthDay = charge.feeOnMonthDay;
353+
354+
return result;
355+
})
318356
};
319357
}
358+
private getUniqueCharges<T extends { id?: number | string; chargeId?: number | string }>(charges: T[]): T[] {
359+
const uniqueChargesMap = new Map<number | string, T>();
360+
361+
for (const charge of charges ?? []) {
362+
const chargeId = charge.chargeId;
363+
if (chargeId == null) {
364+
continue;
365+
}
366+
uniqueChargesMap.set(chargeId, charge);
367+
}
368+
369+
return Array.from(uniqueChargesMap.values());
370+
}
320371

321372
get selectedClientMembers() {
322373
return { selectedMembers: this.activeClientMembers.filter((item: any) => item.selected) };

0 commit comments

Comments
 (0)