feat: migrate from mock data to real SQL data

This commit is contained in:
Michael committed 2026-02-21 13:08:11 +01:00
1 parent 4f4a0dbd62
commit 7a1843e454
1 file changed
+41 -23
+41 -23
View File
@@ -1,41 +1,59 @@
"use server"; "use server";
import { FOURNISSEURS, generateMockRows } from "@/lib/mock-data"; import { db } from "@/db";
import { ventesProduits } from "@/db/schema";
import { getProductRows } from "./api/get-product-rows";
import type { ProductRow } from "@/types/grid"; import type { ProductRow } from "@/types/grid";
/** /**
* Get the list of all unique suppliers (Mocked for now). * Get the list of all unique suppliers from the database.
*/ */
export async function getFournisseurs() { export async function getFournisseurs() {
// Simulating a database latency try {
await new Promise((r) => setTimeout(r, 100)); const results = await db
return FOURNISSEURS; .selectDistinct({
code: ventesProduits.codeFournisseur,
nom: ventesProduits.nomFournisseur,
})
.from(ventesProduits)
.orderBy(ventesProduits.nomFournisseur);
return results.map(r => ({
code: r.code ?? "UNKNOWN",
nom: r.nom ?? "Fournisseur Inconnu"
}));
} catch (error) {
console.error("Error fetching suppliers:", error);
return [];
}
} }
/** /**
* Get the list of all magasins (Mocked). * Get the list of all magasins (stores) from the database.
*/ */
export async function getMagasins() { export async function getMagasins() {
await new Promise((r) => setTimeout(r, 50)); try {
return [ const results = await db
{ code: "M001", nom: "BORDEAUX LAC" }, .selectDistinct({
{ code: "M002", nom: "TOULOUSE BLAGNAC" }, code: ventesProduits.magasin,
{ code: "M003", nom: "NANTES BEAUJOIRE" }, nom: ventesProduits.magasin, // Using code as name as no name column exists
{ code: "M004", nom: "LILLE FLANDRES" }, })
{ code: "M005", nom: "MARSEILLE VALENTINE" }, .from(ventesProduits)
]; .orderBy(ventesProduits.magasin);
return results.map(r => ({
code: r.code,
nom: r.nom
}));
} catch (error) {
console.error("Error fetching stores:", error);
return [];
}
} }
/** /**
* Get grid data for a specific supplier and store (Mocked for now). * Get real product data for a specific supplier and store.
* In SQL:
* - If magasin === "TOTAL": SELECT SUM(quantite), AVG(marge)... GROUP BY produit_id
* - If magasin === "M001": SELECT quantite, marge... WHERE magasin_id = 'M001'
*/ */
export async function getGridData(codeFournisseur: string, magasin: string = "TOTAL"): Promise<ProductRow[]> { export async function getGridData(codeFournisseur: string, magasin: string = "TOTAL"): Promise<ProductRow[]> {
// Simulating a database latency return getProductRows({ codeFournisseur, magasin });
await new Promise((r) => setTimeout(r, 300));
// Use the existing mock generator - passing magasin to see how it affects data
return generateMockRows(codeFournisseur, 85);
} }