/** * 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'; /** rxjs Imports */ import { Observable } from 'rxjs'; /** * Shares Service. */ @Injectable({ providedIn: 'root' }) export class SharesService { private http = inject(HttpClient); /** * @param {any} params Optional HTTP params (like offset, limit, search) * @returns {Observable} List of all share accounts */ getSharesAccounts(params?: any): Observable { return this.http.get('/accounts/share', { params }); } /** * @returns {Observable} List of all share accounts with client details summary */ getShareAccountSummaries(searchTerm?: string): Observable { let params = new HttpParams(); if (searchTerm) { params = params.set('searchTerm', searchTerm); } return this.http.get('/shareaccounts/summary', { params }); } /** * @returns {Observable} List of all offices */ getOffices(): Observable { return this.http.get('/offices'); } /** * @returns {Observable} List of all share products */ getShareProducts(): Observable { return this.http.get('/products/share'); } /** * @param {string} accountId Shares Account Id of account to get data for. * @param {boolean} template Shares account template required?. * @returns {Observable} Shares data. */ getSharesAccountData(accountId: string, template: boolean): Observable { const httpParams = new HttpParams().set('template', template.toString()); return this.http.get(`/accounts/share/${accountId}`, { params: httpParams }); } /** * @param {number} offset Paging offset. * @param {number} limit Paging size limit. * @returns {Observable} A page of all (active) share accounts, backed by m_share_account. */ getAllShareAccounts(offset: number = 0, limit: number = 200): Observable { const httpParams = new HttpParams().set('offset', offset.toString()).set('limit', limit.toString()); return this.http.get('/accounts/share', { params: httpParams }); } /** * @param {string} clientId Client Id assosciated with shares account. * @param {string} productId Product Id * @returns {Observable} Shares account template. */ getSharesAccountTemplate(clientId: string, productId?: string): Observable { let httpParams = new HttpParams().set('clientId', clientId); httpParams = productId ? httpParams.set('productId', productId) : httpParams; return this.http.get('/accounts/share/template', { params: httpParams }); } /** * @param {any} sharesAccount Shares Account * @returns {Observable} */ createSharesAccount(sharesAccount: any): Observable { return this.http.post('/accounts/share', sharesAccount); } /** * @param {string} accountId: Shares account Id. * @param {any} sharesAccount Shares Account * @returns {Observable} */ updateSharesAccount(accountId: string, sharesAccount: any): Observable { return this.http.put(`/accounts/share/${accountId}`, sharesAccount); } /** * @param {string} accountId shares account Id * @returns {Observable} */ deleteSharesAccount(accountId: string): Observable { return this.http.delete(`/accounts/share/${accountId}`); } /** * @param {string} accountId Shares Account Id * @param {string} command Command * @param {any} data Data * @returns {Observable} Shares data. */ executeSharesAccountCommand(accountId: string, command: string, data: any): Observable { const httpParams = new HttpParams().set('command', command); return this.http.post(`/accounts/share/${accountId}`, data, { params: httpParams }); } /** * Whether a new additional-share tranche can be funded from the member's linked savings. * * Tranche-scoped, unlike `/sharesavingsdebit/eligibility`: that one values an account's first * application by summing every purchase on the account, which for a further tranche would quote * the member's whole holding history and call an already-settled account ineligible. * * @returns {Observable} `{ eligible, reason, requestedShares, unitPrice, amount, * savingsAccountNo, savingsBalance }`. */ getAdditionalSharesSavingsEligibility(accountId: string, requestedShares: number): Observable { const httpParams = new HttpParams().set('requestedShares', String(requestedShares)); return this.http.get(`/shareaccounts/${accountId}/additionalshares/savings-eligibility`, { params: httpParams }); } /** * @param {string} accountId Shares Account Id. * @param {any} data Details edit payload. * @returns {Observable} */ submitShareAccountEdit(accountId: string, data: any): Observable { return this.http.post(`/shareaccounts/${accountId}/edit`, data); } /** * @param {string} accountId Shares Account Id. * @returns {Observable} The pending details edit for this account, if any. */ getPendingShareAccountEdit(accountId: string): Observable { return this.http.get(`/shareaccounts/${accountId}/edit/pending`); } /** * @returns {Observable} All Share Account details edits pending approval. */ getAllPendingShareAccountEdits(): Observable { return this.http.get(`/shareaccounts/edit/pending`); } /** * Share accounts submitted and awaiting a checker's approval. * * Its own endpoint rather than `/accounts/share`, which only lists ACTIVE accounts and returns the * whole account graph per row; this returns just the member, product, requested shares and value a * checker decides on. * * @returns {Observable} One row per pending account. */ getPendingShareAccounts(): Observable { return this.http.get('/shareaccounts/pending'); } /** * @param {string} accountId Shares Account Id. * @returns {Observable} */ approveShareAccountEdit(accountId: string): Observable { return this.http.post(`/shareaccounts/${accountId}/edit/approve`, {}); } /** * @param {string} accountId Shares Account Id. * @param {string} rejectReason Optional reason for rejection. * @returns {Observable} */ rejectShareAccountEdit(accountId: string, rejectReason?: string): Observable { return this.http.post(`/shareaccounts/${accountId}/edit/reject`, { rejectReason: rejectReason || '' }); } }