Skip to content

Commit 14a9fdf

Browse files
add validation for teams in the event type creation (#1866)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 7c6e394 commit 14a9fdf

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

apps/web/components/eventtype/CreateEventType.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ export default function CreateEventTypeButton(props: Props) {
8484
const message = `${err.statusCode}: ${err.message}`;
8585
showToast(message, "error");
8686
}
87+
88+
if (err.data?.code === "UNAUTHORIZED") {
89+
const message = `${err.data.code}: You are not able to create this event`;
90+
showToast(message, "error");
91+
}
8792
},
8893
});
8994

apps/web/pages/event-types/[type].tsx

+5
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ const EventTypePage = (props: inferSSRProps<typeof getServerSideProps>) => {
137137
const message = `${err.statusCode}: ${err.message}`;
138138
showToast(message, "error");
139139
}
140+
141+
if (err.data?.code === "UNAUTHORIZED") {
142+
const message = `${err.data.code}: You are not able to update this event`;
143+
showToast(message, "error");
144+
}
140145
},
141146
});
142147

apps/web/server/routers/viewer/eventTypes.tsx

+17-1
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,32 @@ export const eventTypesRouter = createProtectedRouter()
108108
input: createEventTypeInput,
109109
async resolve({ ctx, input }) {
110110
const { schedulingType, teamId, ...rest } = input;
111+
112+
const userId = ctx.user.id;
113+
111114
const data: Prisma.EventTypeCreateInput = {
112115
...rest,
113116
users: {
114117
connect: {
115-
id: ctx.user.id,
118+
id: userId,
116119
},
117120
},
118121
};
119122

120123
if (teamId && schedulingType) {
124+
const hasMembership = await ctx.prisma.membership.findFirst({
125+
where: {
126+
userId,
127+
teamId: teamId,
128+
accepted: true,
129+
},
130+
});
131+
132+
if (!hasMembership) {
133+
console.warn(`User ${userId} does not have permission to create this new event type`);
134+
throw new TRPCError({ code: "UNAUTHORIZED" });
135+
}
136+
121137
data.team = {
122138
connect: {
123139
id: teamId,

0 commit comments

Comments
 (0)