Add functionality to manage and track customer credit notes

Introduces the "Avoirs" feature, including database schema, API endpoints for CRUD operations, and frontend components for viewing and managing credit notes.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 9fcf4b21-eb0c-4e53-a567-e2ce4a6ad869
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/9fcf4b21-eb0c-4e53-a567-e2ce4a6ad869/0C3uBiZ
This commit is contained in:
michaelschal committed 2025-09-08 13:48:14 +00:00
1 parent e75fa65945
commit f3565df01f
6 files changed
+1069 -3

No files matched your search

+252 -1
View File
@@ -41,8 +41,10 @@ import {
insertWeatherDataSchema,
insertWeatherSettingsSchema,
insertWebhookBapConfigSchema,
insertAvoirSchema,
users, groups, userGroups, suppliers, orders, deliveries, publicities, publicityParticipations,
customerOrders, nocodbConfig, dlcProducts, tasks, invoiceVerificationCache, dashboardMessages, webhookBapConfig
customerOrders, nocodbConfig, dlcProducts, tasks, invoiceVerificationCache, dashboardMessages, webhookBapConfig,
avoirs
} from "@shared/schema";
import { hasPermission } from "@shared/permissions";
import { z } from "zod";
@@ -2336,6 +2338,255 @@ export async function registerRoutes(app: Express): Promise<Server> {
}
});
// Avoir routes
app.get('/api/avoirs', requireModulePermission('avoir', 'view'), async (req: any, res) => {
try {
const user = await storage.getUserWithGroups(req.user.claims ? req.user.claims.sub : req.user.id);
if (!user) {
return res.status(404).json({ message: "User not found" });
}
const { storeId } = req.query;
let groupIds: number[] | undefined;
if (user.role === 'admin' || user.role === 'directeur') {
groupIds = storeId ? [parseInt(storeId as string)] : undefined;
} else {
// Managers can only see their group's avoirs
const userGroupIds = user.userGroups.map(ug => ug.groupId);
if (storeId && userGroupIds.includes(parseInt(storeId as string))) {
groupIds = [parseInt(storeId as string)];
} else if (!storeId) {
groupIds = userGroupIds;
} else {
return res.json([]);
}
}
console.log('🔍 Avoirs API called with:', { groupIds, userRole: user.role });
const avoirs = await storage.getAvoirs(groupIds);
console.log('📋 Avoirs returned:', avoirs.length, 'items');
res.json(avoirs);
} catch (error) {
console.error("Error fetching avoirs:", error);
res.status(500).json({ message: "Failed to fetch avoirs" });
}
});
app.get('/api/avoirs/:id', requireModulePermission('avoir', 'view'), async (req: any, res) => {
try {
const user = await storage.getUserWithGroups(req.user.claims ? req.user.claims.sub : req.user.id);
if (!user) {
return res.status(404).json({ message: "User not found" });
}
const id = parseInt(req.params.id);
const avoir = await storage.getAvoir(id);
if (!avoir) {
return res.status(404).json({ message: "Avoir not found" });
}
// Check if user has access to this avoir's group
if (user.role !== 'admin' && user.role !== 'directeur') {
const userGroupIds = user.userGroups.map(ug => ug.groupId);
if (!userGroupIds.includes(avoir.groupId)) {
return res.status(403).json({ message: "Access denied" });
}
}
res.json(avoir);
} catch (error) {
console.error("Error fetching avoir:", error);
res.status(500).json({ message: "Failed to fetch avoir" });
}
});
app.post('/api/avoirs', requireModulePermission('avoir', 'create'), async (req: any, res) => {
try {
const user = await storage.getUserWithGroups(req.user.claims ? req.user.claims.sub : req.user.id);
if (!user) {
return res.status(404).json({ message: "User not found" });
}
// Validate data with Zod schema
const validatedData = insertAvoirSchema.parse({
...req.body,
createdBy: user.id,
});
// Check if user has access to the specified group
if (user.role !== 'admin' && user.role !== 'directeur') {
const userGroupIds = user.userGroups.map(ug => ug.groupId);
if (!userGroupIds.includes(validatedData.groupId)) {
return res.status(403).json({ message: "Access denied to this group" });
}
}
const avoir = await storage.createAvoir(validatedData);
console.log('✅ Avoir created:', avoir.id, 'by user:', user.id);
// Send webhook after avoir creation
try {
const group = await storage.getGroup(avoir.groupId);
if (group && group.webhookUrl) {
const webhookData = {
type: "Avoir",
avoirId: avoir.id,
invoiceReference: avoir.invoiceReference,
amount: avoir.amount,
supplierName: validatedData.supplierId ? (await storage.getSupplier(validatedData.supplierId))?.name : "Unknown",
groupName: group.name,
comment: avoir.comment || "",
commercialProcessed: avoir.commercialProcessed,
createdBy: user.firstName && user.lastName ? `${user.firstName} ${user.lastName}` : user.username,
createdAt: avoir.createdAt
};
const webhookResponse = await fetch(group.webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(webhookData)
});
if (webhookResponse.ok) {
await storage.updateAvoirWebhookStatus(avoir.id, true);
console.log('✅ Avoir webhook sent successfully:', avoir.id);
} else {
console.error('❌ Failed to send avoir webhook:', webhookResponse.status);
}
}
} catch (webhookError) {
console.error('❌ Error sending avoir webhook:', webhookError);
}
res.json(avoir);
} catch (error) {
if (error instanceof z.ZodError) {
return res.status(400).json({ message: "Validation error", errors: error.errors });
}
console.error("Error creating avoir:", error);
res.status(500).json({ message: "Failed to create avoir" });
}
});
app.put('/api/avoirs/:id', requireModulePermission('avoir', 'edit'), async (req: any, res) => {
try {
const user = await storage.getUserWithGroups(req.user.claims ? req.user.claims.sub : req.user.id);
if (!user) {
return res.status(404).json({ message: "User not found" });
}
const id = parseInt(req.params.id);
const existingAvoir = await storage.getAvoir(id);
if (!existingAvoir) {
return res.status(404).json({ message: "Avoir not found" });
}
// Check permissions
if (user.role !== 'admin' && user.role !== 'directeur') {
const userGroupIds = user.userGroups.map(ug => ug.groupId);
if (!userGroupIds.includes(existingAvoir.groupId)) {
return res.status(403).json({ message: "Access denied" });
}
}
const updatedAvoir = await storage.updateAvoir(id, req.body);
console.log('✅ Avoir updated:', id, 'by user:', user.id);
res.json(updatedAvoir);
} catch (error) {
console.error("Error updating avoir:", error);
res.status(500).json({ message: "Failed to update avoir" });
}
});
app.delete('/api/avoirs/:id', requireModulePermission('avoir', 'delete'), async (req: any, res) => {
try {
const user = await storage.getUserWithGroups(req.user.claims ? req.user.claims.sub : req.user.id);
if (!user) {
return res.status(404).json({ message: "User not found" });
}
const id = parseInt(req.params.id);
const avoir = await storage.getAvoir(id);
if (!avoir) {
return res.status(404).json({ message: "Avoir not found" });
}
// Check permissions (only admin and directeur can delete)
if (user.role !== 'admin' && user.role !== 'directeur') {
return res.status(403).json({ message: "Insufficient permissions to delete avoirs" });
}
// For directeur, check group access
if (user.role === 'directeur') {
const userGroupIds = user.userGroups.map(ug => ug.groupId);
if (!userGroupIds.includes(avoir.groupId)) {
return res.status(403).json({ message: "Access denied" });
}
}
await storage.deleteAvoir(id);
console.log('✅ Avoir deleted:', id, 'by user:', user.id);
res.json({ message: "Avoir deleted successfully" });
} catch (error) {
console.error("Error deleting avoir:", error);
res.status(500).json({ message: "Failed to delete avoir" });
}
});
// Avoir status update routes
app.put('/api/avoirs/:id/webhook-status', requireModulePermission('avoir', 'edit'), async (req: any, res) => {
try {
const user = await storage.getUserWithGroups(req.user.claims ? req.user.claims.sub : req.user.id);
if (!user) {
return res.status(404).json({ message: "User not found" });
}
const id = parseInt(req.params.id);
const { webhookSent } = req.body;
// Only allow admin and directeur to update webhook status
if (user.role !== 'admin' && user.role !== 'directeur') {
return res.status(403).json({ message: "Insufficient permissions" });
}
await storage.updateAvoirWebhookStatus(id, webhookSent);
res.json({ message: "Webhook status updated successfully" });
} catch (error) {
console.error("Error updating avoir webhook status:", error);
res.status(500).json({ message: "Failed to update webhook status" });
}
});
app.put('/api/avoirs/:id/nocodb-verification', requireModulePermission('avoir', 'edit'), async (req: any, res) => {
try {
const user = await storage.getUserWithGroups(req.user.claims ? req.user.claims.sub : req.user.id);
if (!user) {
return res.status(404).json({ message: "User not found" });
}
const id = parseInt(req.params.id);
const { verified } = req.body;
// Only allow admin and directeur to update verification status
if (user.role !== 'admin' && user.role !== 'directeur') {
return res.status(403).json({ message: "Insufficient permissions" });
}
await storage.updateAvoirNocodbVerification(id, verified);
console.log('✅ Avoir NocoDB verification updated:', id, 'verified:', verified, 'by user:', user.id);
res.json({ message: "NocoDB verification status updated successfully" });
} catch (error) {
console.error("Error updating avoir NocoDB verification:", error);
res.status(500).json({ message: "Failed to update verification status" });
}
});
// Statistics routes
app.get('/api/stats/monthly', isAuthenticated, async (req: any, res) => {
try {
+227 -1
View File
@@ -69,6 +69,10 @@ import {
webhookBapConfig,
type WebhookBapConfig,
type InsertWebhookBapConfig,
avoirs,
type Avoir,
type InsertAvoir,
type AvoirWithRelations,
} from "@shared/schema";
import { db } from "./db";
import { eq, and, inArray, desc, sql, gte, lte, lt, gt, or, isNull, isNotNull, asc, ne } from "drizzle-orm";
@@ -197,6 +201,15 @@ export interface IStorage {
getAnnouncement(id: number): Promise<AnnouncementWithRelations | undefined>;
updateAnnouncement(id: number, announcement: Partial<InsertAnnouncement>): Promise<AnnouncementWithRelations>;
deleteAnnouncement(id: number): Promise<boolean>;
// Avoir operations
getAvoirs(groupIds?: number[]): Promise<AvoirWithRelations[]>;
getAvoir(id: number): Promise<AvoirWithRelations | undefined>;
createAvoir(avoir: InsertAvoir): Promise<Avoir>;
updateAvoir(id: number, avoir: Partial<InsertAvoir>): Promise<Avoir>;
deleteAvoir(id: number): Promise<void>;
updateAvoirWebhookStatus(id: number, webhookSent: boolean): Promise<void>;
updateAvoirNocodbVerification(id: number, verified: boolean): Promise<void>;
// SAV operations
getSavTickets(filters?: {
@@ -2108,6 +2121,134 @@ export class DatabaseStorage implements IStorage {
return await announcementStorage.deleteAnnouncement(id);
}
// Avoir operations
async getAvoirs(groupIds?: number[]): Promise<AvoirWithRelations[]> {
let query = db
.select({
id: avoirs.id,
supplierId: avoirs.supplierId,
groupId: avoirs.groupId,
invoiceReference: avoirs.invoiceReference,
amount: avoirs.amount,
comment: avoirs.comment,
commercialProcessed: avoirs.commercialProcessed,
status: avoirs.status,
webhookSent: avoirs.webhookSent,
nocodbVerified: avoirs.nocodbVerified,
nocodbVerifiedAt: avoirs.nocodbVerifiedAt,
processedAt: avoirs.processedAt,
createdBy: avoirs.createdBy,
createdAt: avoirs.createdAt,
updatedAt: avoirs.updatedAt,
supplier: suppliers,
group: groups,
creator: {
id: users.id,
firstName: users.firstName,
lastName: users.lastName,
username: users.username,
email: users.email
}
})
.from(avoirs)
.leftJoin(suppliers, eq(avoirs.supplierId, suppliers.id))
.leftJoin(groups, eq(avoirs.groupId, groups.id))
.leftJoin(users, eq(avoirs.createdBy, users.id));
if (groupIds && groupIds.length > 0) {
query = query.where(inArray(avoirs.groupId, groupIds));
}
const results = await query.orderBy(desc(avoirs.createdAt));
return results.map((result: any) => ({
...result,
supplier: result.supplier!,
group: result.group!,
creator: result.creator!,
}));
}
async getAvoir(id: number): Promise<AvoirWithRelations | undefined> {
const [result] = await db
.select({
id: avoirs.id,
supplierId: avoirs.supplierId,
groupId: avoirs.groupId,
invoiceReference: avoirs.invoiceReference,
amount: avoirs.amount,
comment: avoirs.comment,
commercialProcessed: avoirs.commercialProcessed,
status: avoirs.status,
webhookSent: avoirs.webhookSent,
nocodbVerified: avoirs.nocodbVerified,
nocodbVerifiedAt: avoirs.nocodbVerifiedAt,
processedAt: avoirs.processedAt,
createdBy: avoirs.createdBy,
createdAt: avoirs.createdAt,
updatedAt: avoirs.updatedAt,
supplier: suppliers,
group: groups,
creator: {
id: users.id,
firstName: users.firstName,
lastName: users.lastName,
username: users.username,
email: users.email
}
})
.from(avoirs)
.leftJoin(suppliers, eq(avoirs.supplierId, suppliers.id))
.leftJoin(groups, eq(avoirs.groupId, groups.id))
.leftJoin(users, eq(avoirs.createdBy, users.id))
.where(eq(avoirs.id, id));
if (!result) return undefined;
return {
...result,
supplier: result.supplier!,
group: result.group!,
creator: result.creator!,
};
}
async createAvoir(avoirData: InsertAvoir): Promise<Avoir> {
const [avoir] = await db.insert(avoirs).values(avoirData).returning();
return avoir;
}
async updateAvoir(id: number, avoirData: Partial<InsertAvoir>): Promise<Avoir> {
const [avoir] = await db
.update(avoirs)
.set({ ...avoirData, updatedAt: new Date() })
.where(eq(avoirs.id, id))
.returning();
return avoir;
}
async deleteAvoir(id: number): Promise<void> {
await db.delete(avoirs).where(eq(avoirs.id, id));
}
async updateAvoirWebhookStatus(id: number, webhookSent: boolean): Promise<void> {
await db
.update(avoirs)
.set({ webhookSent, updatedAt: new Date() })
.where(eq(avoirs.id, id));
}
async updateAvoirNocodbVerification(id: number, verified: boolean): Promise<void> {
await db
.update(avoirs)
.set({
nocodbVerified: verified,
nocodbVerifiedAt: verified ? new Date() : null,
updatedAt: new Date()
})
.where(eq(avoirs.id, id));
}
// SAV operations
async getSavTickets(filters?: {
groupIds?: number[];
@@ -2441,7 +2582,7 @@ export class MemStorage implements IStorage {
private dlcProducts = new Map<number, DlcProduct>();
private tasks = new Map<number, Task>();
private announcements = new Map<number, Announcement>();
private publicities = new Map<number, Publicity>();
private avoirs = new Map<number, Avoir>();
private savTickets = new Map<number, SavTicket>();
private savTicketHistories = new Map<number, SavTicketHistory[]>();
private invoiceVerificationCache = new Map<string, InvoiceVerificationCache>();
@@ -2456,6 +2597,7 @@ export class MemStorage implements IStorage {
dlcProduct: 1,
task: 1,
announcement: 1,
avoir: 1,
savTicket: 1,
savTicketHistory: 1,
};
@@ -4165,6 +4307,90 @@ export class MemStorage implements IStorage {
);
return await announcementStorage.deleteAnnouncement(id);
}
// Avoir operations
async getAvoirs(groupIds?: number[]): Promise<AvoirWithRelations[]> {
let avoirs = Array.from(this.avoirs.values());
// Filter by groups if provided
if (groupIds && groupIds.length > 0) {
avoirs = avoirs.filter(avoir => groupIds.includes(avoir.groupId));
}
return avoirs
.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())
.map(avoir => ({
...avoir,
supplier: this.suppliers.get(avoir.supplierId)!,
group: this.groups.get(avoir.groupId)!,
creator: this.users.get(avoir.createdBy)!,
}));
}
async getAvoir(id: number): Promise<AvoirWithRelations | undefined> {
const avoir = this.avoirs.get(id);
if (!avoir) return undefined;
return {
...avoir,
supplier: this.suppliers.get(avoir.supplierId)!,
group: this.groups.get(avoir.groupId)!,
creator: this.users.get(avoir.createdBy)!,
};
}
async createAvoir(avoirData: InsertAvoir): Promise<Avoir> {
const id = this.idCounters.avoir++;
const avoir: Avoir = {
id,
...avoirData,
status: 'En attente de demande',
webhookSent: false,
nocodbVerified: false,
nocodbVerifiedAt: null,
processedAt: null,
createdAt: new Date(),
updatedAt: new Date(),
};
this.avoirs.set(id, avoir);
return avoir;
}
async updateAvoir(id: number, avoirData: Partial<InsertAvoir>): Promise<Avoir> {
const existingAvoir = this.avoirs.get(id);
if (!existingAvoir) throw new Error('Avoir not found');
const updatedAvoir = {
...existingAvoir,
...avoirData,
updatedAt: new Date(),
};
this.avoirs.set(id, updatedAvoir);
return updatedAvoir;
}
async deleteAvoir(id: number): Promise<void> {
this.avoirs.delete(id);
}
async updateAvoirWebhookStatus(id: number, webhookSent: boolean): Promise<void> {
const avoir = this.avoirs.get(id);
if (avoir) {
avoir.webhookSent = webhookSent;
avoir.updatedAt = new Date();
this.avoirs.set(id, avoir);
}
}
async updateAvoirNocodbVerification(id: number, verified: boolean): Promise<void> {
const avoir = this.avoirs.get(id);
if (avoir) {
avoir.nocodbVerified = verified;
avoir.nocodbVerifiedAt = verified ? new Date() : null;
avoir.updatedAt = new Date();
this.avoirs.set(id, avoir);
}
}
}
// Use MemStorage in development, DatabaseStorage in production