|
| 1 | +/** |
| 2 | + * Copyright since 2025 Mifos Initiative |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | + */ |
| 8 | + |
| 9 | +/** Angular Imports */ |
| 10 | +import { Component, OnInit, inject } from '@angular/core'; |
| 11 | +import { HttpErrorResponse } from '@angular/common/http'; |
| 12 | +import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; |
| 13 | +import { TranslateService } from '@ngx-translate/core'; |
| 14 | + |
| 15 | +/** Custom Services */ |
| 16 | +import { AlertService } from 'app/core/alert/alert.service'; |
| 17 | +import { amountValueValidator } from 'app/shared/validators/amount-value.validator'; |
| 18 | +import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; |
| 19 | +import { CdkTextareaAutosize } from '@angular/cdk/text-field'; |
| 20 | +import { PositiveNumberDirective } from 'app/directives/positive-number.directive'; |
| 21 | +import { LoanAccountActionsBaseComponent } from '../loan-account-actions-base.component'; |
| 22 | +import { WorkingCapitalLoanDiscountUpdateRequest } from 'app/loans/loans.service'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Update discount action for Working Capital Loan. |
| 26 | + */ |
| 27 | +@Component({ |
| 28 | + selector: 'mifosx-update-discount', |
| 29 | + standalone: true, |
| 30 | + templateUrl: './update-discount.component.html', |
| 31 | + imports: [ |
| 32 | + ...STANDALONE_SHARED_IMPORTS, |
| 33 | + CdkTextareaAutosize, |
| 34 | + PositiveNumberDirective |
| 35 | + ] |
| 36 | +}) |
| 37 | +export class UpdateDiscountComponent extends LoanAccountActionsBaseComponent implements OnInit { |
| 38 | + private formBuilder = inject(UntypedFormBuilder); |
| 39 | + private alertService = inject(AlertService); |
| 40 | + private translateService = inject(TranslateService); |
| 41 | + |
| 42 | + readonly maxNoteLength = 500; |
| 43 | + |
| 44 | + updateDiscountForm: UntypedFormGroup; |
| 45 | + isSubmitting = false; |
| 46 | + submitErrorMessage = ''; |
| 47 | + discountValue = 0; |
| 48 | + |
| 49 | + ngOnInit(): void { |
| 50 | + this.discountValue = this.dataObject?.discount ?? this.dataObject?.discountAmount ?? 0; |
| 51 | + this.updateDiscountForm = this.formBuilder.group({ |
| 52 | + discountAmount: [ |
| 53 | + this.discountValue, |
| 54 | + [ |
| 55 | + Validators.required, |
| 56 | + Validators.min(0), |
| 57 | + amountValueValidator() |
| 58 | + ] |
| 59 | + ], |
| 60 | + note: [ |
| 61 | + '', |
| 62 | + Validators.maxLength(this.maxNoteLength) |
| 63 | + ] |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + submit(): void { |
| 68 | + if (!this.updateDiscountForm.valid || this.isSubmitting) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + this.isSubmitting = true; |
| 73 | + this.submitErrorMessage = ''; |
| 74 | + |
| 75 | + const formValue = this.updateDiscountForm.value; |
| 76 | + const payload: WorkingCapitalLoanDiscountUpdateRequest = { |
| 77 | + discountAmount: Number(formValue.discountAmount), |
| 78 | + note: formValue.note, |
| 79 | + locale: this.settingsService.language.code, |
| 80 | + dateFormat: this.settingsService.dateFormat |
| 81 | + }; |
| 82 | + |
| 83 | + this.loanService.updateWorkingCapitalLoanDiscount(this.loanId, payload).subscribe({ |
| 84 | + next: () => { |
| 85 | + this.alertService.alert({ |
| 86 | + type: 'Success', |
| 87 | + message: this.translateService.instant('labels.messages.workingCapitalDiscountUpdated') |
| 88 | + }); |
| 89 | + this.isSubmitting = false; |
| 90 | + this.gotoLoanDefaultView(); |
| 91 | + }, |
| 92 | + error: (error: HttpErrorResponse) => { |
| 93 | + this.submitErrorMessage = this.mapDiscountError(error); |
| 94 | + this.isSubmitting = false; |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + private mapDiscountError(error: HttpErrorResponse): string { |
| 100 | + const backendError = error?.error?.errors?.[0]; |
| 101 | + return ( |
| 102 | + backendError?.defaultUserMessage || |
| 103 | + error?.error?.defaultUserMessage || |
| 104 | + this.translateService.instant('labels.messages.unableToUpdateDiscount') |
| 105 | + ); |
| 106 | + } |
| 107 | +} |
0 commit comments