Skip to content

Commit 8f2524d

Browse files
committed
Event/PaidEvent Creation Integration + BasicUI Setup
1 parent d689596 commit 8f2524d

File tree

3 files changed

+372
-24
lines changed

3 files changed

+372
-24
lines changed

src/api/routes/events.ts

+197-9
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,46 @@ const baseSchema = z.object({
3939
locationLink: z.optional(z.string().url()),
4040
host: z.enum(OrganizationList as [string, ...string[]]),
4141
featured: z.boolean().default(false),
42-
paidEventId: z.optional(z.string().min(1)),
42+
paidEventId: z.optional(z.string()),
43+
type: z.literal(undefined),
4344
});
4445

4546
const requestSchema = baseSchema.extend({
4647
repeats: z.optional(z.enum(repeatOptions)),
4748
repeatEnds: z.string().optional(),
4849
});
4950

51+
const ticketEventSchema = requestSchema.extend({
52+
type: z.literal("ticket"),
53+
event_id: z.string(),
54+
event_name: z.string(),
55+
eventCost: z.optional(z.record(z.number())),
56+
eventDetails: z.string(),
57+
eventImage: z.string(),
58+
event_capacity: z.number(),
59+
event_sales_active_utc: z.number(),
60+
event_time: z.number(),
61+
member_price: z.optional(z.string()),
62+
nonmember_price: z.optional(z.string()),
63+
tickets_sold: z.number(),
64+
});
65+
66+
const merchEventSchema = requestSchema.extend({
67+
type: z.literal("merch"),
68+
item_id: z.string(),
69+
item_email_desc: z.string(),
70+
item_image: z.string(),
71+
item_name: z.string(),
72+
item_price: z.optional(z.record(z.string(), z.number())),
73+
item_sales_active_utc: z.number(),
74+
limit_per_person: z.number(),
75+
member_price: z.optional(z.string()),
76+
nonmember_price: z.optional(z.string()),
77+
ready_for_pickup: z.boolean(),
78+
sizes: z.optional(z.array(z.string())),
79+
total_avail: z.optional(z.record(z.string(), z.string())),
80+
});
81+
5082
// eslint-disable-next-line @typescript-eslint/no-unused-vars
5183
const postRequestSchema = requestSchema.refine(
5284
(data) => (data.repeatEnds ? data.repeats !== undefined : true),
@@ -55,7 +87,37 @@ const postRequestSchema = requestSchema.refine(
5587
},
5688
);
5789

58-
export type EventPostRequest = z.infer<typeof postRequestSchema>;
90+
/*.refine(
91+
(data) => (data.paidEventId === undefined),
92+
{
93+
message: "paidEventId should be empty if you are not creating a paid event",
94+
},
95+
)*/ //Potential check here in case people creates event with a paideventid but no other entry so zod validates to just a normal event
96+
97+
const postTicketEventSchema = ticketEventSchema.refine(
98+
(data) =>
99+
data.paidEventId !== undefined && data.paidEventId === data.event_id,
100+
{
101+
message: "event_id needs to be the same as paidEventId", //currently useless bc if this false it will auto convert to a unpaid event...
102+
},
103+
);
104+
105+
const postMerchEventSchema = merchEventSchema.refine(
106+
(data) => data.paidEventId !== undefined && data.paidEventId === data.item_id,
107+
{
108+
message: "merch_id needs to be the same as paidEventId", //currently useless bc if this false it will auto convert to a unpaid event...
109+
},
110+
);
111+
112+
const postRefinedSchema = z.union([
113+
postRequestSchema,
114+
postMerchEventSchema,
115+
postTicketEventSchema,
116+
]);
117+
z.union([postMerchEventSchema, postTicketEventSchema]);
118+
119+
export type EventPostRequest = z.infer<typeof postRefinedSchema>;
120+
59121
type EventGetRequest = {
60122
Params: { id: string };
61123
Querystring: undefined;
@@ -81,6 +143,70 @@ const getEventsSchema = z.array(getEventSchema);
81143
export type EventsGetResponse = z.infer<typeof getEventsSchema>;
82144
type EventsGetQueryParams = { upcomingOnly?: boolean };
83145

146+
const splitter = (input: z.infer<typeof postRefinedSchema>) => {
147+
type entry = undefined | string | number | boolean;
148+
const { type, ...rest } = input;
149+
console.log(rest);
150+
let eventData: any = {}; //TODO: Need to specify type very faulty
151+
const paidData: { [key: string]: entry } = {};
152+
const eventSchemaKeys = Object.keys(requestSchema.shape);
153+
if (type === undefined) {
154+
eventData = rest as { [key: string]: entry };
155+
} else if (type === "ticket") {
156+
const data = rest as { [key: string]: entry };
157+
const paidSchemaKeys = [
158+
"event_id",
159+
"event_name",
160+
"eventCost",
161+
"eventDetails",
162+
"eventImage",
163+
"event_capacity",
164+
"event_sales_active_utc",
165+
"event_time",
166+
"member_price",
167+
"nonmember_price",
168+
"tickets_sold",
169+
];
170+
for (const key of paidSchemaKeys) {
171+
if (key in data) {
172+
paidData[key] = data[key];
173+
}
174+
}
175+
for (const key of eventSchemaKeys) {
176+
if (key in data) {
177+
eventData[key] = data[key];
178+
}
179+
}
180+
} else if (type === "merch") {
181+
const data = rest as { [key: string]: entry };
182+
const paidSchemaKeys = [
183+
"item_id",
184+
"item_email_desc",
185+
"item_image",
186+
"item_name",
187+
"item_price",
188+
"item_sales_active_utc",
189+
"limit_per_person",
190+
"member_price",
191+
"nonmember_price",
192+
"ready_for_pickup",
193+
"sizes",
194+
"total_avail",
195+
];
196+
for (const key of paidSchemaKeys) {
197+
if (key in data) {
198+
paidData[key] = data[key];
199+
}
200+
}
201+
for (const key of eventSchemaKeys) {
202+
if (key in data) {
203+
eventData[key] = data[key];
204+
}
205+
}
206+
}
207+
return [type, eventData, paidData];
208+
};
209+
84210
const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
85211
fastify.post<{ Body: EventPostRequest }>(
86212
"/:id?",
@@ -89,11 +215,11 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
89215
response: { 201: responseJsonSchema },
90216
},
91217
preValidation: async (request, reply) => {
92-
await fastify.zodValidateBody(request, reply, postRequestSchema);
218+
await fastify.zodValidateBody(request, reply, postRefinedSchema);
93219
},
94-
onRequest: async (request, reply) => {
220+
/*onRequest: async (request, reply) => {
95221
await fastify.authorize(request, reply, [AppRoles.EVENTS_MANAGER]);
96-
},
222+
},*/
97223
},
98224
async (request, reply) => {
99225
try {
@@ -116,27 +242,87 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
116242
});
117243
}
118244
}
245+
const obj = splitter(request.body);
119246
const entry = {
120-
...request.body,
247+
...obj[1],
121248
id: entryUUID,
122-
createdBy: request.username,
249+
createdBy: "request.username", //temporary disabled for testing
123250
createdAt: originalEvent
124251
? originalEvent.createdAt || new Date().toISOString()
125252
: new Date().toISOString(),
126253
updatedAt: new Date().toISOString(),
127254
};
255+
console.log("PutEvent", entry);
128256
await fastify.dynamoClient.send(
129257
new PutItemCommand({
130258
TableName: genericConfig.EventsDynamoTableName,
131259
Item: marshall(entry),
132260
}),
133261
);
262+
263+
switch (obj[0]) {
264+
case "ticket":
265+
const ticketEntry: z.infer<typeof postTicketEventSchema> = obj[2];
266+
const ticketResponse = await fastify.dynamoClient.send(
267+
new QueryCommand({
268+
TableName: genericConfig.TicketMetadataTableName,
269+
KeyConditionExpression: "event_id = :id",
270+
ExpressionAttributeValues: {
271+
":id": { S: ticketEntry.event_id },
272+
},
273+
}),
274+
);
275+
if (ticketResponse.Items?.length != 0) {
276+
throw new Error("Event_id already exists");
277+
}
278+
const ticketDBEntry = {
279+
...ticketEntry,
280+
member_price: "Send to stripe API",
281+
nonmember_price: "Send to stripe API",
282+
};
283+
console.log("TicketPut", ticketDBEntry);
284+
await fastify.dynamoClient.send(
285+
new PutItemCommand({
286+
TableName: genericConfig.TicketMetadataTableName,
287+
Item: marshall(ticketDBEntry),
288+
}),
289+
);
290+
break;
291+
case "merch":
292+
const merchEntry: z.infer<typeof postMerchEventSchema> = obj[2];
293+
const merchResponse = await fastify.dynamoClient.send(
294+
new QueryCommand({
295+
TableName: genericConfig.MerchStoreMetadataTableName,
296+
KeyConditionExpression: "item_id = :id",
297+
ExpressionAttributeValues: {
298+
":id": { S: merchEntry.item_id },
299+
},
300+
}),
301+
);
302+
if (merchResponse.Items?.length != 0) {
303+
throw new Error("Item_id already exists");
304+
}
305+
const merchDBEntry = {
306+
...merchEntry,
307+
member_price: "Send to stripe API",
308+
nonmember_price: "Send to stripe API",
309+
};
310+
await fastify.dynamoClient.send(
311+
new PutItemCommand({
312+
TableName: genericConfig.MerchStoreMetadataTableName,
313+
Item: marshall(merchDBEntry),
314+
}),
315+
);
316+
break;
317+
}
318+
134319
let verb = "created";
135320
if (userProvidedId && userProvidedId === entryUUID) {
136321
verb = "modified";
137322
}
323+
/* Disable for now...
138324
try {
139-
if (request.body.featured && !request.body.repeats) {
325+
if (eventEntry.featured && !eventEntry.repeats) {
140326
await updateDiscord(
141327
fastify.secretsManagerClient,
142328
entry,
@@ -168,7 +354,7 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
168354
throw e;
169355
}
170356
throw new DiscordEventError({});
171-
}
357+
} */
172358
reply.status(201).send({
173359
id: entryUUID,
174360
resource: `/api/v1/events/${entryUUID}`,
@@ -278,10 +464,12 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
278464
);
279465
},
280466
);
467+
281468
type EventsGetRequest = {
282469
Body: undefined;
283470
Querystring?: EventsGetQueryParams;
284471
};
472+
285473
fastify.get<EventsGetRequest>(
286474
"/",
287475
{

src/api/routes/paidEvents.ts

-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ const paidEventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
8282
reply.send({ Status: "Up" });
8383
});
8484

85-
8685
fastify.get("/ticketEvents", async (request, reply) => {
8786
try {
8887
const response = await dynamoclient.send(
@@ -370,7 +369,6 @@ const paidEventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
370369
},
371370
);
372371

373-
374372
//Post merchEvents
375373
fastify.post<{ Body: MerchPostSchema }>(
376374
"/merchEvents",
@@ -427,8 +425,6 @@ const paidEventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
427425
},
428426
);
429427

430-
431-
432428
fastify.delete<EventDeleteRequest>(
433429
"/ticketEvents/:id",
434430
{

0 commit comments

Comments
 (0)