Skip to content

Commit 2e5953f

Browse files
committed
paidEvent POST merch update
1 parent 8624549 commit 2e5953f

File tree

1 file changed

+70
-3
lines changed

1 file changed

+70
-3
lines changed

src/api/routes/paidEvents.ts

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ const MerchPostSchema = z.object({
5959
item_price: z.optional(z.record(z.string(), z.number())),
6060
item_sales_active_utc: z.number(),
6161
limit_per_person: z.number(),
62-
member_price: z.string(),
63-
nonmember_price: z.string(),
62+
member_price: z.optional(z.string()),
63+
nonmember_price: z.optional(z.string()),
6464
ready_for_pickup: z.boolean(),
6565
sizes: z.optional(z.array(z.string())),
6666
total_avail: z.optional(z.record(z.string(), z.string())),
6767
});
6868

6969
type TicketPostSchema = z.infer<typeof TicketPostSchema>;
70+
type MerchPostSchema = z.infer<typeof MerchPostSchema>;
7071

7172
const responseJsonSchema = zodToJsonSchema(
7273
z.object({
@@ -76,9 +77,12 @@ const responseJsonSchema = zodToJsonSchema(
7677
);
7778

7879
const paidEventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
80+
//Healthz
7981
fastify.get("/", (request, reply) => {
8082
reply.send({ Status: "Up" });
8183
});
84+
85+
8286
fastify.get("/ticketEvents", async (request, reply) => {
8387
try {
8488
const response = await dynamoclient.send(
@@ -104,6 +108,7 @@ const paidEventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
104108
});
105109
}
106110
});
111+
107112
fastify.get("/merchEvents", async (request, reply) => {
108113
try {
109114
const response = await dynamoclient.send(
@@ -130,7 +135,6 @@ const paidEventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
130135
}
131136
});
132137

133-
//helper get no validation
134138
fastify.get<EventGetRequest>(
135139
"/ticketEvents/:id",
136140
async (request: FastifyRequest<EventGetRequest>, reply) => {
@@ -161,6 +165,7 @@ const paidEventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
161165
},
162166
);
163167

168+
//Get merchEvents by id
164169
fastify.get<EventGetRequest>(
165170
"/merchEvents/:id",
166171
async (request: FastifyRequest<EventGetRequest>, reply) => {
@@ -191,6 +196,7 @@ const paidEventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
191196
},
192197
);
193198

199+
//Update ticketEvents by id
194200
fastify.put<EventUpdateRequest>(
195201
"/ticketEvents/:id",
196202
{
@@ -249,6 +255,7 @@ const paidEventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
249255
},
250256
);
251257

258+
//Update merchEvents by id
252259
fastify.put<EventUpdateRequest>(
253260
"/merchEvents/:id",
254261
{
@@ -307,6 +314,7 @@ const paidEventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
307314
},
308315
);
309316

317+
//Post ticketEvents
310318
fastify.post<{ Body: TicketPostSchema }>(
311319
"/ticketEvents",
312320
{
@@ -362,6 +370,65 @@ const paidEventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
362370
},
363371
);
364372

373+
374+
//Post merchEvents
375+
fastify.post<{ Body: MerchPostSchema }>(
376+
"/merchEvents",
377+
{
378+
schema: {
379+
response: { 200: responseJsonSchema },
380+
},
381+
preValidation: async (request, reply) => {
382+
await fastify.zodValidateBody(request, reply, MerchPostSchema);
383+
},
384+
/*onRequest: async (request, reply) => {
385+
await fastify.authorize(request, reply, [AppRoles.EVENTS_MANAGER]);
386+
},*/ //validation taken off
387+
},
388+
async (request: FastifyRequest<{ Body: MerchPostSchema }>, reply) => {
389+
const id = request.body.item_id;
390+
try {
391+
//Verify if item_id already exists
392+
const response = await dynamoclient.send(
393+
new QueryCommand({
394+
TableName: genericConfig.TicketMetadataTableName,
395+
KeyConditionExpression: "item_id = :id",
396+
ExpressionAttributeValues: {
397+
":id": { S: id },
398+
},
399+
}),
400+
);
401+
if (response.Items?.length != 0) {
402+
throw new Error("Item_id already exists");
403+
}
404+
const entry = {
405+
...request.body,
406+
member_price: "Send to stripe API",
407+
nonmember_price: "Send to stripe API",
408+
};
409+
await dynamoclient.send(
410+
new PutItemCommand({
411+
TableName: genericConfig.TicketMetadataTableName,
412+
Item: marshall(entry),
413+
}),
414+
);
415+
reply.send({
416+
id: id,
417+
resource: `/api/v1/paidEvents/merchEvents/${id}`,
418+
});
419+
} catch (e: unknown) {
420+
if (e instanceof Error) {
421+
request.log.error("Failed to post to DynamoDB: " + e.toString());
422+
}
423+
throw new DatabaseFetchError({
424+
message: "Failed to post event to Dynamo table.",
425+
});
426+
}
427+
},
428+
);
429+
430+
431+
365432
fastify.delete<EventDeleteRequest>(
366433
"/ticketEvents/:id",
367434
{

0 commit comments

Comments
 (0)