* Add db schema * Add `CredentialRepository.findByTeamIdAndSlugs` * Add enabled app slugs for attribute syncing * Create repository for `IntegrationAttributeSync` * Create zod schemas * Create `AttributeSyncUserRuleOutputMapper` * Create `IntegrationAttributeSyncService` * Create DI contianer * Create trpc endpoints * Create page * Include team name in `CredentialRepository.findByTeamIdAndSlugs` * Update schema and relations * Update types and schemas * Add more methods to IntegrationAttributeSyncRepository * Add more services to `IntegrationAttributeSyncService` - getById - Init updateIncludeRulesAndMappings * Refactor `getTeams.handler` to use repository * Create `createAttributeSync` trpc endpoint * Create `updateAttributeSync` trpc endpoint * Add router to trpc * Create attribute sync child components * Pass custom actions to `FormCard` * Create `IntegrationAttributeSyncCard` * Pass inital props via server side * Fix prop * Only refetch on mutation * Fixes * Add form error when duplicate field and attribute combo * Add `updateTransactionWithRUleAndMappings` logic * Adjust zod schemas * Service add `updateIncludeRulesAndMappings` * Pass orgId from server to component * Rename types * Add deleteById method to repository * Add name to integrationAttributeSync * Add deleteById method to service * Rename method * Add deleteAttributeSync trpc endpoint * Make the IntegrationAttributeSyncCard a dummy component * test: add tests for IntegrationAttributeSync feature Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * Move creating a attribute sync record to the service * Add i18n strings * Safe select credential in find by id and team * Fix default credentialId value in form * Update repository return types * Add i18n string * Make credentialId optional for form schema * Fix label * Add cascade deletes * Add verification that syncs belong to org * Create mapper for repository output * Type fixes * Remove old test file * Pass `attributeOptions` from parent to children * Infer types from zod schema * Type fixes * Type fix * Clean up * Add i18n strings * Remove unused file * Address feedback * Add migration file * Address feedback * Add validation for new integration values * Remove unused router * Move away from z.infer to z.ZodType * Clean up comments * Type fix * Type fixes * Type fix * fix: add passthrough to syncFormDataSchema to preserve extra fields Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: remove incorrect test that expected extra fields to pass through syncFormDataSchema Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * Add endpoint for SF to call * Create scratch org config * Create sf cli scripts * Create package logic * Update README * Remove unused file * Add indexes * Add aria label * Address feedback - consistent validation * Fix import paths for attribute types * Add `CredentialRepository.findByAppIdAndKeyValue` * Get credential by instance URL * Verify incoming sfdc orgId matches credential sfdc orgId * Rename method * Get user name integration syncs * refactor: change attributeSyncRules array to singular attributeSyncRule The database schema enforces a one-to-one relationship between IntegrationAttributeSync and AttributeSyncRule (via @unique constraint), and the UI only supports a single rule. This change makes the TypeScript type match the database schema and UI behavior. Changes: - Update IntegrationAttributeSync interface to use attributeSyncRule: AttributeSyncRule | null - Update mapper to return singular rule instead of wrapping in array - Update UI component to access sync.attributeSyncRule directly - Update IIntegrationAttributeSyncUpdateParams Omit type - Update tests to use attributeSyncRule: null instead of attributeSyncRules: [] Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * Convert `membershipRepository.findAllByUserId` to a normal method * Add temp files to git ignore * Init and process team conditions * Biome formatting * Add `AttributeService` to get user attributes * Add DI container for AttributeService * AttributeSyncService evaluate attribute conditions * Create DI container for attributeSyncService * Return result for full condition * Evaluate if attribute sync should apply to user * Add method * Change PrismaAttributeOptionRepository to instance methods * Init AttributeSyncFieldMappingsService and process attribute syncs * Add AttributeSyncFieldMappingService DI container * Refactor orgId to organizationId * Add membership validation to sync field service * AttributeSYncFieldMappingService use repository methods * AttributeSyncFieldMappingService.processMappings add mapping logic * Add DI tokens * user-sync endpoint to implement attribute syncing * Validate team belongs to org for rule * test: add tests for AttributeSyncRuleService, AttributeSyncFieldMappingService, and AttributeService Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * Remove duplicate migration file * Fix merge conflict * fix: resolve type errors in attribute sync feature (#26814) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * Rename variable * Fix log typo * Fix file name * Add error logging * Use credential teamId * fix: add missing mockTeamRepository and team validation to tests Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * Fix naming * Fix file import * Type fix * Pass MembershipRepository as a dep in AttributeSyncFieldMappingService * Type fix * fix: add mockMembershipRepository to AttributeSyncFieldMappingService tests Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * Update packages/app-store/salesforce/api/user-sync.ts Add error handling when getting orgId from stored salesforce id Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * fix: address Cubic code review comments - PrismaAttributeRepository: use nested select instead of include: true for options - CredentialRepository: use this.primaClient instead of global prisma, use select instead of include for relations - AttributeSyncFieldMappingService: optimize O(n*m) complexity with Map lookup for O(1) access Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * Fix typo in CredentialRepository * Update README * Update sfdx-project * Add SFDC package tests * fix: improve Salesforce Apex test assertions to verify actual behavior - Enhanced CalComHttpMock to track HTTP callout invocations and capture requests - Updated UserUpdateHandlerTest to verify HTTP callouts are made with correct data - Updated CalComCalloutQueueableTest to verify HTTP callouts are made correctly - Replaced System.assert(true, ...) with meaningful assertions that verify: - Correct number of HTTP callouts - Request body contains expected fields - Request method is POST Addresses Cubic AI review feedback (confidence 9/10 issues only) Co-Authored-By: unknown <> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Hariom Balhara <hariombalhara@gmail.com> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
561 lines
18 KiB
TypeScript
561 lines
18 KiB
TypeScript
import type { PrismaAttributeOptionRepository } from "@calcom/features/attributes/repositories/PrismaAttributeOptionRepository";
|
|
import type { PrismaAttributeRepository } from "@calcom/features/attributes/repositories/PrismaAttributeRepository";
|
|
import type { PrismaAttributeToUserRepository } from "@calcom/features/attributes/repositories/PrismaAttributeToUserRepository";
|
|
import type { MembershipRepository } from "@calcom/features/membership/repositories/MembershipRepository";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import type { IFieldMapping } from "../repositories/IIntegrationAttributeSyncRepository";
|
|
import { AttributeSyncFieldMappingService } from "./AttributeSyncFieldMappingService";
|
|
|
|
describe("AttributeSyncFieldMappingService", () => {
|
|
let service: AttributeSyncFieldMappingService;
|
|
let mockAttributeToUserRepository: {
|
|
deleteMany: ReturnType<typeof vi.fn>;
|
|
createManySkipDuplicates: ReturnType<typeof vi.fn>;
|
|
};
|
|
let mockAttributeRepository: {
|
|
findManyByIdsAndOrgIdWithOptions: ReturnType<typeof vi.fn>;
|
|
};
|
|
let mockAttributeOptionRepository: {
|
|
createMany: ReturnType<typeof vi.fn>;
|
|
findMany: ReturnType<typeof vi.fn>;
|
|
};
|
|
let mockMembershipRepository: {
|
|
findUniqueByUserIdAndTeamId: ReturnType<typeof vi.fn>;
|
|
};
|
|
|
|
beforeEach(() => {
|
|
vi.resetAllMocks();
|
|
|
|
mockAttributeToUserRepository = {
|
|
deleteMany: vi.fn(),
|
|
createManySkipDuplicates: vi.fn(),
|
|
};
|
|
|
|
mockAttributeRepository = {
|
|
findManyByIdsAndOrgIdWithOptions: vi.fn(),
|
|
};
|
|
|
|
mockAttributeOptionRepository = {
|
|
createMany: vi.fn(),
|
|
findMany: vi.fn(),
|
|
};
|
|
|
|
mockMembershipRepository = {
|
|
findUniqueByUserIdAndTeamId: vi.fn(),
|
|
};
|
|
|
|
service = new AttributeSyncFieldMappingService({
|
|
attributeToUserRepository: mockAttributeToUserRepository as unknown as PrismaAttributeToUserRepository,
|
|
attributeRepository: mockAttributeRepository as unknown as PrismaAttributeRepository,
|
|
attributeOptionRepository: mockAttributeOptionRepository as unknown as PrismaAttributeOptionRepository,
|
|
membershipRepository: mockMembershipRepository as unknown as MembershipRepository,
|
|
});
|
|
});
|
|
|
|
describe("syncIntegrationFieldsToAttributes", () => {
|
|
const baseParams = {
|
|
userId: 1,
|
|
organizationId: 100,
|
|
};
|
|
|
|
it("should return early when no membership found", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue(null);
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings: [
|
|
{ id: "mapping-1", integrationFieldName: "Department", attributeId: "attr-1", enabled: true },
|
|
],
|
|
integrationFields: { Department: "Engineering" },
|
|
});
|
|
|
|
expect(mockAttributeRepository.findManyByIdsAndOrgIdWithOptions).not.toHaveBeenCalled();
|
|
expect(mockAttributeToUserRepository.deleteMany).not.toHaveBeenCalled();
|
|
expect(mockAttributeToUserRepository.createManySkipDuplicates).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should return early when no enabled mappings with matching integration fields", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
teamId: 100,
|
|
accepted: true,
|
|
role: "MEMBER",
|
|
disableImpersonation: false,
|
|
});
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings: [
|
|
{ id: "mapping-1", integrationFieldName: "Department", attributeId: "attr-1", enabled: false },
|
|
],
|
|
integrationFields: { Department: "Engineering" },
|
|
});
|
|
|
|
expect(mockAttributeRepository.findManyByIdsAndOrgIdWithOptions).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should sync SINGLE_SELECT attribute with matching option", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
teamId: 100,
|
|
accepted: true,
|
|
role: "MEMBER",
|
|
disableImpersonation: false,
|
|
});
|
|
|
|
mockAttributeRepository.findManyByIdsAndOrgIdWithOptions.mockResolvedValue([
|
|
{
|
|
id: "attr-1",
|
|
name: "Department",
|
|
type: "SINGLE_SELECT",
|
|
options: [
|
|
{ id: "opt-1", value: "Engineering", slug: "engineering" },
|
|
{ id: "opt-2", value: "Sales", slug: "sales" },
|
|
],
|
|
},
|
|
]);
|
|
|
|
const syncFieldMappings: IFieldMapping[] = [
|
|
{ id: "mapping-1", integrationFieldName: "Department", attributeId: "attr-1", enabled: true },
|
|
];
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings,
|
|
integrationFields: { Department: "Engineering" },
|
|
});
|
|
|
|
expect(mockAttributeToUserRepository.deleteMany).toHaveBeenCalledWith({
|
|
memberId: 1,
|
|
attributeOption: {
|
|
attributeId: { in: ["attr-1"] },
|
|
},
|
|
});
|
|
|
|
expect(mockAttributeToUserRepository.createManySkipDuplicates).toHaveBeenCalledWith([
|
|
{ memberId: 1, attributeOptionId: "opt-1" },
|
|
]);
|
|
});
|
|
|
|
it("should sync MULTI_SELECT attribute with multiple matching options", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
teamId: 100,
|
|
accepted: true,
|
|
role: "MEMBER",
|
|
disableImpersonation: false,
|
|
});
|
|
|
|
mockAttributeRepository.findManyByIdsAndOrgIdWithOptions.mockResolvedValue([
|
|
{
|
|
id: "attr-1",
|
|
name: "Skills",
|
|
type: "MULTI_SELECT",
|
|
options: [
|
|
{ id: "opt-1", value: "JavaScript", slug: "javascript" },
|
|
{ id: "opt-2", value: "TypeScript", slug: "typescript" },
|
|
{ id: "opt-3", value: "Python", slug: "python" },
|
|
],
|
|
},
|
|
]);
|
|
|
|
const syncFieldMappings: IFieldMapping[] = [
|
|
{ id: "mapping-1", integrationFieldName: "Skills", attributeId: "attr-1", enabled: true },
|
|
];
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings,
|
|
integrationFields: { Skills: "JavaScript, TypeScript" },
|
|
});
|
|
|
|
expect(mockAttributeToUserRepository.createManySkipDuplicates).toHaveBeenCalledWith([
|
|
{ memberId: 1, attributeOptionId: "opt-1" },
|
|
{ memberId: 1, attributeOptionId: "opt-2" },
|
|
]);
|
|
});
|
|
|
|
it("should handle case-insensitive option matching", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
teamId: 100,
|
|
accepted: true,
|
|
role: "MEMBER",
|
|
disableImpersonation: false,
|
|
});
|
|
|
|
mockAttributeRepository.findManyByIdsAndOrgIdWithOptions.mockResolvedValue([
|
|
{
|
|
id: "attr-1",
|
|
name: "Department",
|
|
type: "SINGLE_SELECT",
|
|
options: [{ id: "opt-1", value: "Engineering", slug: "engineering" }],
|
|
},
|
|
]);
|
|
|
|
const syncFieldMappings: IFieldMapping[] = [
|
|
{ id: "mapping-1", integrationFieldName: "Department", attributeId: "attr-1", enabled: true },
|
|
];
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings,
|
|
integrationFields: { Department: "ENGINEERING" },
|
|
});
|
|
|
|
expect(mockAttributeToUserRepository.createManySkipDuplicates).toHaveBeenCalledWith([
|
|
{ memberId: 1, attributeOptionId: "opt-1" },
|
|
]);
|
|
});
|
|
|
|
it("should create new option for TEXT attribute when no matching option exists", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
teamId: 100,
|
|
accepted: true,
|
|
role: "MEMBER",
|
|
disableImpersonation: false,
|
|
});
|
|
|
|
mockAttributeRepository.findManyByIdsAndOrgIdWithOptions.mockResolvedValue([
|
|
{
|
|
id: "attr-1",
|
|
name: "Title",
|
|
type: "TEXT",
|
|
options: [],
|
|
},
|
|
]);
|
|
|
|
mockAttributeOptionRepository.findMany.mockResolvedValue([
|
|
{ id: "new-opt-1", attributeId: "attr-1", value: "Senior Engineer", slug: "senior-engineer" },
|
|
]);
|
|
|
|
const syncFieldMappings: IFieldMapping[] = [
|
|
{ id: "mapping-1", integrationFieldName: "Title", attributeId: "attr-1", enabled: true },
|
|
];
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings,
|
|
integrationFields: { Title: "Senior Engineer" },
|
|
});
|
|
|
|
expect(mockAttributeOptionRepository.createMany).toHaveBeenCalledWith({
|
|
createManyInput: [{ attributeId: "attr-1", value: "Senior Engineer", slug: "senior-engineer" }],
|
|
});
|
|
|
|
expect(mockAttributeToUserRepository.createManySkipDuplicates).toHaveBeenCalledWith([
|
|
{ memberId: 1, attributeOptionId: "new-opt-1" },
|
|
]);
|
|
});
|
|
|
|
it("should use existing option for TEXT attribute when matching option exists", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
teamId: 100,
|
|
accepted: true,
|
|
role: "MEMBER",
|
|
disableImpersonation: false,
|
|
});
|
|
|
|
mockAttributeRepository.findManyByIdsAndOrgIdWithOptions.mockResolvedValue([
|
|
{
|
|
id: "attr-1",
|
|
name: "Title",
|
|
type: "TEXT",
|
|
options: [{ id: "opt-1", value: "Senior Engineer", slug: "senior-engineer" }],
|
|
},
|
|
]);
|
|
|
|
const syncFieldMappings: IFieldMapping[] = [
|
|
{ id: "mapping-1", integrationFieldName: "Title", attributeId: "attr-1", enabled: true },
|
|
];
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings,
|
|
integrationFields: { Title: "Senior Engineer" },
|
|
});
|
|
|
|
expect(mockAttributeOptionRepository.createMany).not.toHaveBeenCalled();
|
|
expect(mockAttributeToUserRepository.createManySkipDuplicates).toHaveBeenCalledWith([
|
|
{ memberId: 1, attributeOptionId: "opt-1" },
|
|
]);
|
|
});
|
|
|
|
it("should skip mapping when attribute not found", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
teamId: 100,
|
|
accepted: true,
|
|
role: "MEMBER",
|
|
disableImpersonation: false,
|
|
});
|
|
|
|
mockAttributeRepository.findManyByIdsAndOrgIdWithOptions.mockResolvedValue([]);
|
|
|
|
const syncFieldMappings: IFieldMapping[] = [
|
|
{ id: "mapping-1", integrationFieldName: "Department", attributeId: "attr-1", enabled: true },
|
|
];
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings,
|
|
integrationFields: { Department: "Engineering" },
|
|
});
|
|
|
|
expect(mockAttributeToUserRepository.deleteMany).not.toHaveBeenCalled();
|
|
expect(mockAttributeToUserRepository.createManySkipDuplicates).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should skip non-matching options for SINGLE_SELECT attribute", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
teamId: 100,
|
|
accepted: true,
|
|
role: "MEMBER",
|
|
disableImpersonation: false,
|
|
});
|
|
|
|
mockAttributeRepository.findManyByIdsAndOrgIdWithOptions.mockResolvedValue([
|
|
{
|
|
id: "attr-1",
|
|
name: "Department",
|
|
type: "SINGLE_SELECT",
|
|
options: [
|
|
{ id: "opt-1", value: "Engineering", slug: "engineering" },
|
|
{ id: "opt-2", value: "Sales", slug: "sales" },
|
|
],
|
|
},
|
|
]);
|
|
|
|
const syncFieldMappings: IFieldMapping[] = [
|
|
{ id: "mapping-1", integrationFieldName: "Department", attributeId: "attr-1", enabled: true },
|
|
];
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings,
|
|
integrationFields: { Department: "Marketing" },
|
|
});
|
|
|
|
expect(mockAttributeToUserRepository.deleteMany).toHaveBeenCalledWith({
|
|
memberId: 1,
|
|
attributeOption: {
|
|
attributeId: { in: ["attr-1"] },
|
|
},
|
|
});
|
|
|
|
expect(mockAttributeToUserRepository.createManySkipDuplicates).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should handle multiple mappings for different attributes", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
teamId: 100,
|
|
accepted: true,
|
|
role: "MEMBER",
|
|
disableImpersonation: false,
|
|
});
|
|
|
|
mockAttributeRepository.findManyByIdsAndOrgIdWithOptions.mockResolvedValue([
|
|
{
|
|
id: "attr-1",
|
|
name: "Department",
|
|
type: "SINGLE_SELECT",
|
|
options: [{ id: "opt-1", value: "Engineering", slug: "engineering" }],
|
|
},
|
|
{
|
|
id: "attr-2",
|
|
name: "Level",
|
|
type: "SINGLE_SELECT",
|
|
options: [{ id: "opt-2", value: "Senior", slug: "senior" }],
|
|
},
|
|
]);
|
|
|
|
const syncFieldMappings: IFieldMapping[] = [
|
|
{ id: "mapping-1", integrationFieldName: "Department", attributeId: "attr-1", enabled: true },
|
|
{ id: "mapping-2", integrationFieldName: "Level", attributeId: "attr-2", enabled: true },
|
|
];
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings,
|
|
integrationFields: { Department: "Engineering", Level: "Senior" },
|
|
});
|
|
|
|
expect(mockAttributeToUserRepository.deleteMany).toHaveBeenCalledWith({
|
|
memberId: 1,
|
|
attributeOption: {
|
|
attributeId: { in: ["attr-1", "attr-2"] },
|
|
},
|
|
});
|
|
|
|
expect(mockAttributeToUserRepository.createManySkipDuplicates).toHaveBeenCalledWith([
|
|
{ memberId: 1, attributeOptionId: "opt-1" },
|
|
{ memberId: 1, attributeOptionId: "opt-2" },
|
|
]);
|
|
});
|
|
|
|
it("should only process enabled mappings", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
teamId: 100,
|
|
accepted: true,
|
|
role: "MEMBER",
|
|
disableImpersonation: false,
|
|
});
|
|
|
|
mockAttributeRepository.findManyByIdsAndOrgIdWithOptions.mockResolvedValue([
|
|
{
|
|
id: "attr-1",
|
|
name: "Department",
|
|
type: "SINGLE_SELECT",
|
|
options: [{ id: "opt-1", value: "Engineering", slug: "engineering" }],
|
|
},
|
|
]);
|
|
|
|
const syncFieldMappings: IFieldMapping[] = [
|
|
{ id: "mapping-1", integrationFieldName: "Department", attributeId: "attr-1", enabled: true },
|
|
{ id: "mapping-2", integrationFieldName: "Level", attributeId: "attr-2", enabled: false },
|
|
];
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings,
|
|
integrationFields: { Department: "Engineering", Level: "Senior" },
|
|
});
|
|
|
|
expect(mockAttributeRepository.findManyByIdsAndOrgIdWithOptions).toHaveBeenCalledWith({
|
|
attributeIds: ["attr-1"],
|
|
orgId: 100,
|
|
});
|
|
});
|
|
|
|
it("should handle SINGLE_SELECT with comma-separated value by taking first value only", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
teamId: 100,
|
|
accepted: true,
|
|
role: "MEMBER",
|
|
disableImpersonation: false,
|
|
});
|
|
|
|
mockAttributeRepository.findManyByIdsAndOrgIdWithOptions.mockResolvedValue([
|
|
{
|
|
id: "attr-1",
|
|
name: "Department",
|
|
type: "SINGLE_SELECT",
|
|
options: [
|
|
{ id: "opt-1", value: "Engineering", slug: "engineering" },
|
|
{ id: "opt-2", value: "Sales", slug: "sales" },
|
|
],
|
|
},
|
|
]);
|
|
|
|
const syncFieldMappings: IFieldMapping[] = [
|
|
{ id: "mapping-1", integrationFieldName: "Department", attributeId: "attr-1", enabled: true },
|
|
];
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings,
|
|
integrationFields: { Department: "Engineering, Sales" },
|
|
});
|
|
|
|
expect(mockAttributeToUserRepository.createManySkipDuplicates).toHaveBeenCalledWith([
|
|
{ memberId: 1, attributeOptionId: "opt-1" },
|
|
]);
|
|
});
|
|
|
|
it("should handle NUMBER attribute type", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
teamId: 100,
|
|
accepted: true,
|
|
role: "MEMBER",
|
|
disableImpersonation: false,
|
|
});
|
|
|
|
mockAttributeRepository.findManyByIdsAndOrgIdWithOptions.mockResolvedValue([
|
|
{
|
|
id: "attr-1",
|
|
name: "Years of Experience",
|
|
type: "NUMBER",
|
|
options: [],
|
|
},
|
|
]);
|
|
|
|
mockAttributeOptionRepository.findMany.mockResolvedValue([
|
|
{ id: "new-opt-1", attributeId: "attr-1", value: "5", slug: "5" },
|
|
]);
|
|
|
|
const syncFieldMappings: IFieldMapping[] = [
|
|
{ id: "mapping-1", integrationFieldName: "Experience", attributeId: "attr-1", enabled: true },
|
|
];
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings,
|
|
integrationFields: { Experience: "5" },
|
|
});
|
|
|
|
expect(mockAttributeOptionRepository.createMany).toHaveBeenCalledWith({
|
|
createManyInput: [{ attributeId: "attr-1", value: "5", slug: "5" }],
|
|
});
|
|
});
|
|
|
|
it("should clear old assignments even when field value is empty", async () => {
|
|
mockMembershipRepository.findUniqueByUserIdAndTeamId.mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
teamId: 100,
|
|
accepted: true,
|
|
role: "MEMBER",
|
|
disableImpersonation: false,
|
|
});
|
|
|
|
mockAttributeRepository.findManyByIdsAndOrgIdWithOptions.mockResolvedValue([
|
|
{
|
|
id: "attr-1",
|
|
name: "Skills",
|
|
type: "MULTI_SELECT",
|
|
options: [{ id: "opt-1", value: "JavaScript", slug: "javascript" }],
|
|
},
|
|
]);
|
|
|
|
const syncFieldMappings: IFieldMapping[] = [
|
|
{ id: "mapping-1", integrationFieldName: "Skills", attributeId: "attr-1", enabled: true },
|
|
];
|
|
|
|
await service.syncIntegrationFieldsToAttributes({
|
|
...baseParams,
|
|
syncFieldMappings,
|
|
integrationFields: { Skills: "" },
|
|
});
|
|
|
|
expect(mockAttributeToUserRepository.deleteMany).toHaveBeenCalledWith({
|
|
memberId: 1,
|
|
attributeOption: {
|
|
attributeId: { in: ["attr-1"] },
|
|
},
|
|
});
|
|
|
|
expect(mockAttributeToUserRepository.createManySkipDuplicates).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
});
|