@@ -5,12 +5,15 @@ import {
55  QueryCommand , 
66  ScanCommand , 
77  ConditionalCheckFailedException , 
8+   PutItemCommand , 
9+   DeleteItemCommand , 
810}  from  "@aws-sdk/client-dynamodb" ; 
911import  {  genericConfig  }  from  "../config.js" ; 
10- import  {  unmarshall  }  from  "@aws-sdk/util-dynamodb" ; 
12+ import  {  marshall ,   unmarshall  }  from  "@aws-sdk/util-dynamodb" ; 
1113import  {  DatabaseFetchError  }  from  "../errors/index.js" ; 
1214import  {  z  }  from  "zod" ; 
1315import  {  zodToJsonSchema  }  from  "zod-to-json-schema" ; 
16+ import  {  AppRoles  }  from  "../roles.js" ; 
1417
1518const  dynamoclient  =  new  DynamoDBClient ( { 
1619  region : genericConfig . AwsRegion , 
@@ -28,22 +31,28 @@ type EventUpdateRequest = {
2831  Body : {  attribute : string ;  value : string  } ; 
2932} ; 
3033
31- /*const updateJsonSchema = zodToJsonSchema( 
32-   z.object({ 
33-     event_id: z.number(), 
34-     event_name: z.string(), 
35-     eventCost: z.record(z.number()), 
36-     eventDetails: z.optional(z.string()), 
37-     eventImage: z.optional(z.string()), 
38-     eventProps: z.record(z.string(), z.string()), 
39-     event_capacity: z.number(), 
40-     event_sales_active_utc: z.string(), 
41-     event_time: z.number(), 
42-     member_price: z.optional(z.string()), 
43-     nonmember_price: z.optional(z.string()), 
44-     tickets_sold: z.number() 
45-   }) 
46- )*/ 
34+ type  EventDeleteRequest  =  { 
35+   Params : {  id : string  } ; 
36+   Querystring : undefined ; 
37+   Body : undefined ; 
38+ } ; 
39+ 
40+ export  const  postTicketSchema  =  z . object ( { 
41+   event_id : z . string ( ) , 
42+   event_name : z . string ( ) , 
43+   eventCost : z . record ( z . number ( ) ) , 
44+   eventDetails : z . optional ( z . string ( ) ) , 
45+   eventImage : z . optional ( z . string ( ) ) , 
46+   eventProps : z . optional ( z . record ( z . string ( ) ,  z . string ( ) ) ) , 
47+   event_capacity : z . number ( ) , 
48+   event_sales_active_utc : z . string ( ) , 
49+   event_time : z . number ( ) , 
50+   member_price : z . optional ( z . string ( ) ) , 
51+   nonmember_price : z . optional ( z . string ( ) ) , 
52+   tickets_sold : z . number ( ) , 
53+ } ) ; 
54+ 
55+ type  TicketPostSchema  =  z . infer < typeof  postTicketSchema > ; 
4756
4857const  responseJsonSchema  =  zodToJsonSchema ( 
4958  z . object ( { 
@@ -214,20 +223,18 @@ const paidEventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
214223        } ) ; 
215224      }  catch  ( e : unknown )  { 
216225        if  ( e  instanceof  Error )  { 
217-           request . log . error ( "Failed to get from  DynamoDB: "  +  e . toString ( ) ) ; 
226+           request . log . error ( "Failed to update to  DynamoDB: "  +  e . toString ( ) ) ; 
218227        } 
219228        if  ( e  instanceof  ConditionalCheckFailedException )  { 
220229          request . log . error ( "Attribute does not exist" ) ; 
221230        } 
222231        throw  new  DatabaseFetchError ( { 
223-           message : "Failed to get  event from  Dynamo table." , 
232+           message : "Failed to update  event in  Dynamo table." , 
224233        } ) ; 
225234      } 
226235    } , 
227236  ) ; 
228237
229-   //Multiple attribute udpates... 
230- 
231238  fastify . put < EventUpdateRequest > ( 
232239    "/merchEvents/:id" , 
233240    { 
@@ -274,13 +281,102 @@ const paidEventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
274281        } ) ; 
275282      }  catch  ( e : unknown )  { 
276283        if  ( e  instanceof  Error )  { 
277-           request . log . error ( "Failed to get from  DynamoDB: "  +  e . toString ( ) ) ; 
284+           request . log . error ( "Failed to update to  DynamoDB: "  +  e . toString ( ) ) ; 
278285        } 
279286        if  ( e  instanceof  ConditionalCheckFailedException )  { 
280287          request . log . error ( "Attribute does not exist" ) ; 
281288        } 
282289        throw  new  DatabaseFetchError ( { 
283-           message : "Failed to get event from Dynamo table." , 
290+           message : "Failed to update event in Dynamo table." , 
291+         } ) ; 
292+       } 
293+     } , 
294+   ) ; 
295+ 
296+   fastify . post < {  Body : TicketPostSchema  } > ( 
297+     "/ticketEvents" , 
298+     { 
299+       schema : { 
300+         response : {  200 : responseJsonSchema  } , 
301+       } , 
302+       preValidation : async  ( request ,  reply )  =>  { 
303+         await  fastify . zodValidateBody ( request ,  reply ,  postTicketSchema ) ; 
304+       } , 
305+       /*onRequest: async (request, reply) => { 
306+         await fastify.authorize(request, reply, [AppRoles.EVENTS_MANAGER]); 
307+       },*/  //validation taken off 
308+     } , 
309+     async  ( request : FastifyRequest < {  Body : TicketPostSchema  } > ,  reply )  =>  { 
310+       const  id  =  request . body . event_id ; 
311+       try  { 
312+         //Verify if event_id already exists 
313+         const  response  =  await  dynamoclient . send ( 
314+           new  QueryCommand ( { 
315+             TableName : genericConfig . TicketMetadataTableName , 
316+             KeyConditionExpression : "event_id = :id" , 
317+             ExpressionAttributeValues : { 
318+               ":id" : {  S : id  } , 
319+             } , 
320+           } ) , 
321+         ) ; 
322+         if  ( response . Items ?. length  !=  0 )  { 
323+           throw  new  Error ( "Event_id already exists" ) ; 
324+         } 
325+         const  entry  =  { 
326+           ...request . body , 
327+         } ; 
328+         await  dynamoclient . send ( 
329+           new  PutItemCommand ( { 
330+             TableName : genericConfig . TicketMetadataTableName , 
331+             Item : marshall ( entry ) , 
332+           } ) , 
333+         ) ; 
334+         reply . send ( { 
335+           id : id , 
336+           resource : `/api/v1/paidEvents/ticketEvents/${ id }  ` , 
337+         } ) ; 
338+       }  catch  ( e : unknown )  { 
339+         if  ( e  instanceof  Error )  { 
340+           request . log . error ( "Failed to post to DynamoDB: "  +  e . toString ( ) ) ; 
341+         } 
342+         throw  new  DatabaseFetchError ( { 
343+           message : "Failed to post event to Dynamo table." , 
344+         } ) ; 
345+       } 
346+     } , 
347+   ) ; 
348+ 
349+   fastify . delete < EventDeleteRequest > ( 
350+     "/ticketEvents/:id" , 
351+     { 
352+       schema : { 
353+         response : {  200 : responseJsonSchema  } , 
354+       } , 
355+       onRequest : async  ( request ,  reply )  =>  { 
356+         await  fastify . authorize ( request ,  reply ,  [ AppRoles . EVENTS_MANAGER ] ) ; 
357+       } ,  //auth 
358+     } , 
359+     async  ( request : FastifyRequest < EventDeleteRequest > ,  reply )  =>  { 
360+       const  id  =  request . params . id ; 
361+       try  { 
362+         await  dynamoclient . send ( 
363+           new  DeleteItemCommand ( { 
364+             TableName : genericConfig . TicketMetadataTableName , 
365+             Key : { 
366+               event_id : {  S : id  } , 
367+             } , 
368+           } ) , 
369+         ) ; 
370+         reply . send ( { 
371+           id : id , 
372+           resource : `/api/v1/paidEvents/ticketEvents/${ id }  ` , 
373+         } ) ; 
374+       }  catch  ( e : unknown )  { 
375+         if  ( e  instanceof  Error )  { 
376+           request . log . error ( "Failed to delete from DynamoDB: "  +  e . toString ( ) ) ; 
377+         } 
378+         throw  new  DatabaseFetchError ( { 
379+           message : "Failed to delete event from Dynamo table." , 
284380        } ) ; 
285381      } 
286382    } , 
0 commit comments