Save attendee reminders to db

docs/contribute-to-app-store
Joe Au-Yeung 2022-03-05 23:33:34 -05:00
parent ddf9323908
commit 52b9e1b809
3 changed files with 118 additions and 4 deletions

View File

@ -84,7 +84,7 @@ const AttendeeReminderTypeForm: FC<Props> = (props) => {
className="focus:border-primary-500 focus:ring-primary-500 block w-12 rounded-sm border-gray-300 shadow-sm [appearance:textfield] ltr:mr-2 rtl:ml-2 sm:text-sm"
placeholder="30"
defaultValue={selectedAttendeeReminder?.time}
{...register("time", { required: true })}
{...register("time", { required: true, valueAsNumber: true })}
/>
<Controller
name="timeUnit"

View File

@ -1,9 +1,16 @@
import { EventTypeCustomInput, MembershipRole, PeriodType, Prisma } from "@prisma/client";
import {
EventTypeCustomInput,
MembershipRole,
EventTypeAttendeeReminder,
PeriodType,
Prisma,
} from "@prisma/client";
import { z } from "zod";
import {
_AvailabilityModel,
_DestinationCalendarModel,
_EventTypeAttendeeReminderModel,
_EventTypeCustomInputModel,
_EventTypeModel,
} from "@calcom/prisma/zod";
@ -63,6 +70,42 @@ function handleCustomInputs(customInputs: EventTypeCustomInput[], eventTypeId: n
};
}
function handleAttendeeReminders(attendeeReminders: EventTypeAttendeeReminder[], eventTypeId: number) {
const remindersToDelete = attendeeReminders.filter((input) => input.id > 0).map((e) => e.id);
const remindersToCreate = attendeeReminders
.filter((input) => input.id < 0)
.map((input) => ({
method: input.method,
timeUnit: input.timeUnit,
time: input.time,
}));
const remindersToUpdate = attendeeReminders
.filter((input) => input.id > 0)
.map((input) => ({
data: {
method: input.method,
timeUnit: input.timeUnit,
time: input.time,
},
where: {
id: input.id,
},
}));
return {
deleteMany: {
eventTypeId,
NOT: {
id: { in: remindersToDelete },
},
},
createMany: {
data: remindersToCreate,
},
update: remindersToUpdate,
};
}
const AvailabilityInput = _AvailabilityModel.pick({
days: true,
startTime: true,
@ -84,6 +127,7 @@ const EventTypeUpdateInput = _EventTypeModel
externalId: true,
}),
users: z.array(stringOrNumber).optional(),
attendeeReminders: z.array(_EventTypeAttendeeReminderModel).optional(),
})
.partial()
.merge(
@ -190,8 +234,17 @@ export const eventTypesRouter = createProtectedRouter()
.mutation("update", {
input: EventTypeUpdateInput.strict(),
async resolve({ ctx, input }) {
const { availability, periodType, locations, destinationCalendar, customInputs, users, id, ...rest } =
input;
const {
availability,
periodType,
locations,
destinationCalendar,
customInputs,
attendeeReminders,
users,
id,
...rest
} = input;
const data: Prisma.EventTypeUpdateInput = rest;
data.locations = locations ?? undefined;
@ -211,6 +264,10 @@ export const eventTypesRouter = createProtectedRouter()
data.customInputs = handleCustomInputs(customInputs, id);
}
if (attendeeReminders) {
data.attendeeReminders = handleAttendeeReminders(attendeeReminders, id);
}
if (users) {
data.users = {
set: [],

View File

@ -0,0 +1,57 @@
-- CreateEnum
CREATE TYPE "EventTypeAttendeeReminderMethod" AS ENUM ('email', 'sms');
-- CreateTable
CREATE TABLE "EventTypeAttendeeReminder" (
"id" SERIAL NOT NULL,
"eventTypeId" INTEGER NOT NULL,
"method" TEXT NOT NULL,
"secondsBeforeEvent" INTEGER NOT NULL,
CONSTRAINT "EventTypeAttendeeReminder_pkey" PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE "EventTypeAttendeeReminder" ADD CONSTRAINT "EventTypeAttendeeReminder_eventTypeId_fkey" FOREIGN KEY ("eventTypeId") REFERENCES "EventType"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- RenameIndex
ALTER INDEX "Booking.uid_unique" RENAME TO "Booking_uid_key";
-- RenameIndex
ALTER INDEX "DailyEventReference_bookingId_unique" RENAME TO "DailyEventReference_bookingId_key";
-- RenameIndex
ALTER INDEX "DestinationCalendar.bookingId_unique" RENAME TO "DestinationCalendar_bookingId_key";
-- RenameIndex
ALTER INDEX "DestinationCalendar.eventTypeId_unique" RENAME TO "DestinationCalendar_eventTypeId_key";
-- RenameIndex
ALTER INDEX "DestinationCalendar.userId_unique" RENAME TO "DestinationCalendar_userId_key";
-- RenameIndex
ALTER INDEX "EventType.userId_slug_unique" RENAME TO "EventType_userId_slug_key";
-- RenameIndex
ALTER INDEX "Payment.externalId_unique" RENAME TO "Payment_externalId_key";
-- RenameIndex
ALTER INDEX "Payment.uid_unique" RENAME TO "Payment_uid_key";
-- RenameIndex
ALTER INDEX "Team.slug_unique" RENAME TO "Team_slug_key";
-- RenameIndex
ALTER INDEX "VerificationRequest.identifier_token_unique" RENAME TO "VerificationRequest_identifier_token_key";
-- RenameIndex
ALTER INDEX "VerificationRequest.token_unique" RENAME TO "VerificationRequest_token_key";
-- RenameIndex
ALTER INDEX "Webhook.id_unique" RENAME TO "Webhook_id_key";
-- RenameIndex
ALTER INDEX "users.email_unique" RENAME TO "users_email_key";
-- RenameIndex
ALTER INDEX "users.username_unique" RENAME TO "users_username_key";