Files
calendar/packages/features/ee/workflows/lib/getWorkflowReminders.ts
T
devin-ai-integration[bot]GitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>alex@cal.com <me@alexvanandel.com>
79b6278883 refactor: replace Prisma.validator<Select>() with satisfies syntax (#22270)
* refactor: replace Prisma.validator<Select>() with satisfies syntax

- Convert all Prisma.validator<Prisma.SomeSelect>()({...}) patterns to {...} satisfies Prisma.SomeSelect
- Update import { Prisma } to import type { Prisma } where only used for types
- Maintain existing functionality while modernizing TypeScript syntax
- Covers 89+ files across packages/prisma/selects, repository classes, tRPC handlers, and API modules

Co-Authored-By: alex@cal.com <me@alexvanandel.com>

* refactor: complete remaining Prisma.validator conversions

- Update test fixture files with satisfies syntax
- Apply lint-staged formatting fixes
- Complete refactoring of all remaining files

Co-Authored-By: alex@cal.com <me@alexvanandel.com>

* revert: remove unintended platform library update

- Revert yarn.lock changes that updated @calcom/platform-libraries from 0.0.236 to 0.0.239
- This was an unintended side effect of the refactoring process
- Keep only the intended Prisma.validator → satisfies syntax changes

Co-Authored-By: alex@cal.com <me@alexvanandel.com>

* fix: update ESLint plugin references to correct package name

- Change @calcom/eslint to @calcom/eslint-plugin-eslint in eslint-preset.js
- Resolves 'Failed to load plugin @calcom/eslint' error causing CI failures

Co-Authored-By: alex@cal.com <me@alexvanandel.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: alex@cal.com <me@alexvanandel.com>
2025-07-04 21:08:05 +01:00

218 lines
5.3 KiB
TypeScript

import dayjs from "@calcom/dayjs";
import prisma from "@calcom/prisma";
import type { EventType, User, WorkflowReminder, WorkflowStep, Prisma } from "@calcom/prisma/client";
import { WorkflowMethods } from "@calcom/prisma/enums";
type PartialWorkflowStep =
| (Partial<WorkflowStep> & { workflow: { userId?: number; teamId?: number } })
| null;
type Booking = Prisma.BookingGetPayload<{
include: {
attendees: true;
};
}>;
type PartialBooking =
| (Pick<
Booking,
| "startTime"
| "endTime"
| "location"
| "description"
| "metadata"
| "customInputs"
| "responses"
| "uid"
| "attendees"
| "userPrimaryEmail"
| "smsReminderNumber"
| "title"
> & {
eventType:
| (Partial<EventType> & {
slug: string;
team: { parentId?: number; hideBranding: boolean };
hosts: { user: { email: string; destinationCalendar?: { primaryEmail: string } } }[] | undefined;
})
| null;
} & {
user: Partial<User> | null;
})
| null;
export type PartialWorkflowReminder = Pick<
WorkflowReminder,
"id" | "isMandatoryReminder" | "scheduledDate" | "uuid"
> & {
booking: PartialBooking | null;
} & { workflowStep: PartialWorkflowStep };
async function getWorkflowReminders<T extends Prisma.WorkflowReminderSelect>(
filter: Prisma.WorkflowReminderWhereInput,
select: T
): Promise<Array<Prisma.WorkflowReminderGetPayload<{ select: T }>>> {
const pageSize = 90;
let pageNumber = 0;
const filteredWorkflowReminders: Array<Prisma.WorkflowReminderGetPayload<{ select: T }>> = [];
while (true) {
const newFilteredWorkflowReminders = await prisma.workflowReminder.findMany({
where: filter,
select: select,
skip: pageNumber * pageSize,
take: pageSize,
});
if (newFilteredWorkflowReminders.length === 0) {
break;
}
filteredWorkflowReminders.push(
...(newFilteredWorkflowReminders as Array<Prisma.WorkflowReminderGetPayload<{ select: T }>>)
);
pageNumber++;
}
return filteredWorkflowReminders;
}
type RemindersToDeleteType = { referenceId: string | null; id: number };
export async function getAllRemindersToDelete(): Promise<RemindersToDeleteType[]> {
const whereFilter: Prisma.WorkflowReminderWhereInput = {
method: WorkflowMethods.EMAIL,
cancelled: true,
referenceId: {
not: null,
},
scheduledDate: {
lt: dayjs().toISOString(),
},
};
const select = {
referenceId: true,
id: true,
} satisfies Prisma.WorkflowReminderSelect;
const remindersToDelete = await getWorkflowReminders(whereFilter, select);
return remindersToDelete;
}
type RemindersToCancelType = { referenceId: string | null; id: number };
export async function getAllRemindersToCancel(): Promise<RemindersToCancelType[]> {
const whereFilter: Prisma.WorkflowReminderWhereInput = {
cancelled: true,
scheduled: true, //if it is false then they are already cancelled
scheduledDate: {
lte: dayjs().add(1, "hour").toISOString(),
},
};
const select = {
referenceId: true,
id: true,
} satisfies Prisma.WorkflowReminderSelect;
const remindersToCancel = await getWorkflowReminders(whereFilter, select);
return remindersToCancel;
}
export const select = {
id: true,
scheduledDate: true,
isMandatoryReminder: true,
uuid: true,
workflowStep: {
select: {
action: true,
sendTo: true,
reminderBody: true,
emailSubject: true,
template: true,
sender: true,
includeCalendarEvent: true,
workflow: {
select: {
userId: true,
teamId: true,
},
},
},
},
booking: {
select: {
startTime: true,
endTime: true,
location: true,
description: true,
smsReminderNumber: true,
userPrimaryEmail: true,
user: {
select: {
email: true,
name: true,
timeZone: true,
locale: true,
username: true,
timeFormat: true,
hideBranding: true,
},
},
metadata: true,
uid: true,
customInputs: true,
responses: true,
attendees: true,
eventType: {
select: {
bookingFields: true,
title: true,
slug: true,
hosts: {
select: {
user: {
select: {
email: true,
destinationCalendar: {
select: {
primaryEmail: true,
},
},
},
},
},
},
recurringEvent: true,
team: {
select: {
parentId: true,
hideBranding: true,
},
},
customReplyToEmail: true,
},
},
},
},
} satisfies Prisma.WorkflowReminderSelect;
export async function getAllUnscheduledReminders(): Promise<PartialWorkflowReminder[]> {
const whereFilter: Prisma.WorkflowReminderWhereInput = {
method: WorkflowMethods.EMAIL,
scheduled: false,
scheduledDate: {
lte: dayjs().add(2, "hour").toISOString(),
},
OR: [{ cancelled: false }, { cancelled: null }],
};
const unscheduledReminders = (await getWorkflowReminders(whereFilter, select)) as PartialWorkflowReminder[];
return unscheduledReminders;
}