fix(erp): enable erp pages and menu items
This commit is contained in:
63
front-end-agency/lib/api-docs.ts
Normal file
63
front-end-agency/lib/api-docs.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
export interface Document {
|
||||
id: string;
|
||||
tenant_id: string;
|
||||
parent_id: string | null;
|
||||
title: string;
|
||||
content: string; // JSON String for blocks
|
||||
status: 'draft' | 'published';
|
||||
created_by: string;
|
||||
last_updated_by: string;
|
||||
version: number;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export interface DocumentActivity {
|
||||
id: string;
|
||||
document_id: string;
|
||||
user_id: string;
|
||||
user_name: string;
|
||||
action: string;
|
||||
description: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
const BASE_URL = '/api/documents';
|
||||
|
||||
async function fetchWithAuth(url: string, options: RequestInit = {}) {
|
||||
const token = localStorage.getItem('token');
|
||||
const response = await fetch(url, {
|
||||
cache: 'no-store',
|
||||
...options,
|
||||
headers: {
|
||||
...options.headers,
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`API error: ${response.status}`);
|
||||
}
|
||||
if (response.status === 204) return null;
|
||||
|
||||
const text = await response.text();
|
||||
if (!text) return null;
|
||||
|
||||
try {
|
||||
const data = JSON.parse(text);
|
||||
return data === null ? [] : data;
|
||||
} catch (e) {
|
||||
console.error('Failed to parse API response:', text);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export const docApi = {
|
||||
getDocuments: () => fetchWithAuth(BASE_URL),
|
||||
getDocument: (id: string) => fetchWithAuth(`${BASE_URL}/${id}`),
|
||||
getSubpages: (id: string) => fetchWithAuth(`${BASE_URL}/${id}/subpages`),
|
||||
getActivities: (id: string) => fetchWithAuth(`${BASE_URL}/${id}/activities`),
|
||||
createDocument: (data: Partial<Document>) => fetchWithAuth(BASE_URL, { method: 'POST', body: JSON.stringify(data) }),
|
||||
updateDocument: (id: string, data: Partial<Document>) => fetchWithAuth(`${BASE_URL}/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
|
||||
deleteDocument: (id: string) => fetchWithAuth(`${BASE_URL}/${id}`, { method: 'DELETE' }),
|
||||
};
|
||||
154
front-end-agency/lib/api-erp.ts
Normal file
154
front-end-agency/lib/api-erp.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
export interface FinancialCategory {
|
||||
id: string;
|
||||
tenant_id: string;
|
||||
name: string;
|
||||
type: 'income' | 'expense';
|
||||
color: string;
|
||||
is_active: boolean;
|
||||
}
|
||||
|
||||
export interface BankAccount {
|
||||
id: string;
|
||||
tenant_id: string;
|
||||
name: string;
|
||||
bank_name: string;
|
||||
initial_balance: number;
|
||||
current_balance: number;
|
||||
is_active: boolean;
|
||||
}
|
||||
|
||||
export interface Entity {
|
||||
id: string;
|
||||
tenant_id: string;
|
||||
name: string;
|
||||
document: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
type: 'customer' | 'supplier' | 'both';
|
||||
status: string;
|
||||
address: string;
|
||||
city: string;
|
||||
state: string;
|
||||
zip: string;
|
||||
notes: string;
|
||||
}
|
||||
|
||||
export interface FinancialTransaction {
|
||||
id: string;
|
||||
tenant_id: string;
|
||||
account_id?: string;
|
||||
category_id?: string;
|
||||
entity_id?: string;
|
||||
crm_customer_id?: string;
|
||||
company_id?: string;
|
||||
description: string;
|
||||
amount: number;
|
||||
type: 'income' | 'expense';
|
||||
status: 'pending' | 'paid' | 'cancelled';
|
||||
due_date?: string;
|
||||
payment_date?: string;
|
||||
payment_method?: string;
|
||||
attachments: string[];
|
||||
}
|
||||
|
||||
export interface Product {
|
||||
id: string;
|
||||
tenant_id: string;
|
||||
name: string;
|
||||
sku: string;
|
||||
description: string;
|
||||
price: number;
|
||||
cost_price: number;
|
||||
type: 'product' | 'service';
|
||||
stock_quantity: number;
|
||||
is_active: boolean;
|
||||
}
|
||||
|
||||
export interface Order {
|
||||
id: string;
|
||||
tenant_id: string;
|
||||
customer_id?: string;
|
||||
entity_id?: string;
|
||||
status: 'draft' | 'confirmed' | 'completed' | 'cancelled';
|
||||
total_amount: number;
|
||||
notes: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface OrderItem {
|
||||
id: string;
|
||||
order_id: string;
|
||||
product_id: string;
|
||||
quantity: number;
|
||||
unit_price: number;
|
||||
total_price: number;
|
||||
}
|
||||
|
||||
// API Services
|
||||
const BASE_URL = '/api/erp';
|
||||
|
||||
async function fetchWithAuth(url: string, options: RequestInit = {}) {
|
||||
const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null;
|
||||
const response = await fetch(url, {
|
||||
cache: 'no-store',
|
||||
...options,
|
||||
headers: {
|
||||
...options.headers,
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`API error: ${response.status}`);
|
||||
}
|
||||
|
||||
// Handle empty responses (like 200 OK with no body or 204 No Content)
|
||||
const contentType = response.headers.get('content-type');
|
||||
if (!contentType || !contentType.includes('application/json')) {
|
||||
return { status: 'ok' };
|
||||
}
|
||||
|
||||
const text = await response.text();
|
||||
if (!text) return null;
|
||||
|
||||
try {
|
||||
const data = JSON.parse(text);
|
||||
return data === null ? [] : data;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export const erpApi = {
|
||||
// Finance
|
||||
getFinancialCategories: () => fetchWithAuth(`${BASE_URL}/finance/categories`),
|
||||
createFinancialCategory: (data: Partial<FinancialCategory>) => fetchWithAuth(`${BASE_URL}/finance/categories`, { method: 'POST', body: JSON.stringify(data) }),
|
||||
|
||||
getBankAccounts: () => fetchWithAuth(`${BASE_URL}/finance/accounts`),
|
||||
createBankAccount: (data: Partial<BankAccount>) => fetchWithAuth(`${BASE_URL}/finance/accounts`, { method: 'POST', body: JSON.stringify(data) }),
|
||||
updateBankAccount: (id: string, data: Partial<BankAccount>) => fetchWithAuth(`${BASE_URL}/finance/accounts/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
|
||||
deleteBankAccount: (id: string) => fetchWithAuth(`${BASE_URL}/finance/accounts/${id}`, { method: 'DELETE' }),
|
||||
|
||||
getTransactions: () => fetchWithAuth(`${BASE_URL}/finance/transactions`),
|
||||
createTransaction: (data: Partial<FinancialTransaction>) => fetchWithAuth(`${BASE_URL}/finance/transactions`, { method: 'POST', body: JSON.stringify(data) }),
|
||||
updateTransaction: (id: string, data: Partial<FinancialTransaction>) => fetchWithAuth(`${BASE_URL}/finance/transactions/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
|
||||
deleteTransaction: (id: string) => fetchWithAuth(`${BASE_URL}/finance/transactions/${id}`, { method: 'DELETE' }),
|
||||
|
||||
// Products
|
||||
getProducts: () => fetchWithAuth(`${BASE_URL}/products`),
|
||||
createProduct: (data: Partial<Product>) => fetchWithAuth(`${BASE_URL}/products`, { method: 'POST', body: JSON.stringify(data) }),
|
||||
updateProduct: (id: string, data: Partial<Product>) => fetchWithAuth(`${BASE_URL}/products/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
|
||||
deleteProduct: (id: string) => fetchWithAuth(`${BASE_URL}/products/${id}`, { method: 'DELETE' }),
|
||||
|
||||
// Orders
|
||||
getOrders: () => fetchWithAuth(`${BASE_URL}/orders`),
|
||||
createOrder: (data: { order: Partial<Order>, items: Partial<OrderItem>[] }) => fetchWithAuth(`${BASE_URL}/orders`, { method: 'POST', body: JSON.stringify(data) }),
|
||||
deleteOrder: (id: string) => fetchWithAuth(`${BASE_URL}/orders/${id}`, { method: 'DELETE' }),
|
||||
|
||||
// Entities
|
||||
getEntities: (type?: string) => fetchWithAuth(`${BASE_URL}/entities${type ? `?type=${type}` : ''}`),
|
||||
createEntity: (data: Partial<Entity>) => fetchWithAuth(`${BASE_URL}/entities`, { method: 'POST', body: JSON.stringify(data) }),
|
||||
updateEntity: (id: string, data: Partial<Entity>) => fetchWithAuth(`${BASE_URL}/entities/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
|
||||
deleteEntity: (id: string) => fetchWithAuth(`${BASE_URL}/entities/${id}`, { method: 'DELETE' }),
|
||||
};
|
||||
@@ -43,13 +43,24 @@ export async function apiRequest<T = any>(
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
// Handle empty responses
|
||||
const text = await response.text();
|
||||
let data: any = {};
|
||||
|
||||
if (text) {
|
||||
try {
|
||||
data = JSON.parse(text);
|
||||
} catch (e) {
|
||||
// If not JSON but has text, might be an error or plain text
|
||||
data = { message: text };
|
||||
}
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.message || `Erro ${response.status}`);
|
||||
}
|
||||
|
||||
return data;
|
||||
return data as T;
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
throw error;
|
||||
|
||||
10
front-end-agency/lib/format.ts
Normal file
10
front-end-agency/lib/format.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export const formatCurrency = (value: number | string) => {
|
||||
return new Intl.NumberFormat('pt-BR', {
|
||||
style: 'currency',
|
||||
currency: 'BRL',
|
||||
}).format(Number(value));
|
||||
};
|
||||
|
||||
export const formatDate = (date: string | Date) => {
|
||||
return new Intl.DateTimeFormat('pt-BR').format(new Date(date));
|
||||
};
|
||||
Reference in New Issue
Block a user