Files
calendar/packages/trpc/server/routers/viewer/delegationCredential/add.handler.ts
T
Benny JooGitHubbenny@cal.com <sldisek783@gmail.com>benny@cal.com <sldisek783@gmail.com>benny@cal.com <sldisek783@gmail.com>benny@cal.com <sldisek783@gmail.com>Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
96bec9f7b9 refactor: Move repositories from @calcom/lib to @calcom/features domain folders (#27570)
* refactor: move repositories from lib to features domain folders

- Move HolidayRepository to features/holidays/repositories
- Move PrismaTrackingRepository to features/bookings/repositories
- Move PrismaBookingPaymentRepository to features/bookings/repositories
- Move PrismaRoutingFormResponseRepository to features/routing-forms/repositories
- Move PrismaAssignmentReasonRepository to features/assignment-reason/repositories
- Move VerificationTokenRepository to features/auth/repositories
- Move WorkspacePlatformRepository to features/workspace-platform/repositories
- Move DTO files to their respective feature domains
- Merge lib DestinationCalendarRepository into features version
- Merge lib SelectedCalendarRepository into features version
- Update all import paths across the codebase

This follows the vertical slice architecture pattern by organizing
repositories by domain rather than by technical layer.

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

* fix: update VerificationTokenService import path to new location

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

* fix: update test file imports to use new repository locations

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

* mv

* fix structure

* fix

* refactor: merge unit tests for SelectedCalendarRepository into single file

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

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-02-09 08:41:04 -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/features/workspace-platform/repositories/WorkspacePlatformRepository";
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);
}
}