add validation for teams in the event type creation (#1866)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Edward Fernández 2022-02-15 16:51:01 -05:00 committed by GitHub
parent 7c6e394416
commit 14a9fdf78c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 1 deletions

View file

@ -84,6 +84,11 @@ export default function CreateEventTypeButton(props: Props) {
const message = `${err.statusCode}: ${err.message}`; const message = `${err.statusCode}: ${err.message}`;
showToast(message, "error"); showToast(message, "error");
} }
if (err.data?.code === "UNAUTHORIZED") {
const message = `${err.data.code}: You are not able to create this event`;
showToast(message, "error");
}
}, },
}); });

View file

@ -137,6 +137,11 @@ const EventTypePage = (props: inferSSRProps<typeof getServerSideProps>) => {
const message = `${err.statusCode}: ${err.message}`; const message = `${err.statusCode}: ${err.message}`;
showToast(message, "error"); showToast(message, "error");
} }
if (err.data?.code === "UNAUTHORIZED") {
const message = `${err.data.code}: You are not able to update this event`;
showToast(message, "error");
}
}, },
}); });

View file

@ -108,16 +108,32 @@ export const eventTypesRouter = createProtectedRouter()
input: createEventTypeInput, input: createEventTypeInput,
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const { schedulingType, teamId, ...rest } = input; const { schedulingType, teamId, ...rest } = input;
const userId = ctx.user.id;
const data: Prisma.EventTypeCreateInput = { const data: Prisma.EventTypeCreateInput = {
...rest, ...rest,
users: { users: {
connect: { connect: {
id: ctx.user.id, id: userId,
}, },
}, },
}; };
if (teamId && schedulingType) { if (teamId && schedulingType) {
const hasMembership = await ctx.prisma.membership.findFirst({
where: {
userId,
teamId: teamId,
accepted: true,
},
});
if (!hasMembership) {
console.warn(`User ${userId} does not have permission to create this new event type`);
throw new TRPCError({ code: "UNAUTHORIZED" });
}
data.team = { data.team = {
connect: { connect: {
id: teamId, id: teamId,