From af306a910d08eb0578ff12070156ba3ac9b36a80 Mon Sep 17 00:00:00 2001 From: mihir-4116 Date: Mon, 13 Nov 2023 17:56:26 +0530 Subject: [PATCH] chore: code review changes --- .../v2/destinations/gladly/procWorkflow.yaml | 32 ++++++++++++------- .../v2/destinations/gladly/rtWorkflow.yaml | 5 ++- src/cdk/v2/destinations/gladly/utils.js | 24 ++++++++------ 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/cdk/v2/destinations/gladly/procWorkflow.yaml b/src/cdk/v2/destinations/gladly/procWorkflow.yaml index d51c59ec36..5dd5fae8ff 100644 --- a/src/cdk/v2/destinations/gladly/procWorkflow.yaml +++ b/src/cdk/v2/destinations/gladly/procWorkflow.yaml @@ -9,7 +9,7 @@ bindings: path: ../../../../v0/util - name: getDestinationExternalID path: ../../../../v0/util - - name: httpPOST + - name: httpGET path: ../../../../adapters/network - name: processAxiosResponse path: ../../../../adapters/utils/networkUtils @@ -54,26 +54,36 @@ steps: template: | $.validatePayload($.context.payload) - - name: createUser - description: Prepare create user payload + - name: findCustomer + description: Find if customer is exist or not based on email, phone or externalCustomerId template: | const requestOptions = { headers: $.getHeaders(.destination) } - const requestPayload = $.context.payload - const endpoint = $.getEndpoint(.destination) - const rawResponse = await $.httpPOST(endpoint, requestPayload, requestOptions) + const endpoint = $.getEndpoint(.destination) + "?" + $.getQueryParams($.context.payload); + const rawResponse = await $.httpGET(endpoint,requestOptions) const processedResponse = $.processAxiosResponse(rawResponse) - processedResponse.status == 400 ? $.assertHttpResp(processedResponse, "Unable to create or update user due to " + JSON.stringify(processedResponse.response)); processedResponse - - name: buildResponseForProcessTransformation - description: build response for updateUser + - name: createCustomer + description: Build response for create customer + condition: $.outputs.findCustomer.status === 400 || ($.outputs.findCustomer.status === 200 && $.outputs.findCustomer.response.length === 0) + template: | + const response = $.defaultRequestConfig() + response.body.JSON = $.removeUndefinedAndNullValues($.context.payload) + response.endpoint = $.getEndpoint(.destination) + response.method = "POST" + response.headers = $.getHeaders(.destination) + console.log("res", $.outputs.findCustomer); + response + + - name: updateCustomer + description: Build response for update customer + condition: $.outputs.findCustomer.status === 200 && $.outputs.findCustomer.response.length > 0 template: | const response = $.defaultRequestConfig() response.body.JSON = $.removeUndefinedAndNullValues($.context.payload.{~["id"]}) - response.endpoint = $.getEndpoint(.destination) + "/" + $.context.payload.id + response.endpoint = $.getEndpoint(.destination) + "/" + $.outputs.findCustomer.response[0].id response.method = "PATCH" response.headers = $.getHeaders(.destination) - response.status = $.outputs.createUser.status response diff --git a/src/cdk/v2/destinations/gladly/rtWorkflow.yaml b/src/cdk/v2/destinations/gladly/rtWorkflow.yaml index 4230789632..341e5552c8 100644 --- a/src/cdk/v2/destinations/gladly/rtWorkflow.yaml +++ b/src/cdk/v2/destinations/gladly/rtWorkflow.yaml @@ -16,13 +16,12 @@ steps: - name: successfulEvents template: | - const status = $.getStatusCode($.requestMetadata, $.outputs.transform#idx.output.status) $.outputs.transform#idx.output.({ - "batchedRequest": .{~["status"]}, + "batchedRequest": ., "batched": false, "destination": ^[idx].destination, "metadata": ^[idx].metadata[], - "statusCode": status + "statusCode": 200 })[] - name: failedEvents template: | diff --git a/src/cdk/v2/destinations/gladly/utils.js b/src/cdk/v2/destinations/gladly/utils.js index a51fd7b194..7e572a7188 100644 --- a/src/cdk/v2/destinations/gladly/utils.js +++ b/src/cdk/v2/destinations/gladly/utils.js @@ -2,11 +2,9 @@ const get = require('get-value'); const { InstrumentationError } = require('@rudderstack/integrations-lib'); const { base64Convertor, - isNewStatusCodesAccepted, getDestinationExternalID, } = require('../../../../v0/util'); const { MappedToDestinationKey } = require('../../../../constants'); -const { HTTP_STATUS_CODES } = require('../../../../v0/util/constant'); const reservedCustomAttributes = [ 'email', @@ -139,20 +137,28 @@ const validatePayload = (payload) => { } }; -const getStatusCode = (requestMetadata, statusCode) => { - if (isNewStatusCodesAccepted(requestMetadata) && statusCode === 200) { - return HTTP_STATUS_CODES.SUPPRESS_EVENTS; +const getQueryParams = (payload) => { + if (payload.emails && payload.emails.length > 0) { + return `email=${encodeURIComponent(payload.emails[0].original)}` } - if(statusCode === 409)return 200; - return statusCode; -}; + + if (payload.phones && payload.phones.length > 0) { + return `phoneNumber=${encodeURIComponent(payload.phones[0].original)}` + } + + if (payload.externalCustomerId) { + return `externalCustomerId=${encodeURIComponent(payload.externalCustomerId)}` + } + + return undefined; +} module.exports = { getHeaders, getEndpoint, formatField, - getStatusCode, getCustomerId, + getQueryParams, validatePayload, getCustomAttributes, getExternalCustomerId