feat: update event types for default availability change (#13964)
* feat: update event types for default availability change * chore: add toast --------- Co-authored-by: sean-brydon <[email protected]> Co-authored-by: sean-brydon <[email protected]> Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
6 files changed
+111
-13
No files matched your search
+8
-10
@@ -10,7 +10,12 @@ export const BulkUpdateEventSchema = z.object({
|
||||
eventTypeIds: z.array(z.number()),
|
||||
});
|
||||
|
||||
export function BulkEditDefaultConferencingModal(props: { open: boolean; setOpen: (open: boolean) => void }) {
|
||||
export function BulkEditDefaultForEventsModal(props: {
|
||||
open: boolean;
|
||||
setOpen: (open: boolean) => void;
|
||||
bulkUpdateFunction: ({ eventTypeIds }: { eventTypeIds: number[] }) => void;
|
||||
isPending: boolean;
|
||||
}) {
|
||||
const { t } = useLocale();
|
||||
const utils = trpc.useContext();
|
||||
const { data, isFetching } = trpc.viewer.eventTypes.bulkEventFetch.useQuery();
|
||||
@@ -21,13 +26,6 @@ export function BulkEditDefaultConferencingModal(props: { open: boolean; setOpen
|
||||
},
|
||||
});
|
||||
|
||||
const updateLocationsMutation = trpc.viewer.eventTypes.bulkUpdateToDefaultLocation.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.viewer.getUsersDefaultConferencingApp.invalidate();
|
||||
props.setOpen(false);
|
||||
},
|
||||
});
|
||||
|
||||
const eventTypesSelected = form.watch("eventTypeIds");
|
||||
|
||||
if (isFetching || !open || !data?.eventTypes) return null;
|
||||
@@ -42,7 +40,7 @@ export function BulkEditDefaultConferencingModal(props: { open: boolean; setOpen
|
||||
<Form
|
||||
form={form}
|
||||
handleSubmit={(values) => {
|
||||
updateLocationsMutation.mutate(values);
|
||||
props.bulkUpdateFunction(values);
|
||||
}}>
|
||||
<div className="flex flex-col space-y-2">
|
||||
{data.eventTypes.length > 0 && (
|
||||
@@ -81,7 +79,7 @@ export function BulkEditDefaultConferencingModal(props: { open: boolean; setOpen
|
||||
utils.viewer.getUsersDefaultConferencingApp.invalidate();
|
||||
}}
|
||||
/>
|
||||
<Button type="submit" color="primary" loading={updateLocationsMutation.isPending}>
|
||||
<Button type="submit" color="primary" loading={props.isPending}>
|
||||
{t("update")}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
@@ -1,5 +1,6 @@
|
||||
import authedProcedure from "../../../../procedures/authedProcedure";
|
||||
import { router } from "../../../../trpc";
|
||||
import { ZBulkUpdateToDefaultAvailabilityInputSchema } from "./bulkUpdateDefaultAvailability.schema";
|
||||
import { ZCreateInputSchema } from "./create.schema";
|
||||
import { ZDeleteInputSchema } from "./delete.schema";
|
||||
import { ZScheduleDuplicateSchema } from "./duplicate.schema";
|
||||
@@ -16,6 +17,7 @@ type ScheduleRouterHandlerCache = {
|
||||
duplicate?: typeof import("./duplicate.handler").duplicateHandler;
|
||||
getScheduleByUserId?: typeof import("./getScheduleByUserId.handler").getScheduleByUserIdHandler;
|
||||
getScheduleByEventSlug?: typeof import("./getScheduleByEventTypeSlug.handler").getScheduleByEventSlugHandler;
|
||||
bulkUpdateToDefaultAvailability?: typeof import("./bulkUpdateDefaultAvailability.handler").bulkUpdateToDefaultAvailabilityHandler;
|
||||
};
|
||||
|
||||
const UNSTABLE_HANDLER_CACHE: ScheduleRouterHandlerCache = {};
|
||||
@@ -137,4 +139,22 @@ export const scheduleRouter = router({
|
||||
input,
|
||||
});
|
||||
}),
|
||||
bulkUpdateToDefaultAvailability: authedProcedure
|
||||
.input(ZBulkUpdateToDefaultAvailabilityInputSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.bulkUpdateToDefaultAvailability) {
|
||||
UNSTABLE_HANDLER_CACHE.bulkUpdateToDefaultAvailability = await import(
|
||||
"./bulkUpdateDefaultAvailability.handler"
|
||||
).then((mod) => mod.bulkUpdateToDefaultAvailabilityHandler);
|
||||
}
|
||||
|
||||
if (!UNSTABLE_HANDLER_CACHE.bulkUpdateToDefaultAvailability) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.bulkUpdateToDefaultAvailability({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
}),
|
||||
});
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../../trpc";
|
||||
import type { TBulkUpdateToDefaultAvailabilityInputSchema } from "./bulkUpdateDefaultAvailability.schema";
|
||||
|
||||
type BulkUpdateToDefaultAvailabilityOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TBulkUpdateToDefaultAvailabilityInputSchema;
|
||||
};
|
||||
|
||||
export const bulkUpdateToDefaultAvailabilityHandler = async ({
|
||||
ctx,
|
||||
input,
|
||||
}: BulkUpdateToDefaultAvailabilityOptions) => {
|
||||
const { eventTypeIds } = input;
|
||||
const defaultScheduleId = ctx.user.defaultScheduleId;
|
||||
|
||||
if (!defaultScheduleId) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Default schedule not set",
|
||||
});
|
||||
}
|
||||
|
||||
return await prisma.eventType.updateMany({
|
||||
where: {
|
||||
id: {
|
||||
in: eventTypeIds,
|
||||
},
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
data: {
|
||||
scheduleId: defaultScheduleId,
|
||||
},
|
||||
});
|
||||
};
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZBulkUpdateToDefaultAvailabilityInputSchema = z.object({
|
||||
eventTypeIds: z.array(z.number()),
|
||||
});
|
||||
|
||||
export type TBulkUpdateToDefaultAvailabilityInputSchema = z.infer<
|
||||
typeof ZBulkUpdateToDefaultAvailabilityInputSchema
|
||||
>;
|
||||
Reference in new issue
Block a user