2025-04-09 15:51:58 +02:00
|
|
|
import userStore from '$lib/stores/user-store';
|
2025-01-11 20:14:12 +01:00
|
|
|
import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
|
2025-03-06 15:25:03 -06:00
|
|
|
import type { UserGroup } from '$lib/types/user-group.type';
|
2024-08-12 11:00:25 +02:00
|
|
|
import type { User, UserCreate } from '$lib/types/user.type';
|
2025-04-09 15:51:58 +02:00
|
|
|
import { bustProfilePictureCache } from '$lib/utils/profile-picture-util';
|
|
|
|
|
import { get } from 'svelte/store';
|
2024-08-12 11:00:25 +02:00
|
|
|
import APIService from './api-service';
|
|
|
|
|
|
|
|
|
|
export default class UserService extends APIService {
|
2025-01-11 20:14:12 +01:00
|
|
|
async list(options?: SearchPaginationSortRequest) {
|
2024-08-12 11:00:25 +02:00
|
|
|
const res = await this.api.get('/users', {
|
2025-01-11 20:14:12 +01:00
|
|
|
params: options
|
2024-08-12 11:00:25 +02:00
|
|
|
});
|
|
|
|
|
return res.data as Paginated<User>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async get(id: string) {
|
|
|
|
|
const res = await this.api.get(`/users/${id}`);
|
|
|
|
|
return res.data as User;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getCurrent() {
|
|
|
|
|
const res = await this.api.get('/users/me');
|
|
|
|
|
return res.data as User;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(user: UserCreate) {
|
|
|
|
|
const res = await this.api.post('/users', user);
|
|
|
|
|
return res.data as User;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-06 15:25:03 -06:00
|
|
|
async getUserGroups(userId: string) {
|
|
|
|
|
const res = await this.api.get(`/users/${userId}/groups`);
|
|
|
|
|
return res.data as UserGroup[];
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-12 11:00:25 +02:00
|
|
|
async update(id: string, user: UserCreate) {
|
|
|
|
|
const res = await this.api.put(`/users/${id}`, user);
|
|
|
|
|
return res.data as User;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateCurrent(user: UserCreate) {
|
|
|
|
|
const res = await this.api.put('/users/me', user);
|
|
|
|
|
return res.data as User;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async remove(id: string) {
|
|
|
|
|
await this.api.delete(`/users/${id}`);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-19 14:28:45 +01:00
|
|
|
async updateProfilePicture(userId: string, image: File) {
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('file', image!);
|
|
|
|
|
|
2025-04-09 15:51:58 +02:00
|
|
|
bustProfilePictureCache(userId);
|
2025-02-19 14:28:45 +01:00
|
|
|
await this.api.put(`/users/${userId}/profile-picture`, formData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateCurrentUsersProfilePicture(image: File) {
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('file', image!);
|
|
|
|
|
|
2025-04-09 15:51:58 +02:00
|
|
|
bustProfilePictureCache(get(userStore)!.id);
|
2025-02-19 14:28:45 +01:00
|
|
|
await this.api.put('/users/me/profile-picture', formData);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-18 14:59:31 -05:00
|
|
|
async resetCurrentUserProfilePicture() {
|
2025-04-09 15:51:58 +02:00
|
|
|
bustProfilePictureCache(get(userStore)!.id);
|
2025-03-18 14:59:31 -05:00
|
|
|
await this.api.delete(`/users/me/profile-picture`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async resetProfilePicture(userId: string) {
|
2025-04-09 15:51:58 +02:00
|
|
|
bustProfilePictureCache(userId);
|
2025-03-18 14:59:31 -05:00
|
|
|
await this.api.delete(`/users/${userId}/profile-picture`);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-10 12:45:45 +01:00
|
|
|
async createOneTimeAccessToken(expiresAt: Date, userId: string) {
|
2024-08-12 11:00:25 +02:00
|
|
|
const res = await this.api.post(`/users/${userId}/one-time-access-token`, {
|
|
|
|
|
userId,
|
2024-10-31 17:22:58 +01:00
|
|
|
expiresAt
|
2024-08-12 11:00:25 +02:00
|
|
|
});
|
|
|
|
|
return res.data.token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async exchangeOneTimeAccessToken(token: string) {
|
|
|
|
|
const res = await this.api.post(`/one-time-access-token/${token}`);
|
|
|
|
|
return res.data as User;
|
|
|
|
|
}
|
2025-01-19 15:30:31 +01:00
|
|
|
|
2025-04-20 18:32:40 +02:00
|
|
|
async requestOneTimeAccessEmailAsUnauthenticatedUser(email: string, redirectPath?: string) {
|
2025-01-19 15:30:31 +01:00
|
|
|
await this.api.post('/one-time-access-email', { email, redirectPath });
|
|
|
|
|
}
|
2025-03-06 15:25:03 -06:00
|
|
|
|
2025-04-20 18:32:40 +02:00
|
|
|
async requestOneTimeAccessEmailAsAdmin(userId: string, expiresAt: Date) {
|
|
|
|
|
await this.api.post(`/users/${userId}/one-time-access-email`, { expiresAt });
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-06 15:25:03 -06:00
|
|
|
async updateUserGroups(id: string, userGroupIds: string[]) {
|
|
|
|
|
const res = await this.api.put(`/users/${id}/user-groups`, { userGroupIds });
|
|
|
|
|
return res.data as User;
|
|
|
|
|
}
|
2024-08-12 11:00:25 +02:00
|
|
|
}
|