|
| 1 | +import type * as schema from "@ctrlplane/db/schema"; |
| 2 | +import type { Span } from "@ctrlplane/logger"; |
| 3 | +import type { WorkspaceEngine } from "@ctrlplane/workspace-engine-sdk"; |
| 4 | + |
| 5 | +import type { GoEventPayload, GoMessage } from "../events.js"; |
| 6 | +import { createSpanWrapper } from "../../span.js"; |
| 7 | +import { sendGoEvent } from "../client.js"; |
| 8 | +import { Event } from "../events.js"; |
| 9 | + |
| 10 | +const getOapiSystem = ( |
| 11 | + system: schema.System, |
| 12 | +): WorkspaceEngine["schemas"]["System"] => ({ |
| 13 | + id: system.id, |
| 14 | + workspaceId: system.workspaceId, |
| 15 | + name: system.name, |
| 16 | + description: system.description, |
| 17 | +}); |
| 18 | + |
| 19 | +const convertSystemToGoEvent = ( |
| 20 | + system: schema.System, |
| 21 | + eventType: keyof GoEventPayload, |
| 22 | +): GoMessage<keyof GoEventPayload> => ({ |
| 23 | + workspaceId: system.workspaceId, |
| 24 | + eventType, |
| 25 | + data: getOapiSystem(system), |
| 26 | + timestamp: Date.now(), |
| 27 | +}); |
| 28 | + |
| 29 | +export const dispatchSystemCreated = createSpanWrapper( |
| 30 | + "dispatchSystemCreated", |
| 31 | + async (span: Span, system: schema.System) => { |
| 32 | + span.setAttribute("system.id", system.id); |
| 33 | + span.setAttribute("system.workspaceId", system.workspaceId); |
| 34 | + span.setAttribute("system.name", system.name); |
| 35 | + span.setAttribute("system.description", system.description); |
| 36 | + |
| 37 | + await sendGoEvent(convertSystemToGoEvent(system, Event.SystemCreated)); |
| 38 | + }, |
| 39 | +); |
| 40 | + |
| 41 | +export const dispatchSystemUpdated = createSpanWrapper( |
| 42 | + "dispatchSystemUpdated", |
| 43 | + async (span: Span, system: schema.System) => { |
| 44 | + span.setAttribute("system.id", system.id); |
| 45 | + span.setAttribute("system.workspaceId", system.workspaceId); |
| 46 | + span.setAttribute("system.name", system.name); |
| 47 | + span.setAttribute("system.description", system.description); |
| 48 | + |
| 49 | + await sendGoEvent(convertSystemToGoEvent(system, Event.SystemUpdated)); |
| 50 | + }, |
| 51 | +); |
| 52 | + |
| 53 | +export const dispatchSystemDeleted = createSpanWrapper( |
| 54 | + "dispatchSystemDeleted", |
| 55 | + async (span: Span, system: schema.System) => { |
| 56 | + span.setAttribute("system.id", system.id); |
| 57 | + span.setAttribute("system.workspaceId", system.workspaceId); |
| 58 | + span.setAttribute("system.name", system.name); |
| 59 | + span.setAttribute("system.description", system.description); |
| 60 | + |
| 61 | + await sendGoEvent(convertSystemToGoEvent(system, Event.SystemDeleted)); |
| 62 | + }, |
| 63 | +); |
0 commit comments