diff --git a/api/src/routes/compliance-orders.ts b/api/src/routes/compliance-orders.ts index 5c1c91c..bf9c9dc 100644 --- a/api/src/routes/compliance-orders.ts +++ b/api/src/routes/compliance-orders.ts @@ -1565,14 +1565,30 @@ router.put("/api/v1/compliance-orders/:id/intake", async (req, res) => { : order.intake_data) || {}; const merged = { ...oldIntake, ...intake_data }; - // Update the order with merged intake data + // Look up telecom_entity_id from FRN + let telecomEntityId: number | null = null; + const frn = entity?.frn || merged.frn; + if (frn) { + try { + const teResult = await pool.query( + `SELECT id FROM telecom_entities WHERE frn = $1 LIMIT 1`, + [frn], + ); + if (teResult.rows.length > 0) { + telecomEntityId = (teResult.rows[0] as any).id; + } + } catch {} + } + + // Update the order with merged intake data + entity link await pool.query( `UPDATE compliance_orders SET intake_data = $1, payment_status = CASE WHEN payment_status = 'pending_intake' THEN 'paid' ELSE payment_status END, - intake_data_validated = TRUE + intake_data_validated = TRUE, + telecom_entity_id = COALESCE($3, telecom_entity_id) WHERE order_number = $2`, - [JSON.stringify(merged), id], + [JSON.stringify(merged), id, telecomEntityId], ); // If entity data was provided, update the telecom_entity too @@ -1636,9 +1652,10 @@ router.put("/api/v1/compliance-orders/:id/intake", async (req, res) => { await pool.query( `UPDATE compliance_orders SET intake_data = jsonb_set(COALESCE(intake_data::jsonb, '{}'::jsonb), '{entity_filled}', 'true'::jsonb), - payment_status = 'paid' + payment_status = 'paid', + telecom_entity_id = COALESCE($2, telecom_entity_id) WHERE order_number = $1 AND payment_status = 'pending_intake'`, - [po.order_number], + [po.order_number, telecomEntityId], ); // Dispatch worker setImmediate(async () => {