Files
calendar/packages/trpc/server/routers/viewer/workflows/workflowOrder.handler.ts
T
devin-ai-integration[bot]GitHubalex@cal.com <me@alexvanandel.com>Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>alex@cal.com <me@alexvanandel.com>
79b6278883 refactor: replace Prisma.validator<Select>() with satisfies syntax (#22270)
* refactor: replace Prisma.validator<Select>() with satisfies syntax

- Convert all Prisma.validator<Prisma.SomeSelect>()({...}) patterns to {...} satisfies Prisma.SomeSelect
- Update import { Prisma } to import type { Prisma } where only used for types
- Maintain existing functionality while modernizing TypeScript syntax
- Covers 89+ files across packages/prisma/selects, repository classes, tRPC handlers, and API modules

Co-Authored-By: alex@cal.com <me@alexvanandel.com>

* refactor: complete remaining Prisma.validator conversions

- Update test fixture files with satisfies syntax
- Apply lint-staged formatting fixes
- Complete refactoring of all remaining files

Co-Authored-By: alex@cal.com <me@alexvanandel.com>

* revert: remove unintended platform library update

- Revert yarn.lock changes that updated @calcom/platform-libraries from 0.0.236 to 0.0.239
- This was an unintended side effect of the refactoring process
- Keep only the intended Prisma.validator → satisfies syntax changes

Co-Authored-By: alex@cal.com <me@alexvanandel.com>

* fix: update ESLint plugin references to correct package name

- Change @calcom/eslint to @calcom/eslint-plugin-eslint in eslint-preset.js
- Resolves 'Failed to load plugin @calcom/eslint' error causing CI failures

Co-Authored-By: alex@cal.com <me@alexvanandel.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: alex@cal.com <me@alexvanandel.com>
2025-07-04 21:08:05 +01:00

178 lines
3.8 KiB
TypeScript

import type { TFormSchema } from "@calcom/app-store/routing-forms/trpc/forms.schema";
import { hasFilter } from "@calcom/features/filters/lib/hasFilter";
import { prisma } from "@calcom/prisma";
import type { Prisma } from "@calcom/prisma/client";
import { entries } from "@calcom/prisma/zod-utils";
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
import { TRPCError } from "@trpc/server";
import type { TWorkflowOrderInputSchema } from "./workflowOrder.schema";
type RoutingFormOrderOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: TWorkflowOrderInputSchema;
};
export const workflowOrderHandler = async ({ ctx, input }: RoutingFormOrderOptions) => {
const { user } = ctx;
const { include: includedFields } = {
include: {
activeOn: {
select: {
eventType: {
select: {
id: true,
title: true,
parentId: true,
_count: {
select: {
children: true,
},
},
},
},
},
},
steps: true,
team: {
select: {
id: true,
slug: true,
name: true,
members: true,
logoUrl: true,
},
},
},
} satisfies Prisma.WorkflowDefaultArgs;
const allWorkflows = await prisma.workflow.findMany({
where: {
OR: [
{
userId: user.id,
},
{
team: {
members: {
some: {
userId: user.id,
accepted: true,
},
},
},
},
],
},
include: includedFields,
orderBy: [
{
position: "desc",
},
{
id: "asc",
},
],
});
const allWorkflowIds = new Set(allWorkflows.map((workflow) => workflow.id));
if (input.ids.some((id) => !allWorkflowIds.has(id))) {
throw new TRPCError({
code: "UNAUTHORIZED",
});
}
await Promise.all(
input.ids.reverse().map((id, position) => {
return prisma.workflow.update({
where: {
id: id,
},
data: {
position,
},
});
})
);
};
type SupportedFilters = Omit<NonNullable<NonNullable<TFormSchema>["filters"]>, "upIds"> | undefined;
export function getPrismaWhereFromFilters(
user: {
id: number;
},
filters: SupportedFilters
) {
const where = {
OR: [] as Prisma.App_RoutingForms_FormWhereInput[],
};
const prismaQueries: Record<
keyof NonNullable<typeof filters>,
(...args: [number[]]) => Prisma.App_RoutingForms_FormWhereInput
> & {
all: () => Prisma.App_RoutingForms_FormWhereInput;
} = {
userIds: (userIds: number[]) => ({
userId: {
in: userIds,
},
teamId: null,
}),
teamIds: (teamIds: number[]) => ({
team: {
id: {
in: teamIds ?? [],
},
members: {
some: {
userId: user.id,
accepted: true,
},
},
},
}),
all: () => ({
OR: [
{
userId: user.id,
},
{
team: {
members: {
some: {
userId: user.id,
accepted: true,
},
},
},
},
],
}),
};
if (!filters || !hasFilter(filters)) {
where.OR.push(prismaQueries.all());
} else {
for (const entry of entries(filters)) {
if (!entry) {
continue;
}
const [filterName, filter] = entry;
const getPrismaQuery = prismaQueries[filterName];
// filter might be accidentally set undefined as well
if (!getPrismaQuery || !filter) {
continue;
}
where.OR.push(getPrismaQuery(filter));
}
}
return where;
}