/** * 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'; /** * Users service. */ @Injectable({ providedIn: 'root' }) export class UsersService { private http = inject(HttpClient); /** * @returns {Observable} Users data */ getUsers(): Observable { return this.http.get(`/users`); } /** * @returns {Observable} Users template data */ getUsersTemplate(): Observable { return this.http.get('/users/template'); } /** * @param {any} user User to be created. * @returns {Observable} */ createUser(user: any): Observable { return this.http.post('/users', user); } /** * @param {string} userId user ID of user. * @param {any} user user to be updated. * @returns {Observable} User. */ editUser(userId: string, user: any): Observable { return this.http.put(`/users/${userId}`, user); } /** * @param {string} userId user ID of user. * @returns {Observable} User. */ getUser(userId: string): Observable { return this.http.get(`/users/${userId}`); } /** * Change User Password * @param userId User Id of users * @param password New Password of the user * @returns {Observable} */ changePassword(userId: string, passwordObj: any) { return this.http.put(`/users/${userId}`, passwordObj); } /** * @param {string} userId user ID of user. * @returns {Observable} */ deleteUser(userId: string): Observable { return this.http.delete(`/users/${userId}`); } /** * @param {any} officeId ID of office to retrieve staff from. * @returns {Observable} Staff data. */ getStaff(officeId: any): Observable { const httpParams = new HttpParams().set('officeId', officeId.toString()).set('status', 'all'); return this.http.get('/staff', { params: httpParams }); } /** * @param {string} status Status filter (e.g. 'PENDING') * @param {string} username Username filter * @param {string} fromDate From date filter * @param {string} toDate To date filter * @param {number} offset Offset * @param {number} limit Limit * @returns {Observable} Pending users */ getPendingUsers( status?: string, username?: string, fromDate?: string, toDate?: string, offset: number = 0, limit: number = 20 ): Observable { let httpParams = new HttpParams().set('offset', offset.toString()).set('limit', limit.toString()); if (status) { httpParams = httpParams.set('status', status); } if (username) { httpParams = httpParams.set('username', username); } if (fromDate) { httpParams = httpParams.set('fromDate', fromDate); } if (toDate) { httpParams = httpParams.set('toDate', toDate); } return this.http.get('/users/pending', { params: httpParams }); } /** * @param {string | number} id Pending request ID * @returns {Observable} */ getPendingUser(id: string | number): Observable { return this.http.get(`/users/pending/${id}`); } /** * @param {string | number} id Pending request ID * @returns {Observable} */ approvePendingUser(id: string | number): Observable { return this.http.post(`/users/pending/${id}/approve`, {}); } /** * @param {string | number} id Pending request ID * @param {string} reason Rejection reason * @returns {Observable} */ rejectPendingUser(id: string | number, reason: string): Observable { return this.http.post(`/users/pending/${id}/reject`, { reason }); } }