From 480d9fb2434c146574018ed0344c2efcb70239b8 Mon Sep 17 00:00:00 2001 From: odentas Date: Wed, 5 Nov 2025 18:04:45 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20Corriger=20le=20tri=20des=20notes=20pour?= =?UTF-8?q?=20consid=C3=A9rer=20source=5Fcreated=5Fat=20et=20created=5Fat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/contrats/[id]/notes/route.ts | 63 ++++++++++------------------ 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/app/api/contrats/[id]/notes/route.ts b/app/api/contrats/[id]/notes/route.ts index 5f6cd06..b70f488 100644 --- a/app/api/contrats/[id]/notes/route.ts +++ b/app/api/contrats/[id]/notes/route.ts @@ -9,52 +9,35 @@ export async function GET(req: Request, ctx: { params: { id: string } }) { // Try to get organization id from header if present const activeOrg = req.headers.get("x-active-org-id") || undefined; - // Query Supabase notes table + // Query Supabase notes table (no ordering here, we'll sort in JS) let q = sb.from("notes").select("*"); q = q.eq("contract_id", id); if (activeOrg) q = q.eq("organization_id", activeOrg); - // Order by source_created_at (newest first). If absent, fall back to created_at desc. - q = q.order("source_created_at", { ascending: false, nullsFirst: false }); - q = q.order("created_at", { ascending: false }); - - // Execute query and handle case where 'idx' column doesn't exist in the table - let data: any = null; - try { - const res = await q; - data = res.data; - if (res.error) { - // If error mentions missing 'source_created_at' column, retry ordering by created_at - const msg = res.error.message ? String(res.error.message) : ""; - if (msg.includes("source_created_at") && msg.includes("does not exist") || msg.includes("column \"source_created_at\"")) { - const q2 = sb.from("notes").select("*").eq("contract_id", id); - if (activeOrg) q2.eq("organization_id", activeOrg); - const res2 = await q2.order("created_at", { ascending: false }); - if (res2.error) { - return NextResponse.json({ error: "supabase_error", message: res2.error.message }, { status: 502 }); - } - data = res2.data; - } else { - return NextResponse.json({ error: "supabase_error", message: res.error.message }, { status: 502 }); - } - } - } catch (err: any) { - // Some clients throw; handle missing column fallback as well - const msg = err?.message ? String(err.message) : String(err); - if ((msg.includes("source_created_at") && msg.includes("does not exist")) || msg.includes("column \"source_created_at\" does not exist") || msg.includes("column \"source_created_at\"")) { - const q2 = sb.from("notes").select("*").eq("contract_id", id); - if (activeOrg) q2.eq("organization_id", activeOrg); - const res2 = await q2.order("created_at", { ascending: false }); - if (res2.error) { - return NextResponse.json({ error: "supabase_error", message: res2.error.message }, { status: 502 }); - } - data = res2.data; - } else { - return NextResponse.json({ error: "supabase_error", message: msg }, { status: 502 }); - } + // Execute query + const { data, error } = await q; + + if (error) { + return NextResponse.json({ error: "supabase_error", message: error.message }, { status: 502 }); } - return NextResponse.json({ items: Array.isArray(data) ? data : [] }, { status: 200 }); + // Sort notes by the most relevant date: + // - For Airtable imports: use source_created_at + // - For new notes: use created_at + // Sort in descending order (newest first) + const sortedData = Array.isArray(data) ? data.sort((a, b) => { + const dateA = a.source_created_at || a.created_at; + const dateB = b.source_created_at || b.created_at; + + if (!dateA && !dateB) return 0; + if (!dateA) return 1; // dateA null goes after + if (!dateB) return -1; // dateB null goes after + + // Descending order: most recent first + return new Date(dateB).getTime() - new Date(dateA).getTime(); + }) : []; + + return NextResponse.json({ items: sortedData }, { status: 200 }); } catch (e: any) { return NextResponse.json({ error: "internal_error", message: e?.message || String(e) }, { status: 500 }); }