Files
pocket-id/frontend/src/lib/services/user-service.ts

121 lines
3.9 KiB
TypeScript
Raw Normal View History

import userStore from '$lib/stores/user-store';
import type { ListRequestOptions, Paginated } from '$lib/types/list-request.type';
import type { SignupTokenDto } from '$lib/types/signup-token.type';
import type { UserGroup } from '$lib/types/user-group.type';
import type { User, UserCreate, UserSignUp } from '$lib/types/user.type';
import { cachedProfilePicture } from '$lib/utils/cached-image-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 {
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>;
};
2024-08-12 11:00:25 +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;
};
2024-08-12 11:00:25 +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;
};
2024-08-12 11:00:25 +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;
};
2024-08-12 11:00:25 +02:00
getUserGroups = async (userId: string) => {
const res = await this.api.get(`/users/${userId}/groups`);
return res.data as UserGroup[];
};
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;
};
2024-08-12 11:00:25 +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;
};
2024-08-12 11:00:25 +02:00
remove = async (id: string) => {
2024-08-12 11:00:25 +02:00
await this.api.delete(`/users/${id}`);
};
2024-08-12 11:00:25 +02:00
updateProfilePicture = async (userId: string, image: File) => {
const formData = new FormData();
formData.append('file', image!);
await this.api.put(`/users/${userId}/profile-picture`, formData);
cachedProfilePicture.bustCache(userId);
};
updateCurrentUsersProfilePicture = async (image: File) => {
const formData = new FormData();
formData.append('file', image!);
await this.api.put('/users/me/profile-picture', formData);
cachedProfilePicture.bustCache(get(userStore)!.id);
};
resetCurrentUserProfilePicture = async () => {
await this.api.delete(`/users/me/profile-picture`);
cachedProfilePicture.bustCache(get(userStore)!.id);
};
resetProfilePicture = async (userId: string) => {
await this.api.delete(`/users/${userId}/profile-picture`);
cachedProfilePicture.bustCache(userId);
};
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;
};
2024-08-12 11:00:25 +02:00
createSignupToken = async (ttl: string | number, usageLimit: number) => {
const res = await this.api.post(`/signup-tokens`, { ttl, usageLimit });
return res.data.token;
};
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-01-19 15:30:31 +01: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 });
};
requestOneTimeAccessEmailAsAdmin = async (userId: string, ttl: string | number) => {
await this.api.post(`/users/${userId}/one-time-access-email`, { ttl });
};
updateUserGroups = async (id: string, userGroupIds: string[]) => {
const res = await this.api.put(`/users/${id}/user-groups`, { userGroupIds });
return res.data as User;
};
signup = async (data: UserSignUp) => {
const res = await this.api.post(`/signup`, data);
return res.data as User;
};
signupInitialUser = async (data: UserSignUp) => {
const res = await this.api.post(`/signup/setup`, data);
return res.data as User;
};
listSignupTokens = async (options?: ListRequestOptions) => {
const res = await this.api.get('/signup-tokens', { params: options });
return res.data as Paginated<SignupTokenDto>;
};
deleteSignupToken = async (tokenId: string) => {
await this.api.delete(`/signup-tokens/${tokenId}`);
};
2024-08-12 11:00:25 +02:00
}