From b8f70482ab6439920b154ac40a4d4e3d2e8f085c Mon Sep 17 00:00:00 2001 From: 1v4n4 Date: Fri, 14 Jun 2024 10:18:31 +0200 Subject: [PATCH] Add log for new feature --- dist/crm/gpdedatahub/client.js | 37 ++++++++++++++++++++-------------- src/crm/gpdedatahub/client.ts | 1 + 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/dist/crm/gpdedatahub/client.js b/dist/crm/gpdedatahub/client.js index d2a201d..bf23020 100644 --- a/dist/crm/gpdedatahub/client.js +++ b/dist/crm/gpdedatahub/client.js @@ -18,33 +18,40 @@ dotenv_1.default.config(); const url = process.env.CRM_URL; const username = process.env.CRM_USERNAME; const password = process.env.CRM_PASSWORD; +const testUrl = process.env.CRM_TEST_URL; +const testUsername = process.env.CRM_TEST_USERNAME; +const testPassword = process.env.CRM_TEST_PASSWORD; if (!url || !username || !password) { - console.error("no credentials"); - process.exit(); + console.error("No credentials"); + process.exit(1); } -const authToken = `${username}:${password}`; -const tokenEncoded = Buffer.from(authToken).toString('base64'); -const headers = { - "Authorization": `Basic ${tokenEncoded}`, - "Content-type": "application/json" +const getEncodedToken = (username, password) => { + return username && password ? Buffer.from(`${username}:${password}`).toString('base64') : null; }; +const tokenEncoded = getEncodedToken(username, password); +// If there is no test environment, use production credentials +const testTokenEncoded = getEncodedToken(testUsername, testPassword) || tokenEncoded; const postAction = (action) => __awaiter(void 0, void 0, void 0, function* () { + const isTest = action.action.testing || false; + const requestUrl = isTest ? (testUrl || url) : url; + const headers = { + "Authorization": `Basic ${isTest ? testTokenEncoded : tokenEncoded}`, + "Content-type": "application/json" + }; const body = JSON.stringify(action); try { - const response = yield fetch(url, { + const response = yield fetch(requestUrl, { method: "POST", - headers: { - "Authorization": `Basic ${tokenEncoded}`, - "Content-type": "application/json" - }, + headers: headers, body: body }); - //we only get status = 200 if everything is fine; + // We only get status = 200 if everything is fine; + console.log(action.actionId, requestUrl); return response.status; } catch (error) { - console.error('post error: ', error); - throw (error); + console.error('Post error:', error); + throw error; } }); exports.postAction = postAction; diff --git a/src/crm/gpdedatahub/client.ts b/src/crm/gpdedatahub/client.ts index a073aab..fa67440 100644 --- a/src/crm/gpdedatahub/client.ts +++ b/src/crm/gpdedatahub/client.ts @@ -42,6 +42,7 @@ export const postAction = async (action: GPAction): Promise => { }); // We only get status = 200 if everything is fine; + console.log(action.actionId, requestUrl) return response.status; } catch (error: any) { console.error('Post error:', error);