Files
calendar/packages/features/ee/dsync/lib/assignValueToUser.test.ts
T
Benny JooGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
f5d345b133 refactor: remove @calcom/web imports from @calcom/features and add @calcom/testing package (#26480)
* fix: remove @calcom/web imports from packages/features to eliminate circular dependency

- Migrate UserTableUser and MemberPermissions types to packages/features/users/types/user-table.ts
- Migrate useGeo hook to packages/features/geo/GeoContext.tsx
- Migrate buildLegacyRequest to packages/lib/buildLegacyCtx.ts
- Migrate Calendar component to packages/features/calendars/weeklyview/components/
- Move test utilities (bookingScenario, fixtures) to packages/features/test/
- Update all imports in packages/features to use new locations
- Add re-exports in apps/web for backward compatibility

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: delete original implementation files and fix type issues

- Delete original calendar component files in apps/web (keep only re-export stubs)
- Migrate OutOfOfficeInSlots to packages/features/bookings/components
- Convert apps/web OutOfOfficeInSlots to re-export stub
- Fix className vs class issue in Calendar.tsx
- Fix @calcom/trpc import violation in user-table.ts by using structural type

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: add missing isGroup and contains fields to UserTableUser attributes type

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: update customRole type to match actual Prisma Role model

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix build

* fix

* fix

* cleanup weeklyview

* fix

* refactor to mv MemberPermissions to types package

* add types dependency to features

* fix

* fix

* fix

* fix

* fix

* fix

* rename

* rename

* migrate

* migrate

* migrate

* fix

* fix

* fix

* refactor: move test utilities from packages/features/test to tests/libs

- Move bookingScenario utilities to tests/libs/bookingScenario
- Move fixtures to tests/libs/fixtures
- Update all imports in packages/features test files to use new location
- Update all imports in apps/web test files to use new location
- Eliminates duplication of test utilities between packages/features and apps/web

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: correct relative import paths for tests/libs in test files

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* refactor: replace test utility implementations with re-exports to tests/libs

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: fix test import paths and move signup handler tests to apps/web

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: move buildLegacyCtx to packages/lib and restore handlers to packages/features

- Move buildLegacyCtx from apps/web/lib to packages/lib to break circular dependency
- Update apps/web/lib/buildLegacyCtx.ts to re-export from @calcom/lib
- Restore signup handlers and tests to packages/features/auth/signup/handlers
- Update handler imports to use @calcom/lib/buildLegacyCtx instead of @calcom/web/lib/buildLegacyCtx

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: update recurring-event.test.ts imports to use tests/libs path

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* refactor: delete test re-export files and update imports to use tests/libs directly

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: update remaining test imports to use tests/libs directly

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: update handleRecurringEventBooking calls to match function signature (1 arg)

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* remove

* migrate tests

* migrate tests

* refactor: update test mock imports by removing  and using async  for mock creators.

* fix type errors

* fix: add type assertion for MockUser in p2002.test-suite.ts

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: restore locale import in compareReminderBodyToTemplate.test.ts using relative path

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* feat: create @calcom/testing package and migrate tests from /tests directory

- Created new @calcom/testing package in /packages/testing
- Moved all files from /tests to /packages/testing
- Updated all imports across the codebase to use @calcom/testing alias
- Removed /tests directory at root level

This allows other packages like @calcom/features and @calcom/web to import
testing utilities using the @calcom/testing alias instead of relative paths.

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix

* fix

* fix: add missing useBookings export to @calcom/atoms package

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: add missing useCalendarsBusyTimes and useConnectedCalendars exports to @calcom/atoms

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* chore: add @calcom/testing as explicit devDependency to packages that use it

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* refactor: move setupVitest.ts into @calcom/testing package

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix

* chore: add biome rules to restrict @calcom/testing imports

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix

* rename libs to lib

* rename libs to lib

* add rule

* add rule

* refactor: remove @calcom/features imports from @calcom/testing

- Move mockPaymentSuccessWebhookFromStripe to fresh-booking.test.ts
- Replace ProfileRepository.generateProfileUid() with uuidv4()
- Clone Tracking type into @calcom/testing/src/lib/types.ts
- Update imports in expects.ts and getMockRequestDataForBooking.ts
- Move source files into src/ folder
- Move CalendarManager mock to @calcom/features

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: add explicit exports for nested paths in @calcom/testing

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* improve

* improve

* fix

* fix

* fix type checks

* fix type checks

* fix type checks

* fix tests

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-01-08 16:59:11 +09:00

1043 lines
30 KiB
TypeScript

import prismock from "@calcom/testing/lib/__mocks__/prisma";
import { describe, expect, it, vi, beforeEach } from "vitest";
import prisma from "@calcom/prisma";
import type { AttributeOption } from "@calcom/prisma/client";
import { AttributeType, MembershipRole } from "@calcom/prisma/enums";
import { assignValueToUserInOrgBulk, buildPrismaQueriesForAttributeOptionToUser } from "./assignValueToUser";
function buildMockAttribute(data: {
teamId: number;
id: string;
type: AttributeType;
name: string;
slug: string;
isLocked: boolean;
}) {
return {
usersCanEditRelation: true,
isWeightsEnabled: false,
createdAt: new Date(),
updatedAt: new Date(),
enabled: true,
...data,
};
}
async function createMockAttribute({
options,
...attributeData
}: Parameters<typeof buildMockAttribute>[0] & {
options: Pick<AttributeOption, "id" | "value" | "slug">[];
}) {
const attribute = await prismock.attribute.create({ data: buildMockAttribute(attributeData) });
const attributeOptions = await prismock.attributeOption.createMany({
data: options.map((option) => ({
...option,
attributeId: attribute.id,
})),
});
return {
attribute,
attributeOptions,
};
}
async function createMockUserWithMembership({ orgId }: { orgId: number }) {
const user = await prismock.user.create({
data: {
name: "Test User",
email: "test@test.com",
},
});
const membership = await prismock.membership.create({
data: {
createdAt: new Date(),
role: MembershipRole.MEMBER,
disableImpersonation: false,
accepted: true,
teamId: orgId,
userId: user.id,
},
});
return {
user: user,
membership: membership,
};
}
async function createMockDSyncData({
directoryId,
tenant,
organizationId,
}: {
directoryId: string;
tenant: string;
organizationId: number;
}) {
return prismock.dSyncData.create({
data: {
directoryId,
tenant,
organizationId,
},
});
}
async function createMockAssignment({
membershipId,
attributeOptionId,
dsyncId,
}: {
membershipId: number;
attributeOptionId: string;
orgId: number;
dsyncId: string | null;
}) {
let dsync;
if (dsyncId) {
dsync = await prismock.dSyncData.findUnique({
where: {
id: dsyncId as unknown as number,
},
});
}
const attributeToUser = await prismock.attributeToUser.create({
data: {
memberId: membershipId,
attributeOptionId,
createdByDSyncId: dsync ? (dsync.id as unknown as string) : null,
},
});
return {
attributeToUser,
dsync,
};
}
describe("addValueForMember", () => {
const orgId = 1001;
beforeEach(() => {
vi.clearAllMocks();
});
describe("Misc", () => {
let dsyncDoingAssignment_id: string;
beforeEach(async () => {
const dsync = await createMockDSyncData({
directoryId: "1",
tenant: "testorg-000",
organizationId: orgId,
});
dsyncDoingAssignment_id = dsync.id as unknown as string;
});
it("should correctly identify attribute and attribute options by name and set them", async () => {
await createMockAttribute({
teamId: orgId,
id: "1",
name: "department",
slug: "department-slug",
type: AttributeType.SINGLE_SELECT,
isLocked: true,
options: [
{ id: "101", value: "engineering", slug: "engineering-slug" },
{ id: "102", value: "sales", slug: "sales-slug" },
],
});
const { user, membership } = await createMockUserWithMembership({ orgId });
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: "Sales",
},
updater: {
userId: 999,
},
});
const createdAttributeToUsers = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
});
expect(createdAttributeToUsers).toHaveLength(1);
expect(createdAttributeToUsers[0].attributeOptionId).toEqual("102");
// Do another update. This should reset the options assigned to the user and add just this one
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: "engineering",
},
updater: {
userId: 999,
},
});
const createdAttributeToUsers2 = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
});
expect(createdAttributeToUsers2).toHaveLength(1);
expect(createdAttributeToUsers2[0].attributeOptionId).toEqual("101");
});
it("should allow setting an array of strings as a value for a multi-select attribute", async () => {
await createMockAttribute({
teamId: orgId,
id: "1",
name: "department",
slug: "department-slug",
type: AttributeType.MULTI_SELECT,
isLocked: true,
options: [
{ id: "101", value: "engineering", slug: "engineering-slug" },
{ id: "102", value: "sales", slug: "sales-slug" },
{ id: "103", value: "marketing", slug: "marketing-slug" },
],
});
const { user, membership } = await createMockUserWithMembership({ orgId });
// In Cal.com user created pool
await createMockAssignment({
orgId,
membershipId: membership.id,
// Sales
attributeOptionId: "102",
dsyncId: null,
});
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: ["marketing", "engineering", "unknown"],
},
// Setting value using the same dsyncId that was used to create the option in SCIM Pool
updater: {
dsyncId: dsyncDoingAssignment_id,
},
});
const attributeToUsers = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
});
expect(attributeToUsers).toHaveLength(2);
expect(attributeToUsers[0].attributeOptionId).toEqual("103");
expect(attributeToUsers[1].attributeOptionId).toEqual("101");
});
});
describe("Locked Attribute(Source of Truth - Dsync)", () => {
const isLocked = true;
describe("Update from SCIM", async () => {
let dsyncDoingAssignment_id: string;
beforeEach(async () => {
const dsync = await createMockDSyncData({
directoryId: "1",
tenant: "testorg-000",
organizationId: orgId,
});
dsyncDoingAssignment_id = dsync.id as unknown as string;
});
it("should reset all the options(Multi-Select) in all the SCIM Pools and Cal.com User Pool", async () => {
await createMockAttribute({
teamId: orgId,
id: "1",
name: "department",
slug: "department-slug",
type: AttributeType.MULTI_SELECT,
isLocked,
options: [
{ id: "101", value: "engineering", slug: "engineering-slug" },
{ id: "102", value: "sales", slug: "sales-slug" },
{ id: "103", value: "marketing", slug: "marketing-slug" },
],
});
const { user, membership } = await createMockUserWithMembership({ orgId });
// In Cal.com user created pool
await createMockAssignment({
orgId,
membershipId: membership.id,
// Sales
attributeOptionId: "102",
dsyncId: null,
});
// In SCIM dsync-1 Pool
await createMockAssignment({
orgId,
membershipId: membership.id,
// Engineering
attributeOptionId: "101",
dsyncId: dsyncDoingAssignment_id,
});
const dsync2 = await createMockDSyncData({
directoryId: "2",
tenant: "testorg-000",
organizationId: orgId,
});
// In SCIM dsync-2 Pool
await createMockAssignment({
orgId,
membershipId: membership.id,
// Engineering
attributeOptionId: "101",
dsyncId: dsync2!.id as unknown as string,
});
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: "marketing",
},
// Setting value using the same dsyncId that was used to create the option in SCIM Pool
updater: {
dsyncId: dsyncDoingAssignment_id,
},
});
const attributeToUsers = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
});
expect(attributeToUsers).toHaveLength(1);
// Cal.com User Pool - Intact
expect(attributeToUsers[0].attributeOptionId).toEqual("103");
});
it("should allow setting an array of strings as a value for a multi-select attribute", async () => {
await createMockAttribute({
teamId: orgId,
id: "1",
name: "department",
slug: "department-slug",
type: AttributeType.MULTI_SELECT,
isLocked,
options: [
{ id: "101", value: "engineering", slug: "engineering-slug" },
{ id: "102", value: "sales", slug: "sales-slug" },
{ id: "103", value: "marketing", slug: "marketing-slug" },
],
});
const { user, membership } = await createMockUserWithMembership({ orgId });
// In Cal.com user created pool
await createMockAssignment({
orgId,
membershipId: membership.id,
// Sales
attributeOptionId: "102",
dsyncId: null,
});
// In SCIM dsync-1 Pool
await createMockAssignment({
orgId,
membershipId: membership.id,
// Engineering
attributeOptionId: "101",
dsyncId: dsyncDoingAssignment_id,
});
const dsync2 = await createMockDSyncData({
directoryId: "2",
tenant: "testorg-000",
organizationId: orgId,
});
// In SCIM dsync-2 Pool
await createMockAssignment({
orgId,
membershipId: membership.id,
// Engineering
attributeOptionId: "101",
dsyncId: dsync2!.id as unknown as string,
});
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: ["marketing", "engineering"],
},
// Setting value using the same dsyncId that was used to create the option in SCIM Pool
updater: {
dsyncId: dsyncDoingAssignment_id,
},
});
const attributeToUsers = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
});
expect(attributeToUsers).toHaveLength(2);
expect(attributeToUsers[0].attributeOptionId).toEqual("103");
expect(attributeToUsers[1].attributeOptionId).toEqual("101");
});
it("should not recreate an assignment that already exists", async () => {
await createMockAttribute({
teamId: orgId,
id: "1",
name: "department",
slug: "department-slug",
type: AttributeType.SINGLE_SELECT,
isLocked,
options: [
{ id: "101", value: "engineering", slug: "engineering-slug" },
{ id: "102", value: "sales", slug: "sales-slug" },
],
});
const { user, membership } = await createMockUserWithMembership({ orgId });
// In Cal.com user created pool
await createMockAssignment({
orgId,
membershipId: membership.id,
// Sales
attributeOptionId: "102",
dsyncId: null,
});
const beforeAssignments = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
});
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: "Sales",
},
updater: {
dsyncId: dsyncDoingAssignment_id,
},
});
const afterAssignments = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
});
expect(afterAssignments).toHaveLength(1);
expect(afterAssignments[0].attributeOptionId).toEqual("102");
expect(beforeAssignments[0].id).toEqual(afterAssignments[0].id);
expect(beforeAssignments[0].createdByDSyncId).toEqual(afterAssignments[0].createdByDSyncId);
expect(afterAssignments[0].updatedByDSyncId).toEqual(dsyncDoingAssignment_id);
});
it("should be able to set a value for TEXT(non-enum) attribute that already has an option set by Cal.com user", async () => {
await createMockAttribute({
teamId: orgId,
id: "1",
name: "department",
slug: "department-slug",
type: AttributeType.TEXT,
isLocked,
options: [{ id: "102", value: "sales", slug: "sales-slug" }],
});
const { user, membership } = await createMockUserWithMembership({ orgId });
// "Sales" Option set by cal.com user
await createMockAssignment({
orgId,
membershipId: membership.id,
// Sales
attributeOptionId: "102",
dsyncId: null,
});
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: "not-already-an-option",
},
updater: {
dsyncId: dsyncDoingAssignment_id,
},
});
const attributeToUsers = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
select: {
attributeOption: {
select: {
id: true,
value: true,
},
},
},
});
expect(attributeToUsers).toHaveLength(1);
expect(attributeToUsers[0].attributeOption.value).toEqual("not-already-an-option");
});
it.skip("should only delete the assignments for the user that is being pushed", async () => {
// TODO
});
});
});
describe("Unlocked Attribute", () => {
const isLocked = false;
describe("Update from SCIM", () => {
let dsyncDoingAssignment_id: string;
beforeEach(async () => {
const dsync = await createMockDSyncData({
directoryId: "1",
tenant: "testorg-000",
organizationId: orgId,
});
dsyncDoingAssignment_id = dsync.id as unknown as string;
});
it("should reset the options(Multi-Select) in the SCIM Pool only and keep Cal.com User Pool options intact when assignValueToUserInOrgBulk is from SCIM", async () => {
await createMockAttribute({
teamId: orgId,
id: "1",
name: "department",
slug: "department-slug",
type: AttributeType.MULTI_SELECT,
isLocked,
options: [
{ id: "scim-101", value: "engineering", slug: "engineering-slug" },
{ id: "calcom-102", value: "sales", slug: "sales-slug" },
{ id: "103", value: "marketing", slug: "marketing-slug" },
],
});
const { user, membership } = await createMockUserWithMembership({ orgId });
// In Cal.com user created pool
await createMockAssignment({
orgId,
membershipId: membership.id,
// Sales
attributeOptionId: "calcom-102",
dsyncId: null,
});
// In SCIM Pool
await createMockAssignment({
orgId,
membershipId: membership.id,
// Engineering
attributeOptionId: "scim-101",
dsyncId: dsyncDoingAssignment_id,
});
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: "marketing",
},
updater: {
dsyncId: dsyncDoingAssignment_id,
},
});
const attributeToUsers = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
});
expect(attributeToUsers).toHaveLength(2);
// Cal.com User Pool - Intact
expect(attributeToUsers[0].attributeOptionId).toEqual("calcom-102");
// SCIM Pool - Previous value removed
expect(attributeToUsers[1].attributeOptionId).toEqual("103");
});
it("should not be able to set a value for single-select attribute that already has an option set by Cal.com user", async () => {
await createMockAttribute({
teamId: orgId,
id: "1",
name: "department",
slug: "department-slug",
type: AttributeType.SINGLE_SELECT,
isLocked,
options: [
{ id: "101", value: "engineering", slug: "engineering-slug" },
{ id: "102", value: "sales", slug: "sales-slug" },
{ id: "103", value: "marketing", slug: "marketing-slug" },
],
});
const { user, membership } = await createMockUserWithMembership({ orgId });
// "Sales" Option set by cal.com user
await createMockAssignment({
orgId,
membershipId: membership.id,
// Sales
attributeOptionId: "102",
dsyncId: null,
});
const dsync = await createMockDSyncData({
directoryId: "1",
tenant: "testorg-000",
organizationId: orgId,
});
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: "engineering",
},
updater: {
dsyncId: dsync.id as unknown as string,
},
});
const attributeToUsers = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
select: {
attributeOption: {
select: {
id: true,
value: true,
},
},
},
});
expect(attributeToUsers).toHaveLength(1);
expect(attributeToUsers[0].attributeOption.value).toEqual("sales");
expect(attributeToUsers[0].attributeOption.id).toEqual("102");
});
it("should not be able to set a value for TEXT(non-enum) attribute that already has an option set by Cal.com user", async () => {
await createMockAttribute({
teamId: orgId,
id: "1",
name: "department",
slug: "department-slug",
type: AttributeType.TEXT,
isLocked,
options: [{ id: "102", value: "sales", slug: "sales-slug" }],
});
const { user, membership } = await createMockUserWithMembership({ orgId });
// "Sales" Option set by cal.com user
await createMockAssignment({
orgId,
membershipId: membership.id,
// Sales
attributeOptionId: "102",
dsyncId: null,
});
const dsync = await createMockDSyncData({
directoryId: "1",
tenant: "testorg-000",
organizationId: orgId,
});
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: "not-already-an-option",
},
updater: {
dsyncId: dsync.id as unknown as string,
},
});
const attributeToUsers = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
select: {
attributeOption: {
select: {
id: true,
value: true,
},
},
},
});
expect(attributeToUsers).toHaveLength(1);
expect(attributeToUsers[0].attributeOption.value).toEqual("sales");
expect(attributeToUsers[0].attributeOption.id).toEqual("102");
});
it("should not recreate an assignment that already exists", async () => {
await createMockAttribute({
teamId: orgId,
id: "1",
name: "department",
slug: "department-slug",
type: AttributeType.SINGLE_SELECT,
isLocked,
options: [
{ id: "101", value: "engineering", slug: "engineering-slug" },
{ id: "102", value: "sales", slug: "sales-slug" },
],
});
const { user, membership } = await createMockUserWithMembership({ orgId });
// In Cal.com user created pool
await createMockAssignment({
orgId,
membershipId: membership.id,
// Sales
attributeOptionId: "102",
dsyncId: null,
});
const beforeAssignments = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
});
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: "Sales",
},
updater: {
dsyncId: dsyncDoingAssignment_id,
},
});
const afterAssignments = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
});
expect(afterAssignments).toHaveLength(1);
expect(afterAssignments[0].attributeOptionId).toEqual("102");
expect(beforeAssignments[0].id).toEqual(afterAssignments[0].id);
});
it.skip("should only delete the assignments for the user that is being pushed", async () => {
// TODO
});
});
describe("Update from Cal.com", () => {
const userDoingAssignment_id = 999;
it("should reset the options(Multi-Select) in the Cal.com User Pool only and keep SCIM Pool options intact when assignValueToUserInOrgBulk is from Cal.com User Pool", async () => {
await createMockAttribute({
teamId: orgId,
id: "1",
name: "department",
slug: "department-slug",
type: AttributeType.MULTI_SELECT,
isLocked: false,
options: [
{ id: "101", value: "engineering", slug: "engineering-slug" },
{ id: "102", value: "sales", slug: "sales-slug" },
{ id: "103", value: "marketing", slug: "marketing-slug" },
],
});
const { user, membership } = await createMockUserWithMembership({ orgId });
// In Cal.com user created pool
await createMockAssignment({
orgId,
membershipId: membership.id,
// Sales
attributeOptionId: "102",
dsyncId: null,
});
const dsync = await createMockDSyncData({
directoryId: "1",
tenant: "testorg-000",
organizationId: orgId,
});
// In SCIM Pool
await createMockAssignment({
orgId,
membershipId: membership.id,
// Engineering
attributeOptionId: "101",
dsyncId: dsync.id,
});
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: "marketing",
},
updater: {
userId: userDoingAssignment_id,
},
});
const attributeToUsers = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
});
expect(attributeToUsers).toHaveLength(2);
// Cal.com User Pool - Previous value removed and new value added
expect(attributeToUsers[1].attributeOptionId).toEqual("103");
});
it("should not be able to assign values beyond the options for SINGLE_SELECT(enum based) attribute", async () => {
await createMockAttribute({
teamId: orgId,
id: "1",
name: "department",
slug: "department-slug",
type: AttributeType.SINGLE_SELECT,
isLocked: false,
options: [
{ id: "101", value: "engineering", slug: "engineering-slug" },
{ id: "102", value: "sales", slug: "sales-slug" },
{ id: "103", value: "marketing", slug: "marketing-slug" },
],
});
const { user, membership } = await createMockUserWithMembership({ orgId });
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: "not-already-an-option",
},
updater: {
userId: userDoingAssignment_id,
},
});
const attributeToUsers = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
select: {
attributeOption: {
select: {
id: true,
value: true,
},
},
},
});
expect(attributeToUsers).toHaveLength(0);
});
it("should be able to assign values beyond the options for TEXT(non-enum) attribute", async () => {
await createMockAttribute({
teamId: orgId,
id: "1",
name: "department",
slug: "department-slug",
type: AttributeType.TEXT,
isLocked: false,
options: [
{ id: "101", value: "engineering", slug: "engineering-slug" },
{ id: "102", value: "sales", slug: "sales-slug" },
{ id: "103", value: "marketing", slug: "marketing-slug" },
],
});
const { user, membership } = await createMockUserWithMembership({ orgId });
await assignValueToUserInOrgBulk({
orgId,
userId: user.id,
attributeLabelToValueMap: {
Department: "not-already-an-option",
},
updater: {
userId: userDoingAssignment_id,
},
});
const attributeToUsers = await prisma.attributeToUser.findMany({
where: {
memberId: membership.id,
},
select: {
attributeOption: {
select: {
id: true,
value: true,
},
},
},
});
expect(attributeToUsers).toHaveLength(1);
expect(attributeToUsers[0].attributeOption.value).toEqual("not-already-an-option");
});
it.skip("should only delete the assignments for the user that is being pushed", async () => {
// TODO
});
});
});
});
describe("buildPrismaQueriesForAttributeOptionToUser", () => {
describe("when building queries for attribute option to user", () => {
const mockCreator = { userId: 123 };
const mockMemberId = 456;
const mockOrgId = 789;
it("should not return any queries when there are no assignments", () => {
const result = buildPrismaQueriesForAttributeOptionToUser({
orgId: mockOrgId,
memberId: mockMemberId,
existingAttributeOptionAssignments: [],
attributeOptionsToAssign: [],
updater: mockCreator,
});
expect(result.attributeToUserCreateManyInput).toHaveLength(0);
expect(result.attributeToUserUpdateManyInput).toBeNull();
expect(result.attributeToUserDeleteQueries.locked).toBeNull();
expect(result.attributeToUserDeleteQueries.unlocked).toEqual(null);
});
it("should create new assignments for new options", () => {
const attributeOptionsToAssign = [
{
attribute: { id: "attr1", isLocked: false },
optionsToAssign: [{ id: "opt1", label: "Option 1" }],
},
];
const result = buildPrismaQueriesForAttributeOptionToUser({
orgId: mockOrgId,
memberId: mockMemberId,
existingAttributeOptionAssignments: [],
attributeOptionsToAssign,
updater: mockCreator,
});
expect(result.attributeToUserCreateManyInput).toHaveLength(1);
expect(result.attributeToUserCreateManyInput[0]).toEqual({
memberId: mockMemberId,
attributeOptionId: "opt1",
createdById: mockCreator.userId,
createdByDSyncId: null,
});
expect(result.attributeToUserUpdateManyInput).toBeNull();
expect(result.attributeToUserDeleteQueries.locked).toBeNull();
expect(result.attributeToUserDeleteQueries.unlocked).toBeNull();
});
it("should handle locked attributes correctly", () => {
const attributeOptionsToAssign = [
{
attribute: { id: "attr1", isLocked: true },
optionsToAssign: [{ id: "opt1", label: "Option 1" }],
},
];
const result = buildPrismaQueriesForAttributeOptionToUser({
orgId: mockOrgId,
memberId: mockMemberId,
existingAttributeOptionAssignments: [],
attributeOptionsToAssign,
updater: mockCreator,
});
expect(result.attributeToUserDeleteQueries.locked).toEqual({
memberId: mockMemberId,
attributeOption: {
attribute: {
id: {
in: ["attr1"],
},
},
},
});
});
it("should update existing assignments with same values", () => {
const existingAssignments = [
{
id: "assign1",
attributeOption: {
label: "APAC",
attribute: { id: "territory" },
},
},
] as any;
const attributeOptionsToAssign = [
{
attribute: { id: "territory", isLocked: false },
optionsToAssign: [{ id: "1", label: "ApAc" }],
},
];
const result = buildPrismaQueriesForAttributeOptionToUser({
orgId: mockOrgId,
memberId: mockMemberId,
existingAttributeOptionAssignments: existingAssignments,
attributeOptionsToAssign,
updater: mockCreator,
});
expect(result.attributeToUserUpdateManyInput).toEqual({
where: {
memberId: mockMemberId,
id: { in: ["assign1"] },
},
data: {
updatedById: mockCreator.userId,
updatedByDSyncId: null,
},
});
});
});
});