Skip to content

Commit

Permalink
chore: code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-4116 committed Nov 13, 2023
1 parent 3460dc1 commit af306a9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
32 changes: 21 additions & 11 deletions src/cdk/v2/destinations/gladly/procWorkflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
5 changes: 2 additions & 3 deletions src/cdk/v2/destinations/gladly/rtWorkflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
24 changes: 15 additions & 9 deletions src/cdk/v2/destinations/gladly/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit af306a9

Please sign in to comment.