'use strict' ; import { Router , RequestHandler , Request , Response , NextFunction } from 'express' ; import { setupMetaverseAPI , finishMetaverseAPI } from '@Route-Tools/middleware' ; import { accountFromAuthToken , accountFromParams } from '@Route-Tools/middleware' ; import { Perm } from '@Route-Tools/Perm' ; import { checkAccessToEntity } from '@Route-Tools/Permissions' ; import { buildAccountInfo } from '@Route-Tools/Util' ; import { Accounts } from '@Entities/Accounts' ; import { VKeyedCollection } from '@Tools/vTypes' ; import { Logger } from '@Tools/Logging' ; const procGetAccountId = async ( req , resp , next ) => { if ( req . vAuthAccount ) { if ( req . vAccount ) { if ( await checkAccessToEntity ( req . vAuthToken , req . vAccount , [ Perm . OWNER , Perm . ADMIN ] ) ) { req . vRestResp . Data = { account : await buildAccountInfo ( req , req . vAccount ) } ; } else { req . vRestResp . respondFailure ( 'Unauthorized' ) ; } ; } else { req . vRestResp . respondFailure ( 'Target account not found' ) ; } ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; } ; next ( ) ; } ; const procPostAccountId = async ( req , resp , next ) => { if ( req . vRestResp ) { if ( req . vAuthAccount ) { if ( req . vAccount ) { const valuesToSet = req . body . accounts ; const updates = { } ; for ( const field of [ 'email' , 'public_key' ] ) { if ( valuesToSet . hasOwnProperty ( field ) ) { await Accounts . setField ( req . vAuthToken , req . vAccount , field , valuesToSet . field , req . vAuthAccount , updates ) ; } ; } ; if ( valuesToSet . hasOwnProperty ( 'images' ) ) { if ( valuesToSet . images . hero ) { await Accounts . setField ( req . vAuthToken , req . vAccount , 'images_hero' , valuesToSet . images . hero , req . vAuthAccount , updates ) ; } ; if ( valuesToSet . images . tiny ) { await Accounts . setField ( req . vAuthToken , req . vAccount , 'images_tiny' , valuesToSet . images . tiny , req . vAuthAccount , updates ) ; } ; if ( valuesToSet . images . thumbnail ) { await Accounts . setField ( req . vAuthToken , req . vAccount , 'images_thumbnail' , valuesToSet . images . thumbnail , req . vAuthAccount , updates ) ; } ; } ; await Accounts . updateEntityFields ( req . vAuthAccount , updates ) ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Account not specified' ) ; } ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; } ; } ; next ( ) ; } ; const procDeleteAccountId = async ( req , resp , next ) => { if ( req . vAuthAccount ) { if ( req . vAccount ) { if ( Accounts . isAdmin ( req . vAuthAccount ) ) { await Accounts . removeAccount ( req . vAccount ) ; await Accounts . removeAccountContext ( req . vAccount ) ; } else { req . vRestResp . respondFailure ( 'Not an administrator' ) ; } ; } else { req . vRestResp . respondFailure ( 'Target account does not exist' ) ; } ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; } ; next ( ) ; } ; export const name = '/api/v1/account/:accountId' ; export const router = Router ( ) ; router . get ( '/api/v1/account/:accountId' , [ setupMetaverseAPI , accountFromAuthToken , accountFromParams , procGetAccountId , finishMetaverseAPI ] ) ; router . post ( '/api/v1/account/:accountId' , [ setupMetaverseAPI , accountFromAuthToken , accountFromParams , procPostAccountId , finishMetaverseAPI ] ) ; router . delete ( '/api/v1/account/:accountId' , [ setupMetaverseAPI , accountFromAuthToken , accountFromParams , procDeleteAccountId , finishMetaverseAPI ] ) ;