'use strict' ; import { Request } from 'express' ; import { Places } from '@Entities/Places' ; import { PlaceEntity } from '@Entities/PlaceEntity' ; import { CriteriaFilter } from '@Entities/EntityFilters/CriteriaFilter' ; import { Maturity } from '@Entities/Sets/Maturity' ; import { VKeyedCollection } from '@Tools/vTypes' ; import { Logger } from '@Tools/Logging' ; export class PlaceFilterInfo extends CriteriaFilter { private _maturity : string [ ] ; private _checkIfOnline : boolean = false ; private _checkIfActive : boolean = false ; private _tags : string [ ] ; private _ascending : number = 1 ; private _orderByName : boolean = false ; private _orderByUsers : boolean = false ; private _search : string ; private _doingQuery : boolean = false ; public constructor ( ) { super ( ) ; return ; } public parametersFromRequest ( pRequest ) { try { if ( typeof ( pRequest . query . maturity ) === 'string' ) { this . _maturity = pRequest . query . maturity . split ( ',' ) ; let allLegal = true ; for ( const mat of this . _maturity ) { if ( ! Maturity . KnownMaturity ( mat ) ) { allLegal = false ; break ; } ; } ; if ( allLegal ) { if ( this . _maturity . includes ( Maturity . UNRATED ) ) { this . _maturity . push ( null ) ; } ; } else { Logger . cdebug ( 'query-detail' , `PlaceFilterInfo.parametersFromRequest: passed undefined maturity level ${ JSON . stringify ( this . _maturity ) }` ) ; this . _maturity = undefined ; } ; Logger . cdebug ( 'query-detail' , `PlaceFilterInfo.parametersFromRequest: maturity=${ JSON . stringify ( this . _maturity ) }` ) ; } ; if ( typeof ( pRequest . query . tag ) === 'string' ) { this . _tags = pRequest . query . tag . split ( ',' ) ; Logger . cdebug ( 'query-detail' , `PlaceFilterInfo.parametersFromRequest: tags=${ JSON . stringify ( this . _tags ) }` ) ; } ; if ( typeof ( pRequest . query . status ) === 'string' ) { const statuses = pRequest . query . status . split ( ',' ) ; for ( const stat of statuses ) { switch ( stat ) { case 'online' : this . _checkIfOnline = true ; break ; case 'active' : this . _checkIfActive = true ; break ; default : break ; } ; } ; } ; if ( typeof ( pRequest . query . order ) === 'string' ) { const orderings = pRequest . query . order . split ( ',' ) ; for ( const order of orderings ) { switch ( order ) { case 'ascending' : this . _ascending = 1 ; break ; case 'decending' : this . _ascending = - 1 ; break ; case 'num_users' : this . _orderByUsers = true ; break ; case 'name' : this . _orderByName = true ; break ; default : break ; } ; } ; Logger . cdebug ( 'query-detail' , `PlaceFilterInfo.parametersFromRequest: order: ${ this . _ascending }, users: ${ this . _orderByUsers }, name: ${ this . _orderByName }` ) ; } ; if ( typeof ( pRequest . query . search ) === 'string' ) { const searcher = pRequest . query . search ; this . _search = searcher ; Logger . cdebug ( 'query-detail' , `PlaceFilterInfo.parametersFromRequest: search: ${ this . _search }` ) ; } ; } catch ( e ) { Logger . error ( 'PlaceFilterInfo: parameters from request: exception: ' + e ) ; } ; } ; public addResponseFields ( pRequest ) { return ; } ; public criteriaTest ( pThingy ) { let ret = false ; if ( this . _doingQuery ) { ret = true ; } else { if ( this . _maturity ) { if ( pThingy . hasOwnProperty ( 'maturity' ) ) { if ( this . _maturity . includes ( ( pThingy as PlaceEntity ) . maturity ) ) { ret = true ; } ; } else { if ( this . _maturity . includes ( Maturity . UNRATED ) ) { ret = true ; } ; } ; } ; if ( ! ret && this . _tags && pThingy . hasOwnProperty ( 'tags' ) ) { for ( const aTag of this . _tags ) { if ( ( pThingy as PlaceEntity ) . tags . includes ( aTag ) ) { ret = true ; break ; } ; } ; } ; if ( ! ret && this . _search && pThingy . hasOwnProperty ( 'name' ) ) { const matches = ( pThingy as PlaceEntity ) . name . match ( this . _search ) ; ret = matches && ( matches . length === 1 ) ; } ; } ; return ret ; } ; public async criteriaTestAsync ( pThingy ) { return this . criteriaTest ( pThingy ) ; } ; public criteriaParameters ( ) { this . _doingQuery = true ; const criteria = { } ; if ( this . _maturity ) { criteria [ 'maturity' ] = { '$in' : this . _maturity } ; } ; if ( this . _checkIfActive ) { criteria . currentAttendance = { '$gt' : 0 } ; } ; if ( this . _checkIfOnline ) { criteria . lastActivity = { '$gte' : Places . dateWhenNotActive ( ) } ; } ; if ( this . _tags ) { criteria [ 'tags' ] = { '$in' : this . _tags } ; } ; if ( this . _search ) { criteria [ 'name' ] = { '$regex' : this . _search , '$options' : 'i' } ; } ; return criteria ; } ; public sortCriteriaParameters ( ) { if ( this . _orderByUsers || this . _orderByName ) { const sorting = { } ; if ( this . _orderByUsers ) { sorting . attendance = this . _ascending ; } if ( this . _orderByName ) { sorting . name = this . _ascending ; } Logger . cdebug ( 'query-detail' , `PlaceFilterInfo.sortCriteriaParameters: ${ JSON . stringify ( sorting ) }` ) ; return sorting ; } return null ; } ; } ;