Skip to content

Commit 0955014

Browse files
committed
feat(webapp): link new org admin to their Attio workspace + set role
1 parent cea4abd commit 0955014

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

apps/webapp/app/models/organization.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export async function createOrganization(
9090
slug: organization.slug,
9191
companySize: organization.companySize,
9292
createdAt: organization.createdAt,
93+
adminUserId: userId,
9394
});
9495

9596
return { ...organization };

apps/webapp/app/services/attio.server.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
});
1920
export type AttioWorkspaceSync = z.infer<typeof AttioWorkspaceSyncSchema>;
2021

@@ -30,8 +31,8 @@ export type AttioUserSync = z.infer<typeof AttioUserSyncSchema>;
3031
class 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

Comments
 (0)