Files
calendar/apps/web/lib/apps/routing-forms/[...pages]/getServerSidePropsSingleForm.ts
T

126 lines
2.9 KiB
TypeScript

import { enrichFormWithMigrationData } from "@calcom/app-store/routing-forms/enrichFormWithMigrationData";
import { getSerializableForm } from "@calcom/app-store/routing-forms/lib/getSerializableForm";
import { getRoutingFormPermissions } from "@calcom/features/pbac/lib/resource-permissions";
import type { AppGetServerSidePropsContext, AppPrisma, AppUser } from "@calcom/types/AppGetServerSideProps";
export const getServerSidePropsForSingleFormView = async function getServerSidePropsForSingleFormView(
context: AppGetServerSidePropsContext,
prisma: AppPrisma,
user: AppUser
) {
if (!user) {
return {
redirect: {
permanent: false,
destination: "/auth/login",
},
};
}
const { params } = context;
if (!params) {
return {
notFound: true,
};
}
const appPages = params.pages.slice(1);
const formId = appPages[0];
if (!formId || appPages.length > 1) {
return {
notFound: true,
};
}
const form = await prisma.app_RoutingForms_Form.findUnique({
where: {
id: formId,
},
include: {
user: {
select: {
id: true,
movedToProfileId: true,
organization: {
select: {
slug: true,
},
},
username: true,
theme: true,
brandColor: true,
darkBrandColor: true,
metadata: true,
},
},
team: {
select: {
slug: true,
name: true,
parent: {
select: { slug: true },
},
parentId: true,
metadata: true,
},
},
_count: {
select: {
responses: true,
},
},
},
});
if (!form) {
return {
notFound: true,
};
}
const { user: _u, ...formWithoutUser } = form;
const formWithoutProfileInfo = {
...formWithoutUser,
team: form.team
? {
slug: form.team.slug,
name: form.team.name,
}
: null,
};
const { UserRepository } = await import("@calcom/features/users/repositories/UserRepository");
const userRepo = new UserRepository(prisma);
const formWithUserInfoProfile = {
...form,
user: await userRepo.enrichUserWithItsProfile({ user: form.user }),
};
const permissions = await getRoutingFormPermissions({
userId: user.id,
formUserId: form.userId,
formTeamId: form.teamId,
formTeamParentId: form.team?.parentId ?? null,
});
if (!permissions) {
return {
notFound: true,
};
}
return {
props: {
form: await getSerializableForm({ form: formWithoutProfileInfo }),
enrichedWithUserProfileForm: await getSerializableForm({
form: enrichFormWithMigrationData(formWithUserInfoProfile),
}),
permissions: {
canCreate: permissions.canCreate,
canRead: permissions.canRead,
canEdit: permissions.canEdit,
canDelete: permissions.canDelete,
},
},
};
};