'use strict' ; import { Entity } from '@Entities/Entity' ; import { AccountEntity } from '@Entities/AccountEntity' ; import { AuthToken } from '@Entities/AuthToken' ; import { EntityFieldsDefn , FieldDefn , ValidateResponse } from '@Route-Tools/EntityFieldDefn' ; import { checkAccessToEntity } from '@Route-Tools/Permissions' ; import { VKeyedCollection } from '@Tools/vTypes' ; import { Logger } from '@Tools/Logging' ; export async function getEntityField ( pPerms , pAuthToken , pEntity , pField , pRequestingAccount ? ) { let val ; const perms = pPerms [ pField ] ; if ( perms ) { Logger . cdebug ( 'field-setting' , `getEntityField: get ${ pField }, perms.entity_field=${ perms . entity_field }` ) ; if ( await checkAccessToEntity ( pAuthToken , pEntity , perms . get_permissions , pRequestingAccount ) ) { Logger . cdebug ( 'field-setting' , `getEntityField: access passed` ) ; if ( typeof ( perms . getter ) === 'function' ) { Logger . cdebug ( 'field-setting' , `getEntityField: doing getter` ) ; val = await perms . getter ( perms , pEntity ) ; Logger . cdebug ( 'field-setting' , `getEntityField: ${ pField }=>${ JSON . stringify ( val ) }` ) ; } else { Logger . cdebug ( 'field-setting' , `getEntityField: no function getter ${ pField }, perms.entity_field=${ perms . entity_field }, typeof(perms.getter)=${ typeof ( perms . getter ) }` ) ; Logger . cdebug ( 'field-setting' , `getEntityField: no function getter perms=${ JSON . stringify ( perms ) }` ) ; } ; } ; } ; return val ; } ; export async function setEntityField ( pPerms , pAuthToken , pEntity , pField , pVal , pRequestingAccount ? , pUpdates ? ) { let validity ; const perms = pPerms [ pField ] ; if ( perms ) { try { Logger . cdebug ( 'field-setting' , `setEntityField: ${ pField }=>${ JSON . stringify ( pVal ) }` ) ; if ( await checkAccessToEntity ( pAuthToken , pEntity , perms . set_permissions , pRequestingAccount ) ) { Logger . cdebug ( 'field-setting' , `setEntityField: access passed` ) ; validity = await perms . validate ( perms , pEntity , pVal , pAuthToken ) ; if ( validity . valid ) { Logger . cdebug ( 'field-setting' , `setEntityField: value validated` ) ; if ( typeof ( perms . setter ) === 'function' ) { await perms . setter ( perms , pEntity , pVal ) ; if ( pUpdates ) { getEntityUpdateForField ( pPerms , pEntity , pField , pUpdates ) ; } ; } ; } else { Logger . cdebug ( 'field-setting' , `value did not validate` ) ; } ; } else { validity = { valid : false , reason : 'account cannot set field' } ; } ; } catch ( err ) { Logger . error ( `setEntityField: exception: ${ err }` ) ; validity = { valid : false , reason : 'exception setting: ' + err } ; } ; } else { Logger . error ( `setEntityField: no perms field. Field=${ pField }` ) ; validity = { valid : false , reason : `setEntityField: no perms field. Field=${ pField }` } ; } ; return validity ; } ; export function getEntityUpdateForField ( pPerms , pEntity , pField , pExisting ? ) { const ret = pExisting ?? { } ; if ( Array . isArray ( pField ) ) { pField . forEach ( fld => { const perms = pPerms [ fld ] ; makeEntityFieldUpdate ( perms , pEntity , ret ) ; } ) ; } else { const perms = pPerms [ pField ] ; makeEntityFieldUpdate ( perms , pEntity , ret ) ; } ; return ret ; } ; function makeEntityFieldUpdate ( pPerms , pEntity , pRet ) { if ( pPerms ) { if ( pPerms . updater ) { pPerms . updater ( pPerms , pEntity , pRet ) ; } else { pRet [ pPerms . entity_field ] = ( pEntity as any ) [ pPerms . entity_field ] ; } ; } ; } ;