🏷️(app-desk) update interfaces business logic

Update interfaces of User / Team / Access
to get what is needed for the frontend.
This commit is contained in:
Anthony LC
2024-03-08 15:02:03 +01:00
committed by Anthony LC
parent 9be973a776
commit b0d3f73ba2
6 changed files with 37 additions and 23 deletions

View File

@@ -1,16 +1,6 @@
import { fetchAPI } from '@/api';
/**
* Represents user data retrieved from the API.
* This interface is incomplete, and will be
* refactored in a near future.
*
* @interface UserData
* @property {string} email - The email of the user.
*/
export interface UserData {
email: string;
}
import { User } from './types';
/**
* Asynchronously retrieves the current user's data from the API.
@@ -20,12 +10,12 @@ export interface UserData {
* @async
* @function getMe
* @throws {Error} Throws an error if the API request fails.
* @returns {Promise<UserData>} A promise that resolves to the user data.
* @returns {Promise<User>} A promise that resolves to the user data.
*/
export const getMe = async (): Promise<UserData> => {
export const getMe = async (): Promise<User> => {
const response = await fetchAPI(`users/me/`);
if (!response.ok) {
throw new Error(`Couldn't fetch user data: ${response.statusText}`);
}
return response.json() as Promise<UserData>;
return response.json() as Promise<User>;
};

View File

@@ -1 +1,2 @@
export * from './getMe';
export * from './types';

View File

@@ -0,0 +1,12 @@
/**
* Represents user retrieved from the API.
* @interface User
* @property {string} id - The id of the user.
* @property {string} email - The email of the user.
* @property {string} name - The name of the user.
*/
export interface User {
id: string;
email: string;
name?: string;
}

View File

@@ -1,2 +1,3 @@
export * from './Auth';
export * from './useAuthStore';
export * from './api/types';

View File

@@ -1,6 +1,6 @@
import { create } from 'zustand';
import { UserData, getMe } from '@/features/auth/api';
import { User, getMe } from '@/features/auth/api';
export const login = () => {
window.location.replace(
@@ -12,7 +12,7 @@ interface AuthStore {
authenticated: boolean;
initAuth: () => void;
logout: () => void;
userData?: UserData;
userData?: User;
}
const initialState = {
@@ -26,7 +26,7 @@ export const useAuthStore = create<AuthStore>((set) => ({
initAuth: () => {
getMe()
.then((data: UserData) => {
.then((data: User) => {
set({ authenticated: true, userData: data });
})
.catch(() => {

View File

@@ -1,3 +1,5 @@
import { User } from '@/features/auth/';
export enum Role {
MEMBER = 'member',
ADMIN = 'administrator',
@@ -8,6 +10,13 @@ export interface Access {
id: string;
role: Role;
user: User;
abilities: {
delete: boolean;
get: boolean;
patch: boolean;
put: boolean;
set_role_to: Role[];
};
}
export interface Team {
@@ -16,10 +25,11 @@ export interface Team {
accesses: Access[];
created_at: string;
updated_at: string;
}
export interface User {
id: string;
email: string;
name?: string;
abilities: {
delete: boolean;
get: boolean;
manage_accesses: boolean;
patch: boolean;
put: boolean;
};
}