'use strict' ; import Config from '@Base/config' ; import { Router , RequestHandler , Request , Response , NextFunction } from 'express' ; import { setupMetaverseAPI , finishMetaverseAPI } from '@Route-Tools/middleware' ; import { accountFromAuthToken } from '@Route-Tools/middleware' ; import { buildPlaceInfo } from '@Route-Tools/Util' ; import { Domains } from '@Entities/Domains' ; import { Places } from '@Entities/Places' ; import { Perm } from '@Route-Tools/Perm' ; import { checkAccessToEntity } from '@Route-Tools/Permissions' ; import { PaginationInfo } from '@Entities/EntityFilters/PaginationInfo' ; import { PlaceFilterInfo } from '@Entities/EntityFilters/PlaceFilterInfo' ; import { VisibilityFilter } from '@Entities/EntityFilters/VisibilityFilter' ; import { Maturity } from '@Entities/Sets/Maturity' ; import { IsNullOrEmpty , IsNotNullOrEmpty } from '@Tools/Misc' ; import { Logger } from '@Tools/Logging' ; import { placeFields } from '@Entities/PlaceFields' ; const procGetPlaces = async ( req , resp , next ) => { const pager = new PaginationInfo ( ) ; const placer = new PlaceFilterInfo ( ) ; const visibilitier = new VisibilityFilter ( req . vAuthAccount ) ; pager . parametersFromRequest ( req ) ; placer . parametersFromRequest ( req ) ; visibilitier . parametersFromRequest ( req ) ; const places = [ ] ; for await ( const place of Places . enumerateAsync ( placer , pager ) ) { const aDomain = await Domains . getDomainWithId ( place . domainId ) ; if ( aDomain && IsNotNullOrEmpty ( aDomain . networkAddr ) ) { if ( await visibilitier . criteriaTestAsync ( place , aDomain ) ) { places . push ( await buildPlaceInfo ( place , aDomain ) ) ; } ; } ; } ; req . vRestResp . Data = { 'places' : places , 'maturity-categories' : Maturity . MaturityCategories } ; visibilitier . addResponseFields ( req ) ; placer . addResponseFields ( req ) ; pager . addResponseFields ( req ) ; next ( ) ; } ; export const procPostPlaces = async ( req , resp , next ) => { if ( req . vAuthAccount ) { let requestedName ; let requestedDesc ; let requestedAddr ; let requestedDomainId ; if ( req . body . place ) { requestedName = req . body . place . name ; requestedDesc = req . body . place . description ; requestedAddr = req . body . place . address ; requestedDomainId = req . body . place . domainId ; } else { requestedName = req . body . place_id ; requestedAddr = req . body . path ; requestedDomainId = req . body . domain_id ; } ; if ( requestedName && requestedAddr && requestedDomainId ) { const aDomain = await Domains . getDomainWithId ( requestedDomainId ) ; if ( aDomain ) { if ( await checkAccessToEntity ( req . vAuthToken , aDomain , [ Perm . SPONSOR , Perm . ADMIN ] , req . vAuthAccount ) ) { const ifValid = await placeFields . name . validate ( placeFields . name , req . vAuthAccount , requestedName ) ; if ( ifValid . valid ) { const newPlace = await Places . createPlace ( aDomain . sponsorAccountId ) ; newPlace . name = requestedName ; newPlace . description = requestedDesc ; newPlace . path = requestedAddr ; newPlace . domainId = aDomain . id ; newPlace . maturity = aDomain . maturity ?? Maturity . UNRATED ; newPlace . managers = [ req . vAuthAccount . username ] ; Places . addPlace ( newPlace ) ; req . vRestResp . Data = buildPlaceInfo ( newPlace , aDomain ) ; } else { req . vRestResp . respondFailure ( ifValid . reason ?? 'place name already exists or is too long' ) ; } ; } else { req . vRestResp . respondFailure ( 'unauthorized' ) ; } ; } else { req . vRestResp . respondFailure ( 'name/address/domainId not specified' ) ; } ; } else { req . vRestResp . respondFailure ( 'name, address, and domainId must be specified' ) ; } ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; } ; next ( ) ; } ; export const name = '/api/v1/places' ; export const router = Router ( ) ; router . get ( '/api/v1/places' , [ setupMetaverseAPI , accountFromAuthToken , procGetPlaces , finishMetaverseAPI ] ) ; router . post ( '/api/v1/places' , [ setupMetaverseAPI , accountFromAuthToken , procPostPlaces , finishMetaverseAPI ] ) ;