import { ApiResponse } from '@elastic/elasticsearch' ; import { Search } from '@elastic/elasticsearch/api/requestParams' ; import { pick } from 'lodash' ; import { Injectable } from '@nestjs/common' ; import { ElasticsearchService } from '@nestjs/elasticsearch' ; import { ConfigService } from '@fc/config' ; import { ElasticsearchConfig } from '@fc/elasticsearch' ; import { LoggerService } from '@fc/logger' ; import { ICsmrTracksInputTrack , ICsmrTracksOutputTrack } from '../interfaces' ; see ICsmrTracksOutputTrack const TRACK_PROPERTIES = [ 'event' , 'date' , 'accountId' , 'spId' , 'spName' , 'spAcr' , 'country' , 'city' , ] ; see https @ Injectable ( ) export class CsmrTracksElasticsearchService { constructor ( private readonly config , private readonly logger , private readonly elasticsearch ) { this . logger . setContext ( this . constructor . name ) ; } see http param { string } spId returns { Promise < ICsmrTracksOutputTrack [ ] > } async getTracksByAccountId ( accountId ) { let response ; const { tracksIndex } = this . config . get < ElasticsearchConfig > ( 'Elasticsearch' ) ; const query = { index : tracksIndex , body : { query : { match : { accountId , } , } , } , } ; try { response = await this . elasticsearch . search ( query ) ; } catch ( error ) { this . logger . trace ( { error } ) ; return [ ] ; } const rawTracks = response . body . hits . hits as ICsmrTracksInputTrack [ ] ; const tracks = this . getFormatedTracks ( rawTracks ) ; return tracks ; } param { ICsmrTracksInputTrack [ ] } rawTracks returns { ICsmrTracksOutputTrack [ ] } private getFormatedTracks ( rawTracks ) { const filteredProperties = rawTracks . map ( ( track ) => { const trackId = track . _id ; const source = track . _source ; const properties = pick ( source , TRACK_PROPERTIES ) ; return { ... properties , trackId } ; } ) as ICsmrTracksOutputTrack [ ] ; return filteredProperties ; } }