-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(webapp): sync new orgs + users to Attio CRM on signup #3896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+194
−19
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8de9b62
feat(webapp): add Attio CRM sync client + worker jobs
isshaddad cea4abd
feat(webapp): sync new users + orgs to Attio on signup
isshaddad 0955014
feat(webapp): link new org admin to their Attio workspace + set role
isshaddad 9e6e96b
Merge branch 'main' into feat/tri-10431-attio-signup-sync
isshaddad 6d3e1d1
feat(webapp): set workspace email_domain from admin email
isshaddad e0af6a8
Apply suggestion from @devin-ai-integration[bot]
matt-aitken f4a2fbd
Update apps/webapp/app/services/attio.server.ts
isshaddad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import { z } from "zod"; | ||
| import { prisma } from "~/db.server"; | ||
| import { env } from "~/env.server"; | ||
| import { logger } from "./logger.server"; | ||
|
|
||
| // Syncs new orgs/users into Attio (workspaces/users objects) at signup, via the | ||
| // common worker so a slow Attio never blocks signup. Ongoing field updates are | ||
| // handled by the scheduled sync, not here. No-op without ATTIO_API_KEY. | ||
|
|
||
| const ATTIO_API = "https://api.attio.com/v2"; | ||
| const IS_TEST = env.APP_ENV !== "production"; | ||
|
|
||
| export const AttioWorkspaceSyncSchema = z.object({ | ||
| orgId: z.string(), | ||
| title: z.string(), | ||
| slug: z.string(), | ||
| companySize: z.string().nullish(), | ||
| createdAt: z.coerce.date(), | ||
| adminUserId: z.string(), | ||
| }); | ||
| export type AttioWorkspaceSync = z.infer<typeof AttioWorkspaceSyncSchema>; | ||
|
|
||
| export const AttioUserSyncSchema = z.object({ | ||
| userId: z.string(), | ||
| email: z.string(), | ||
| referralSource: z.string().nullish(), | ||
| marketingEmails: z.boolean(), | ||
| createdAt: z.coerce.date(), | ||
| }); | ||
| export type AttioUserSync = z.infer<typeof AttioUserSyncSchema>; | ||
|
|
||
| class AttioClient { | ||
| constructor(private readonly apiKey: string) {} | ||
|
|
||
| // Create-or-update by unique attribute; returns the record id. Throws on failure so the worker retries. | ||
| async #assert(object: string, matchingAttribute: string, values: Record<string, unknown>): Promise<string> { | ||
| const url = `${ATTIO_API}/objects/${object}/records?matching_attribute=${matchingAttribute}`; | ||
| const response = await fetch(url, { | ||
| method: "PUT", | ||
| headers: { Authorization: `Bearer ${this.apiKey}`, "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ data: { values } }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const body = await response.text(); | ||
| logger.error("Attio assert failed", { object, matchingAttribute, status: response.status, body }); | ||
| throw new Error(`Attio assert ${object} failed with status ${response.status}`); | ||
| } | ||
|
|
||
| return ((await response.json()) as any).data?.id?.record_id as string; | ||
| } | ||
|
|
||
| async upsertWorkspace(payload: AttioWorkspaceSync, emailDomain?: string) { | ||
| // The creating user is an admin of the new org — set their role and link them to the workspace. | ||
| const adminRecordId = await this.#assert("users", "user_id", { | ||
| user_id: payload.adminUserId, | ||
| role: "Admin", | ||
| is_test: IS_TEST, | ||
| }); | ||
|
|
||
| await this.#assert("workspaces", "workspace_id", { | ||
| workspace_id: payload.orgId, | ||
| name: payload.title, | ||
| org_slug: payload.slug, | ||
| company_size: payload.companySize ?? undefined, | ||
| email_domain: emailDomain, | ||
| signup_date: toDate(payload.createdAt), | ||
| plan: "Free", | ||
| account_status: "Active", | ||
| is_test: IS_TEST, | ||
| users: [{ target_object: "users", target_record_id: adminRecordId }], | ||
| }); | ||
| } | ||
|
|
||
| async upsertUser(payload: AttioUserSync) { | ||
| await this.#assert("users", "user_id", { | ||
| user_id: payload.userId, | ||
| primary_email_address: payload.email, | ||
| marketing_opt_in: payload.marketingEmails, | ||
| referral_source: payload.referralSource ?? undefined, | ||
| signup_date: toDate(payload.createdAt), | ||
| is_test: IS_TEST, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Attio `date` attributes want a bare YYYY-MM-DD value. | ||
| function toDate(date: Date): string { | ||
| return date.toISOString().slice(0, 10); | ||
| } | ||
|
|
||
| // Domain from an email; the cloud-side matcher normalizes it further. | ||
| function domainFromEmail(email: string | undefined): string | undefined { | ||
| return email?.split("@")[1]?.toLowerCase().trim() || undefined; | ||
| } | ||
|
|
||
| export const attioClient = env.ATTIO_API_KEY ? new AttioClient(env.ATTIO_API_KEY) : null; | ||
|
|
||
| export async function enqueueAttioWorkspaceSync(payload: AttioWorkspaceSync) { | ||
| if (!attioClient) return; | ||
| try { | ||
| // Lazy import to avoid a circular dependency with commonWorker (which imports this module's schemas). | ||
| const { commonWorker } = await import("~/v3/commonWorker.server"); | ||
| await commonWorker.enqueue({ id: `attio:workspace:${payload.orgId}`, job: "attio.syncWorkspace", payload }); | ||
| } catch (error) { | ||
| logger.error("Failed to enqueue Attio workspace sync", { orgId: payload.orgId, error }); | ||
| } | ||
| } | ||
|
|
||
| export async function enqueueAttioUserSync(payload: AttioUserSync) { | ||
| if (!attioClient) return; | ||
| try { | ||
| const { commonWorker } = await import("~/v3/commonWorker.server"); | ||
| await commonWorker.enqueue({ id: `attio:user:${payload.userId}`, job: "attio.syncUser", payload }); | ||
| } catch (error) { | ||
| logger.error("Failed to enqueue Attio user sync", { userId: payload.userId, error }); | ||
| } | ||
| } | ||
|
|
||
| export async function runAttioWorkspaceSync(payload: AttioWorkspaceSync) { | ||
| if (!attioClient) return; | ||
| const admin = await prisma.user.findUnique({ | ||
| where: { id: payload.adminUserId }, | ||
| select: { email: true }, | ||
| }); | ||
|
matt-aitken marked this conversation as resolved.
Outdated
|
||
| await attioClient.upsertWorkspace(payload, domainFromEmail(admin?.email)); | ||
| } | ||
|
|
||
| export async function runAttioUserSync(payload: AttioUserSync) { | ||
| if (!attioClient) return; | ||
| await attioClient.upsertUser(payload); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.