@@ -15,6 +15,7 @@ export const AttioWorkspaceSyncSchema = z.object({
1515 slug : z . string ( ) ,
1616 companySize : z . string ( ) . nullish ( ) ,
1717 createdAt : z . coerce . date ( ) ,
18+ adminUserId : z . string ( ) ,
1819} ) ;
1920export type AttioWorkspaceSync = z . infer < typeof AttioWorkspaceSyncSchema > ;
2021
@@ -30,8 +31,8 @@ export type AttioUserSync = z.infer<typeof AttioUserSyncSchema>;
3031class AttioClient {
3132 constructor ( private readonly apiKey : string ) { }
3233
33- // Create-or-update by unique attribute; throws on failure so the worker retries.
34- async #assert( object : string , matchingAttribute : string , values : Record < string , unknown > ) {
34+ // Create-or-update by unique attribute; returns the record id. Throws on failure so the worker retries.
35+ async #assert( object : string , matchingAttribute : string , values : Record < string , unknown > ) : Promise < string > {
3536 const url = `${ ATTIO_API } /objects/${ object } /records?matching_attribute=${ matchingAttribute } ` ;
3637 const response = await fetch ( url , {
3738 method : "PUT" ,
@@ -44,9 +45,18 @@ class AttioClient {
4445 logger . error ( "Attio assert failed" , { object, matchingAttribute, status : response . status , body } ) ;
4546 throw new Error ( `Attio assert ${ object } failed with status ${ response . status } ` ) ;
4647 }
48+
49+ return ( ( await response . json ( ) ) as any ) . data ?. id ?. record_id as string ;
4750 }
4851
4952 async upsertWorkspace ( payload : AttioWorkspaceSync ) {
53+ // The creating user is an admin of the new org — set their role and link them to the workspace.
54+ const adminRecordId = await this . #assert( "users" , "user_id" , {
55+ user_id : payload . adminUserId ,
56+ role : "Admin" ,
57+ is_test : IS_TEST ,
58+ } ) ;
59+
5060 await this . #assert( "workspaces" , "workspace_id" , {
5161 workspace_id : payload . orgId ,
5262 name : payload . title ,
@@ -56,6 +66,7 @@ class AttioClient {
5666 plan : "Free" ,
5767 account_status : "Active" ,
5868 is_test : IS_TEST ,
69+ users : [ { target_object : "users" , target_record_id : adminRecordId } ] ,
5970 } ) ;
6071 }
6172
0 commit comments