Files
grimmory/booklore-ui/src/app/settings/user-management/user.service.ts
T
2025-05-27 21:46:36 -07:00

196 lines
5.2 KiB
TypeScript

import {inject, Injectable, Injector} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {BehaviorSubject, Observable, throwError} from 'rxjs';
import {API_CONFIG} from '../../config/api-config';
import {RxStompService} from '../../shared/websocket/rx-stomp.service';
import {Library} from '../../book/model/library.model';
import {catchError} from 'rxjs/operators';
import {CbxPageSpread, CbxPageViewMode} from '../../book/model/book.model';
export interface EntityViewPreferences {
global: EntityViewPreference;
overrides: EntityViewPreferenceOverride[];
}
export interface EntityViewPreference {
sortKey: string;
sortDir: 'ASC' | 'DESC';
view: 'GRID' | 'TABLE';
}
export interface EntityViewPreferenceOverride {
entityType: 'LIBRARY' | 'SHELF';
entityId: number;
preferences: EntityViewPreference;
}
export interface SidebarLibrarySorting {
field: string;
order: string;
}
export interface SidebarShelfSorting {
field: string;
order: string;
}
export interface PerBookSetting {
pdf: string;
epub: string;
cbx: string;
}
export type PageSpread = 'off' | 'even' | 'odd';
export interface PdfReaderSetting {
pageSpread: PageSpread;
pageZoom: string;
showSidebar: boolean;
}
export interface EpubReaderSetting {
theme: string;
font: string;
fontSize: number;
flow: string;
}
export interface CbxReaderSetting {
pageSpread: CbxPageSpread;
pageViewMode: CbxPageViewMode;
}
export interface UserSettings {
perBookSetting: PerBookSetting;
pdfReaderSetting: PdfReaderSetting;
epubReaderSetting: EpubReaderSetting;
cbxReaderSetting: CbxReaderSetting;
sidebarLibrarySorting: SidebarLibrarySorting;
sidebarShelfSorting: SidebarShelfSorting;
entityViewPreferences: EntityViewPreferences;
}
export interface User {
id: number;
username: string;
name: string;
email: string;
assignedLibraries: Library[];
permissions: {
admin: boolean;
canUpload: boolean;
canDownload: boolean;
canEmailBook: boolean;
canEditMetadata: boolean;
canManipulateLibrary: boolean;
};
userSettings: UserSettings;
provisioningMethod?: 'LOCAL' | 'OIDC' | 'REMOTE';
}
@Injectable({
providedIn: 'root'
})
export class UserService {
private readonly apiUrl = `${API_CONFIG.BASE_URL}/api/v1/auth/register`;
private readonly userUrl = `${API_CONFIG.BASE_URL}/api/v1/users`;
private http = inject(HttpClient);
private injector = inject(Injector);
private rxStompService?: RxStompService;
private userStateSubject = new BehaviorSubject<User | null>(null);
userState$ = this.userStateSubject.asObservable();
constructor() {
this.getMyself().subscribe(user => {
this.userStateSubject.next(user);
this.startWebSocket();
});
}
getMyself(): Observable<User> {
return this.http.get<User>(`${this.userUrl}/me`);
}
createUser(userData: Omit<User, 'id'>): Observable<void> {
return this.http.post<void>(this.apiUrl, userData);
}
getUsers(): Observable<User[]> {
return this.http.get<User[]>(this.userUrl);
}
updateUser(userId: number, updateData: Partial<User>): Observable<User> {
return this.http.put<User>(`${this.userUrl}/${userId}`, updateData);
}
deleteUser(userId: number): Observable<void> {
return this.http.delete<void>(`${this.userUrl}/${userId}`);
}
changeUserPassword(userId: number, newPassword: string): Observable<void> {
const payload = {
userId: userId,
newPassword: newPassword
};
return this.http.put<void>(`${this.userUrl}/change-user-password`, payload).pipe(
catchError((error) => {
const errorMessage = error?.error?.message || 'An unexpected error occurred. Please try again.';
return throwError(() => new Error(errorMessage));
})
);
}
changePassword(currentPassword: string, newPassword: string): Observable<void> {
const payload = {
currentPassword: currentPassword,
newPassword: newPassword
};
return this.http.put<void>(`${this.userUrl}/change-password`, payload).pipe(
catchError((error) => {
const errorMessage = error?.error?.message || 'An unexpected error occurred. Please try again.';
return throwError(() => new Error(errorMessage));
})
);
}
updateUserSetting(userId: number, key: string, value: any): void {
const payload = {
key,
value
};
this.http.put<void>(`${this.userUrl}/${userId}/settings`, payload, {
headers: {'Content-Type': 'application/json'},
responseType: 'text' as 'json'
}).subscribe(() => {
const currentUser = this.userStateSubject.getValue();
if (currentUser) {
const updatedSettings = {...currentUser.userSettings, [key]: value};
const updatedUser = {...currentUser, settings: updatedSettings};
this.userStateSubject.next(updatedUser);
}
});
}
private startWebSocket(): void {
const token = this.getToken();
if (token) {
const rxStompService = this.getRxStompService();
rxStompService.activate();
}
}
private getRxStompService(): RxStompService {
if (!this.rxStompService) {
this.rxStompService = this.injector.get(RxStompService);
}
return this.rxStompService;
}
getToken(): string | null {
return localStorage.getItem('accessToken');
}
}