- Move user settings to a separate table - Make settings tabs' elements more consistent
169 lines
4.6 KiB
TypeScript
169 lines
4.6 KiB
TypeScript
import {inject, Injectable, Injector} from '@angular/core';
|
|
import {HttpClient} from '@angular/common/http';
|
|
import {BehaviorSubject, Observable, tap, 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';
|
|
|
|
export interface SidebarLibrarySorting {
|
|
field: string;
|
|
order: string;
|
|
}
|
|
|
|
export interface SidebarShelfSorting {
|
|
field: string;
|
|
order: string;
|
|
}
|
|
|
|
export interface PerBookSetting {
|
|
pdf: string;
|
|
epub: 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;
|
|
}
|
|
|
|
export interface UserSettings {
|
|
perBookSetting: PerBookSetting;
|
|
pdfReaderSetting: PdfReaderSetting;
|
|
epubReaderSetting: EpubReaderSetting;
|
|
sidebarLibrarySorting: SidebarLibrarySorting;
|
|
sidebarShelfSorting: SidebarShelfSorting
|
|
}
|
|
|
|
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 userDataSubject = new BehaviorSubject<User | null>(null);
|
|
userData$ = this.userDataSubject.asObservable();
|
|
|
|
constructor() {
|
|
this.getMyself().subscribe(user => {
|
|
this.userDataSubject.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.userDataSubject.getValue();
|
|
if (currentUser) {
|
|
const updatedSettings = {...currentUser.userSettings, [key]: value};
|
|
const updatedUser = {...currentUser, settings: updatedSettings};
|
|
this.userDataSubject.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');
|
|
}
|
|
}
|