Files
Benny JooandGitHub e91bf53d80 refactor: Remove circular deps between @calcom/lib and @calcom/features [2] (#24438)
* move SystemField to features

* migrate workflow service

* merge two tests for team repository

* update imports and migrate team repository

* migrate delegation credential repository

* migrate credential repository

* migrate entityPermissionUtils

* migrate hashedLink service and repository

* migrate membership service

* update imports

* remove file

* migrate buildEventUrlFromBooking

* migrate getAllUserBookings to features

* update imports

* update organizationMock

* migrate slots

* migrate date-ranges to schedules dir

* migrate getAggregatedAvailability

* fix

* refactor

* migrate useCreateEventType hook to features

* migrate assignValueToUser

* migrate validateUsername to auth features

* migrate system field back to lib

* migrate getLabelValueMapFromResponses back to lib

* update imports

* use relative path

* fix type checks

* fix

* fix

* fix tests

* update gh codeowners

* fix

* fix
2025-10-17 06:48:08 -03:00

65 lines
1.9 KiB
TypeScript

import type { z } from "zod";
import { DelegationCredentialRepository } from "@calcom/features/delegation-credentials/repositories/DelegationCredentialRepository";
import { WorkspacePlatformRepository } from "@calcom/lib/server/repository/workspacePlatform";
import { TRPCError } from "@trpc/server";
import type { DelegationCredentialCreateSchema } from "./schema";
import {
ensureDelegationCredentialNotAlreadyConfigured,
ensureNoServiceAccountKey,
handleDelegationCredentialError,
} from "./utils";
export default async function handler({
input,
ctx,
}: {
input: z.infer<typeof DelegationCredentialCreateSchema>;
ctx: { user: { id: number; organizationId: number | null } };
}) {
const { workspacePlatformSlug, domain, serviceAccountKey } = input;
const { user } = ctx;
const { organizationId } = user;
if (!organizationId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You must be part of an organization to add a delegation credential",
});
}
try {
const workspacePlatform = await WorkspacePlatformRepository.findBySlug({
slug: workspacePlatformSlug,
});
if (!workspacePlatform) {
throw new TRPCError({
code: "NOT_FOUND",
message: `Workspace platform ${workspacePlatformSlug} not found`,
});
}
await ensureDelegationCredentialNotAlreadyConfigured({
domain,
currentOrganizationId: organizationId,
delegationCredentialBeingUpdatedId: null,
});
const createdDelegation = await DelegationCredentialRepository.create({
workspacePlatformId: workspacePlatform.id,
domain,
// We don't want to enable by default because enabling requires some checks to be completed and it has a separate flow.
enabled: false,
organizationId,
serviceAccountKey,
});
return ensureNoServiceAccountKey(createdDelegation);
} catch (error) {
handleDelegationCredentialError(error);
}
}