mirror of
https://github.com/R0m1k3/LogiFlow.git
synced 2026-10-11 17:27:31 +02:00
Add a feature to notify users about pending client calls
Introduce functionality for tracking and displaying pending client calls. This includes new API endpoints for fetching and marking clients as called, along with frontend components to alert users and manage these calls. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 95e8aab4-f39f-4b48-aa57-a759ec15ba3d Replit-Commit-Checkpoint-Type: intermediate_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/95e8aab4-f39f-4b48-aa57-a759ec15ba3d/U4JJoTO
This commit is contained in:
1 parent
c0a7b06c34
commit
6c8f8a47ad
6 files changed
+377
-3
No files matched your search
@@ -2498,6 +2498,80 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
||||
}
|
||||
});
|
||||
|
||||
// Client call tracking routes
|
||||
app.get('/api/customer-orders/pending-calls', isAuthenticated, 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" });
|
||||
}
|
||||
|
||||
let groupIds: number[] | undefined;
|
||||
|
||||
// Only admin, directeur, and manager can view pending calls
|
||||
if (user.role === 'employee') {
|
||||
return res.status(403).json({ message: "Insufficient permissions" });
|
||||
}
|
||||
|
||||
if (user.role === 'admin') {
|
||||
// Admin can optionally filter by store
|
||||
const { storeId } = req.query;
|
||||
if (storeId) {
|
||||
groupIds = [parseInt(storeId as string)];
|
||||
}
|
||||
} else {
|
||||
// For directeur and manager, filter by their assigned groups
|
||||
const userGroupIds = (user as any).userGroups?.map((ug: any) => ug.groupId) || [];
|
||||
groupIds = userGroupIds;
|
||||
}
|
||||
|
||||
const pendingCalls = await storage.getPendingClientCalls(groupIds);
|
||||
|
||||
console.log(`📞 Pending client calls fetched: ${pendingCalls.length} calls for user ${user.role}`);
|
||||
res.json(pendingCalls);
|
||||
} catch (error) {
|
||||
console.error("Error fetching pending client calls:", error);
|
||||
res.status(500).json({ message: "Failed to fetch pending client calls" });
|
||||
}
|
||||
});
|
||||
|
||||
app.patch('/api/customer-orders/:id/mark-called', isAuthenticated, 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);
|
||||
|
||||
// Only admin, directeur, and manager can mark calls
|
||||
if (user.role === 'employee') {
|
||||
return res.status(403).json({ message: "Insufficient permissions" });
|
||||
}
|
||||
|
||||
// Check if user has access to this customer order
|
||||
const customerOrder = await storage.getCustomerOrder(id);
|
||||
if (!customerOrder) {
|
||||
return res.status(404).json({ message: "Customer order not found" });
|
||||
}
|
||||
|
||||
if (user.role !== 'admin') {
|
||||
const userGroupIds = (user as any).userGroups?.map((ug: any) => ug.groupId) || [];
|
||||
if (!userGroupIds.includes(customerOrder.groupId)) {
|
||||
return res.status(403).json({ message: "Access denied to this customer order" });
|
||||
}
|
||||
}
|
||||
|
||||
const updatedOrder = await storage.markClientCalled(id, user.id);
|
||||
|
||||
console.log(`📞 Client marked as called: order ${id} by user ${user.id} (${user.role})`);
|
||||
res.json(updatedOrder);
|
||||
} catch (error) {
|
||||
console.error("Error marking client as called:", error);
|
||||
res.status(500).json({ message: "Failed to mark client as called" });
|
||||
}
|
||||
});
|
||||
|
||||
// Avoir routes
|
||||
app.get('/api/avoirs', isAuthenticated, async (req: any, res) => {
|
||||
try {
|
||||
|
||||
@@ -179,6 +179,10 @@ export interface IStorage {
|
||||
createCustomerOrder(customerOrder: InsertCustomerOrder): Promise<CustomerOrder>;
|
||||
updateCustomerOrder(id: number, customerOrder: Partial<InsertCustomerOrder>): Promise<CustomerOrder>;
|
||||
deleteCustomerOrder(id: number): Promise<void>;
|
||||
|
||||
// Client call tracking
|
||||
getPendingClientCalls(groupIds?: number[]): Promise<CustomerOrderWithRelations[]>;
|
||||
markClientCalled(customerOrderId: number, calledBy: string): Promise<CustomerOrder>;
|
||||
|
||||
// DLC Product operations
|
||||
getDlcProducts(groupIds?: number[], filters?: { status?: string; supplierId?: number; search?: string; }): Promise<DlcProductWithRelations[]>;
|
||||
@@ -1676,6 +1680,50 @@ export class DatabaseStorage implements IStorage {
|
||||
await db.delete(customerOrders).where(eq(customerOrders.id, id));
|
||||
}
|
||||
|
||||
async getPendingClientCalls(groupIds?: number[]): Promise<CustomerOrderWithRelations[]> {
|
||||
const conditions = [
|
||||
eq(dlcProducts.status, 'valides'),
|
||||
eq(customerOrders.customerNotified, false)
|
||||
];
|
||||
|
||||
if (groupIds && groupIds.length > 0) {
|
||||
conditions.push(inArray(customerOrders.groupId, groupIds));
|
||||
}
|
||||
|
||||
const results = await db
|
||||
.select({
|
||||
customerOrder: customerOrders,
|
||||
supplier: suppliers,
|
||||
group: groups
|
||||
})
|
||||
.from(customerOrders)
|
||||
.innerJoin(dlcProducts, eq(customerOrders.gencode, dlcProducts.gencode))
|
||||
.leftJoin(suppliers, eq(customerOrders.supplierId, suppliers.id))
|
||||
.leftJoin(groups, eq(customerOrders.groupId, groups.id))
|
||||
.where(and(...conditions))
|
||||
.orderBy(desc(customerOrders.createdAt));
|
||||
|
||||
return results.map(result => ({
|
||||
...result.customerOrder,
|
||||
supplier: result.supplier!,
|
||||
group: result.group!,
|
||||
creator: null // Will be populated if needed
|
||||
}));
|
||||
}
|
||||
|
||||
async markClientCalled(customerOrderId: number, calledBy: string): Promise<CustomerOrder> {
|
||||
const [updated] = await db
|
||||
.update(customerOrders)
|
||||
.set({
|
||||
customerNotified: true,
|
||||
updatedAt: new Date()
|
||||
})
|
||||
.where(eq(customerOrders.id, customerOrderId))
|
||||
.returning();
|
||||
|
||||
return updated;
|
||||
}
|
||||
|
||||
async getDlcProducts(groupIds?: number[], filters?: { status?: string; supplierId?: number; search?: string; }): Promise<DlcProductWithRelations[]> {
|
||||
let query = db
|
||||
.select({
|
||||
@@ -3421,6 +3469,49 @@ export class MemStorage implements IStorage {
|
||||
this.customerOrders.delete(id);
|
||||
}
|
||||
|
||||
async getPendingClientCalls(groupIds?: number[]): Promise<CustomerOrderWithRelations[]> {
|
||||
let orders = Array.from(this.customerOrders.values());
|
||||
|
||||
// Filter by groupIds if provided
|
||||
if (groupIds && groupIds.length > 0) {
|
||||
orders = orders.filter(order => groupIds.includes(order.groupId));
|
||||
}
|
||||
|
||||
// Find orders where client not notified and product is available
|
||||
const pendingOrders = orders.filter(order => {
|
||||
if (order.customerNotified) return false;
|
||||
|
||||
// Check if there's a DLC product with status 'valides' matching this gencode
|
||||
const matchingDlcProduct = Array.from(this.dlcProducts.values())
|
||||
.find(dlc => dlc.gencode === order.gencode && dlc.status === 'valides');
|
||||
|
||||
return !!matchingDlcProduct;
|
||||
});
|
||||
|
||||
return pendingOrders.map(order => ({
|
||||
...order,
|
||||
supplier: this.suppliers.get(order.supplierId)!,
|
||||
group: this.groups.get(order.groupId)!,
|
||||
creator: this.users.get(order.createdBy)!
|
||||
}));
|
||||
}
|
||||
|
||||
async markClientCalled(customerOrderId: number, calledBy: string): Promise<CustomerOrder> {
|
||||
const order = this.customerOrders.get(customerOrderId);
|
||||
if (!order) {
|
||||
throw new Error(`CustomerOrder with id ${customerOrderId} not found`);
|
||||
}
|
||||
|
||||
const updatedOrder = {
|
||||
...order,
|
||||
customerNotified: true,
|
||||
updatedAt: new Date()
|
||||
};
|
||||
|
||||
this.customerOrders.set(customerOrderId, updatedOrder);
|
||||
return updatedOrder;
|
||||
}
|
||||
|
||||
async getDlcProducts(groupIds?: number[], filters?: { status?: string; supplierId?: number; search?: string; }): Promise<DlcProductWithRelations[]> {
|
||||
let products = Array.from(this.dlcProducts.values());
|
||||
|
||||
|
||||
Reference in new issue
Block a user