@@ -2,7 +2,8 @@ import { eq, takeFirst } from "@ctrlplane/db";
2
2
import { db } from "@ctrlplane/db/client" ;
3
3
import * as schema from "@ctrlplane/db/schema" ;
4
4
5
- import { sendNodeEvent } from "../client.js" ;
5
+ import type { FullResource , PbResource } from "../events.js" ;
6
+ import { sendGoEvent , sendNodeEvent } from "../client.js" ;
6
7
import { Event } from "../events.js" ;
7
8
8
9
const getFullResource = async ( resource : schema . Resource ) => {
@@ -16,19 +17,46 @@ const getFullResource = async (resource: schema.Resource) => {
16
17
return { ...resource , metadata } ;
17
18
} ;
18
19
20
+ const getPbResource = ( resource : FullResource ) : PbResource => ( {
21
+ id : resource . id ,
22
+ name : resource . name ,
23
+ version : resource . version ,
24
+ kind : resource . kind ,
25
+ identifier : resource . identifier ,
26
+ workspaceId : resource . workspaceId ,
27
+ createdAt : resource . createdAt . toISOString ( ) ,
28
+ providerId : resource . providerId ?? undefined ,
29
+ lockedAt : resource . lockedAt ?. toISOString ( ) ?? undefined ,
30
+ updatedAt : resource . updatedAt ?. toISOString ( ) ?? undefined ,
31
+ deletedAt : resource . deletedAt ?. toISOString ( ) ?? undefined ,
32
+ metadata : resource . metadata ,
33
+ } ) ;
34
+
35
+ const convertFullResourceToNodeEvent = ( fullResource : FullResource ) => ( {
36
+ workspaceId : fullResource . workspaceId ,
37
+ eventType : Event . ResourceCreated ,
38
+ eventId : fullResource . id ,
39
+ timestamp : Date . now ( ) ,
40
+ source : "api" as const ,
41
+ payload : fullResource ,
42
+ } ) ;
43
+
44
+ const convertFullResourceToGoEvent = ( fullResource : FullResource ) => ( {
45
+ workspaceId : fullResource . workspaceId ,
46
+ eventType : Event . ResourceCreated as const ,
47
+ payload : getPbResource ( fullResource ) ,
48
+ timestamp : Date . now ( ) ,
49
+ } ) ;
50
+
19
51
export const dispatchResourceCreated = (
20
52
resource : schema . Resource ,
21
- source ?: "api" | "scheduler" | "user-action" ,
53
+ _ ?: "api" | "scheduler" | "user-action" ,
22
54
) =>
23
55
getFullResource ( resource ) . then ( ( fullResource ) =>
24
- sendNodeEvent ( {
25
- workspaceId : resource . workspaceId ,
26
- eventType : Event . ResourceCreated ,
27
- eventId : resource . id ,
28
- timestamp : Date . now ( ) ,
29
- source : source ?? "api" ,
30
- payload : fullResource ,
31
- } ) ,
56
+ Promise . all ( [
57
+ sendNodeEvent ( convertFullResourceToNodeEvent ( fullResource ) ) ,
58
+ sendGoEvent ( convertFullResourceToGoEvent ( fullResource ) ) ,
59
+ ] ) ,
32
60
) ;
33
61
34
62
export const dispatchResourceUpdated = async (
@@ -41,29 +69,28 @@ export const dispatchResourceUpdated = async (
41
69
getFullResource ( current ) ,
42
70
] ) ;
43
71
44
- sendNodeEvent ( {
45
- workspaceId : current . workspaceId ,
46
- eventType : Event . ResourceUpdated ,
47
- eventId : current . id ,
48
- timestamp : Date . now ( ) ,
49
- source : source ?? "api" ,
50
- payload : { previous : previousFullResource , current : currentFullResource } ,
51
- } ) ;
72
+ await Promise . all ( [
73
+ sendNodeEvent ( {
74
+ workspaceId : current . workspaceId ,
75
+ eventType : Event . ResourceUpdated ,
76
+ eventId : current . id ,
77
+ timestamp : Date . now ( ) ,
78
+ source : source ?? "api" ,
79
+ payload : { previous : previousFullResource , current : currentFullResource } ,
80
+ } ) ,
81
+ sendGoEvent ( convertFullResourceToGoEvent ( currentFullResource ) ) ,
82
+ ] ) ;
52
83
} ;
53
84
54
85
export const dispatchResourceDeleted = (
55
86
resource : schema . Resource ,
56
- source ?: "api" | "scheduler" | "user-action" ,
87
+ _ ?: "api" | "scheduler" | "user-action" ,
57
88
) =>
58
89
getFullResource ( resource ) . then ( ( fullResource ) =>
59
- sendNodeEvent ( {
60
- workspaceId : resource . workspaceId ,
61
- eventType : Event . ResourceDeleted ,
62
- eventId : resource . id ,
63
- timestamp : Date . now ( ) ,
64
- source : source ?? "api" ,
65
- payload : fullResource ,
66
- } ) ,
90
+ Promise . all ( [
91
+ sendNodeEvent ( convertFullResourceToNodeEvent ( fullResource ) ) ,
92
+ sendGoEvent ( convertFullResourceToGoEvent ( fullResource ) ) ,
93
+ ] ) ,
67
94
) ;
68
95
69
96
export const getWorkspaceIdForResource = async ( resourceId : string ) =>
0 commit comments