/** * 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 { HttpClient, HttpParams } from '@angular/common/http'; import { Observable } from 'rxjs'; /** * Shared clearing services used across CTS and NACH — return-reason-code master, settlement-batch posting, * and reconciliation. */ @Injectable({ providedIn: 'root' }) export class ClearingService { private http = inject(HttpClient); // ----- Return reason codes ----- /** appliesTo: 'CTS' | 'NACH'. Returns a raw array of reason codes. */ getReturnReasonCodes(appliesTo: string): Observable { return this.http.get('/clearing/return-reason-codes', { params: new HttpParams().set('appliesTo', appliesTo) }); } // ----- Settlement batches ----- /** Server-side paginated — returns { pageItems, totalFilteredRecords }. */ getSettlementBatches(clearingType?: string, status?: string, offset = 0, limit = 20): Observable { let params = new HttpParams().set('offset', offset).set('limit', limit); if (clearingType) params = params.set('clearingType', clearingType); if (status) params = params.set('status', status); return this.http.get('/clearing/settlement-batches', { params }); } getSettlementBatch(id: string | number): Observable { return this.http.get(`/clearing/settlement-batches/${id}`); } /** payload: { clearingType, sourceBatchId }. */ prepareSettlement(payload: any): Observable { return this.http.post('/clearing/settlement-batches', payload); } postSettlement(id: string | number): Observable { return this.http.post( `/clearing/settlement-batches/${id}`, {}, { params: new HttpParams().set('command', 'post') } ); } rejectSettlement(id: string | number, payload: any): Observable { return this.http.post(`/clearing/settlement-batches/${id}`, payload, { params: new HttpParams().set('command', 'reject') }); } // ----- Reconciliations ----- getReconciliations(clearingType?: string, offset = 0, limit = 20): Observable { let params = new HttpParams().set('offset', offset).set('limit', limit); if (clearingType) params = params.set('clearingType', clearingType); return this.http.get('/clearing/reconciliations', { params }); } runReconciliation(payload: any): Observable { return this.http.post('/clearing/reconciliations', payload); } }