* feat: Enhance SCIM user attribute handling and sync process - Introduced a new PR_TODO.md file to track ongoing tasks and fixes. - Updated `getAttributesFromScimPayload` to accept a directoryId and ignore core user attributes during syncing. - Modified `handleUserEvents` to pass directoryId when syncing custom attributes. - Improved attribute creation logic to handle unique options and added support for optional isGroup property in attribute options. - Added utility function `getOptionsWithValidContains` to ensure valid sub-options in attribute options. - Updated tests to reflect changes in attribute handling and ensure proper functionality. This commit addresses issues with user attribute syncing and enhances the overall attribute management process. * test: Add unit tests for getOptionsWithValidContains utility function - Introduced a new test file for the getOptionsWithValidContains function. - Added tests to verify the removal of duplicate options, initialization of empty contains arrays, filtering of non-existent sub-options, and handling of empty options arrays. - Ensured comprehensive coverage of the utility's functionality to enhance reliability and maintainability.
72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import slugify from "@calcom/lib/slugify";
|
|
import prisma from "@calcom/prisma";
|
|
import type { Attribute } from "@calcom/prisma/client";
|
|
import { Prisma } from "@calcom/prisma/client";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "../../../trpc";
|
|
import type { ZCreateAttributeSchema } from "./create.schema";
|
|
import { getOptionsWithValidContains } from "./utils";
|
|
|
|
type GetOptions = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: ZCreateAttributeSchema;
|
|
};
|
|
|
|
const typesWithOptions = ["SINGLE_SELECT", "MULTI_SELECT"];
|
|
|
|
const createAttributesHandler = async ({ input, ctx }: GetOptions) => {
|
|
const org = ctx.user.organization;
|
|
|
|
if (!org.id) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
message: "You need to be apart of an organization to use this feature",
|
|
});
|
|
}
|
|
|
|
const slug = slugify(input.name);
|
|
const uniqueOptions = getOptionsWithValidContains(input.options);
|
|
|
|
const typeHasOptions = typesWithOptions.includes(input.type);
|
|
|
|
let attributes: Attribute;
|
|
try {
|
|
attributes = await prisma.attribute.create({
|
|
data: {
|
|
slug,
|
|
name: input.name,
|
|
type: input.type,
|
|
isLocked: input.isLocked,
|
|
teamId: org.id,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
|
|
throw new Error("A record with that name already exists. Please choose another one.");
|
|
} else {
|
|
throw error; // Re-throw the error if it's not a unique constraint violation
|
|
}
|
|
}
|
|
|
|
// Only assign options for the types that have options
|
|
// TEXT/NUMBER don't have options
|
|
if (typeHasOptions) {
|
|
await prisma.attributeOption.createMany({
|
|
data: uniqueOptions.map(({ value, isGroup }) => ({
|
|
attributeId: attributes.id,
|
|
value,
|
|
slug: slugify(value),
|
|
isGroup,
|
|
})),
|
|
});
|
|
}
|
|
|
|
return attributes;
|
|
};
|
|
|
|
export default createAttributesHandler;
|