Spaces:
Runtime error
Runtime error
File size: 2,514 Bytes
6aa6081 9b72922 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | import api from './api';
import type { User, PaginatedResponse } from '../types';
export interface UserFilters {
page?: number;
per_page?: number;
role?: string;
search?: string;
is_active?: string;
}
export async function listUsers(filters: UserFilters = {}): Promise<PaginatedResponse<User>> {
const params = new URLSearchParams();
Object.entries(filters).forEach(([k, v]) => { if (v !== undefined && v !== '') params.set(k, String(v)); });
const { data } = await api.get(`/users?${params}`);
return data;
}
export async function getUser(id: number): Promise<User> {
const { data } = await api.get(`/users/${id}`);
return data;
}
export async function createUser(payload: Partial<User> & { name: string; lastname: string; role: string; password?: string }): Promise<User> {
const { data } = await api.post('/users', payload);
return data;
}
export async function updateUser(id: number, payload: Partial<User>): Promise<User> {
const { data } = await api.put(`/users/${id}`, payload);
return data;
}
export async function deactivateUser(id: number): Promise<void> {
await api.delete(`/users/${id}`);
}
export async function listUserPermissions(userId: number): Promise<string[]> {
const { data } = await api.get(`/users/${userId}/permissions`);
return data.map((p: any) => p.permission);
}
export async function setUserPermissions(userId: number, permissions: string[]): Promise<void> {
await api.put(`/users/${userId}/permissions`, { permissions });
}
export async function getAvailablePermissions(): Promise<string[]> {
const { data } = await api.get('/users/0/permissions/available');
return data;
}
export interface UserPhotoUploadResponse {
photo_url: string;
encoding_extracted: boolean;
warning: string | null;
}
export async function uploadUserPhoto(
userId: number,
file: Blob,
filename: string = 'photo.png',
): Promise<UserPhotoUploadResponse> {
const formData = new FormData();
formData.append('file', file, filename);
const { data } = await api.post(`/users/${userId}/photo`, formData, {
headers: { 'Content-Type': 'multipart/form-data' },
});
return data;
}
export function dataUrlToBlob(dataUrl: string): Blob {
const [meta, b64] = dataUrl.split(',');
const mime = meta.match(/data:(.*?);base64/)?.[1] || 'image/png';
const binary = atob(b64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
return new Blob([bytes], { type: mime });
}
|