Skip to content

Commit b1e6567

Browse files
committed
clean up naming scheme and add ID to get all request
1 parent ae0b4cf commit b1e6567

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src/routes/events.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import moment from "moment-timezone";
1818

1919
const repeatOptions = ["weekly", "biweekly"] as const;
2020

21-
const baseBodySchema = z.object({
21+
const baseSchema = z.object({
2222
title: z.string().min(1),
2323
description: z.string().min(1),
2424
start: z.string(),
@@ -30,16 +30,20 @@ const baseBodySchema = z.object({
3030
paidEventId: z.optional(z.string().min(1)),
3131
});
3232

33-
const requestBodySchema = baseBodySchema
34-
.extend({
35-
repeats: z.optional(z.enum(repeatOptions)),
36-
repeatEnds: z.string().optional(),
37-
})
38-
.refine((data) => (data.repeatEnds ? data.repeats !== undefined : true), {
33+
const requestSchema = baseSchema.extend({
34+
repeats: z.optional(z.enum(repeatOptions)),
35+
repeatEnds: z.string().optional(),
36+
});
37+
38+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
39+
const postRequestSchema = requestSchema.refine(
40+
(data) => (data.repeatEnds ? data.repeats !== undefined : true),
41+
{
3942
message: "repeats is required when repeatEnds is defined",
40-
});
43+
},
44+
);
4145

42-
type EventPostRequest = z.infer<typeof requestBodySchema>;
46+
type EventPostRequest = z.infer<typeof postRequestSchema>;
4347

4448
const responseJsonSchema = zodToJsonSchema(
4549
z.object({
@@ -49,9 +53,15 @@ const responseJsonSchema = zodToJsonSchema(
4953
);
5054

5155
// GET
52-
const getResponseBodySchema = z.array(requestBodySchema);
53-
const getResponseJsonSchema = zodToJsonSchema(getResponseBodySchema);
54-
export type EventGetResponse = z.infer<typeof getResponseBodySchema>;
56+
const getEventSchema = requestSchema.extend({
57+
id: z.string(),
58+
});
59+
60+
// export type EventGetResponse = z.infer<typeof getEventSchema>;
61+
// const getEventJsonSchema = zodToJsonSchema(getEventSchema);
62+
63+
const getEventsSchema = z.array(getEventSchema);
64+
export type EventsGetResponse = z.infer<typeof getEventsSchema>;
5565
type EventsGetQueryParams = { upcomingOnly?: boolean };
5666

5767
const dynamoClient = new DynamoDBClient({
@@ -66,7 +76,7 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
6676
response: { 200: responseJsonSchema },
6777
},
6878
preValidation: async (request, reply) => {
69-
await fastify.zodValidateBody(request, reply, requestBodySchema);
79+
await fastify.zodValidateBody(request, reply, requestSchema);
7080
},
7181
onRequest: async (request, reply) => {
7282
await fastify.authorize(request, reply, [AppRoles.MANAGER]);
@@ -112,7 +122,7 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
112122
querystring: {
113123
upcomingOnly: { type: "boolean" },
114124
},
115-
response: { 200: getResponseJsonSchema },
125+
response: { 200: getEventsSchema },
116126
},
117127
},
118128
async (request: FastifyRequest<EventsGetRequest>, reply) => {
@@ -123,7 +133,7 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
123133
);
124134
const items = response.Items?.map((item) => unmarshall(item));
125135
const currentTimeChicago = moment().tz("America/Chicago");
126-
let parsedItems = getResponseBodySchema.parse(items);
136+
let parsedItems = getEventsSchema.parse(items);
127137
if (upcomingOnly) {
128138
parsedItems = parsedItems.filter((item) => {
129139
try {

0 commit comments

Comments
 (0)