2023-09-30 01:52:32 -03:00
|
|
|
import { PrismockClient } from "prismock";
|
2023-05-24 20:35:44 -03:00
|
|
|
import { beforeEach, vi } from "vitest";
|
|
|
|
|
2023-09-30 01:52:32 -03:00
|
|
|
import logger from "@calcom/lib/logger";
|
|
|
|
import * as selects from "@calcom/prisma/selects";
|
2023-08-24 06:14:10 -03:00
|
|
|
|
2023-05-24 20:35:44 -03:00
|
|
|
vi.mock("@calcom/prisma", () => ({
|
|
|
|
default: prisma,
|
2023-09-06 16:23:53 -03:00
|
|
|
prisma,
|
2023-09-30 01:52:32 -03:00
|
|
|
...selects,
|
2023-05-24 20:35:44 -03:00
|
|
|
}));
|
|
|
|
|
2023-09-30 01:52:32 -03:00
|
|
|
const handlePrismockBugs = () => {
|
|
|
|
const __updateBooking = prismock.booking.update;
|
2023-11-14 17:23:44 -03:00
|
|
|
const __findFirstOrThrowBooking = prismock.booking.findFirstOrThrow;
|
2023-09-30 01:52:32 -03:00
|
|
|
const __findManyWebhook = prismock.webhook.findMany;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
prismock.booking.update = (...rest: any[]) => {
|
|
|
|
// There is a bug in prismock where it considers `createMany` and `create` itself to have the data directly
|
|
|
|
// In booking flows, we encounter such scenario, so let's fix that here directly till it's fixed in prismock
|
|
|
|
if (rest[0].data.references?.createMany) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
rest[0].data.references.createMany = rest[0].data.references?.createMany.data;
|
|
|
|
logger.silly("Fixed Prismock bug");
|
|
|
|
}
|
|
|
|
if (rest[0].data.references?.create) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
rest[0].data.references.create = rest[0].data.references?.create.data;
|
|
|
|
logger.silly("Fixed Prismock bug-1");
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
return __updateBooking(...rest);
|
|
|
|
};
|
|
|
|
|
2023-11-14 17:23:44 -03:00
|
|
|
prismock.booking.findFirstOrThrow = (...rest: any[]) => {
|
|
|
|
const { where } = rest[0];
|
|
|
|
delete where.NOT;
|
|
|
|
logger.silly("Fixed Prismock bug with using NOT in where clause");
|
|
|
|
|
|
|
|
return __findFirstOrThrowBooking(...rest);
|
|
|
|
};
|
|
|
|
|
2023-09-30 01:52:32 -03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
prismock.webhook.findMany = (...rest: any[]) => {
|
|
|
|
// There is some bug in prismock where it can't handle complex where clauses
|
|
|
|
if (rest[0].where?.OR && rest[0].where.AND) {
|
|
|
|
rest[0].where = undefined;
|
|
|
|
logger.silly("Fixed Prismock bug-2");
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
return __findManyWebhook(...rest);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-05-24 20:35:44 -03:00
|
|
|
beforeEach(() => {
|
2023-09-30 01:52:32 -03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
prismock.reset();
|
|
|
|
handlePrismockBugs();
|
2023-05-24 20:35:44 -03:00
|
|
|
});
|
|
|
|
|
2023-09-30 01:52:32 -03:00
|
|
|
const prismock = new PrismockClient();
|
|
|
|
|
|
|
|
const prisma = prismock;
|
2023-05-24 20:35:44 -03:00
|
|
|
export default prisma;
|