2023-05-09 19:27:05 +00:00
|
|
|
import logger from "@calcom/lib/logger";
|
2023-08-24 09:14:10 +00:00
|
|
|
import type { PrismaClient } from "@calcom/prisma";
|
2023-05-09 19:27:05 +00:00
|
|
|
import { TRPCError } from "@calcom/trpc/server";
|
|
|
|
|
|
|
|
import { jsonLogicToPrisma } from "../jsonLogicToPrisma";
|
|
|
|
import { getSerializableForm } from "../lib/getSerializableForm";
|
|
|
|
import type { Response } from "../types/types";
|
|
|
|
import type { TReportInputSchema } from "./report.schema";
|
|
|
|
|
|
|
|
interface ReportHandlerOptions {
|
|
|
|
ctx: {
|
|
|
|
prisma: PrismaClient;
|
|
|
|
};
|
|
|
|
input: TReportInputSchema;
|
|
|
|
}
|
|
|
|
export const reportHandler = async ({ ctx: { prisma }, input }: ReportHandlerOptions) => {
|
|
|
|
// Can be any prisma `where` clause
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const prismaWhere: Record<string, any> = input.jsonLogicQuery
|
|
|
|
? jsonLogicToPrisma(input.jsonLogicQuery)
|
|
|
|
: {};
|
|
|
|
const skip = input.cursor ?? 0;
|
|
|
|
const take = 50;
|
|
|
|
logger.debug(
|
|
|
|
`Built Prisma where ${JSON.stringify(prismaWhere)} from jsonLogicQuery ${JSON.stringify(
|
|
|
|
input.jsonLogicQuery
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
const form = await prisma.app_RoutingForms_Form.findUnique({
|
|
|
|
where: {
|
|
|
|
id: input.formId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!form) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "NOT_FOUND",
|
|
|
|
message: "Form not found",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// TODO: Second argument is required to return deleted operators.
|
2023-06-15 08:58:07 +00:00
|
|
|
const serializedForm = await getSerializableForm({ form, withDeletedFields: true });
|
2023-05-09 19:27:05 +00:00
|
|
|
|
|
|
|
const rows = await prisma.app_RoutingForms_FormResponse.findMany({
|
|
|
|
where: {
|
|
|
|
formId: input.formId,
|
|
|
|
...prismaWhere,
|
|
|
|
},
|
|
|
|
take,
|
|
|
|
skip,
|
|
|
|
});
|
|
|
|
const fields = serializedForm?.fields || [];
|
|
|
|
const headers = fields.map((f) => f.label + (f.deleted ? "(Deleted)" : ""));
|
2023-06-01 20:29:13 +00:00
|
|
|
const responses: (string | number)[][] = [];
|
2023-05-09 19:27:05 +00:00
|
|
|
rows.forEach((r) => {
|
2023-06-01 20:29:13 +00:00
|
|
|
const rowResponses: (string | number)[] = [];
|
2023-05-09 19:27:05 +00:00
|
|
|
responses.push(rowResponses);
|
|
|
|
fields.forEach((field) => {
|
|
|
|
if (!r.response) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const response = r.response as Response;
|
|
|
|
const value = response[field.id]?.value || "";
|
2023-06-01 20:29:13 +00:00
|
|
|
let transformedValue;
|
2023-05-09 19:27:05 +00:00
|
|
|
if (value instanceof Array) {
|
2023-06-01 20:29:13 +00:00
|
|
|
transformedValue = value.join(", ");
|
2023-05-09 19:27:05 +00:00
|
|
|
} else {
|
2023-06-01 20:29:13 +00:00
|
|
|
transformedValue = value;
|
2023-05-09 19:27:05 +00:00
|
|
|
}
|
2023-06-01 20:29:13 +00:00
|
|
|
rowResponses.push(transformedValue);
|
2023-05-09 19:27:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
const areThereNoResultsOrLessThanAskedFor = !rows.length || rows.length < take;
|
|
|
|
return {
|
|
|
|
headers,
|
|
|
|
responses,
|
|
|
|
nextCursor: areThereNoResultsOrLessThanAskedFor ? null : skip + rows.length,
|
|
|
|
};
|
|
|
|
};
|
2023-05-12 12:01:05 +00:00
|
|
|
|
|
|
|
export default reportHandler;
|