'use strict' ; import { Request } from 'express' ; import { CriteriaFilter } from '@Entities/EntityFilters/CriteriaFilter' ; import { Accounts } from '@Entities/Accounts' ; import { AccountEntity } from '@Entities/AccountEntity' ; import { Logger } from '@Tools/Logging' ; import { prototype } from 'events' ; export class AccountScopeFilter extends CriteriaFilter { private _asAdmin = false ; private _accessingAcct : AccountEntity ; private _field : string ; private _targetAcct : string = undefined ; private _doingQuery : boolean = false ; public constructor ( pRequestorAccount , pField = 'id' ) { super ( ) ; this . _accessingAcct = pRequestorAccount ; this . _field = pField ; return ; } public parametersFromRequest ( pRequest ) { try { if ( pRequest . query . hasOwnProperty ( 'asAdmin' ) ) { const askingForAdmin = pRequest . query . asAdmin === 'true' ; if ( askingForAdmin ) { if ( Accounts . isAdmin ( this . _accessingAcct ) ) { this . _asAdmin = true ; } ; } ; } ; if ( pRequest . query . acct ) { this . _targetAcct = pRequest . query . account as string ; } ; Logger . cdebug ( 'query-detail' , `AccountScopeFilter.parametersFromRequest: asAdmin=${ this . _asAdmin }, target=${ this . _targetAcct }` ) ; } catch ( e ) { Logger . error ( 'AccountScopeFilter: parameters from request: exception: ' + e ) ; } ; } ; public addResponseFields ( pRequest ) { return ; } ; public AsAdmin ( ) { return this . _asAdmin ; } ; public criteriaTest ( pToTest ) { if ( ! this . _doingQuery ) { if ( this . _asAdmin ) { if ( this . _targetAcct ) { return typeof ( pToTest [ this . _field ] ) !== 'undefined' && pToTest [ this . _field ] === this . _targetAcct ; } return true ; } ; return typeof ( pToTest [ this . _field ] ) !== 'undefined' && pToTest [ this . _field ] === this . _accessingAcct . id ; } return true ; } ; public async criteriaTestAsync ( pThingy ) { return this . criteriaTest ( pThingy ) ; } ; public criteriaParameters ( ) { this . _doingQuery = true ; const criteria = { } ; if ( ! this . _asAdmin ) { criteria [ this . _field ] = this . _accessingAcct . id ; } ; if ( this . _asAdmin && typeof ( this . _targetAcct ) !== 'undefined' ) { criteria [ this . _field ] = this . _targetAcct ; } return criteria ; } ; public sortCriteriaParameters ( ) { return null ; } ; } ;