Implement staged update merge into services table (P4 discovery)

This commit is contained in:
2026-03-01 04:13:39 +00:00
parent 12ca955de5
commit b10e88e14d

View File

@@ -72,7 +72,23 @@ export function registerDiscoveryRoutes(app: FastifyInstance) {
await withTenant(tenantId, async (client) => { await withTenant(tenantId, async (client) => {
await client.query('UPDATE staged_updates SET status = $1 WHERE id = $2', [newStatus, id]); 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 }; return { status: newStatus };