diff --git a/products/04-lightweight-idp/src/api/discovery.ts b/products/04-lightweight-idp/src/api/discovery.ts index 4fde9be..444633a 100644 --- a/products/04-lightweight-idp/src/api/discovery.ts +++ b/products/04-lightweight-idp/src/api/discovery.ts @@ -72,7 +72,23 @@ export function registerDiscoveryRoutes(app: FastifyInstance) { await withTenant(tenantId, async (client) => { await client.query('UPDATE staged_updates SET status = $1 WHERE id = $2', [newStatus, id]); - // TODO: If applied, merge changes into services table + // If applied, merge staged changes into services table + if (action === 'apply') { + const staged = await client.query('SELECT * FROM staged_updates WHERE id = $1', [id]); + if (staged.rows[0]) { + const changes = staged.rows[0].changes; + await client.query( + `INSERT INTO services (tenant_id, name, type, owner, owner_source, tags, last_discovered_at) + VALUES ($1, $2, $3, $4, $5, $6, now()) + ON CONFLICT (tenant_id, name) DO UPDATE SET + type = COALESCE(EXCLUDED.type, services.type), + owner = COALESCE(EXCLUDED.owner, services.owner), + tags = services.tags || EXCLUDED.tags, + last_discovered_at = now(), updated_at = now()`, + [tenantId, staged.rows[0].service_name, changes.type, changes.owner, changes.ownerSource ?? 'heuristic', JSON.stringify(changes.tags ?? {})], + ); + } + } }); return { status: newStatus };