'use strict' ; import { Router , RequestHandler , Request , Response , NextFunction } from 'express' ; import { setupMetaverseAPI , finishMetaverseAPI , tokenFromParams , accountFromParams } from '@Route-Tools/middleware' ; import { accountFromAuthToken , param1FromParams } from '@Route-Tools/middleware' ; import { Accounts } from '@Entities/Accounts' ; import { VKeyedCollection } from '@Tools/vTypes' ; const procGetField = async ( req , resp , next ) => { if ( req . vAuthAccount ) { if ( req . vAccount ) { req . vRestResp . Data = await Accounts . getField ( req . vAuthToken , req . vAccount , req . vParam1 , req . vAuthAccount ) ; } else { req . vRestResp . respondFailure ( 'Target account not found' ) ; } ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; } ; next ( ) ; } ; const procPostField = async ( req , resp , next ) => { if ( req . vAuthAccount ) { if ( req . vAccount ) { if ( req . body . hasOwnProperty ( 'set' ) ) { const updates = { } ; const success = await Accounts . setField ( req . vAuthToken , req . vAccount , req . vParam1 , req . body . set , req . vAuthAccount , updates ) ; if ( success . valid ) { Accounts . updateEntityFields ( req . vAccount , updates ) ; } else { req . vRestResp . respondFailure ( 'value could not be set:' + success . reason ) ; } ; } else { req . vRestResp . respondFailure ( 'no set value given' ) ; } ; } else { req . vRestResp . respondFailure ( 'Target account not found' ) ; } ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; } ; next ( ) ; } ; export const name = '/api/v1/account/:accountId/field/:fieldname' ; export const router = Router ( ) ; router . get ( '/api/v1/account/:accountId/field/:param1' , [ setupMetaverseAPI , accountFromAuthToken , accountFromParams , param1FromParams , procGetField , finishMetaverseAPI ] ) ; router . post ( '/api/v1/account/:accountId/field/:param1' , [ setupMetaverseAPI , accountFromAuthToken , accountFromParams , param1FromParams , procPostField , finishMetaverseAPI ] ) ;