diff --git a/src/app/loans/loans-view/loan-account-actions/loan-reschedule/loan-reschedule.component.ts b/src/app/loans/loans-view/loan-account-actions/loan-reschedule/loan-reschedule.component.ts index b7e2c443a..78957b591 100644 --- a/src/app/loans/loans-view/loan-account-actions/loan-reschedule/loan-reschedule.component.ts +++ b/src/app/loans/loans-view/loan-account-actions/loan-reschedule/loan-reschedule.component.ts @@ -126,11 +126,25 @@ export class LoanRescheduleComponent implements OnInit { }; data.loanId = this.loanId; - // Add waived penalty charge IDs if penalties are being waived + // Waive penalties first if selected, then submit reschedule if (this.waivePenalties.value && this.selectedPenalties.length > 0) { - data.chargeIds = this.selectedPenalties; + this.penaltyManagementService.waivePenalties(this.loanId, this.selectedPenalties).subscribe({ + next: () => { + this.submitReschedule(data); + }, + error: (error: any) => { + console.error('Error waiving penalties:', error); + // Continue with reschedule even if waive fails + this.submitReschedule(data); + } + }); + } else { + this.submitReschedule(data); } + } + /** Submit the reschedule after penalties are waived */ + private submitReschedule(data: any) { this.loanService.submitRescheduleData(data).subscribe((response: any) => { // TODO: needs to be updated // mentioned in Community App: diff --git a/src/app/loans/loans-view/loan-account-actions/make-repayment/make-repayment.component.ts b/src/app/loans/loans-view/loan-account-actions/make-repayment/make-repayment.component.ts index a2d7364ab..5367971ed 100644 --- a/src/app/loans/loans-view/loan-account-actions/make-repayment/make-repayment.component.ts +++ b/src/app/loans/loans-view/loan-account-actions/make-repayment/make-repayment.component.ts @@ -326,11 +326,25 @@ export class MakeRepaymentComponent implements OnInit { } delete data.skipInterestRefund; - // Add waived penalty charge IDs if penalties are being waived + // Waive penalties first if selected, then submit repayment if (this.waivePenalties && this.selectedPenalties.length > 0) { - data.chargeIds = this.selectedPenalties; + this.penaltyManagementService.waivePenalties(this.loanId, this.selectedPenalties).subscribe({ + next: () => { + this.submitRepayment(data); + }, + error: (error: any) => { + console.error('Error waiving penalties:', error); + // Continue with repayment even if waive fails + this.submitRepayment(data); + } + }); + } else { + this.submitRepayment(data); } + } + /** Submit the repayment after penalties are waived */ + private submitRepayment(data: any) { this.loanService.submitLoanActionButton(this.loanId, data, this.command).subscribe((response: any) => { this.router.navigate(['../../transactions'], { relativeTo: this.route }); }); diff --git a/src/app/loans/services/penalty-management.service.ts b/src/app/loans/services/penalty-management.service.ts index a0339935d..d20e2d6f6 100644 --- a/src/app/loans/services/penalty-management.service.ts +++ b/src/app/loans/services/penalty-management.service.ts @@ -1,6 +1,6 @@ import { Injectable, inject } from '@angular/core'; -import { Observable } from 'rxjs'; -import { map } from 'rxjs/operators'; +import { Observable, forkJoin, of } from 'rxjs'; +import { map, catchError } from 'rxjs/operators'; import { LoansService } from '../loans.service'; import { Dates } from 'app/core/utils/dates'; @@ -187,4 +187,30 @@ export class PenaltyManagementService { // Fall back to the raw name (translate pipe will no-op if not a key) return rawName; } + + /** + * Waive selected penalties for a loan + * Uses the existing executeLoansAccountChargesCommand API with 'waive' command + * @param loanId The loan ID + * @param penaltyIds Array of penalty/charge IDs to waive + * @returns Observable that completes when all penalties are waived + */ + waivePenalties(loanId: string, penaltyIds: number[]): Observable { + if (!penaltyIds || penaltyIds.length === 0) { + return of([]); + } + + // Create waive requests for each penalty + const waiveRequests = penaltyIds.map((chargeId: number) => + this.loanService.executeLoansAccountChargesCommand(loanId, 'waive', {}, chargeId).pipe( + catchError((error: any) => { + console.error(`Error waiving penalty ${chargeId}:`, error); + // Return null for failed waive operations so we can continue with others + return of(null); + }) + ) + ); + + return forkJoin(waiveRequests); + } }