@@ -39,14 +39,46 @@ const baseSchema = z.object({
39
39
locationLink : z . optional ( z . string ( ) . url ( ) ) ,
40
40
host : z . enum ( OrganizationList as [ string , ...string [ ] ] ) ,
41
41
featured : z . boolean ( ) . default ( false ) ,
42
- paidEventId : z . optional ( z . string ( ) . min ( 1 ) ) ,
42
+ paidEventId : z . optional ( z . string ( ) ) ,
43
+ type : z . literal ( undefined ) ,
43
44
} ) ;
44
45
45
46
const requestSchema = baseSchema . extend ( {
46
47
repeats : z . optional ( z . enum ( repeatOptions ) ) ,
47
48
repeatEnds : z . string ( ) . optional ( ) ,
48
49
} ) ;
49
50
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
+
50
82
// eslint-disable-next-line @typescript-eslint/no-unused-vars
51
83
const postRequestSchema = requestSchema . refine (
52
84
( data ) => ( data . repeatEnds ? data . repeats !== undefined : true ) ,
@@ -55,7 +87,37 @@ const postRequestSchema = requestSchema.refine(
55
87
} ,
56
88
) ;
57
89
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
+
59
121
type EventGetRequest = {
60
122
Params : { id : string } ;
61
123
Querystring : undefined ;
@@ -81,6 +143,70 @@ const getEventsSchema = z.array(getEventSchema);
81
143
export type EventsGetResponse = z . infer < typeof getEventsSchema > ;
82
144
type EventsGetQueryParams = { upcomingOnly ?: boolean } ;
83
145
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
+
84
210
const eventsPlugin : FastifyPluginAsync = async ( fastify , _options ) => {
85
211
fastify . post < { Body : EventPostRequest } > (
86
212
"/:id?" ,
@@ -89,11 +215,11 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
89
215
response : { 201 : responseJsonSchema } ,
90
216
} ,
91
217
preValidation : async ( request , reply ) => {
92
- await fastify . zodValidateBody ( request , reply , postRequestSchema ) ;
218
+ await fastify . zodValidateBody ( request , reply , postRefinedSchema ) ;
93
219
} ,
94
- onRequest : async ( request , reply ) => {
220
+ /* onRequest: async (request, reply) => {
95
221
await fastify.authorize(request, reply, [AppRoles.EVENTS_MANAGER]);
96
- } ,
222
+ },*/
97
223
} ,
98
224
async ( request , reply ) => {
99
225
try {
@@ -116,27 +242,87 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
116
242
} ) ;
117
243
}
118
244
}
245
+ const obj = splitter ( request . body ) ;
119
246
const entry = {
120
- ...request . body ,
247
+ ...obj [ 1 ] ,
121
248
id : entryUUID ,
122
- createdBy : request . username ,
249
+ createdBy : " request.username" , //temporary disabled for testing
123
250
createdAt : originalEvent
124
251
? originalEvent . createdAt || new Date ( ) . toISOString ( )
125
252
: new Date ( ) . toISOString ( ) ,
126
253
updatedAt : new Date ( ) . toISOString ( ) ,
127
254
} ;
255
+ console . log ( "PutEvent" , entry ) ;
128
256
await fastify . dynamoClient . send (
129
257
new PutItemCommand ( {
130
258
TableName : genericConfig . EventsDynamoTableName ,
131
259
Item : marshall ( entry ) ,
132
260
} ) ,
133
261
) ;
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
+
134
319
let verb = "created" ;
135
320
if ( userProvidedId && userProvidedId === entryUUID ) {
136
321
verb = "modified" ;
137
322
}
323
+ /* Disable for now...
138
324
try {
139
- if ( request . body . featured && ! request . body . repeats ) {
325
+ if (eventEntry. featured && !eventEntry .repeats) {
140
326
await updateDiscord(
141
327
fastify.secretsManagerClient,
142
328
entry,
@@ -168,7 +354,7 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
168
354
throw e;
169
355
}
170
356
throw new DiscordEventError({});
171
- }
357
+ } */
172
358
reply . status ( 201 ) . send ( {
173
359
id : entryUUID ,
174
360
resource : `/api/v1/events/${ entryUUID } ` ,
@@ -278,10 +464,12 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
278
464
) ;
279
465
} ,
280
466
) ;
467
+
281
468
type EventsGetRequest = {
282
469
Body : undefined ;
283
470
Querystring ?: EventsGetQueryParams ;
284
471
} ;
472
+
285
473
fastify . get < EventsGetRequest > (
286
474
"/" ,
287
475
{
0 commit comments