2025-01-11 20:14:12 +01:00
|
|
|
import type { Paginated, SearchPaginationSortRequest } from '$lib/types/pagination.type';
|
2024-08-12 11:00:25 +02:00
|
|
|
import type { User, UserCreate } from '$lib/types/user.type';
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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}`);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-31 17:22:58 +01:00
|
|
|
async createOneTimeAccessToken(userId: string, expiresAt: Date) {
|
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
|
|
|
|
|
|
|
|
async requestOneTimeAccessEmail(email: string, redirectPath?: string) {
|
|
|
|
|
await this.api.post('/one-time-access-email', { email, redirectPath });
|
|
|
|
|
}
|
2024-08-12 11:00:25 +02:00
|
|
|
}
|