import { Startup } from "./Startup"; import fetch from "node-fetch"; import https from "https"; import { MSTLabel } from "../components/MST_KeysObject/MST_KeysObject"; import { TestMSTLabel } from "./Test_MST_KeysObject"; import { logger } from "../Logger/logger"; import { CommonKeyword } from "../components/commonKeyword"; /** * Class to read value for Language testing */ export class lmt { /** * Fetch list of label for Language Testing * @param {string} key */ static async getLabel(key: string) { if (typeof Startup.mstfinal === `undefined`) { const prodcutName = process.env.PRODUCT; let productCode; if (prodcutName == undefined) { productCode = `Dewdrop`; } else { productCode = await lmt.getProdcutcode(prodcutName); } await this.matchKeys(productCode).then(async function(map) { Startup.mstfinal = map; }); } if ((Startup.mstfinal).has(key)) { if ((Startup.mstfinal).get(key) !== `undefined`) { logger.info(`MST_Key for ` + key + ` is :` + Startup.mstfinal.get(key)); return (Startup.mstfinal).get(key); } else { logger.info(`MST_Key for ` + key + ` is undefined`); return key; } } else { logger.info(`Issue while performing operation in DDCC: MST Key not presentt for`, key); return key; } // return (await client.getStepData("mstMap")).get(key); } /** * Fetch User Language */ static async getUserLangCode() { const agent = new https.Agent({ rejectUnauthorized: false, }); const saas_token: any = await CommonKeyword.getCookie(); return await fetch(await this.getLangApi() as string, { method: `GET`, agent, headers: { 'cookie': `SAAS_COMMON_BASE_TOKEN_ID=` + saas_token, 'Content-Type': `application/json`, }, }) .then(async function(response: any) { console.log( `Response: ${response.status} ${response.statusText}` ); return response.json(); }) .then(async function(json: JSON) { return json; }) .catch(async function(err: any) { console.error(err); }); } /** * Get the language APi based on the setup provided * @param {string }setup */ static async getLangApi() { const apiURl = await CommonKeyword.getHomeURl() + `/api/a/dd/users/getDetails`; console.log(`API:`, apiURl); return apiURl; } /** * Get MSTKey API based on setup * @param {string}setup */ static async getMSTKeyAPI() { const homeURL = await CommonKeyword.getHomeURl(); const apiURl = await homeURL + `/lmt/translations/DewDrops/`; return apiURl; } /** * Returns Prodcut name for lmt * @param {string} prodcutName */ static async getProdcutcode(prodcutName: string) { let prodcut; switch (prodcutName) { case `iSupplier`: prodcut = `SIM`; break; case `DD_CNS`: prodcut = `CNS`; break; case `iPerform`: prodcut = `SPM`; break; case `DD_Homes`: prodcut = `Dewdrop`; break; default: prodcut = prodcutName; break; } return prodcut; } /** * Fetch user MSTkey from the json using api * @param {string} productCode */ static async get_USER_MST_keys(productCode: string) { const agent = new https.Agent({ rejectUnauthorized: false, }); let MST_USER: Map = new Map(); const detailsAPI: any = await this.getUserLangCode(); if (detailsAPI != undefined) { const userLang: string = detailsAPI[`langCode`]; console.log(`User Language: ` + userLang); return await fetch(await lmt.getMSTKeyAPI() as string + userLang + `.json?cb=` + detailsAPI[`data`][`localeDetails`][productCode][`lastModifiedLocale`], { method: `GET`, timeout: 20000, agent, }) .then(async function(response: any) { console.log( `Response: ${response.status} ${response.statusText}` ); return response.json(); }) .then(async function(json: any) { MST_USER = json; if (MST_USER != undefined) { return MST_USER; } else { throw new Error(`Language API failed to respond in 20 seconds`); } }); } else { throw new Error(`Deails API failed to respond in 20 seconds`); } } /** * Fetch MST label used for testing */ static async getAutomationPair() { const jSonObject = JSON.parse(JSON.stringify(MSTLabel)); const testJsonObject = JSON.parse(JSON.stringify(TestMSTLabel)); const map = new Map(); for (const value in jSonObject) { if (Object.prototype.hasOwnProperty.call(jSonObject, value)) { map.set(value, jSonObject[value]); } } for (const value in testJsonObject) { if (Object.prototype.hasOwnProperty.call(testJsonObject, value)) { map.set(value, testJsonObject[value]); } } return map; } /** * Filter value from MST key based on testing labels * @param {string} productCode */ static async matchKeys(productCode: string) { const AutoToMstValue = new Map(); console.log(`inside matchkeys`); const MST_user: any = await this.get_USER_MST_keys(productCode); if (typeof Startup.automationLMTPair === `undefined`) { Startup.automationLMTPair = await lmt.getAutomationPair(); } Startup.automationLMTPair.forEach(async function(val: string, key: string) { if (val != undefined) { let FinalValue: string = ``; let i: number = 1; if (val.indexOf !== undefined) { const keyArray: string[] = val.split(`,`); keyArray.forEach(async (keyAuto) => { FinalValue = FinalValue + MST_user[keyAuto]; if (i < keyArray.length) { FinalValue = FinalValue + ` `; i = i + 1; } }); } else { FinalValue = MST_user[val]; } AutoToMstValue.set(key, FinalValue.trim()); } }); console.log(`Create MST:`, AutoToMstValue); return AutoToMstValue; } }