Files
cal.diy/packages/lib/server/repository/selectedSlots.ts
T

113 lines
2.4 KiB
TypeScript

import type { Prisma } from "@prisma/client";
import { prisma } from "@calcom/prisma";
type WhereCondition = {
slotUtcStartDate?: Date | string;
slotUtcEndDate?: Date | string;
eventTypeId?: number;
uid?: string | { not: string };
releaseAt?: { gt: Date };
};
export type FindManyArgs = {
where?: WhereCondition & {
OR?: WhereCondition[];
};
select?: Prisma.SelectedSlotsSelect;
};
export type TimeSlot = {
utcStartIso: string;
utcEndIso: string;
};
export class SelectedSlotsRepository {
static async findMany({ where, select }: FindManyArgs) {
return await prisma.selectedSlots.findMany({ where, select });
}
static async findFirst({ where }: { where: Prisma.SelectedSlotsWhereInput }) {
return await prisma.selectedSlots.findFirst({
where,
});
}
static async findReservedByOthers({
slot,
eventTypeId,
uid,
}: {
slot: TimeSlot;
eventTypeId: number;
uid: string;
}) {
return await this.findFirst({
where: {
slotUtcStartDate: slot.utcStartIso,
slotUtcEndDate: slot.utcEndIso,
eventTypeId,
uid: { not: uid },
releaseAt: { gt: new Date() },
},
});
}
static async findManyReservedByOthers(slots: TimeSlot[], eventTypeId: number, uid: string) {
return await this.findMany({
where: {
OR: slots.map((slot) => ({
slotUtcStartDate: slot.utcStartIso,
slotUtcEndDate: slot.utcEndIso,
eventTypeId,
uid: { not: uid },
releaseAt: { gt: new Date() },
})),
},
select: {
slotUtcStartDate: true,
slotUtcEndDate: true,
},
});
}
static async findManyUnexpiredSlots({
userIds,
currentTimeInUtc,
}: {
userIds: number[];
currentTimeInUtc: string;
}) {
return prisma.selectedSlots.findMany({
where: {
userId: { in: userIds },
releaseAt: { gt: currentTimeInUtc },
},
select: {
id: true,
slotUtcStartDate: true,
slotUtcEndDate: true,
userId: true,
isSeat: true,
eventTypeId: true,
uid: true,
},
});
}
static async deleteManyExpiredSlots({
eventTypeId,
currentTimeInUtc,
}: {
eventTypeId: number;
currentTimeInUtc: string;
}) {
return prisma.selectedSlots.deleteMany({
where: {
eventTypeId: { equals: eventTypeId },
releaseAt: { lt: currentTimeInUtc },
},
});
}
}