@@ -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