fix: Corriger le tri des notes pour considérer source_created_at et created_at

This commit is contained in:
odentas 2025-11-05 18:04:45 +01:00
parent e8db8960fb
commit 480d9fb243

View file

@ -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 });
}