/** * Copyright since 2025 Mifos Initiative * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /** Angular Imports */ import { Injectable, inject } from '@angular/core'; import { ActivatedRouteSnapshot } from '@angular/router'; /** rxjs Imports */ import { Observable, of } from 'rxjs'; import { catchError, map, switchMap } from 'rxjs/operators'; /** Custom Services */ import { SavingsService } from '../savings.service'; import { VoucherCorrectionService } from '../../cashier/voucher-correction.service'; /** * Savings Account data resolver. */ @Injectable() export class SavingsAccountViewResolver { private savingsService = inject(SavingsService); private voucherCorrectionService = inject(VoucherCorrectionService); /** * Returns the Savings Account data, without the deposits a completed voucher correction took off this account: * the passbook and the transaction list read their rows from here, and a deposit that was posted to the wrong * account and moved should not appear on it at all. * @param {ActivatedRouteSnapshot} route Route Snapshot * @returns {Observable} */ resolve(route: ActivatedRouteSnapshot): Observable { const savingAccountId = route.paramMap.get('savingAccountId'); return this.savingsService.getSavingsAccountData(savingAccountId).pipe( switchMap((account: any) => this.voucherCorrectionService.voidedTransactions(Number(savingAccountId)).pipe( catchError(() => of([] as number[])), map((voided: number[]) => { if (voided.length && Array.isArray(account?.transactions)) { account.transactions = account.transactions.filter((txn: any) => !voided.includes(txn.id)); } return account; }) ) ) ); } }