2025-04-09 15:51:58 +02:00
|
|
|
import userStore from '$lib/stores/user-store';
|
2025-10-13 11:12:55 +02:00
|
|
|
import type { ListRequestOptions, Paginated } from '$lib/types/list-request.type';
|
2025-06-27 15:01:10 -05:00
|
|
|
import type { SignupTokenDto } from '$lib/types/signup-token.type';
|
2025-03-06 15:25:03 -06:00
|
|
|
import type { UserGroup } from '$lib/types/user-group.type';
|
2025-06-27 15:01:10 -05:00
|
|
|
import type { User, UserCreate, UserSignUp } from '$lib/types/user.type';
|
2025-06-16 15:59:14 +02:00
|
|
|
import { cachedProfilePicture } from '$lib/utils/cached-image-util';
|
2025-04-09 15:51:58 +02:00
|
|
|
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-10-13 11:12:55 +02:00
|
|
|
list = async (options?: ListRequestOptions) => {
|
|
|
|
|
const res = await this.api.get('/users', { params: options });
|
2024-08-12 11:00:25 +02:00
|
|
|
return res.data as Paginated<User>;
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2024-08-12 11:00:25 +02:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
get = async (id: string) => {
|
2024-08-12 11:00:25 +02:00
|
|
|
const res = await this.api.get(`/users/${id}`);
|
|
|
|
|
return res.data as User;
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2024-08-12 11:00:25 +02:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
getCurrent = async () => {
|
2024-08-12 11:00:25 +02:00
|
|
|
const res = await this.api.get('/users/me');
|
|
|
|
|
return res.data as User;
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2024-08-12 11:00:25 +02:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
create = async (user: UserCreate) => {
|
2024-08-12 11:00:25 +02:00
|
|
|
const res = await this.api.post('/users', user);
|
|
|
|
|
return res.data as User;
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2024-08-12 11:00:25 +02:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
getUserGroups = async (userId: string) => {
|
2025-03-06 15:25:03 -06:00
|
|
|
const res = await this.api.get(`/users/${userId}/groups`);
|
|
|
|
|
return res.data as UserGroup[];
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2025-03-06 15:25:03 -06:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
update = async (id: string, user: UserCreate) => {
|
2024-08-12 11:00:25 +02:00
|
|
|
const res = await this.api.put(`/users/${id}`, user);
|
|
|
|
|
return res.data as User;
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2024-08-12 11:00:25 +02:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
updateCurrent = async (user: UserCreate) => {
|
2024-08-12 11:00:25 +02:00
|
|
|
const res = await this.api.put('/users/me', user);
|
|
|
|
|
return res.data as User;
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2024-08-12 11:00:25 +02:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
remove = async (id: string) => {
|
2024-08-12 11:00:25 +02:00
|
|
|
await this.api.delete(`/users/${id}`);
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2024-08-12 11:00:25 +02:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
updateProfilePicture = async (userId: string, image: File) => {
|
2025-02-19 14:28:45 +01:00
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('file', image!);
|
|
|
|
|
await this.api.put(`/users/${userId}/profile-picture`, formData);
|
2025-06-16 15:59:14 +02:00
|
|
|
cachedProfilePicture.bustCache(userId);
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2025-02-19 14:28:45 +01:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
updateCurrentUsersProfilePicture = async (image: File) => {
|
2025-02-19 14:28:45 +01:00
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('file', image!);
|
|
|
|
|
await this.api.put('/users/me/profile-picture', formData);
|
2025-06-16 15:59:14 +02:00
|
|
|
cachedProfilePicture.bustCache(get(userStore)!.id);
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2025-02-19 14:28:45 +01:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
resetCurrentUserProfilePicture = async () => {
|
2025-03-18 14:59:31 -05:00
|
|
|
await this.api.delete(`/users/me/profile-picture`);
|
2025-06-16 15:59:14 +02:00
|
|
|
cachedProfilePicture.bustCache(get(userStore)!.id);
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2025-03-18 14:59:31 -05:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
resetProfilePicture = async (userId: string) => {
|
2025-03-18 14:59:31 -05:00
|
|
|
await this.api.delete(`/users/${userId}/profile-picture`);
|
2025-06-16 15:59:14 +02:00
|
|
|
cachedProfilePicture.bustCache(userId);
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2025-03-18 14:59:31 -05:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
createOneTimeAccessToken = async (userId: string = 'me', ttl?: string | number) => {
|
|
|
|
|
const res = await this.api.post(`/users/${userId}/one-time-access-token`, { userId, ttl });
|
2024-08-12 11:00:25 +02:00
|
|
|
return res.data.token;
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2024-08-12 11:00:25 +02:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
createSignupToken = async (ttl: string | number, usageLimit: number) => {
|
|
|
|
|
const res = await this.api.post(`/signup-tokens`, { ttl, usageLimit });
|
2025-06-27 15:01:10 -05:00
|
|
|
return res.data.token;
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2025-06-27 15:01:10 -05:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
exchangeOneTimeAccessToken = async (token: string) => {
|
2024-08-12 11:00:25 +02:00
|
|
|
const res = await this.api.post(`/one-time-access-token/${token}`);
|
|
|
|
|
return res.data as User;
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2025-01-19 15:30:31 +01:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
requestOneTimeAccessEmailAsUnauthenticatedUser = async (email: string, redirectPath?: string) => {
|
2025-01-19 15:30:31 +01:00
|
|
|
await this.api.post('/one-time-access-email', { email, redirectPath });
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2025-03-06 15:25:03 -06:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
requestOneTimeAccessEmailAsAdmin = async (userId: string, ttl: string | number) => {
|
2025-08-21 23:02:56 -07:00
|
|
|
await this.api.post(`/users/${userId}/one-time-access-email`, { ttl });
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2025-04-20 18:32:40 +02:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
updateUserGroups = async (id: string, userGroupIds: string[]) => {
|
2025-03-06 15:25:03 -06:00
|
|
|
const res = await this.api.put(`/users/${id}/user-groups`, { userGroupIds });
|
|
|
|
|
return res.data as User;
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2025-06-27 15:01:10 -05:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
signup = async (data: UserSignUp) => {
|
2025-06-27 15:01:10 -05:00
|
|
|
const res = await this.api.post(`/signup`, data);
|
|
|
|
|
return res.data as User;
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2025-06-27 15:01:10 -05:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
signupInitialUser = async (data: UserSignUp) => {
|
2025-06-27 23:38:02 +02:00
|
|
|
const res = await this.api.post(`/signup/setup`, data);
|
|
|
|
|
return res.data as User;
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2025-06-27 23:38:02 +02:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
listSignupTokens = async (options?: ListRequestOptions) => {
|
|
|
|
|
const res = await this.api.get('/signup-tokens', { params: options });
|
2025-06-27 15:01:10 -05:00
|
|
|
return res.data as Paginated<SignupTokenDto>;
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2025-06-27 15:01:10 -05:00
|
|
|
|
2025-10-13 11:12:55 +02:00
|
|
|
deleteSignupToken = async (tokenId: string) => {
|
2025-06-27 15:01:10 -05:00
|
|
|
await this.api.delete(`/signup-tokens/${tokenId}`);
|
2025-10-13 11:12:55 +02:00
|
|
|
};
|
2024-08-12 11:00:25 +02:00
|
|
|
}
|