Add super admin user management
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m46s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m46s
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { CampaignPreview, CampaignProcessSummary, CampaignQueueSummary, DashboardAnalytics, DateRange, OrderData, RfmAnalytics, StockData } from './types';
|
||||
import type { AuthUser, CampaignPreview, CampaignProcessSummary, CampaignQueueSummary, CreateUserResult, DashboardAnalytics, DateRange, ManagedUser, OrderData, RfmAnalytics, StockData } from './types';
|
||||
import { formatDateParam } from './dateRanges';
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL || '/api';
|
||||
@@ -14,8 +14,10 @@ export const login = async (email: string, password: string): Promise<boolean> =
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
const data = await response.json() as { token?: string; user?: AuthUser };
|
||||
if (!data.token || !data.user) return false;
|
||||
localStorage.setItem('auth_token', data.token);
|
||||
localStorage.setItem('auth_user', JSON.stringify(data.user));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -27,6 +29,7 @@ export const login = async (email: string, password: string): Promise<boolean> =
|
||||
|
||||
export const logout = () => {
|
||||
localStorage.removeItem('auth_token');
|
||||
localStorage.removeItem('auth_user');
|
||||
window.location.href = '/#/login';
|
||||
};
|
||||
|
||||
@@ -34,6 +37,22 @@ export const isAuthenticated = (): boolean => {
|
||||
return !!localStorage.getItem('auth_token');
|
||||
};
|
||||
|
||||
export const getCurrentUser = (): AuthUser | null => {
|
||||
const rawUser = localStorage.getItem('auth_user');
|
||||
if (!rawUser) return null;
|
||||
|
||||
try {
|
||||
return JSON.parse(rawUser) as AuthUser;
|
||||
} catch {
|
||||
localStorage.removeItem('auth_user');
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export const isSuperAdmin = (): boolean => {
|
||||
return getCurrentUser()?.role === 'super_admin';
|
||||
};
|
||||
|
||||
export const fetchStock = async (): Promise<StockData[]> => {
|
||||
try {
|
||||
const token = localStorage.getItem('auth_token');
|
||||
@@ -171,6 +190,59 @@ export const retryCampaignGroup = async (baseProductName: string): Promise<boole
|
||||
}
|
||||
};
|
||||
|
||||
export const fetchUsers = async (): Promise<ManagedUser[]> => {
|
||||
try {
|
||||
const response = await authFetch('/users');
|
||||
if (!response.ok) return [];
|
||||
const data = await response.json() as { users?: ManagedUser[] };
|
||||
return data.users || [];
|
||||
} catch (error) {
|
||||
console.error('Fetch users failed', error);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
export const createUser = async (payload: { name: string; email: string; password?: string }): Promise<CreateUserResult> => {
|
||||
const response = await authFetch('/users', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
const data = await response.json().catch(() => null);
|
||||
if (!response.ok) {
|
||||
throw new Error(data?.error || 'Não foi possível criar o usuário.');
|
||||
}
|
||||
|
||||
return data as CreateUserResult;
|
||||
};
|
||||
|
||||
export const updateUser = async (id: number, payload: { name: string; email: string; isActive: boolean; password?: string }): Promise<ManagedUser> => {
|
||||
const response = await authFetch(`/users/${id}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
const data = await response.json().catch(() => null);
|
||||
if (!response.ok) {
|
||||
throw new Error(data?.error || 'Não foi possível editar o usuário.');
|
||||
}
|
||||
|
||||
return data.user as ManagedUser;
|
||||
};
|
||||
|
||||
export const deleteUser = async (id: number): Promise<void> => {
|
||||
const response = await authFetch(`/users/${id}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const data = await response.json().catch(() => null);
|
||||
throw new Error(data?.error || 'Não foi possível excluir o usuário.');
|
||||
}
|
||||
};
|
||||
|
||||
export const parseOrderDate = (dateStr: string): Date => {
|
||||
if (!dateStr) return new Date(0);
|
||||
if (dateStr.includes('T')) return new Date(dateStr);
|
||||
|
||||
Reference in New Issue
Block a user