'use strict' ; import { Config } from '@Base/config' ; import { Router , RequestHandler , Request , Response , NextFunction } from 'express' ; import { setupMetaverseAPI , finishMetaverseAPI } from '@Route-Tools/middleware' ; import { Logger } from '@Tools/Logging' ; import { Accounts } from '@Entities/Accounts' ; import { Tokens } from '@Entities/Tokens' ; import { IsNotNullOrEmpty } from '@Tools/Misc' ; import { Roles } from '@Entities/Sets/Roles' ; import { SArray , VKeyedCollection } from '@Tools/vTypes' ; const procMakeAdmin = async ( req , resp , next ) => { const adminAccountName = Config [ "metaverse-server" ] [ "base-admin-account" ] ?? 'wilma' ; if ( req . vRestResp ) { const adminAccount = await Accounts . getAccountWithUsername ( adminAccountName ) ; if ( IsNotNullOrEmpty ( adminAccount ) ) { if ( SArray . has ( adminAccount . roles , Roles . ADMIN ) ) { Logger . debug ( `procMakeAdmin: ${ adminAccountName } already has role "admin"` ) ; } else { const updates = { } ; const adminToken = Tokens . createSpecialAdminToken ( ) ; const success = await Accounts . setField ( adminToken , adminAccount , 'roles' , { 'add' : Roles . ADMIN } , adminAccount , updates ) ; if ( success . valid ) { await Accounts . updateEntityFields ( adminAccount , updates ) ; } else { req . vRestResp . respondFailure ( 'could not set admin: ' + success . reason ) ; } ; } ; } else { Logger . error ( `procMakeAdmin: could not fetch account "${ adminAccountName }"` ) ; req . vRestResp . respondFailure ( 'No account named admin account name exists' ) ; } ; } ; next ( ) ; } ; export const name = '/api/maint/makeAdmin' ; export const router = Router ( ) ; router . get ( '/api/maint/makeAdmin' , [ setupMetaverseAPI , procMakeAdmin , finishMetaverseAPI ] ) ;