149 lines
3.9 KiB
TypeScript
149 lines
3.9 KiB
TypeScript
/**
|
|
* 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<any>} Users data
|
|
*/
|
|
getUsers(): Observable<any> {
|
|
return this.http.get(`/users`);
|
|
}
|
|
|
|
/**
|
|
* @returns {Observable<any>} Users template data
|
|
*/
|
|
getUsersTemplate(): Observable<any> {
|
|
return this.http.get('/users/template');
|
|
}
|
|
|
|
/**
|
|
* @param {any} user User to be created.
|
|
* @returns {Observable<any>}
|
|
*/
|
|
createUser(user: any): Observable<any> {
|
|
return this.http.post('/users', user);
|
|
}
|
|
|
|
/**
|
|
* @param {string} userId user ID of user.
|
|
* @param {any} user user to be updated.
|
|
* @returns {Observable<any>} User.
|
|
*/
|
|
editUser(userId: string, user: any): Observable<any> {
|
|
return this.http.put(`/users/${userId}`, user);
|
|
}
|
|
|
|
/**
|
|
* @param {string} userId user ID of user.
|
|
* @returns {Observable<any>} User.
|
|
*/
|
|
getUser(userId: string): Observable<any> {
|
|
return this.http.get(`/users/${userId}`);
|
|
}
|
|
|
|
/**
|
|
* Change User Password
|
|
* @param userId User Id of users
|
|
* @param password New Password of the user
|
|
* @returns {Observable<any>}
|
|
*/
|
|
changePassword(userId: string, passwordObj: any) {
|
|
return this.http.put(`/users/${userId}`, passwordObj);
|
|
}
|
|
|
|
/**
|
|
* @param {string} userId user ID of user.
|
|
* @returns {Observable<any>}
|
|
*/
|
|
deleteUser(userId: string): Observable<any> {
|
|
return this.http.delete(`/users/${userId}`);
|
|
}
|
|
|
|
/**
|
|
* @param {any} officeId ID of office to retrieve staff from.
|
|
* @returns {Observable<any>} Staff data.
|
|
*/
|
|
getStaff(officeId: any): Observable<any> {
|
|
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<any>} Pending users
|
|
*/
|
|
getPendingUsers(
|
|
status?: string,
|
|
username?: string,
|
|
fromDate?: string,
|
|
toDate?: string,
|
|
offset: number = 0,
|
|
limit: number = 20
|
|
): Observable<any> {
|
|
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<any>}
|
|
*/
|
|
getPendingUser(id: string | number): Observable<any> {
|
|
return this.http.get(`/users/pending/${id}`);
|
|
}
|
|
|
|
/**
|
|
* @param {string | number} id Pending request ID
|
|
* @returns {Observable<any>}
|
|
*/
|
|
approvePendingUser(id: string | number): Observable<any> {
|
|
return this.http.post(`/users/pending/${id}/approve`, {});
|
|
}
|
|
|
|
/**
|
|
* @param {string | number} id Pending request ID
|
|
* @param {string} reason Rejection reason
|
|
* @returns {Observable<any>}
|
|
*/
|
|
rejectPendingUser(id: string | number, reason: string): Observable<any> {
|
|
return this.http.post(`/users/pending/${id}/reject`, { reason });
|
|
}
|
|
}
|