Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(loops): sync multi-seat owner and first-name targeting properties

Richie McIlroy1e944f50d3f2
diff --git a/emails/audiences.ts b/emails/audiences.ts
index eea693003..d214e7652 100644
--- a/emails/audiences.ts
+++ b/emails/audiences.ts
@@ -6,6 +6,8 @@ export const contactProperties = {
capTeammate: "boolean",
capCustomer: "boolean",
capPlanName: "string",
+ capMultiSeatOwner: "boolean",
+ capHasFirstName: "boolean",
capCustomerWelcome: "string",
capPromotionalEligible: "boolean",
capOnboardingEligible: "boolean",
diff --git a/packages/database/loops/profile.ts b/packages/database/loops/profile.ts
index 72600d2e9..d3ff54ecd 100644
--- a/packages/database/loops/profile.ts
+++ b/packages/database/loops/profile.ts
@@ -12,6 +12,7 @@ export type CapUser = {
lastName: string | null;
stripeSubscriptionStatus: string | null;
thirdPartyStripeSubscriptionId: string | null;
+ inviteQuota?: number | null;
created_at: string;
defaultOrgId: string | null;
marketingOrigin?: string;
@@ -47,6 +48,24 @@ export type ProfileInput = {
now: Date;
};
+const roleNames = new Set([
+ "accounts",
+ "admin",
+ "billing",
+ "contact",
+ "finance",
+ "hello",
+ "info",
+ "marketing",
+ "office",
+ "operations",
+ "ops",
+ "post",
+ "sales",
+ "support",
+ "team",
+]);
+
export const normalizeEmail = (email: string) => email.trim().toLowerCase();
export const emailHash = (email: string) =>
createHash("sha256").update(normalizeEmail(email)).digest("hex");
@@ -159,6 +178,8 @@ export function classifyProfile(
userId: user?.id ?? `bento:${emailHash(source.email)}`,
firstName,
capGreeting: firstName ? `Hey ${firstName},` : "Hey,",
+ capHasFirstName:
+ Boolean(firstName) && !roleNames.has(firstName.toLowerCase()),
lastName:
[source.last_name, source.lastname, user?.lastName]
.map((value) => value?.trim())
@@ -172,6 +193,13 @@ export function classifyProfile(
capCustomer: customer,
capPlanName: copy.plan,
capCustomerWelcome: copy.welcome,
+ capMultiSeatOwner:
+ directCustomer &&
+ !user?.thirdPartyStripeSubscriptionId &&
+ Number(user?.inviteQuota ?? 1) > 1 &&
+ memberships.some(
+ (member) => !member.tombstoneAt && member.ownerId === user?.id,
+ ),
capPromotionalEligible:
consent === "subscribed" &&
!teammate &&
diff --git a/packages/database/loops/sources.ts b/packages/database/loops/sources.ts
index e5aeb20b3..bf02f12ec 100644
--- a/packages/database/loops/sources.ts
+++ b/packages/database/loops/sources.ts
@@ -23,7 +23,7 @@ type UserSignals = {
export const profileUserQuery = `
SELECT u.id,u.email,u.name,u.lastName,u.emailVerified,u.stripeCustomerId,
- u.stripeSubscriptionStatus,u.thirdPartyStripeSubscriptionId,u.created_at,
+ u.stripeSubscriptionStatus,u.thirdPartyStripeSubscriptionId,u.inviteQuota,u.created_at,
u.defaultOrgId,u.marketingOrigin,
EXISTS(SELECT 1 FROM accounts a WHERE a.userId = u.id LIMIT 1) AS hasAccount,
EXISTS(SELECT 1 FROM accounts a WHERE a.userId = u.id AND BINARY a.provider = 'workos' LIMIT 1) AS hasWorkosAccount,
diff --git a/packages/database/loops/sync-policy.ts b/packages/database/loops/sync-policy.ts
index f86291fb5..5350e6df5 100644
--- a/packages/database/loops/sync-policy.ts
+++ b/packages/database/loops/sync-policy.ts
@@ -26,9 +26,21 @@ export function profileFingerprint(profile: ContactProfile) {
const {
capVerifiedAt: _verified,
capImportedAt: _imported,
+ capMultiSeatOwner,
+ capHasFirstName,
...stable
} = profile;
- return createHash("sha256").update(JSON.stringify(stable)).digest("hex");
+ // Case-study targeting only forces a sync for multi-seat owners, so adding
+ // it does not re-push every contact through the rate-limited queue.
+ return createHash("sha256")
+ .update(
+ JSON.stringify(
+ capMultiSeatOwner
+ ? { ...stable, capMultiSeatOwner, capHasFirstName }
+ : stable,
+ ),
+ )
+ .digest("hex");
}
export function contactUpdate(
diff --git a/scripts/loops/profile.test.ts b/scripts/loops/profile.test.ts
index dc2968c8a..1108c2da4 100644
--- a/scripts/loops/profile.test.ts
+++ b/scripts/loops/profile.test.ts
@@ -1,4 +1,5 @@
import assert from "node:assert/strict";
+import { createHash } from "node:crypto";
import { test } from "node:test";
import {
classifyProfile,
@@ -70,6 +71,72 @@ test("greetings include a name only when a nonblank name is known", () => {
}
});
+test("first-name targeting follows the same nonblank rule as the greeting", () => {
+ const fixture = input();
+ assert.equal(classifyProfile(fixture).capHasFirstName, true);
+ fixture.user = undefined;
+ for (const firstName of [undefined, "", " ", "null"]) {
+ if (firstName === undefined) delete fixture.source.first_name;
+ else fixture.source.first_name = firstName;
+ assert.equal(classifyProfile(fixture).capHasFirstName, false);
+ }
+ for (const roleName of ["Admin", "Operations", "Team", "post"]) {
+ fixture.source.first_name = roleName;
+ assert.equal(classifyProfile(fixture).capHasFirstName, false);
+ }
+});
+
+test("case-study targeting leaves other contacts' sync fingerprints unchanged", () => {
+ const profile = classifyProfile(input());
+ const {
+ capVerifiedAt: _verified,
+ capImportedAt: _imported,
+ capMultiSeatOwner: _owner,
+ capHasFirstName: _name,
+ ...legacy
+ } = profile;
+ assert.equal(
+ profileFingerprint(profile),
+ createHash("sha256").update(JSON.stringify(legacy)).digest("hex"),
+ );
+ assert.notEqual(
+ profileFingerprint(profile),
+ profileFingerprint({ ...profile, capMultiSeatOwner: true }),
+ );
+ assert.notEqual(
+ profileFingerprint({ ...profile, capMultiSeatOwner: true }),
+ profileFingerprint({
+ ...profile,
+ capMultiSeatOwner: true,
+ capHasFirstName: false,
+ }),
+ );
+});
+
+test("only organisation owners paying for more than one seat are multi-seat owners", () => {
+ const value = input();
+ if (!value.user) throw new Error("Missing fixture user");
+ value.user.stripeSubscriptionStatus = "active";
+ value.user.inviteQuota = 1;
+ assert.equal(classifyProfile(value).capMultiSeatOwner, false);
+ value.user.inviteQuota = 3;
+ assert.equal(classifyProfile(value).capMultiSeatOwner, true);
+ value.memberships[0].tombstoneAt = "2026-09-01T00:00:00.000Z";
+ assert.equal(classifyProfile(value).capMultiSeatOwner, false);
+ value.memberships[0].tombstoneAt = null;
+ value.memberships[0].ownerId = "someone-else";
+ assert.equal(classifyProfile(value).capMultiSeatOwner, false);
+ value.memberships[0].ownerId = "cap-user";
+ value.user.thirdPartyStripeSubscriptionId = "sub_team";
+ assert.equal(classifyProfile(value).capMultiSeatOwner, false);
+ value.user.thirdPartyStripeSubscriptionId = null;
+ value.user.stripeSubscriptionStatus = "canceled";
+ assert.equal(classifyProfile(value).capMultiSeatOwner, false);
+ value.user.stripeSubscriptionStatus = null;
+ value.user.inviteQuota = undefined;
+ assert.equal(classifyProfile(value).capMultiSeatOwner, false);
+});
+
test("an explicit source opt-out always wins over a positive custom flag", () => {
assert.equal(
sourceConsent({
diff --git a/scripts/loops/sources.ts b/scripts/loops/sources.ts
index 1b8a3aa10..6acdafca0 100644
--- a/scripts/loops/sources.ts
+++ b/scripts/loops/sources.ts
@@ -16,7 +16,7 @@ export async function readSources(capUrl: string, licenseUrl: string) {
try {
const users = await rows<CapUser>(
cap,
- "SELECT id,email,name,lastName,stripeSubscriptionStatus,thirdPartyStripeSubscriptionId,created_at,defaultOrgId,marketingOrigin FROM users",
+ "SELECT id,email,name,lastName,stripeSubscriptionStatus,thirdPartyStripeSubscriptionId,inviteQuota,created_at,defaultOrgId,marketingOrigin FROM users",
);
const memberships = await rows<Membership>(
cap,