'use strict' ; import Config from '@Base/config' ; import { Entity } from '@Entities/Entity' ; import { PlaceEntity } from '@Entities/PlaceEntity' ; import { Domains } from '@Entities/Domains' ; import { Places } from '@Entities/Places' ; import { Accounts } from '@Entities/Accounts' ; import { AuthToken } from '@Entities/AuthToken' ; import { Maturity } from '@Entities/Sets/Maturity' ; import { Visibility } from '@Entities/Sets/Visibility' ; import { Perm } from '@Route-Tools/Perm' ; import { checkAccessToEntity } from '@Route-Tools/Permissions' ; import { FieldDefn , ValidateResponse } from '@Route-Tools/EntityFieldDefn' ; import { isStringValidator , isSArraySet , isPathValidator , isLongPathValidator , isDateValidator , isObjectValidator , isNumberValidator } from '@Route-Tools/Validators' ; import { simpleGetter , simpleSetter , noSetter , sArraySetter , dateStringGetter , verifyAllSArraySetValues } from '@Route-Tools/Validators' ; import { IsNullOrEmpty , IsNotNullOrEmpty } from '@Tools/Misc' ; import { Logger } from '@Tools/Logging' ; export const placeFields = { 'id' : { entity_field : 'id' , request_field_name : 'id' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . NONE ] , validate : isStringValidator , setter : simpleSetter , getter : simpleGetter } , 'name' : { entity_field : 'name' , request_field_name : 'name' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAIN , Perm . DOMAINACCESS , Perm . MANAGER , Perm . ADMIN ] , validate : async ( pField , pEntity , pVal ) : Promise < ValidateResponse > => { let validity ; if ( typeof ( pVal ) === 'string' && pVal . length > 0 ) { if ( pVal . length <= Config [ 'metaverse-server' ] [ 'max-name-length' ] ) { if ( /^[A-Za-z][A-Za-z0-9\-]*$/ . test ( pVal ) ) { const maybePlace = await Places . getPlaceWithName ( pVal ) ; if ( IsNullOrEmpty ( maybePlace ) || ( pEntity as PlaceEntity ) . id === maybePlace . id ) { validity = { valid : true } ; } else { validity = { valid : false , reason : 'place name already exists' } ; } ; } else { validity = { valid : false , reason : 'place name characters must be A-Za-z0-9-.' } ; } ; } else { validity = { valid : false , reason : 'place name too long' } ; } ; } else { validity = { valid : false , reason : 'place name must be a string' } ; } ; return validity ; } , setter : simpleSetter , getter : simpleGetter } , 'displayName' : { entity_field : 'displayName' , request_field_name : 'displayName' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . MANAGER , Perm . ADMIN ] , validate : isStringValidator , setter : simpleSetter , getter : simpleGetter } , 'description' : { entity_field : 'description' , request_field_name : 'description' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . MANAGER , Perm . ADMIN ] , validate : isStringValidator , setter : simpleSetter , getter : simpleGetter } , 'visibility' : { entity_field : 'visibility' , request_field_name : 'visibility' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . MANAGER , Perm . ADMIN ] , validate : async ( pField , pEntity , pVal ) : Promise < ValidateResponse > => { if ( typeof ( pVal ) === 'string' && Visibility . KnownVisibility ( pVal ) ) { return { valid : true } ; } return { valid : false , reason : 'not accepted visibility value' } ; } , setter : simpleSetter , getter : simpleGetter } , 'domainId' : { entity_field : 'domainId' , request_field_name : 'domainId' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . ADMIN ] , validate : async ( pField , pEntity , pVal , pAuth ) : Promise < ValidateResponse > => { let validity = { valid : false , reason : 'system error' } ; if ( typeof ( pVal ) === 'string' ) { const maybeDomain = await Domains . getDomainWithId ( pVal ) ; if ( IsNotNullOrEmpty ( maybeDomain ) ) { if ( IsNotNullOrEmpty ( pAuth ) ) { if ( await checkAccessToEntity ( pAuth , maybeDomain , [ Perm . SPONSOR , Perm . ADMIN ] ) ) { validity = { valid : true } ; } else { Logger . error ( `PlaceEntity:domainId.validate: attempt to set to non-owned domain. RequesterAId=${ pAuth . accountId }, DomainId=${ pVal }` ) ; validity = { valid : false , reason : 'not authorized to change domainId' } ; } ; } ; } else { Logger . error ( `PlaceEntity:domainId.validate: attempt to set to non-existant domain. RequesterAId=${ pAuth . accountId }, DomainId=${ pVal }` ) ; validity = { valid : false , reason : 'domain not found' } ; } ; } else { validity = { valid : false , reason : 'invalid domainId format' } ; } ; return validity ; } , setter : simpleSetter , getter : simpleGetter } , 'address' : { entity_field : 'path' , request_field_name : 'address' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . MANAGER , Perm . ADMIN ] , validate : isLongPathValidator , setter : simpleSetter , getter : async ( pField , pEntity ) : Promise < any > => { return await Places . getAddressString ( pEntity as PlaceEntity ) ; } } , 'path' : { entity_field : 'path' , request_field_name : 'path' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . MANAGER , Perm . ADMIN ] , validate : isPathValidator , setter : simpleSetter , getter : simpleGetter } , 'maturity' : { entity_field : 'maturity' , request_field_name : 'maturity' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . MANAGER , Perm . ADMIN ] , validate : async ( pField , pEntity , pVal ) : Promise < ValidateResponse > => { if ( typeof ( pVal ) === 'string' && Maturity . KnownMaturity ( pVal ) ) { return { valid : true } ; } return { valid : false , reason : 'not accepted maturity value' } ; } , setter : simpleSetter , getter : simpleGetter } , 'tags' : { entity_field : 'tags' , request_field_name : 'tags' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . MANAGER , Perm . ADMIN ] , validate : isSArraySet , setter : sArraySetter , getter : simpleGetter } , 'managers' : { entity_field : 'managers' , request_field_name : 'managers' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . ADMIN ] , validate : async ( pField , pEntity , pVal ) : Promise < ValidateResponse > => { if ( await verifyAllSArraySetValues ( pVal , async ( name ) => { return IsNotNullOrEmpty ( await Accounts . getAccountWithUsername ( name ) ) ; } ) ) { return { valid : true } ; } return { valid : false , reason : 'unknown user names' } ; } , setter : sArraySetter , getter : simpleGetter } , 'thumbnail' : { entity_field : 'thumbnail' , request_field_name : 'thumbnail' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . MANAGER , Perm . ADMIN ] , validate : isStringValidator , setter : simpleSetter , getter : simpleGetter } , 'images' : { entity_field : 'images' , request_field_name : 'images' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . MANAGER , Perm . ADMIN ] , validate : isSArraySet , setter : sArraySetter , getter : simpleGetter } , 'current_attendance' : { entity_field : 'currentAttendance' , request_field_name : 'current_attendance' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . MANAGER , Perm . ADMIN ] , validate : isNumberValidator , setter : simpleSetter , getter : simpleGetter } , 'current_images' : { entity_field : 'currentImages' , request_field_name : 'current_images' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . MANAGER , Perm . ADMIN ] , validate : isSArraySet , setter : sArraySetter , getter : simpleGetter } , 'current_info' : { entity_field : 'currentInfo' , request_field_name : 'current_info' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . DOMAINACCESS , Perm . MANAGER , Perm . ADMIN ] , validate : isObjectValidator , setter : simpleSetter , getter : simpleGetter } , 'current_last_update_time' : { entity_field : 'currentLastUpdateTime' , request_field_name : 'current_last_update_time' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . NONE ] , validate : isDateValidator , setter : simpleSetter , getter : dateStringGetter } , 'current_api_key' : { entity_field : 'currentAPIKeyTokenId' , request_field_name : 'current_api_key' , get_permissions : [ Perm . DOMAINACCESS , Perm . ADMIN ] , set_permissions : [ Perm . NONE ] , validate : isStringValidator , setter : noSetter , getter : async ( pField , pEntity ) : Promise < any > => { if ( pEntity . hasOwnProperty ( 'currentAPIKeyTokenId' ) ) { return Places . getCurrentInfoAPIKey ( pEntity as PlaceEntity ) ; } ; return 'unknown' ; } } , 'addr_of_first_contact' : { entity_field : 'iPAddrOfFirstContact' , request_field_name : 'addr_of_first_contact' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . NONE ] , validate : isStringValidator , setter : simpleSetter , getter : simpleGetter } , 'when_place_entry_created' : { entity_field : 'whenCreated' , request_field_name : 'when_place_entry_created' , get_permissions : [ Perm . ALL ] , set_permissions : [ Perm . NONE ] , validate : isDateValidator , setter : noSetter , getter : dateStringGetter } } ;