@@ -40,6 +40,27 @@ interface User {
4040 picture ?: string ;
4141}
4242
43+ /**
44+ * Represents an organization event from Auth0
45+ */
46+ interface Organization {
47+ id : string ;
48+ name : string ;
49+ display_name ?: string ;
50+ branding ?: {
51+ logo_url ?: string ;
52+ colors ?: {
53+ primary ?: string ;
54+ page_background ?: string ;
55+ } ;
56+ } ;
57+ metadata ?: {
58+ [ key : string ] : any ;
59+ } ;
60+ created_at ?: string ;
61+ updated_at ?: string ;
62+ }
63+
4364/*
4465eventsApp.use('/!*', async (c, next) => {
4566 const auth = bearerAuth({
@@ -71,6 +92,13 @@ eventsApp.post('/', async (c) => {
7192 case 'user.deleted' :
7293 await handleUserDeleted ( user , c ) ;
7394 break ;
95+ case 'organization.created' :
96+ case 'organization.updated' :
97+ await handleOrganizationUpsert ( user , time , c , type === 'organization.created' ) ;
98+ break ;
99+ case 'organization.deleted' :
100+ await handleOrganizationDeleted ( user , c ) ;
101+ break ;
74102 default :
75103 console . log ( `Event type '${ type } ' not implemented yet.` ) ;
76104 }
@@ -135,7 +163,7 @@ async function handleUserUpsert(user: User, time: string, c: Context, isNewUser:
135163
136164 try {
137165 await c . env . DB . prepare (
138- `REPLACE INTO users(
166+ `INSERT INTO users(
139167 auth0_user_id,
140168 auth0_org_id,
141169 email,
@@ -152,7 +180,23 @@ async function handleUserUpsert(user: User, time: string, c: Context, isNewUser:
152180 app_metadata,
153181 identities,
154182 last_event_processed
155- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` )
183+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
184+ ON CONFLICT(auth0_user_id) DO UPDATE SET
185+ auth0_org_id = excluded.auth0_org_id,
186+ email = excluded.email,
187+ email_verified = excluded.email_verified,
188+ name = excluded.name,
189+ picture = excluded.picture,
190+ blocked = excluded.blocked,
191+ family_name = excluded.family_name,
192+ given_name = excluded.given_name,
193+ nickname = excluded.nickname,
194+ phone_number = excluded.phone_number,
195+ phone_verified = excluded.phone_verified,
196+ user_metadata = excluded.user_metadata,
197+ app_metadata = excluded.app_metadata,
198+ identities = excluded.identities,
199+ last_event_processed = excluded.last_event_processed` )
156200 . bind (
157201 user_id ,
158202 auth0OrgId ,
@@ -180,4 +224,67 @@ async function handleUserUpsert(user: User, time: string, c: Context, isNewUser:
180224 }
181225}
182226
227+ async function handleOrganizationDeleted ( organization : Organization , c : Context ) {
228+ const { id} = organization ;
229+
230+ try {
231+ // Delete the organization by Auth0 org id
232+ await c . env . DB . prepare ( `DELETE FROM Organizations WHERE auth0_org_id = ?` )
233+ . bind ( id )
234+ . run ( ) ;
235+ } catch ( err : any ) {
236+ console . error ( `Database error while deleting org_id=${ id } :` , err ) ;
237+ throw err ;
238+ }
239+ }
240+
241+
242+ async function handleOrganizationUpsert ( organization : Organization , time : string , c : Context , isNewOrg : boolean ) {
243+ const {
244+ id,
245+ name,
246+ display_name,
247+ branding,
248+ metadata,
249+ } = organization ;
250+
251+ // Convert complex objects to JSON strings for storage
252+ const brandingJson = branding ? JSON . stringify ( branding ) : null ;
253+ const metadataJson = metadata ? JSON . stringify ( metadata ) : null ;
254+
255+ try {
256+ await c . env . DB . prepare (
257+ `INSERT INTO Organizations(
258+ auth0_org_id,
259+ name,
260+ display_name,
261+ branding,
262+ metadata,
263+ org_type,
264+ last_event_processed
265+ ) VALUES (?, ?, ?, ?, ?, ?, ?)
266+ ON CONFLICT(auth0_org_id) DO UPDATE SET
267+ name = excluded.name,
268+ display_name = excluded.display_name,
269+ branding = excluded.branding,
270+ metadata = excluded.metadata,
271+ last_event_processed = excluded.last_event_processed` )
272+ . bind (
273+ id ,
274+ name || null ,
275+ display_name || null ,
276+ brandingJson ,
277+ metadataJson ,
278+ 'supplier' , // Default org_type for new organizations from Auth0
279+ time
280+ )
281+ . run ( ) ;
282+
283+ console . log ( `Organization ${ id } successfully ${ isNewOrg ? 'inserted' : 'updated' } into Organizations.` ) ;
284+ } catch ( err : any ) {
285+ console . error ( `Database error while upserting org_id=${ id } :` , err ) ;
286+ throw err ;
287+ }
288+ }
289+
183290export default eventsApp ;
0 commit comments