import { GuidGenerator } from 'common' ; import { Dictionary , keyBy } from 'lodash' ; import moment from 'moment' ; import { ApiController , OnDemandPageScanRunResultProvider , ScanResultResponse , WebsiteScanResultProvider } from 'service-library' ; import { OnDemandPageScanResult , WebsiteScanResult , ScanGroupType } from 'storage-documents' ; import { ScanResponseConverter } from '../converters/scan-response-converter' ; export abstract class BaseScanResultController extends ApiController { protected abstract readonly onDemandPageScanRunResultProvider : OnDemandPageScanRunResultProvider ; protected abstract readonly websiteScanResultProvider : WebsiteScanResultProvider ; protected abstract readonly guidGenerator : GuidGenerator ; protected abstract readonly scanResponseConverter : ScanResponseConverter ; protected async isRequestMadeTooSoon ( scanId ) { const timeRequested = moment ( this . guidGenerator . getGuidTimestamp ( scanId ) ) ; const timeCurrent = moment ( ) ; const scanRequestProcessingDelayInSeconds = ( await this . getRestApiConfig ( ) ) . scanRequestProcessingDelayInSeconds ; return timeRequested . add ( scanRequestProcessingDelayInSeconds , 'seconds' ) . isAfter ( timeCurrent ) ; } protected async getScanResultMapKeyByScanId ( scanIds ) { const scanResultItems = await this . onDemandPageScanRunResultProvider . readScanRuns ( scanIds ) ; return keyBy ( scanResultItems , ( item ) => item . id ) ; } protected async getWebsiteScanResult ( pageScanResult , scanGroupType = 'deep-scan' ) { const websiteScanRef = pageScanResult . websiteScanRefs ?. find ( ( ref ) => ref . scanGroupType === scanGroupType ) ; if ( websiteScanRef ) { return this . websiteScanResultProvider . read ( websiteScanRef . id , true ) ; } return undefined ; } protected getTooSoonRequestResponse ( scanId ) { return { scanId , url : undefined , run : { state : 'pending' , } , } ; } protected isScanIdValid ( scanId ) { return ( this . guidGenerator . isValidV6Guid ( scanId ) && moment ( this . guidGenerator . getGuidTimestamp ( scanId ) ) . isBefore ( moment ( ) . add ( 10 , 'seconds' ) ) ) ; } protected getScanResultResponse ( pageScanResult , websiteScanResult ) { const segment = '/api/' ; const baseUrl = this . context . req . url . substring ( 0 , this . context . req . url . indexOf ( segment ) + segment . length ) ; return this . scanResponseConverter . getScanResultResponse ( baseUrl , this . apiVersion , pageScanResult , websiteScanResult ) ; } }