/* common helper class */ const moment_timezone = require('moment-timezone'); const aws = require('aws-sdk'); import * as CONFIG from './config' import * as Interfaces from '../Interfaces/interfaces'; import { SNS } from '@virtualkey/vk-helper-sns' import { FifoQueue, FifoMessage } from '@virtualkey/vk-helper-sqs'; import * as TypeDef from '../TypeDef/TypeDef'; export class CommonHelper { /** * get desired date format by passing date as param * @param date */ public static formatDate(date: any = new Date(), isDateInUnixFormat: boolean = false, format: string = 'YYYY-MM-DD') { if (isDateInUnixFormat) { return moment_timezone.unix(date).format(format); } return moment_timezone(date).format(format); } public static getCurrentUTCTime(): string { console.log('Inside getCurrentUTCTime with input'); let unixTime: string; try { unixTime = moment_timezone((new Date())).unix(); console.log('Unix Timestamp in Seconds' + JSON.stringify(unixTime)); } catch (err) { console.log('Error at getCurrentUTCTime' + JSON.stringify(err)); } return unixTime; } /** * Function to convert relative date from current date to format YYYY-MM-DD * @param relativeDate */ public static getAbsoluteDate(relativeDate: number): Date{ console.log("Inside getAbsoluteDate to convert date for "+relativeDate); let absoluteDate:any = new Date(); let currentDate: any = new Date(); absoluteDate.setDate(currentDate.getDate() + relativeDate); absoluteDate = this.formatDate(absoluteDate); console.log("Output of getAbsoluteDate "+ absoluteDate); return absoluteDate; } /** * * @param functionName * @param payload */ public static async invokeLambda(functionName: String, payload: any){ aws.config.update({ accessKeyId: CONFIG.LAMBDA_CREDENTIALS.ACCESS_KEY_ID, secretAccessKey: CONFIG.LAMBDA_CREDENTIALS.SECRET_ACCESS_KEY, region: CONFIG.LAMBDA_CREDENTIALS.REGION }); let lambda = new aws.Lambda(); let lambdaPayload = { FunctionName: functionName, Payload: JSON.stringify(payload) } try { const result: any = await lambda.invoke(lambdaPayload).promise(); console.log('invokeLambda result '+JSON.stringify(result)); const resultPayload: any = JSON.parse(result.Payload); console.log('invokeLambda resultPayload '+JSON.stringify(resultPayload)); return resultPayload; } catch (error) { console.error("Lambda error"+ JSON.stringify(error)); this.throwError(error); } } public static async submitMessageToSqsQueue (queueName: string, payload: any) { console.log('Function submitMessageToSqsQueue to queue '+queueName+ ' with payload '+JSON.stringify(payload)); let accessKeyId = CONFIG.SQS_CREDENTIALS.ACCESS_KEY_ID; let secretAccessKey = CONFIG.SQS_CREDENTIALS.SECRET_ACCESS_KEY; let region = CONFIG.SQS_CREDENTIALS.REGION; let fifoQueue = new FifoQueue(accessKeyId, secretAccessKey, queueName, region); let MessageBody = JSON.stringify(payload); let MessageGroupId = payload.MessageGroupId; let message: FifoMessage = { messageGroupId: MessageGroupId, messageBody: MessageBody } let queueResponse: any; try { console.log("submitMessageToSqsQueue MessageBody "+JSON.stringify(message)); queueResponse = await fifoQueue.sendMessage(message); } catch (error) { console.error("SQS Error "+JSON.stringify(error)); this.throwError(error); } return queueResponse; } /** * Helper function to send SNS notification to ERROR SNS topic * @param message: TypeDef.AlertMessage */ public static async sendErrorMessage(message: TypeDef.AlertMessage) { console.debug("Sending error message to sns:", message); try { const accessKeyId = CONFIG.SNS_CREDENTIALS.ACCESS_KEY_ID const secretAccessKey = CONFIG.SNS_CREDENTIALS.SECRET_ACCESS_KEY const topic = CONFIG.SNS_TOPICS.ERROR const region = CONFIG.SNS_CREDENTIALS.REGION let sns = new SNS(accessKeyId, secretAccessKey, topic, region) await sns.send(message.subject, message.body) } catch (error) { console.error("Error publishing alert to sns:", error) console.debug("Message:", message) } } /** * This function throws a custom error * @param code * @param msg */ public static throwError(err: any) { console.log("####Handling Error", JSON.stringify(err)); if (err.errorCodeId) { throw err; } else { throw CONFIG.CONSTANTS.getError("UNHANDLE_EXCEPTION", err.message ? err.message : err) } } /** * This function will give final response object * @param status boolean * @param data final data * @param errorCodeId default 0 , 400 for error * @param errorMessage any message if requied */ public static getResponseObj(status: any, data: any = {}, errorCodeId: string = "0", errorMessage: string = "") { let paginationInfo; data = JSON.parse(JSON.stringify(data)); if (Object.keys(data).indexOf("paginationInfo") > -1) { paginationInfo = data.paginationInfo; delete data.paginationInfo; } let responseObj: Interfaces.ResponseObject = { status, errorCodeId, errorMessage, data, } // add if res pagination object has some value paginationInfo ? responseObj["paginationInfo"] = paginationInfo : null; return responseObj; } }