import { interceptorConstants , ResponseMessagesConstants , GeneralConstants } from '../../projects/shared-library/src/lib/shared/model' ; import * as functions from 'firebase-functions' ; import * as firebase from 'firebase-admin' ; import { readFileSync } from 'fs' ; import { resolve } from 'path' ; export class Utils { static getUTCTimeStamp ( ) { const date = new Date ( new Date ( ) . toUTCString ( ) ) ; const millis = date . getTime ( ) ; return millis ; } static addMinutes ( time , minutes ) { return time + minutes ; } static getValesFromFirebaseSnapshot ( snapshots ) { const values = [ ] ; for ( const snapshot of snapshots . docs ) { values . push ( snapshot . data ( ) ) ; } return values ; } static throwError ( error ) { console . error ( GeneralConstants . Error_Message , error ) ; throw error ; } static sendResponse ( res , status , responseObj ) { return res . status ( status ) . send ( responseObj ) ; } static sendError ( res , error ) { console . error ( GeneralConstants . Error_Message , error ) ; return Utils . sendResponse ( res , interceptorConstants . INTERNAL_ERROR , ResponseMessagesConstants . INTERNAL_SERVER_ERROR ) ; } static getFireStorageBucket ( admin ) { return admin . storage ( ) . bucket ( Utils . getConfig ( ) . storagebucket ) ; } static isEnvironmentProduction ( ) { return ( functions . config ( ) . environment && functions . config ( ) . environment . production && functions . config ( ) . environment . production === GeneralConstants . TRUE ) ? true : false ; } static getESPrefix ( ) { return Utils . getConfig ( ) . esPrefix ; } static getWebsiteUrl ( ) { return Utils . getConfig ( ) . websiteUrl ; } static changeFieldValue ( value ) { return firebase . firestore . FieldValue . increment ( value ) ; } static getTimeStamp ( ) { return firebase . firestore . FieldValue . serverTimestamp ( ) ; } static getConfig ( ) { let config = { } ; try { config = JSON . parse ( readFileSync ( resolve ( __dirname , `../../../configs/${ process . env . GCLOUD_PROJECT }.json` ) , 'utf8' ) ) ; } catch ( e ) { console . error ( 'No config found for project' ) ; } return config ; } static getApiKey ( ) { return Utils . getConfig ( ) . apiKey ; } static getFeedUrl ( ) { return Utils . getConfig ( ) . feedUrl ; } }