- Implemented RentPaymentHistoryServiceTest to validate payment retrieval with refunds and reversals. - Created ReverseRentPaymentServiceTest to ensure correct handling of payment reversals and allocation releases. - Developed rent-payment-allocation-dialog component with form for surplus allocation. - Added rent-payment-refund-dialog component for processing refunds with validation. - Introduced rent-payment-reversal-dialog component for reversing payments with reason input. - Implemented utility functions for formatting Euro values and handling date formatting. - Enhanced rent-payment-dialogs.spec.ts to cover new dialog functionalities and error handling.
87 lines
2.9 KiB
TypeScript
87 lines
2.9 KiB
TypeScript
import { Component, inject, signal } from '@angular/core';
|
||
import {
|
||
AbstractControl,
|
||
FormControl,
|
||
FormGroup,
|
||
ReactiveFormsModule,
|
||
ValidationErrors,
|
||
Validators,
|
||
} from '@angular/forms';
|
||
import { MatButtonModule } from '@angular/material/button';
|
||
import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog';
|
||
import { MatFormFieldModule } from '@angular/material/form-field';
|
||
import { MatInputModule } from '@angular/material/input';
|
||
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
|
||
import { finalize } from 'rxjs';
|
||
import { httpErrorMessage } from '../../services/http-error-message';
|
||
import { formatEuro, utcIsoDate } from '../../services/rent-payment-format';
|
||
import { RentPayment, RentPaymentService } from '../../services/rent-payment.service';
|
||
import { isRentPaymentDataStale, RENT_PAYMENT_DATA_STALE } from '../rent-payment-dialog-result';
|
||
|
||
@Component({
|
||
selector: 'app-rent-payment-refund-dialog',
|
||
imports: [
|
||
ReactiveFormsModule,
|
||
MatButtonModule,
|
||
MatDialogModule,
|
||
MatFormFieldModule,
|
||
MatInputModule,
|
||
MatProgressSpinnerModule,
|
||
],
|
||
templateUrl: './rent-payment-refund-dialog.html',
|
||
styleUrl: './rent-payment-refund-dialog.css',
|
||
})
|
||
export class RentPaymentRefundDialog {
|
||
readonly payment = inject<RentPayment>(MAT_DIALOG_DATA);
|
||
private readonly dialogRef = inject(MatDialogRef<RentPaymentRefundDialog>);
|
||
private readonly payments = inject(RentPaymentService);
|
||
private readonly operationId = crypto.randomUUID();
|
||
readonly today = utcIsoDate();
|
||
readonly saving = signal(false);
|
||
readonly error = signal('');
|
||
readonly formatEuro = formatEuro;
|
||
|
||
readonly form = new FormGroup({
|
||
refundedAt: new FormControl(this.today, {
|
||
nonNullable: true,
|
||
validators: [Validators.required, this.notFuture.bind(this)],
|
||
}),
|
||
amount: new FormControl(this.payment.unallocatedBalance, {
|
||
nonNullable: true,
|
||
validators: [
|
||
Validators.required,
|
||
Validators.min(0.01),
|
||
Validators.max(this.payment.unallocatedBalance),
|
||
],
|
||
}),
|
||
});
|
||
|
||
submit(): void {
|
||
this.form.markAllAsTouched();
|
||
if (this.form.invalid || this.saving()) return;
|
||
this.saving.set(true);
|
||
this.error.set('');
|
||
this.payments
|
||
.refund(this.payment.id, {
|
||
operationId: this.operationId,
|
||
paymentVersion: this.payment.version,
|
||
...this.form.getRawValue(),
|
||
})
|
||
.pipe(finalize(() => this.saving.set(false)))
|
||
.subscribe({
|
||
next: (payment) => this.dialogRef.close(payment),
|
||
error: (error) => {
|
||
if (isRentPaymentDataStale(error)) {
|
||
this.dialogRef.close(RENT_PAYMENT_DATA_STALE);
|
||
return;
|
||
}
|
||
this.error.set(httpErrorMessage(error, 'Impossible d’enregistrer ce remboursement.'));
|
||
},
|
||
});
|
||
}
|
||
|
||
private notFuture(control: AbstractControl<string>): ValidationErrors | null {
|
||
return control.value > this.today ? { futureDate: true } : null;
|
||
}
|
||
}
|