From 9939f75c03765b69ff02a73fea0a6b4b42de69a9 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 26 May 2026 23:43:04 +0200 Subject: [PATCH] Phase7c: Fix Cronjob --- src/lib/db.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/lib/db.ts b/src/lib/db.ts index 3861c24..bdf51ca 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -3342,15 +3342,28 @@ export async function listAutoIssueOrgIds(): Promise { */ export async function listInvoicesPendingReminders(): Promise { await ensureSchema(); + // Bug fix: the prior version did `FROM invoices i JOIN org_billing_config c ON c.zitadel_org_id = i.zitadel_org_id`, + // but INVOICE_LIST_COLUMNS selects unqualified column names — + // `zitadel_org_id` appears in both tables and Postgres rejects + // it as ambiguous. Rewriting as a subquery filter keeps every + // referenced column on the single `invoices` row source, so the + // unqualified list stays valid (matching every other caller of + // INVOICE_LIST_COLUMNS in this file). + // + // Semantics are unchanged: only include invoices belonging to + // orgs that have opted into auto-reminders. Orgs with no + // org_billing_config row at all are excluded (same as before — + // the inner join didn't match for them either). const result = await getPool().query( `SELECT ${INVOICE_LIST_COLUMNS} - FROM invoices i - JOIN org_billing_config c - ON c.zitadel_org_id = i.zitadel_org_id - AND c.auto_reminders_enabled = TRUE - WHERE i.status IN ('open','overdue') - AND i.due_at < now() - INTERVAL '7 days' - ORDER BY i.due_at ASC` + FROM invoices + WHERE status IN ('open','overdue') + AND due_at < now() - INTERVAL '7 days' + AND zitadel_org_id IN ( + SELECT zitadel_org_id FROM org_billing_config + WHERE auto_reminders_enabled = TRUE + ) + ORDER BY due_at ASC` ); return result.rows.map(rowToInvoice); }