'use strict' ; import Config from '@Base/config' ; import { Router , RequestHandler , Request , Response , NextFunction } from 'express' ; import { setupMetaverseAPI , finishMetaverseAPI , param1FromParams , placeFromParams } from '@Route-Tools/middleware' ; import { Tokens } from '@Entities/Tokens' ; import { Places } from '@Entities/Places' ; import { IsNotNullOrEmpty , IsNullOrEmpty } from '@Tools/Misc' ; import { VKeyedCollection } from '@Tools/vTypes' ; import { Logger } from '@Tools/Logging' ; interface CurrentBody { placeId : string ; current_api_key : string ; current_attendance : number ; current_images : string [ ] ; current_info : string ; } ; export const procPostPlaceCurrent = async ( req , resp , next ) => { const reqBody = req . body as CurrentBody ; if ( IsNotNullOrEmpty ( reqBody . placeId ) ) { if ( IsNotNullOrEmpty ( reqBody . current_api_key ) ) { const aPlace = await Places . getPlaceWithId ( reqBody . placeId ) ; if ( IsNotNullOrEmpty ( aPlace ) ) { const apiKey = await Places . getCurrentInfoAPIKey ( aPlace ) ; if ( apiKey ) { if ( apiKey === reqBody . current_api_key ) { const updates = { } ; const specialAuth = Tokens . createSpecialAdminToken ( ) ; for ( const field of [ 'current_attendance' , 'current_images' , 'current_info' ] ) { if ( reqBody . hasOwnProperty ( field ) ) { const validity = await Places . validateFieldValue ( field , req . body [ field ] ) ; if ( validity . valid ) { await Places . setField ( specialAuth , aPlace , field , req . body [ field ] , undefined , updates ) ; } else { req . vRestResp . respondFailure ( `Value for ${ field } is not valid: ${ validity . reason }` ) ; } ; } ; } ; updates . currentLastUpdateTime = new Date ( ) ; await Places . updateEntityFields ( aPlace , updates ) ; } else { req . vRestResp . respondFailure ( 'current_api_key does not match Places key' ) ; } ; } else { req . vRestResp . respondFailure ( 'Place apikey lookup failed' ) ; } ; } else { req . vRestResp . respondFailure ( 'Place specified by placeId does not exist' ) ; } ; } else { req . vRestResp . respondFailure ( 'No current_api_key' ) ; } ; } else { req . vRestResp . respondFailure ( 'No placeId' ) ; } ; next ( ) ; } ; export const name = '/api/v1/places/current' ; export const router = Router ( ) ; router . post ( '/api/v1/places/current' , [ setupMetaverseAPI , procPostPlaceCurrent , finishMetaverseAPI ] ) ;