Skip to content

Commit

Permalink
Merge branch 'develop' into enhance.garl
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 authored Jan 4, 2024
2 parents 423f716 + e0c225d commit a301580
Show file tree
Hide file tree
Showing 7 changed files with 3,104 additions and 2,418 deletions.
14 changes: 2 additions & 12 deletions src/v0/destinations/gainsight_px/data/GainsightPX_Identify.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@
},
{
"destKey": "signUpDate",
"sourceKeys": [
"traits.signUpDate",
"context.traits.signUpDate",
"timestamp",
"originalTimestamp"
],
"sourceKeys": ["traits.signUpDate", "context.traits.signUpDate"],
"required": false,
"metadata": {
"type": "timestamp"
Expand Down Expand Up @@ -109,12 +104,7 @@
},
{
"destKey": "createDate",
"sourceKeys": [
"traits.createDate",
"context.traits.createDate",
"timestamp",
"originalTimestamp"
],
"sourceKeys": ["traits.createDate", "context.traits.createDate"],
"required": false,
"metadata": {
"type": "timestamp"
Expand Down
20 changes: 17 additions & 3 deletions src/v0/destinations/gainsight_px/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* eslint-disable no-nested-ternary */
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib');
const {
InstrumentationError,
ConfigurationError,
formatTimeStamp,
} = require('@rudderstack/integrations-lib');
const { EventType } = require('../../../constants');
const {
isEmptyObject,
Expand Down Expand Up @@ -47,7 +51,7 @@ const identifyResponseBuilder = async (message, { Config }) => {
'Content-Type': JSON_MIME_TYPE,
};

const { success: isPresent } = await objectExists(userId, Config, 'user');
const { success: isUserPresent } = await objectExists(userId, Config, 'user');

let payload = constructPayload(message, identifyMapping);
const name = getValueFromMessage(message, ['traits.name', 'context.traits.name']);
Expand All @@ -56,6 +60,16 @@ const identifyResponseBuilder = async (message, { Config }) => {
payload.firstName = fName;
payload.lastName = lName;
}
// Only for the case of new user creation, if signUpDate is not provided in traits, timestamp / originalTimestamp is mapped
if (!isUserPresent && !payload.signUpDate) {
payload.signUpDate = formatTimeStamp(message.timestamp || message.originalTimestamp);
}

// Only for the case of new user creation, if createDate is not provided in traits, timestamp / originalTimestamp is mapped
if (!isUserPresent && !payload.createDate) {
payload.createDate = formatTimeStamp(message.timestamp || message.originalTimestamp);
}

let customAttributes = {};
customAttributes = extractCustomFields(
message,
Expand All @@ -75,7 +89,7 @@ const identifyResponseBuilder = async (message, { Config }) => {
type: 'USER',
};

if (isPresent) {
if (isUserPresent) {
// update user
response.method = defaultPutRequestConfig.requestMethod;
response.endpoint = `${ENDPOINTS.USERS_ENDPOINT}/${userId}`;
Expand Down
79 changes: 79 additions & 0 deletions src/v0/destinations/sprig/deleteUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const { NetworkError, ConfigurationError } = require('@rudderstack/integrations-lib');
const { httpPOST } = require('../../../adapters/network');
const {
processAxiosResponse,
getDynamicErrorType,
} = require('../../../adapters/utils/networkUtils');
const { isHttpStatusSuccess } = require('../../util');
const { executeCommonValidations } = require('../../util/regulation-api');
const tags = require('../../util/tags');
const { getUserIdBatches } = require('../../util/deleteUserUtils');
const { JSON_MIME_TYPE } = require('../../util/constant');

/**
* This function will help to delete the users one by one from the userAttributes array.
* @param {*} userAttributes Array of objects with userId, email and phone
* @param {*} config Destination.Config provided in dashboard
* @returns
*/
const userDeletionHandler = async (userAttributes, config) => {
const { apiKey } = config;

if (!apiKey) {
throw new ConfigurationError('Api Key is required for user deletion');
}

const endpoint = 'https://api.sprig.com/v2/purge/visitors';
const headers = {
Accept: JSON_MIME_TYPE,
'Content-Type': JSON_MIME_TYPE,
Authorization: `API-Key ${apiKey}`,
};
/**
* userIdBatches = [[u1,u2,u3,...batchSize],[u1,u2,u3,...batchSize]...]
* Ref doc : https://docs.sprig.com/reference/post-v2-purge-visitors-1
*/
const userIdBatches = getUserIdBatches(userAttributes, 100);
// Note: we will only get 400 status code when no user deletion is present for given userIds so we will not throw error in that case
// eslint-disable-next-line no-restricted-syntax
for (const curBatch of userIdBatches) {
// eslint-disable-next-line no-await-in-loop
const deletionResponse = await httpPOST(
endpoint,
{
userIds: curBatch,
},
{
headers,
},
{
destType: 'sprig',
feature: 'deleteUsers',
endpointPath: 'api.sprig.com/v2/purge/visitors',
},
);
const handledDelResponse = processAxiosResponse(deletionResponse);
if (!isHttpStatusSuccess(handledDelResponse.status) && handledDelResponse.status !== 400) {
throw new NetworkError(
'User deletion request failed',
handledDelResponse.status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(handledDelResponse.status),
},
handledDelResponse,
);
}
}

return {
statusCode: 200,
status: 'successful',
};
};
const processDeleteUsers = async (event) => {
const { userAttributes, config } = event;
executeCommonValidations(userAttributes);
const resp = await userDeletionHandler(userAttributes, config);
return resp;
};
module.exports = { processDeleteUsers };
Loading

0 comments on commit a301580

Please sign in to comment.