2023-06-15 08:58:07 +00:00
|
|
|
import { hasFilter } from "@calcom/features/filters/lib/hasFilter";
|
|
|
|
import { entityPrismaWhereClause, canEditEntity } from "@calcom/lib/entityPermissionUtils";
|
|
|
|
import logger from "@calcom/lib/logger";
|
2023-08-24 09:14:10 +00:00
|
|
|
import type { PrismaClient } from "@calcom/prisma";
|
|
|
|
import type { Prisma } from "@calcom/prisma/client";
|
2023-06-15 08:58:07 +00:00
|
|
|
import { entries } from "@calcom/prisma/zod-utils";
|
2023-05-09 19:27:05 +00:00
|
|
|
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
|
|
|
|
|
|
|
import { getSerializableForm } from "../lib/getSerializableForm";
|
2023-06-15 08:58:07 +00:00
|
|
|
import type { TFormSchema } from "./forms.schema";
|
2023-05-09 19:27:05 +00:00
|
|
|
|
|
|
|
interface FormsHandlerOptions {
|
|
|
|
ctx: {
|
|
|
|
prisma: PrismaClient;
|
|
|
|
user: NonNullable<TrpcSessionUser>;
|
|
|
|
};
|
2023-06-15 08:58:07 +00:00
|
|
|
input: TFormSchema;
|
2023-05-09 19:27:05 +00:00
|
|
|
}
|
2023-06-15 08:58:07 +00:00
|
|
|
const log = logger.getChildLogger({ prefix: ["[formsHandler]"] });
|
2023-05-09 19:27:05 +00:00
|
|
|
|
2023-06-15 08:58:07 +00:00
|
|
|
export const formsHandler = async ({ ctx, input }: FormsHandlerOptions) => {
|
2023-05-09 19:27:05 +00:00
|
|
|
const { prisma, user } = ctx;
|
2023-06-15 08:58:07 +00:00
|
|
|
|
|
|
|
const where = getPrismaWhereFromFilters(user, input?.filters);
|
|
|
|
log.debug("Getting forms where", JSON.stringify(where));
|
|
|
|
|
2023-05-09 19:27:05 +00:00
|
|
|
const forms = await prisma.app_RoutingForms_Form.findMany({
|
2023-06-15 08:58:07 +00:00
|
|
|
where,
|
2023-05-09 19:27:05 +00:00
|
|
|
orderBy: {
|
|
|
|
createdAt: "desc",
|
|
|
|
},
|
|
|
|
include: {
|
2023-06-15 08:58:07 +00:00
|
|
|
team: {
|
|
|
|
include: {
|
|
|
|
members: true,
|
|
|
|
},
|
|
|
|
},
|
2023-05-09 19:27:05 +00:00
|
|
|
_count: {
|
|
|
|
select: {
|
|
|
|
responses: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-06-15 08:58:07 +00:00
|
|
|
const totalForms = await prisma.app_RoutingForms_Form.count({
|
|
|
|
where: entityPrismaWhereClause({
|
|
|
|
userId: user.id,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2023-05-09 19:27:05 +00:00
|
|
|
const serializableForms = [];
|
|
|
|
for (let i = 0; i < forms.length; i++) {
|
2023-06-15 08:58:07 +00:00
|
|
|
const form = forms[i];
|
|
|
|
const hasWriteAccess = canEditEntity(form, user.id);
|
|
|
|
serializableForms.push({
|
|
|
|
form: await getSerializableForm({ form: forms[i] }),
|
|
|
|
readOnly: !hasWriteAccess,
|
|
|
|
});
|
2023-05-09 19:27:05 +00:00
|
|
|
}
|
2023-06-15 08:58:07 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
filtered: serializableForms,
|
|
|
|
totalCount: totalForms,
|
|
|
|
};
|
2023-05-09 19:27:05 +00:00
|
|
|
};
|
2023-05-12 12:01:05 +00:00
|
|
|
|
|
|
|
export default formsHandler;
|
2023-06-15 08:58:07 +00:00
|
|
|
export function getPrismaWhereFromFilters(
|
|
|
|
user: {
|
|
|
|
id: number;
|
|
|
|
},
|
|
|
|
filters: NonNullable<TFormSchema>["filters"]
|
|
|
|
) {
|
|
|
|
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;
|
|
|
|
}
|