feat: les admins voient toutes les factures/logs/historique BAP, colonne Utilisateur dans Factures
This commit is contained in:
18
server/db.ts
18
server/db.ts
@@ -250,6 +250,12 @@ export async function getInvoicesByUser(userId: number): Promise<Invoice[]> {
|
||||
return getInvoicesByUserId(userId);
|
||||
}
|
||||
|
||||
export async function getAllInvoices(): Promise<Invoice[]> {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(invoices).orderBy(desc(invoices.createdAt));
|
||||
}
|
||||
|
||||
export async function updateInvoice(id: number, data: Partial<Invoice>) {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
@@ -471,6 +477,12 @@ export async function getImportLogsByUser(userId: number): Promise<ImportLog[]>
|
||||
return db.select().from(importLogs).where(eq(importLogs.userId, userId)).orderBy(desc(importLogs.importedAt));
|
||||
}
|
||||
|
||||
export async function getAllImportLogs(): Promise<ImportLog[]> {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(importLogs).orderBy(desc(importLogs.importedAt));
|
||||
}
|
||||
|
||||
export async function deleteAllImportLogs(userId: number): Promise<void> {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
@@ -809,6 +821,12 @@ export async function getBapHistoryByUser(userId: number): Promise<BapHistory[]>
|
||||
.orderBy(desc(bapHistory.validatedAt));
|
||||
}
|
||||
|
||||
export async function getAllBapHistory(): Promise<BapHistory[]> {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(bapHistory).orderBy(desc(bapHistory.validatedAt));
|
||||
}
|
||||
|
||||
export async function deleteBapHistoryEntry(id: number): Promise<void> {
|
||||
const db = await getDb();
|
||||
if (!db) return;
|
||||
|
||||
@@ -34,6 +34,9 @@ import {
|
||||
toggleUserActive,
|
||||
deleteUser,
|
||||
findDuplicateInvoice,
|
||||
getAllInvoices,
|
||||
getAllImportLogs,
|
||||
getAllBapHistory,
|
||||
createImportLog,
|
||||
getImportLogsByUser,
|
||||
deleteAllImportLogs,
|
||||
@@ -374,6 +377,10 @@ export const appRouter = router({
|
||||
}),
|
||||
|
||||
list: protectedProcedure.query(async ({ ctx }) => {
|
||||
// Les admins voient toutes les factures, les utilisateurs standard voient les leurs
|
||||
if (ctx.user.role === 'admin') {
|
||||
return getAllInvoices();
|
||||
}
|
||||
return getInvoicesByUser(ctx.user.id);
|
||||
}),
|
||||
|
||||
@@ -788,6 +795,10 @@ export const appRouter = router({
|
||||
// ============= BAP HISTORY ROUTES =============
|
||||
bapHistory: router({
|
||||
getAll: protectedProcedure.query(async ({ ctx }) => {
|
||||
// Les admins voient tout l'historique BAP
|
||||
if (ctx.user.role === 'admin') {
|
||||
return getAllBapHistory();
|
||||
}
|
||||
return getBapHistoryByUser(ctx.user.id);
|
||||
}),
|
||||
delete: protectedProcedure
|
||||
@@ -1311,6 +1322,10 @@ export const appRouter = router({
|
||||
// ============= IMPORT LOGS ROUTES =============
|
||||
importLogs: router({
|
||||
getByUser: protectedProcedure.query(async ({ ctx }) => {
|
||||
// Les admins voient tous les logs d'import
|
||||
if (ctx.user.role === 'admin') {
|
||||
return getAllImportLogs();
|
||||
}
|
||||
return getImportLogsByUser(ctx.user.id);
|
||||
}),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user