import moment from 'moment'; import { v4 as uuidv4 } from 'uuid'; import { P2P_SPECIAL_VALUE } from '../enum/p2p'; import envConfig from '../env/env-config'; import { getDisplayName, getNodeProfile } from '../env/nodeProfile'; import * as dateUtil from './dateUtil'; import scenarioContext from './scenarioContext'; export function getStepVariableMap(scenarioName: string) { const context = scenarioContext(); if (context.dc.randomDate === undefined || context.dc.randomDate == '') { context.dc.randomDate = dateUtil.getRandomDate(); } let scenario = envConfig.scenario[scenarioName]; if (context.scenario) { scenario = Object.assign(scenario, context.scenario); } // const envProfile = getEnvProfile(); return { '#APPLICANT#': getDisplayName(scenario.applicant), '#APPLICANT_ADDRESS#': getNodeProfile(scenario.applicant).address, '#BENEFICIARY#': getDisplayName(scenario.beneficiary), '#BENEFICIARY_ADDRESS#': getNodeProfile(scenario.beneficiary).address, '#ISSUING_BANK#': getDisplayName(scenario.issuingBank), '#ISSUING_BANK_ADDRESS#': getNodeProfile(scenario.issuingBank).address, '#ADVISING_BANK#': getDisplayName(scenario.advisingBank), '#ADVISING_BANK_ADDRESS#': getNodeProfile(scenario.advisingBank).address, '#NEW_ADVISING_BANK#': getDisplayName(scenario.newAdvisingBank) ?? '', '#NEW_ADVISING_BANK_ADDRESS#': getNodeProfile(scenario.newAdvisingBank)?.address ?? '', '#CONFIRMING_INSTRUCTION#': scenario.confirmation, '#CONFIRMING_BANK#': getDisplayName(scenario.confirmingBank) ?? '', '#CONFIRMING_BANK_ADDRESS#': getNodeProfile(scenario.confirmingBank)?.address ?? '', '#NEW_CONFIRMING_BANK#': getDisplayName(scenario.newConfirmingBank) ?? '', '#NEW_CONFIRMING_BANK_ADDRESS#': getNodeProfile(scenario.newConfirmingBank)?.address ?? '', '#NOMINATED_BANK#': getDisplayName(scenario.nominatedBank) ?? 'Any bank', '#NOMINATED_BANK_ADDRESS#': getNodeProfile(scenario.nominatedBank)?.address ?? '', '#CORPA#': getDisplayName('corpa'), '#CORPA_ADDRESS#': getNodeProfile('corpa').address, '#CORPB#': getDisplayName('corpb'), '#CORPB_ADDRESS#': getNodeProfile('corpb').address, '#BANKA#': getDisplayName('banka'), '#BANKA_ADDRESS#': getNodeProfile('banka').address, '#BANKB#': getDisplayName('bankb'), '#BANKB_ADDRESS#': getNodeProfile('bankb').address, '#BANKC#': getDisplayName('bankc'), '#BANKC_ADDRESS#': getNodeProfile('bankc').address, '#BANKD#': getDisplayName('bankd'), '#BANKD_ADDRESS#': getNodeProfile('bankd').address, '#RANDOM_DATE#': context.dc.randomDate, '#TODAY_DATE#': dateUtil.getTodayDate(), '#RANDOM_DATE (YY/MM/DD)#': moment(context.dc.randomDate, 'YYYY-MM-DD').format('YY/MM/DD') }; } export function replaceWithSpecialKeyValue(src) { const dateMap = getStepDateVariableMap(); for (const key in dateMap) { src = src.replace(key, dateMap[key]); } const p2pMap = getStepP2PVariableMap(); for (const key in p2pMap) { src = src.replace(key, p2pMap[key]); } const uuidMap = getStepUUIDVariableMap(); for (const key in uuidMap) { src = src.replace(key, uuidMap[key]); } return src; } export function getStepUUIDVariableMap() { return { '#FULL_UUID#': uuidv4(), '#SHORT_UUID#': uuidv4().substring(0, 8) }; } export function getStepDateVariableMap() { if (scenarioContext().dc.randomDate === undefined) { scenarioContext().dc.randomDate = dateUtil.getRandomDate(); } return { '#RANDOM_DATE#': scenarioContext().dc.randomDate, '#TODAY_DATE#': dateUtil.getTodayDate(), '#TIME_STAMP#': dateUtil.getTimestampStr() }; } export function getStepP2PVariableMap() { const varMap = {}; const generalP2P = scenarioContext().p2p; if (generalP2P) { varMap[P2P_SPECIAL_VALUE.LATEST_SUBJECT] = generalP2P.subject; varMap[P2P_SPECIAL_VALUE.LATEST_MESSAGE] = generalP2P.message; varMap[P2P_SPECIAL_VALUE.LATEST_SENT_DATE] = generalP2P.sentDate; varMap[P2P_SPECIAL_VALUE.LATEST_SENT_DATETIME] = generalP2P.sentDateTime; scenarioContext().p2p.replies.forEach((reply, idx) => { const messageNo = idx + 1; varMap[P2P_SPECIAL_VALUE.REPLY_INDEX + messageNo + '#'] = reply.message; varMap[P2P_SPECIAL_VALUE.REPLY_DATE_INDEX + messageNo + '#'] = reply.sentDate; varMap[P2P_SPECIAL_VALUE.REPLY_DATETIME_INDEX + messageNo + '#'] = reply.sentDateTime; if (idx == scenarioContext().p2p.replies.length - 1) { const latestReply = generalP2P.replies[generalP2P.replies.length - 1]; varMap[P2P_SPECIAL_VALUE.LATEST_REPLY] = latestReply.message; varMap[P2P_SPECIAL_VALUE.LATEST_REPLY_DATE] = latestReply.sentDate; varMap[P2P_SPECIAL_VALUE.LATEST_REPLY_DATETIME] = latestReply.sentDateTime; } }); } return varMap; }