Files
calendar/packages/trpc/server/routers/loggedInViewer/bookingUnconfirmedCount.handler.ts
T
53748eb380 add an enum generator, stop importing from @prisma/client (#8548)
* add an enum generator and start importing from it

* keep moving imports

* fix remaining

* Header simplified

* Removed generated file from repo

* Updated .gitignore to exclude enums directory

* Add eslint rule to check for @prisma/client Prisma enum import

* Added another enum import + exclude PrismaClient

---------

Co-authored-by: Alex van Andel <me@alexvanandel.com>
2023-05-02 13:44:05 +02:00

38 lines
1.1 KiB
TypeScript

import { prisma } from "@calcom/prisma";
import { BookingStatus } from "@calcom/prisma/enums";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
type BookingUnconfirmedCountOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
};
export const bookingUnconfirmedCountHandler = async ({ ctx }: BookingUnconfirmedCountOptions) => {
const { user } = ctx;
const count = await prisma.booking.count({
where: {
status: BookingStatus.PENDING,
userId: user.id,
endTime: { gt: new Date() },
},
});
const recurringGrouping = await prisma.booking.groupBy({
by: ["recurringEventId"],
_count: {
recurringEventId: true,
},
where: {
recurringEventId: { not: { equals: null } },
status: { equals: "PENDING" },
userId: user.id,
endTime: { gt: new Date() },
},
});
return recurringGrouping.reduce((prev, current) => {
// recurringEventId is the total number of recurring instances for a booking
// we need to subtract all but one, to represent a single recurring booking
return prev - (current._count?.recurringEventId - 1);
}, count);
};