'use strict' ; import { Router , RequestHandler , Request , Response , NextFunction } from 'express' ; import { setupMetaverseAPI , finishMetaverseAPI } from '@Route-Tools/middleware' ; import { accountFromAuthToken } from '@Route-Tools/middleware' ; import { Domains } from '@Entities/Domains' ; import { Places } from '@Entities/Places' ; import { DomainFields } from '@Entities/DomainFields' ; import { buildDomainInfoV1 } from '@Route-Tools/Util' ; import { buildDomainInfo , buildPlaceInfo } from '@Route-Tools/Util' ; import { PaginationInfo } from '@Entities/EntityFilters/PaginationInfo' ; import { AccountScopeFilter } from '@Entities/EntityFilters/AccountScopeFilter' ; import { VisibilityFilter } from '@Entities/EntityFilters/VisibilityFilter' ; import { HTTPStatusCode } from '@Route-Tools/RESTResponse' ; import { GenUUID , IsNotNullOrEmpty } from '@Tools/Misc' ; import { Logger } from '@Tools/Logging' ; const procGetDomains = async ( req , resp , next ) => { if ( req . vAuthAccount ) { const pager = new PaginationInfo ( ) ; const scoper = new AccountScopeFilter ( req . vAuthAccount , "sponsorAccountId" ) ; const visibilitier = new VisibilityFilter ( req . vAuthAccount ) ; pager . parametersFromRequest ( req ) ; scoper . parametersFromRequest ( req ) ; visibilitier . parametersFromRequest ( req ) ; const domainArray = [ ] ; for await ( const aDomain of Domains . enumerateAsync ( scoper , pager ) ) { if ( await visibilitier . criteriaTestAsync ( req . vAuthAccount , aDomain ) ) { domainArray . push ( await buildDomainInfoV1 ( aDomain ) ) ; } } ; req . vRestResp . Data = { 'domains' : domainArray } ; visibilitier . addResponseFields ( req ) ; scoper . addResponseFields ( req ) ; pager . addResponseFields ( req ) ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; req . vRestResp . HTTPStatus = HTTPStatusCode . Unauthorized ; } ; next ( ) ; } ; const procPostDomains = async ( req , resp , next ) => { if ( req . vAuthAccount ) { if ( req . body && req . body . domain && req . body . domain . label ) { const newDomainName = req . body . domain . label ; if ( IsNotNullOrEmpty ( newDomainName ) ) { const ifValid = await DomainFields . name . validate ( DomainFields . name , req . vAuthAccount , newDomainName ) ; if ( ifValid . valid ) { const generatedAPIkey = GenUUID ( ) ; const newDomain = Domains . createDomain ( ) ; newDomain . name = newDomainName ; newDomain . apiKey = generatedAPIkey ; if ( req . vSenderKey ) { newDomain . iPAddrOfFirstContact = req . vSenderKey ; } ; if ( req . body . domain . network_address ) { newDomain . networkAddr = req . body . domain . network_address ; } ; if ( req . body . domain . network_port ) { newDomain . networkPort = req . body . domain . network_port ; } ; Logger . debug ( `procPostDomains: associating account ${ req . vAuthToken . accountId } with new domain ${ newDomain . id }` ) ; newDomain . sponsorAccountId = req . vAuthToken . accountId ; const newPlacename = await Places . uniqifyPlaceName ( newDomain . name ) ; const newPlace = await Places . createPlace ( req . vAuthToken . accountId ) ; newPlace . domainId = newDomain . id ; newPlace . name = newPlacename ; newPlace . description = 'A place in ' + newDomain . name ; newPlace . maturity = newDomain . maturity ; newPlace . iPAddrOfFirstContact = req . vSenderKey ; newPlace . managers = [ req . vAuthAccount . username ] ; Domains . addDomain ( newDomain ) ; Places . addPlace ( newPlace ) ; const domainInfo = await buildDomainInfo ( newDomain ) ; domainInfo . api_key = newDomain . apiKey ; req . vRestResp . Data = { 'domain' : domainInfo , 'place' : await buildPlaceInfo ( newPlace , newDomain ) } ; req . vRestResp . addAdditionalField ( 'domain' , domainInfo ) ; } else { req . vRestResp . respondFailure ( ifValid . reason ?? 'invalid domain name' ) ; } ; } else { req . vRestResp . respondFailure ( 'label was empty' ) ; } ; } else { req . vRestResp . respondFailure ( 'no label supplied' ) ; } ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; } ; next ( ) ; } ; export const name = '/api/v1/domains' ; export const router = Router ( ) ; router . get ( '/api/v1/domains' , [ setupMetaverseAPI , accountFromAuthToken , procGetDomains , finishMetaverseAPI ] ) ; router . post ( '/api/v1/domains' , [ setupMetaverseAPI , accountFromAuthToken , procPostDomains , finishMetaverseAPI ] ) ;