Add try catch for event type duplicate, fixed catch error loggin (#6130)

Co-authored-by: Peer Richelsen <peeroke@gmail.com>
pull/5897/head^2
alannnc 2022-12-20 14:38:15 -07:00 committed by GitHub
parent 49bf14b196
commit 9487819ec1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 104 additions and 101 deletions

View File

@ -43,12 +43,12 @@ const DuplicateDialog = () => {
showToast(message, "error"); showToast(message, "error");
} }
if (err.data?.code === "BAD_REQUEST") { if (err.data?.code === "INTERNAL_SERVER_ERROR" || err.data?.code === "BAD_REQUEST") {
const message = `${err.data.code}: URL already exists.`; const message = t("unexpected_error_try_again");
showToast(message, "error"); showToast(message, "error");
} }
if (err.data?.code === "UNAUTHORIZED") { if (err.data?.code === "UNAUTHORIZED" || err.data?.code === "FORBIDDEN") {
const message = `${err.data.code}: You are not able to create this event`; const message = `${err.data.code}: You are not able to create this event`;
showToast(message, "error"); showToast(message, "error");
} }

View File

@ -622,111 +622,114 @@ export const eventTypesRouter = router({
}; };
}), }),
duplicate: eventOwnerProcedure.input(EventTypeDuplicateInput.strict()).mutation(async ({ ctx, input }) => { duplicate: eventOwnerProcedure.input(EventTypeDuplicateInput.strict()).mutation(async ({ ctx, input }) => {
const { id: originalEventTypeId, title: newEventTitle, slug: newSlug } = input; try {
const eventType = await ctx.prisma.eventType.findUnique({ const { id: originalEventTypeId, title: newEventTitle, slug: newSlug } = input;
where: { const eventType = await ctx.prisma.eventType.findUnique({
id: originalEventTypeId, where: {
}, id: originalEventTypeId,
include: { },
customInputs: true, include: {
schedule: true, customInputs: true,
users: true, schedule: true,
team: true, users: true,
workflows: true, team: true,
webhooks: true, workflows: true,
}, webhooks: true,
}); },
});
if (!eventType) { if (!eventType) {
throw new TRPCError({ code: "NOT_FOUND" }); throw new TRPCError({ code: "NOT_FOUND" });
} }
// Validate user is owner of event type or in the team // Validate user is owner of event type or in the team
if (eventType.userId !== ctx.user.id) { if (eventType.userId !== ctx.user.id) {
if (eventType.teamId) { if (eventType.teamId) {
const isMember = await ctx.prisma.membership.findFirst({ const isMember = await ctx.prisma.membership.findFirst({
where: { where: {
userId: ctx.user.id, userId: ctx.user.id,
teamId: eventType.teamId, teamId: eventType.teamId,
}, },
}); });
if (!isMember) { if (!isMember) {
throw new TRPCError({ code: "FORBIDDEN" }); throw new TRPCError({ code: "FORBIDDEN" });
}
} }
} }
throw new TRPCError({ code: "FORBIDDEN" });
}
const { const {
customInputs, customInputs,
users, users,
locations, locations,
team, team,
recurringEvent, recurringEvent,
bookingLimits, bookingLimits,
metadata, metadata,
workflows, workflows,
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
id: _id, id: _id,
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
webhooks: _webhooks, webhooks: _webhooks,
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
schedule: _schedule, schedule: _schedule,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - not typed correctly as its set on SSR // @ts-ignore - not typed correctly as its set on SSR
descriptionAsSafeHTML: _descriptionAsSafeHTML, descriptionAsSafeHTML: _descriptionAsSafeHTML,
...rest ...rest
} = eventType; } = eventType;
const data: Prisma.EventTypeCreateInput = { const data: Prisma.EventTypeUncheckedCreateInput = {
...rest, ...rest,
title: newEventTitle, title: newEventTitle,
slug: newSlug, slug: newSlug,
locations: locations ?? undefined, locations: locations ?? undefined,
team: team ? { connect: { id: team.id } } : undefined, teamId: team ? team.id : undefined,
users: users ? { connect: users.map((user) => ({ id: user.id })) } : undefined, users: users ? { connect: users.map((user) => ({ id: user.id })) } : undefined,
recurringEvent: recurringEvent || undefined, recurringEvent: recurringEvent || undefined,
bookingLimits: bookingLimits ?? undefined, bookingLimits: bookingLimits ?? undefined,
metadata: metadata === null ? Prisma.DbNull : metadata, metadata: metadata === null ? Prisma.DbNull : metadata,
};
const newEventType = await ctx.prisma.eventType.create({ data });
// Create custom inputs
if (customInputs) {
const customInputsData = customInputs.map((customInput) => {
const { id: _, options, ...rest } = customInput;
return {
options: options ?? undefined,
...rest,
eventTypeId: newEventType.id,
};
});
await ctx.prisma.eventTypeCustomInput.createMany({
data: customInputsData,
});
}
if (workflows.length > 0) {
const workflowIds = workflows.map((workflow) => {
return { id: workflow.workflowId };
});
const eventUpdateData: Prisma.EventTypeUpdateInput = {
workflows: {
connect: workflowIds,
},
}; };
await ctx.prisma.eventType.update({
where: {
id: newEventType.id,
},
data: eventUpdateData,
});
}
return { const newEventType = await ctx.prisma.eventType.create({ data });
eventType: newEventType,
}; // Create custom inputs
if (customInputs) {
const customInputsData = customInputs.map((customInput) => {
const { id: _, options, ...rest } = customInput;
return {
options: options ?? undefined,
...rest,
eventTypeId: newEventType.id,
};
});
await ctx.prisma.eventTypeCustomInput.createMany({
data: customInputsData,
});
}
if (workflows.length > 0) {
const workflowIds = workflows.map((workflow) => {
return { id: workflow.workflowId };
});
const eventUpdateData: Prisma.EventTypeUpdateInput = {
workflows: {
connect: workflowIds,
},
};
await ctx.prisma.eventType.update({
where: {
id: newEventType.id,
},
data: eventUpdateData,
});
}
return {
eventType: newEventType,
};
} catch (error) {
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR" });
}
}), }),
}); });