'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 { accountFromAuthToken } from '@Route-Tools/middleware' ; import { Perm } from '@Route-Tools/Perm' ; import { checkAccessToEntity } from '@Route-Tools/Permissions' ; import { buildPlaceInfo } from '@Route-Tools/Util' ; import { Accounts } from '@Entities/Accounts' ; import { Places } from '@Entities/Places' ; import { Domains } from '@Entities/Domains' ; import { Maturity } from '@Entities/Sets/Maturity' ; import { IsNotNullOrEmpty , IsNullOrEmpty } from '@Tools/Misc' ; import { VKeyedCollection } from '@Tools/vTypes' ; import { Logger } from '@Tools/Logging' ; export const procGetPlacesPlaceId = async ( req , resp , next ) => { if ( req . vPlace ) { const placeInfo = await buildPlaceInfo ( req . vPlace ) ; if ( await checkAccessToEntity ( req . vAuthToken , req . vPlace , [ Perm . DOMAINACCESS , Perm . ADMIN ] , req . vAuthAccount ) ) { placeInfo . current_api_key = await Places . getCurrentInfoAPIKey ( req . vPlace ) ; } ; req . vRestResp . Data = { 'place' : placeInfo , 'maturity-categories' : Maturity . MaturityCategories } ; } else { req . vRestResp . respondFailure ( 'No such place' ) ; } ; next ( ) ; } ; export const procPutPlacesPlaceId = async ( req , resp , next ) => { if ( req . vAuthAccount ) { if ( req . vPlace ) { if ( await checkAccessToEntity ( req . vAuthToken , req . vPlace , [ Perm . DOMAINACCESS , Perm . ADMIN ] , req . vAuthAccount ) ) { if ( req . body . place ) { const updates = { } ; if ( req . body . place . pointee_query ) { if ( req . body . place . pointee_query !== req . vPlace . domainId ) { Logger . info ( `procPutPlacesPlaceId: domain changing from ${ req . vPlace . domainId } to ${ req . body . place . pointee_query }` ) ; req . vPlace . domainId = req . body . place . pointee_query ; updates . domainId = req . vPlace . domainId ; } ; } ; for ( const field of [ 'path' , 'description' , 'thumbnail' ] ) { if ( req . body . place . hasOwnProperty ( field ) ) { await Places . setField ( req . vAuthToken , req . vPlace , field , req . body . place [ field ] , req . vAuthAccount , updates ) ; } ; } ; Places . updateEntityFields ( req . vPlace , updates ) ; } else { req . vRestResp . respondFailure ( 'badly formed data' ) ; } ; } else { req . vRestResp . respondFailure ( 'unauthorized' ) ; } ; } else { req . vRestResp . respondFailure ( 'Target place not found' ) ; } ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; } ; next ( ) ; } ; export const procDeletePlacesPlaceId = async ( req , resp , next ) => { if ( req . vAuthAccount ) { if ( req . vPlace ) { if ( await checkAccessToEntity ( req . vAuthToken , req . vPlace , [ Perm . DOMAINACCESS , Perm . ADMIN ] , req . vAuthAccount ) ) { Logger . info ( `procDeletePlacesPlaceId: deleting place "${ req . vPlace . name }", id=${ req . vPlace . id }` ) ; await Places . removePlace ( req . vPlace ) ; } else { req . vRestResp . respondFailure ( 'unauthorized' ) ; } ; } else { req . vRestResp . respondFailure ( 'Target place not found' ) ; } ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; } ; next ( ) ; } ; export const name = '/api/v1/places/:placeId' ; export const router = Router ( ) ; router . get ( '/api/v1/places/:placeId' , [ setupMetaverseAPI , accountFromAuthToken , placeFromParams , procGetPlacesPlaceId , finishMetaverseAPI ] ) ; router . put ( '/api/v1/places/:placeId' , [ setupMetaverseAPI , accountFromAuthToken , placeFromParams , procPutPlacesPlaceId , finishMetaverseAPI ] ) ; router . delete ( '/api/v1/places/:placeId' , [ setupMetaverseAPI , accountFromAuthToken , placeFromParams , procDeletePlacesPlaceId , finishMetaverseAPI ] ) ;