'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 { CriteriaFilter } from '@Entities/EntityFilters/CriteriaFilter' ; import { PaginationInfo } from '@Entities/EntityFilters/PaginationInfo' ; import { PlaceFilterInfo } from '@Entities/EntityFilters/PlaceFilterInfo' ; import { buildPlaceInfo } from '@Route-Tools/Util' ; import { Domains } from '@Entities/Domains' ; import { Places } from '@Entities/Places' ; import { PlaceEntity } from '@Entities/PlaceEntity' ; import { Accounts } from '@Entities/Accounts' ; import { Maturity } from '@Entities/Sets/Maturity' ; import { IsNullOrEmpty , IsNotNullOrEmpty } from '@Tools/Misc' ; import { Logger } from '@Tools/Logging' ; const procPlacesUnhooked = async ( req , resp , next ) => { const pager = new PaginationInfo ( ) ; const placer = new PlaceFilterInfo ( ) ; pager . parametersFromRequest ( req ) ; placer . parametersFromRequest ( req ) ; if ( req . vRestResp && req . vAuthAccount ) { if ( Accounts . isAdmin ( req . vAuthAccount ) ) { const places = [ ] ; for await ( const aPlace of unhookedPlaces ( placer , pager ) ) { places . push ( await buildPlaceInfo ( aPlace ) ) ; } ; req . vRestResp . Data = { 'places' : places , 'maturity-categories' : Maturity . MaturityCategories } ; } else { req . vRestResp . respondFailure ( 'not admin' ) ; } ; } else { req . vRestResp . respondFailure ( 'must have account for access' ) ; } ; placer . addResponseFields ( req ) ; pager . addResponseFields ( req ) ; next ( ) ; } ; const procPlacesDeleteUnhooked = async ( req , resp , next ) => { const pager = new PaginationInfo ( ) ; const placer = new PlaceFilterInfo ( ) ; pager . parametersFromRequest ( req ) ; placer . parametersFromRequest ( req ) ; if ( req . vRestResp && req . vAuthAccount ) { if ( Accounts . isAdmin ( req . vAuthAccount ) ) { for await ( const aPlace of unhookedPlaces ( placer , pager ) ) { Logger . info ( `DeleteUnhooked: deleting place ${ aPlace . name } for having no domain` ) ; Places . removePlace ( aPlace ) ; } ; } else { req . vRestResp . respondFailure ( 'not admin' ) ; } ; } else { req . vRestResp . respondFailure ( 'must have account for access' ) ; } ; placer . addResponseFields ( req ) ; pager . addResponseFields ( req ) ; next ( ) ; } ; async function * unhookedPlaces ( pPager , pInfoer ? ) { for await ( const aPlace of Places . enumerateAsync ( pPager , pInfoer ) ) { const aDomain = await Domains . getDomainWithId ( aPlace . domainId ) ; if ( IsNullOrEmpty ( aDomain ) || IsNullOrEmpty ( aDomain . networkAddr ) ) { yield aPlace ; } ; } ; } ; const procPlacesInactive = async ( req , resp , next ) => { const pager = new PaginationInfo ( ) ; const placer = new PlaceFilterInfo ( ) ; pager . parametersFromRequest ( req ) ; placer . parametersFromRequest ( req ) ; if ( req . vRestResp && req . vAuthAccount ) { if ( Accounts . isAdmin ( req . vAuthAccount ) ) { const places = [ ] ; for await ( const aPlace of inactivePlaces ( placer , pager ) ) { places . push ( await buildPlaceInfo ( aPlace ) ) ; } ; req . vRestResp . Data = { 'places' : places , 'maturity-categories' : Maturity . MaturityCategories } ; } else { req . vRestResp . respondFailure ( 'not admin' ) ; } ; } else { req . vRestResp . respondFailure ( 'must have account for access' ) ; } ; placer . addResponseFields ( req ) ; pager . addResponseFields ( req ) ; next ( ) ; } ; const procPlacesDeleteInactive = async ( req , resp , next ) => { const pager = new PaginationInfo ( ) ; const placer = new PlaceFilterInfo ( ) ; pager . parametersFromRequest ( req ) ; placer . parametersFromRequest ( req ) ; if ( req . vRestResp && req . vAuthAccount ) { if ( Accounts . isAdmin ( req . vAuthAccount ) ) { for await ( const aPlace of inactivePlaces ( placer , pager ) ) { Logger . info ( `DeleteInactive: deleting place ${ aPlace . name } for being inactive` ) ; Places . removePlace ( aPlace ) ; } ; } else { req . vRestResp . respondFailure ( 'not admin' ) ; } ; } else { req . vRestResp . respondFailure ( 'must have account for access' ) ; } ; placer . addResponseFields ( req ) ; pager . addResponseFields ( req ) ; next ( ) ; } ; async function * inactivePlaces ( pPager , pInfoer ? ) { const inactivePlaceTime = Places . dateWhenNotActive ( ) ; for await ( const aPlace of Places . enumerateAsync ( pPager , pInfoer ) ) { if ( IsNotNullOrEmpty ( aPlace . lastActivity ) || aPlace . lastActivity < inactivePlaceTime ) { yield aPlace ; } ; } ; } ; export const name = '/api/maint/places/' ; export const router = Router ( ) ; router . get ( '/api/maint/places/unhooked' , [ setupMetaverseAPI , accountFromAuthToken , procPlacesUnhooked , finishMetaverseAPI ] ) ; router . delete ( '/api/maint/places/unhooked' , [ setupMetaverseAPI , accountFromAuthToken , procPlacesDeleteUnhooked , finishMetaverseAPI ] ) ; router . get ( '/api/maint/places/inactive' , [ setupMetaverseAPI , accountFromAuthToken , procPlacesInactive , finishMetaverseAPI ] ) ; router . delete ( '/api/maint/places/inactive' , [ setupMetaverseAPI , accountFromAuthToken , procPlacesDeleteInactive , finishMetaverseAPI ] ) ;