Skip to content
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

feat: add users to Hubspot on creation #21

Merged
merged 5 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"EHRC",
"estree",
"estruyf",
"firstname",
"fkey",
"fontsource",
"Geist",
Expand All @@ -64,6 +65,7 @@
"keyv",
"langchain",
"languagedetect",
"lastname",
"Lewandowski",
"Lexend",
"likert",
Expand Down
15 changes: 15 additions & 0 deletions packages/api/src/router/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { clerkClient } from "@clerk/nextjs/server";
import { demoUsers } from "@oakai/core";
import { createHubspotCustomer } from "@oakai/core/src/analytics/hubspotClient";
import { posthogServerClient } from "@oakai/core/src/analytics/posthogServerClient";
import { z } from "zod";

Expand Down Expand Up @@ -80,6 +81,20 @@ export const authRouter = router({
},
});

const email = updatedUser.emailAddresses[0]?.emailAddress;
if (!email) {
throw new Error("Email address is expected on clerk user");
}

await createHubspotCustomer({
email,
firstName: updatedUser.firstName,
lastName: updatedUser.lastName,
marketingAccepted: Boolean(
updatedUser.privateMetadata.acceptedPrivacyPolicy,
),
});

const { acceptedPrivacyPolicy, acceptedTermsOfUse } =
updatedUser.privateMetadata;

Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@googleapis/docs": "^3.0.0",
"@googleapis/drive": "^8.7.0",
"@googleapis/slides": "^1.0.5",
"@hubspot/api-client": "^11.2.0",
"@oakai/db": "*",
"@oakai/logger": "*",
"@slack/web-api": "^7.3.1",
Expand Down
63 changes: 63 additions & 0 deletions packages/core/src/analytics/hubspotClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Client } from "@hubspot/api-client";
import { ApiException } from "@hubspot/api-client/lib/codegen/crm/contacts/apis/exception";

const accessToken = process.env.HUBSPOT_ACCESS_TOKEN;
if (!accessToken) {
throw new Error("Missing HUBSPOT_ACCESS_TOKEN");
}

const hubspotClient = new Client({ accessToken });

interface CreateHubspotCustomerInput {
email: string;
firstName: string | null;
lastName: string | null;
marketingAccepted: boolean;
}

export const createHubspotCustomer = async ({
email,
firstName,
lastName,
marketingAccepted,
}: CreateHubspotCustomerInput) => {
let id: string | undefined;
try {
const result = await hubspotClient.crm.contacts.basicApi.getById(
email,
undefined,
undefined,
undefined,
undefined,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like they could change the signature here!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm actually a little unimpressed by that package in general. For example, it doesn't export their error instances

"email",
);
id = result.id;
} catch (e) {
const isNotFoundError = e instanceof ApiException && e.code === 404;
if (!isNotFoundError) {
throw e;
}
}

const properties = {
email,
...(firstName && {
firstname: firstName,
}),
...(lastName && {
lastname: lastName,
}),
email_consent_on_ai_account_creation: marketingAccepted ? "true" : "false",
};

if (id) {
return await hubspotClient.crm.contacts.basicApi.update(id, {
properties,
});
}

return await hubspotClient.crm.contacts.basicApi.create({
properties,
associations: [],
});
};
47 changes: 45 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading