95 lines
2.7 KiB
TypeScript
95 lines
2.7 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
import { UserModel } from '../../auth/models/user.model';
|
|
import { User } from '../../accounts/models/user.model';
|
|
import { Roles } from '../../shared/global';
|
|
import { Observable } from 'rxjs';
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class UserService {
|
|
|
|
private readonly userURL = '/users';
|
|
|
|
constructor(private http: HttpClient) {
|
|
}
|
|
|
|
loadUsers(options: LoadUserOptions): Observable<User[]> {
|
|
return this.http.post<User[]>(this.userURL + '/search', options);
|
|
}
|
|
|
|
getUser(id: string, ops?: { withAddresses?: boolean; view?: 'profile' | 'edit' | 'billing' }): Observable<User> {
|
|
let url = `${this.userURL}/${id}`;
|
|
const params: string[] = [];
|
|
if (ops?.withAddresses !== undefined) {
|
|
params.push(`withAddresses=${ops.withAddresses}`);
|
|
}
|
|
if (ops?.view) {
|
|
params.push(`view=${ops.view}`);
|
|
}
|
|
if (params.length) {
|
|
url += `?${params.join('&')}`;
|
|
}
|
|
return this.http.get<User>(url);
|
|
}
|
|
|
|
userNameExists(userName: string): Observable<boolean> {
|
|
const reqData = { username: userName };
|
|
return this.http.post<boolean>(`${this.userURL}/exists`, reqData);
|
|
}
|
|
|
|
saveUser(user: User): Observable<User> {
|
|
return user._id !== '0' ? this.updateUser(user) : this.createUser(user);
|
|
}
|
|
|
|
private createUser(user: User): Observable<User> {
|
|
return this.http.post<User>(this.userURL, user);
|
|
}
|
|
|
|
private updateUser(user: User): Observable<User> {
|
|
return this.http.put<User>(`${this.userURL}/${user._id}`, user);
|
|
}
|
|
|
|
updateLang(lang): Observable<User> {
|
|
return this.http.post<User>(`${this.userURL}/updateLang`, { lang: lang });
|
|
}
|
|
|
|
deleteUser(user: User) {
|
|
return this.http.delete<User>(`${this.userURL}/${user._id}`);
|
|
}
|
|
|
|
getUserDetail(username: string) {
|
|
return this.http.post<User>(`${this.userURL}/getUserDetail`, { username: username });
|
|
}
|
|
|
|
signup(form: any) {
|
|
return this.http.post<any>(`${this.userURL}/signup`, form);
|
|
}
|
|
|
|
requestVerifyEmail(email: string) {
|
|
return this.http.post<any>(`${this.userURL}/signup/requestVerifyEmail`, { email });
|
|
}
|
|
|
|
signupValidate(token: string) {
|
|
return this.http.post<any>(`${this.userURL}/signup/validate`, { token });
|
|
}
|
|
|
|
getAccountType(user: UserModel | User): string {
|
|
if (!user) return '';
|
|
if ('roles' in user && Array.isArray(user.roles) && user.roles.length > 0) {
|
|
const roleId = user.roles[0];
|
|
return Roles[roleId] || '';
|
|
}
|
|
if ('kind' in user && user.kind && Roles[user.kind]) {
|
|
return Roles[user.kind];
|
|
}
|
|
return '';
|
|
}
|
|
}
|
|
|
|
export interface LoadUserOptions {
|
|
byPuid: string;
|
|
accountType?: number;
|
|
} |