@@ -10,17 +10,19 @@ import { genericConfig } from "../../common/config.js";
1010import {
1111 BaseError ,
1212 DatabaseFetchError ,
13+ DatabaseInsertError ,
1314 NotFoundError ,
1415 NotSupportedError ,
1516 TicketNotFoundError ,
1617 TicketNotValidError ,
1718 UnauthenticatedError ,
1819 ValidationError ,
1920} from "../../common/errors/index.js" ;
20- import { unmarshall } from "@aws-sdk/util-dynamodb" ;
21+ import { marshall , unmarshall } from "@aws-sdk/util-dynamodb" ;
2122import { validateEmail } from "../functions/validation.js" ;
2223import { AppRoles } from "../../common/roles.js" ;
2324import { zodToJsonSchema } from "zod-to-json-schema" ;
25+ import { ItemPostData } from "common/types/tickets.js" ;
2426
2527const postMerchSchema = z . object ( {
2628 type : z . literal ( "merch" ) ,
@@ -105,6 +107,12 @@ type TicketsListRequest = {
105107 Body : undefined ;
106108} ;
107109
110+ type TicketsPostRequest = {
111+ Params : { eventId : string } ;
112+ Querystring : undefined ;
113+ Body : ItemPostData ;
114+ } ;
115+
108116const ticketsPlugin : FastifyPluginAsync = async ( fastify , _options ) => {
109117 fastify . get < TicketsListRequest > (
110118 "/" ,
@@ -200,7 +208,6 @@ const ticketsPlugin: FastifyPluginAsync = async (fastify, _options) => {
200208 } ) ;
201209 }
202210 }
203-
204211 reply . send ( { merch : merchItems , tickets : ticketItems } ) ;
205212 } ,
206213 ) ;
@@ -271,6 +278,71 @@ const ticketsPlugin: FastifyPluginAsync = async (fastify, _options) => {
271278 return reply . send ( response ) ;
272279 } ,
273280 ) ;
281+ fastify . post < TicketsPostRequest > (
282+ "/:eventId" ,
283+ {
284+ onRequest : async ( request , reply ) => {
285+ await fastify . authorize ( request , reply , [ AppRoles . TICKETS_MANAGER ] ) ;
286+ } ,
287+ } ,
288+ async ( request , reply ) => {
289+ const eventId = request . params . eventId ;
290+ const eventType = request . body . type ;
291+ const eventActiveSet = request . body . itemSalesActive ;
292+ let newActiveTime : number = 0 ;
293+ if ( typeof eventActiveSet === "boolean" ) {
294+ if ( ! eventActiveSet ) {
295+ newActiveTime = - 1 ;
296+ }
297+ } else {
298+ newActiveTime = parseInt (
299+ ( eventActiveSet . valueOf ( ) / 1000 ) . toFixed ( 0 ) ,
300+ 10 ,
301+ ) ;
302+ }
303+ let command : UpdateItemCommand ;
304+ switch ( eventType ) {
305+ case "merch" :
306+ command = new UpdateItemCommand ( {
307+ TableName : genericConfig . MerchStoreMetadataTableName ,
308+ Key : marshall ( { item_id : eventId } ) ,
309+ UpdateExpression : "SET item_sales_active_utc = :new_val" ,
310+ ConditionExpression : "item_id = :item_id" ,
311+ ExpressionAttributeValues : {
312+ ":new_val" : { N : newActiveTime . toString ( ) } ,
313+ ":item_id" : { S : eventId } ,
314+ } ,
315+ } ) ;
316+ break ;
317+ case "ticket" :
318+ command = new UpdateItemCommand ( {
319+ TableName : genericConfig . TicketMetadataTableName ,
320+ Key : marshall ( { event_id : eventId } ) ,
321+ UpdateExpression : "SET event_sales_active_utc = :new_val" ,
322+ ConditionExpression : "event_id = :item_id" ,
323+ ExpressionAttributeValues : {
324+ ":new_val" : { N : newActiveTime . toString ( ) } ,
325+ ":item_id" : { S : eventId } ,
326+ } ,
327+ } ) ;
328+ break ;
329+ }
330+ try {
331+ await fastify . dynamoClient . send ( command ) ;
332+ } catch ( e ) {
333+ if ( e instanceof ConditionalCheckFailedException ) {
334+ throw new NotFoundError ( {
335+ endpointName : request . url ,
336+ } ) ;
337+ }
338+ fastify . log . error ( e ) ;
339+ throw new DatabaseInsertError ( {
340+ message : "Could not update active time for item." ,
341+ } ) ;
342+ }
343+ return reply . status ( 201 ) . send ( ) ;
344+ } ,
345+ ) ;
274346 fastify . post < { Body : VerifyPostRequest } > (
275347 "/checkIn" ,
276348 {
0 commit comments