Skip to content
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
42 changes: 42 additions & 0 deletions apps/web/src/lib/organizations/organizations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,48 @@ describe('Organizations', () => {
expect(updatedInvitee?.personal_account_disabled).toBe(false);
});

test('skips the customer-source survey when a user joins via invitation', async () => {
const owner = await insertTestUser();
const invitee = await insertTestUser({ customer_source: null });
const organization = await createOrganization('Test Org', owner.id);

const invitation = await inviteUserToOrganization(
organization.id,
owner.id,
invitee.google_user_email,
'member'
);

const result = await acceptOrganizationInvite(invitee.id, invitation.token);
expect(result.success).toBe(true);

const updatedInvitee = await db.query.kilocode_users.findFirst({
where: eq(kilocode_users.id, invitee.id),
});
expect(updatedInvitee?.customer_source).toBe('');
});

test('does not overwrite an existing customer-source answer when accepting an invite', async () => {
const owner = await insertTestUser();
const invitee = await insertTestUser({ customer_source: 'GitHub' });
const organization = await createOrganization('Test Org', owner.id);

const invitation = await inviteUserToOrganization(
organization.id,
owner.id,
invitee.google_user_email,
'member'
);

const result = await acceptOrganizationInvite(invitee.id, invitation.token);
expect(result.success).toBe(true);

const updatedInvitee = await db.query.kilocode_users.findFirst({
where: eq(kilocode_users.id, invitee.id),
});
expect(updatedInvitee?.customer_source).toBe('GitHub');
});

test('rejects accepting a pre-existing invitation into a child organization', async () => {
const owner = await insertTestUser();
const invitee = await insertTestUser({ google_user_email: 'legacy@example.com' });
Expand Down
21 changes: 21 additions & 0 deletions apps/web/src/lib/organizations/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,23 @@ export async function createOrganization(
return organization;
}

/**
* When a user joins an organization through SSO or an invitation we should not
* ask them how they heard about Kilo. Mark the customer-source survey as
* dismissed by setting `customer_source` to an empty string, but only when they
* have not already answered or dismissed it (`customer_source IS NULL`) so an
* existing answer is never overwritten.
*/
export async function skipCustomerSourceSurveyForOrgJoin(
userId: User['id'],
txn?: DrizzleTransaction
): Promise<void> {
await (txn || db)
.update(kilocode_users)
.set({ customer_source: '' })
.where(and(eq(kilocode_users.id, userId), isNull(kilocode_users.customer_source)));
}

export async function addUserToOrganization(
organizationId: Organization['id'],
userId: User['id'],
Expand Down Expand Up @@ -826,6 +843,10 @@ export async function acceptOrganizationInvite(
invited_by: invitation.invited_by,
});

// Users who join through an invitation should not be asked how they
// heard about Kilo.
await skipCustomerSourceSurveyForOrgJoin(userId, tx);

// If the invitation predates the account, the account was created after
// (i.e. because of) a pending invite: this is a brand-new user joining an
// organization via invite, so disable their personal account. Existing
Expand Down
39 changes: 38 additions & 1 deletion apps/web/src/lib/user/sso.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jest.mock('@/lib/organizations/organizations', () => ({
addSsoUserToOrganization: jest.fn(async () => false),
getOrganizationById: jest.fn(async () => ({ id: 'org-local' })),
getOrganizationMembers: jest.fn(async () => []),
skipCustomerSourceSurveyForOrgJoin: jest.fn(async () => {}),
}));

jest.mock('@/lib/organizations/organization-sso-policy', () => ({
Expand All @@ -46,11 +47,15 @@ jest.mock('@sentry/nextjs', () => ({
}));

import { createOrUpdateUser } from '@/lib/user';
import { addSsoUserToOrganization } from '@/lib/organizations/organizations';
import {
addSsoUserToOrganization,
skipCustomerSourceSurveyForOrgJoin,
} from '@/lib/organizations/organizations';
import { processSSOUserLogin } from './sso';

const mockCreateOrUpdateUser = jest.mocked(createOrUpdateUser);
const mockAddSsoUserToOrganization = jest.mocked(addSsoUserToOrganization);
const mockSkipCustomerSourceSurveyForOrgJoin = jest.mocked(skipCustomerSourceSurveyForOrgJoin);
const { mockWorkOSInstance } = jest.requireMock('@workos-inc/node') as {
mockWorkOSInstance: { organizations: { listOrganizations: jest.Mock } };
};
Expand Down Expand Up @@ -110,4 +115,36 @@ describe('processSSOUserLogin', () => {
isNewUser: true,
});
});

it('skips the customer-source survey when the user joins the org via SSO', async () => {
mockAddSsoUserToOrganization.mockResolvedValueOnce(true);
const accountInfo = {
google_user_email: 'new-user@example.com',
google_user_name: 'New User',
google_user_image_url: 'https://example.com/avatar.png',
hosted_domain: 'example.com',
provider: 'workos' as const,
provider_account_id: 'workos-user-123',
};

await expect(processSSOUserLogin(accountInfo)).resolves.toBe(true);

expect(mockSkipCustomerSourceSurveyForOrgJoin).toHaveBeenCalledWith('user-workos');
});

it('does not touch the customer-source survey when the user is already a member', async () => {
mockAddSsoUserToOrganization.mockResolvedValueOnce(false);
const accountInfo = {
google_user_email: 'new-user@example.com',
google_user_name: 'New User',
google_user_image_url: 'https://example.com/avatar.png',
hosted_domain: 'example.com',
provider: 'workos' as const,
provider_account_id: 'workos-user-123',
};

await expect(processSSOUserLogin(accountInfo)).resolves.toBe(true);

expect(mockSkipCustomerSourceSurveyForOrgJoin).not.toHaveBeenCalled();
});
});
4 changes: 4 additions & 0 deletions apps/web/src/lib/user/sso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
addSsoUserToOrganization,
getOrganizationById,
getOrganizationMembers,
skipCustomerSourceSurveyForOrgJoin,
} from '@/lib/organizations/organizations';
import { createAuditLog } from '@/lib/organizations/organization-audit-logs';
import { sendOrgSSOUserJoinedEmail } from '@/lib/email';
Expand Down Expand Up @@ -85,6 +86,9 @@ async function processSSOInternal(
isNewUser: res.isNew,
});
if (added) {
// Users who join through SSO should not be asked how they heard about Kilo.
await skipCustomerSourceSurveyForOrgJoin(savedUser.id);

// get all owners for org
const members = await getOrganizationMembers(kiloOrg.id);
const owners = members.filter(m => m.role === 'owner');
Expand Down