Skip to content

Commit

Permalink
chore: addressed all comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Vikas Venkatraman authored and Vikas Venkatraman committed Jun 17, 2024
1 parent bd9b946 commit 44d6c05
Show file tree
Hide file tree
Showing 9 changed files with 769 additions and 666 deletions.
53 changes: 8 additions & 45 deletions src/v0/destinations/fb_custom_audience/recordTransform.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/* eslint-disable no-const-assign */
const lodash = require('lodash');
const get = require('get-value');
const {
InstrumentationError,
ConfigurationError,
getErrorRespEvents,
} = require('@rudderstack/integrations-lib');
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib');
const { schemaFields } = require('./config');
const { MappedToDestinationKey } = require('../../../constants');
const stats = require('../../../util/stats');
Expand All @@ -15,8 +11,8 @@ const {
checkSubsetOfArray,
returnArrayOfSubarrays,
getSuccessRespEvents,
generateErrorObject,
} = require('../../util');
const { getErrorResponse, createFinalResponse } = require('../../util/recordUtils');
const {
ensureApplicableFormat,
getUpdatedDataElement,
Expand All @@ -26,19 +22,6 @@ const {
getDataSource,
} = require('./util');

function getErrorMetaData(inputs, acceptedOperations) {
const metadata = [];
// eslint-disable-next-line no-restricted-syntax
for (const key in inputs) {
if (!acceptedOperations.includes(key)) {
inputs[key].forEach((input) => {
metadata.push(input.metadata);
});
}
}
return metadata;
}

const processRecordEventArray = (
recordChunksArray,
userSchema,
Expand Down Expand Up @@ -177,8 +160,6 @@ async function processRecordInputs(groupedRecordInputs) {
record.message.action?.toLowerCase(),
);

const finalResponse = [];

let insertResponse;
let deleteResponse;
let updateResponse;
Expand Down Expand Up @@ -238,32 +219,14 @@ async function processRecordInputs(groupedRecordInputs) {
);
}

const eventTypes = ['update', 'insert', 'delete'];
const errorMetaData = [];
const errorMetaDataObject = getErrorMetaData(groupedRecordsByAction, eventTypes);
if (errorMetaDataObject.length > 0) {
errorMetaData.push(errorMetaDataObject);
}
const errorResponse = getErrorResponse(groupedRecordsByAction);

const error = new InstrumentationError('Invalid action type in record event');
const errorObj = generateErrorObject(error);
const errorResponseList = errorMetaData.map((metadata) =>
getErrorRespEvents(metadata, errorObj.status, errorObj.message, errorObj.statTags),
const finalResponse = createFinalResponse(
deleteResponse,
insertResponse,
updateResponse,
errorResponse,
);

if (deleteResponse && deleteResponse.batchedRequest.length > 0) {
finalResponse.push(deleteResponse);
}
if (insertResponse && insertResponse.batchedRequest.length > 0) {
finalResponse.push(insertResponse);
}
if (updateResponse && updateResponse.batchedRequest.length > 0) {
finalResponse.push(updateResponse);
}
if (errorResponseList.length > 0) {
finalResponse.push(...errorResponseList);
}

if (finalResponse.length === 0) {
throw new InstrumentationError(
'Missing valid parameters, unable to generate transformed payload',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
/* eslint-disable no-const-assign */
const lodash = require('lodash');
const { InstrumentationError, getErrorRespEvents } = require('@rudderstack/integrations-lib');
const { InstrumentationError } = require('@rudderstack/integrations-lib');
const {
getValueFromMessage,
getAccessToken,
constructPayload,
returnArrayOfSubarrays,
getSuccessRespEvents,
generateErrorObject,
} = require('../../util');
const { populateConsentFromConfig } = require('../../util/googleUtils');
const { populateIdentifiers, responseBuilder } = require('./util');

const { getErrorResponse, createFinalResponse } = require('../../util/recordUtils');
const { offlineDataJobsMapping, consentConfigMap } = require('./config');

function getErrorMetaData(inputs, acceptedOperations) {
const metadata = [];
// eslint-disable-next-line no-restricted-syntax
for (const key in inputs) {
if (!acceptedOperations.includes(key)) {
inputs[key].forEach((input) => {
metadata.push(input.metadata);
});
}
}
return metadata;
}

const processRecordEventArray = (
records,
message,
Expand Down Expand Up @@ -92,16 +78,14 @@ const processRecordEventArray = (
};

async function processRecordInputs(groupedRecordInputs) {
const { destination, message, metadata } = groupedRecordInputs[0];
const accessToken = getAccessToken(metadata, 'access_token');
const { destination, message, metadata } = groupedRecordInputs[0];
const accessToken = getAccessToken(metadata, 'accessToken');
const developerToken = getValueFromMessage(metadata, 'secret.developer_token');

const groupedRecordsByAction = lodash.groupBy(groupedRecordInputs, (record) =>
record.message.action?.toLowerCase(),
);

const finalResponse = [];

let insertResponse;
let deleteResponse;
let updateResponse;
Expand Down Expand Up @@ -139,30 +123,17 @@ async function processRecordInputs(groupedRecordInputs) {
);
}

const eventTypes = ['update', 'insert', 'delete'];
const errorMetaData = [];
const errorMetaDataObject = getErrorMetaData(groupedRecordsByAction, eventTypes);
if (errorMetaDataObject.length > 0) {
errorMetaData.push(errorMetaDataObject);
}

const error = new InstrumentationError('Invalid action type in record event');
const errorObj = generateErrorObject(error);
const errorResponseList = errorMetaData.map((data) =>
getErrorRespEvents(data, errorObj.status, errorObj.message, errorObj.statTags),
const errorResponse = getErrorResponse(groupedRecordsByAction);
const finalResponse = createFinalResponse(
deleteResponse,
insertResponse,
updateResponse,
errorResponse,
);

if (deleteResponse && deleteResponse.batchedRequest.length > 0) {
finalResponse.push(deleteResponse);
}
if (insertResponse && insertResponse.batchedRequest.length > 0) {
finalResponse.push(insertResponse);
}
if (updateResponse && updateResponse.batchedRequest.length > 0) {
finalResponse.push(updateResponse);
}
if (errorResponseList.length > 0) {
finalResponse.push(...errorResponseList);
if (finalResponse.length === 0) {
throw new InstrumentationError(

Check warning on line 134 in src/v0/destinations/google_adwords_remarketing_lists/recordTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/google_adwords_remarketing_lists/recordTransform.js#L134

Added line #L134 was not covered by tests
'Missing valid parameters, unable to generate transformed payload',
);
}

return finalResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const processEvent = async (metadata, message, destination) => {
);
}

const accessToken = getAccessToken(metadata, 'access_token');
const accessToken = getAccessToken(metadata, 'accessToken');
const developerToken = getValueFromMessage(metadata, 'secret.developer_token');

Object.values(createdPayload).forEach((data) => {
Expand Down
203 changes: 203 additions & 0 deletions src/v0/destinations/google_adwords_remarketing_lists/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
const { populateIdentifiers, responseBuilder } = require('./util');

const accessToken = 'abcd1234';
const developerToken = 'ijkl9101';
const body = {
operations: [
{
create: {
userIdentifiers: [
{
hashedEmail: 'd3142c8f9c9129484daf28df80cc5c955791efed5e69afabb603bc8cb9ffd419',
},
{
hashedPhoneNumber: '8846dcb6ab2d73a0e67dbd569fa17cec2d9d391e5b05d1dd42919bc21ae82c45',
},
{
addressInfo: {
hashedFirstName: '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
hashedLastName: 'dcf000c2386fb76d22cefc0d118a8511bb75999019cd373df52044bccd1bd251',
countryCode: 'US',
postalCode: '1245',
},
},
],
},
},
],
};
const baseDestination = {
Config: {
rudderAccountId: '258Yea7usSKNpbkIaesL9oJ9iYw',
listId: '7090784486',
customerId: '7693729833',
loginCustomerId: '',
subAccount: false,
userSchema: ['email', 'phone', 'addressInfo'],
isHashRequired: true,
typeOfList: 'General',
},
};
const consentBlock = {
adPersonalization: 'UNSPECIFIED',
adUserData: 'UNSPECIFIED',
};
const message = {
action: 'insert',
context: {
ip: '14.5.67.21',
library: {
name: 'http',
},
},
recordId: '2',
rudderId: '2',
fields: {
email: 'd3142c8f9c9129484daf28df80cc5c955791efed5e69afabb603bc8cb9ffd419',
phone: '8846dcb6ab2d73a0e67dbd569fa17cec2d9d391e5b05d1dd42919bc21ae82c45',
firstName: '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
lastName: 'dcf000c2386fb76d22cefc0d118a8511bb75999019cd373df52044bccd1bd251',
country: 'US',
postalCode: '1245',
},
type: 'record',
};
const expectedResponse = {
version: '1',
type: 'REST',
method: 'POST',
endpoint: 'https://googleads.googleapis.com/v16/customers/7693729833/offlineUserDataJobs',
headers: {
Authorization: 'Bearer abcd1234',
'Content-Type': 'application/json',
'developer-token': 'ijkl9101',
},
params: {
listId: '7090784486',
customerId: '7693729833',
consent: {
adPersonalization: 'UNSPECIFIED',
adUserData: 'UNSPECIFIED',
},
},
body: {
JSON: {
operations: [
{
create: {
userIdentifiers: [
{
hashedEmail: 'd3142c8f9c9129484daf28df80cc5c955791efed5e69afabb603bc8cb9ffd419',
},
{
hashedPhoneNumber:
'8846dcb6ab2d73a0e67dbd569fa17cec2d9d391e5b05d1dd42919bc21ae82c45',
},
{
addressInfo: {
hashedFirstName:
'9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
hashedLastName:
'dcf000c2386fb76d22cefc0d118a8511bb75999019cd373df52044bccd1bd251',
countryCode: 'US',
postalCode: '1245',
},
},
],
},
},
],
},
JSON_ARRAY: {},
XML: {},
FORM: {},
},
files: {},
};
const attributeArray = [
{
email: '[email protected]',
phone: '@09876543210',
firstName: 'test',
lastName: 'rudderlabs',
country: 'US',
postalCode: '1245',
},
];

const hashedArray = [
{
hashedEmail: 'd3142c8f9c9129484daf28df80cc5c955791efed5e69afabb603bc8cb9ffd419',
},
{
hashedPhoneNumber: '8846dcb6ab2d73a0e67dbd569fa17cec2d9d391e5b05d1dd42919bc21ae82c45',
},
{
addressInfo: {
hashedFirstName: '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
hashedLastName: 'dcf000c2386fb76d22cefc0d118a8511bb75999019cd373df52044bccd1bd251',
countryCode: 'US',
postalCode: '1245',
},
},
];

describe('GARL utils test', () => {
describe('responseBuilder function tests', () => {
it('Should return correct response for given payload', () => {
const response = responseBuilder(
accessToken,
developerToken,
body,
baseDestination,
message,
consentBlock,
);
expect(response).toEqual(expectedResponse);
});

it('Should throw error if subaccount is true and loginCustomerId is not defined', () => {
try {
const destination2 = Object.create(baseDestination);
destination2.Config.subAccount = true;
destination2.Config.loginCustomerId = '';
const response = responseBuilder(
accessToken,
developerToken,
body,
destination2,
message,
consentBlock,
);
expect(response).toEqual();
} catch (error) {
expect(error.message).toEqual(`loginCustomerId is required as subAccount is true.`);
}
});

it('Should throw error if operationAudienceId is not defined', () => {
try {
const destination1 = Object.create(baseDestination);
destination1.Config.listId = '';
const response = responseBuilder(
accessToken,
developerToken,
body,
destination1,
message,
consentBlock,
);
expect(response).toEqual();
} catch (error) {
expect(error.message).toEqual(`List ID is a mandatory field`);
}
});
});

describe('populateIdentifiers function tests', () => {
it('Should hash and return identifiers for a given list of attributes', () => {
const identifier = populateIdentifiers(attributeArray, baseDestination);
expect(identifier).toEqual(hashedArray);
});
});
});
Loading

0 comments on commit 44d6c05

Please sign in to comment.