feat: enhance diag endpoint to probe mouvements API with multiple date param variants

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
MichaelandClaude Sonnet 4.6 committed 2026-03-18 14:50:32 +01:00
1 parent c056750ea7
commit 9e500b6650
1 file changed
+71 -52
+71 -52
View File
@@ -4,62 +4,81 @@ import { sql } from "drizzle-orm";
const FF_API_BASE = process.env.FF_API_BASE_URL ?? "https://api.ffnancy.fr";
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const target = searchParams.get("target") ?? "all";
const result: Record<string, unknown> = {
timestamp: new Date().toISOString(),
env: {
FF_API_BASE_URL: process.env.FF_API_BASE_URL ?? "(not set — using default)",
FF_API_BASE_resolved: FF_API_BASE,
NODE_ENV: process.env.NODE_ENV,
},
};
// ── Test FF Nancy API ────────────────────────────────────────────────────
if (target === "all" || target === "ff") {
const ffTests: Record<string, unknown> = {};
// Test fournisseurs
try {
const url = `${FF_API_BASE}/api/fournisseurs?limit=5`;
const res = await fetch(url, { cache: "no-store" });
const text = await res.text();
let parsed: unknown = null;
try { parsed = JSON.parse(text); } catch { parsed = text.slice(0, 500); }
ffTests.fournisseurs = { status: res.status, url, raw: parsed };
} catch (e) {
ffTests.fournisseurs = { error: String(e) };
}
// Test articles (avec un code fournisseur de test)
const codefou = searchParams.get("codefou");
if (codefou) {
try {
const url = `${FF_API_BASE}/api/articles?codefou=${codefou}&limit=3`;
const res = await fetch(url, { cache: "no-store" });
const text = await res.text();
let parsed: unknown = null;
try { parsed = JSON.parse(text); } catch { parsed = text.slice(0, 500); }
ffTests.articles = { status: res.status, url, raw: parsed };
} catch (e) {
ffTests.articles = { error: String(e) };
async function probe(url: string) {
try {
const res = await fetch(url, { cache: "no-store" });
const text = await res.text();
let parsed: unknown = null;
try { parsed = JSON.parse(text); } catch { parsed = text.slice(0, 800); }
// Résumé: nb items si tableau, ou clés si objet
let summary: unknown = parsed;
if (Array.isArray(parsed)) {
summary = { count: parsed.length, firstKeys: parsed[0] ? Object.keys(parsed[0]) : [], sample: parsed.slice(0, 2) };
} else if (parsed && typeof parsed === "object") {
const keys = Object.keys(parsed as object);
const firstArrayKey = keys.find(k => Array.isArray((parsed as Record<string, unknown>)[k]));
if (firstArrayKey) {
const arr = (parsed as Record<string, unknown[]>)[firstArrayKey];
summary = { wrapper: firstArrayKey, count: arr.length, firstKeys: arr[0] ? Object.keys(arr[0] as object) : [], sample: arr.slice(0, 2) };
}
}
result.ffNancyApi = ffTests;
return { status: res.status, url, summary };
} catch (e) {
return { error: String(e), url };
}
}
// ── Test PostgreSQL ──────────────────────────────────────────────────────
if (target === "all" || target === "db") {
try {
const countResult = await db.execute(sql`SELECT COUNT(*) as count FROM session_snapshots`);
result.postgresql = { ok: true, snapshots: Number(countResult.rows[0]?.count ?? 0) };
} catch (e) {
result.postgresql = { ok: false, error: String(e) };
}
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const codefou = searchParams.get("codefou") ?? "AUXENCE";
// Date range: 12 derniers mois
const now = new Date();
const dateFin = new Date(now.getFullYear(), now.getMonth(), 0).toISOString().slice(0, 10);
const dateDebut = new Date(now.getFullYear(), now.getMonth() - 12, 1).toISOString().slice(0, 10);
const [
fournisseurs,
articles,
// Tester plusieurs variantes de noms de paramètres pour les mouvements
mvt_datedeb,
mvt_dateDebut,
mvt_from,
mvt_nodate,
] = await Promise.all([
probe(`${FF_API_BASE}/api/fournisseurs?limit=3`),
probe(`${FF_API_BASE}/api/articles?codefou=${codefou}&limit=3`),
probe(`${FF_API_BASE}/api/mouvements/articles?codefou=${codefou}&datedeb=${dateDebut}&datefin=${dateFin}&limit=3`),
probe(`${FF_API_BASE}/api/mouvements/articles?codefou=${codefou}&dateDebut=${dateDebut}&dateFin=${dateFin}&limit=3`),
probe(`${FF_API_BASE}/api/mouvements/articles?codefou=${codefou}&from=${dateDebut}&to=${dateFin}&limit=3`),
probe(`${FF_API_BASE}/api/mouvements/articles?codefou=${codefou}&limit=3`),
]);
// Test PostgreSQL
let postgresql: unknown;
try {
const r = await db.execute(sql`SELECT COUNT(*) as count FROM session_snapshots`);
postgresql = { ok: true, snapshots: Number(r.rows[0]?.count ?? 0) };
} catch (e) {
postgresql = { ok: false, error: String(e) };
}
return NextResponse.json(result, { status: 200 });
return NextResponse.json({
timestamp: new Date().toISOString(),
env: {
FF_API_BASE_URL: process.env.FF_API_BASE_URL ?? "(not set — default used)",
FF_API_BASE_resolved: FF_API_BASE,
},
dateRange: { dateDebut, dateFin },
codefouTested: codefou,
fournisseurs,
articles,
mouvements: {
"datedeb/datefin": mvt_datedeb,
"dateDebut/dateFin": mvt_dateDebut,
"from/to": mvt_from,
"no_date": mvt_nodate,
},
postgresql,
});
}