Skip to content

Commit

Permalink
chore: propagate metadata for user destination
Browse files Browse the repository at this point in the history
  • Loading branch information
Sai Sankeerth committed Jun 19, 2024
1 parent 763283c commit 0c4ce7c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
40 changes: 21 additions & 19 deletions src/v0/destinations/user/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,24 @@ const responseBuilder = async (payload, endpoint, method, apiKey) => {
throw new TransformationError('Something went wrong while constructing the payload');
};

const identifyResponseBuilder = async (message, destination) => {
const identifyResponseBuilder = async (event) => {
const { destination } = event;
let builder;
const user = await retrieveUserFromLookup(message, destination);
const user = await retrieveUserFromLookup(event);
const { Config } = destination;
const { apiKey } = Config;
// If user already exist we will update it else creates a new user
if (!user) {
builder = createOrUpdateUserPayloadBuilder(message, destination);
builder = createOrUpdateUserPayloadBuilder(event);

Check warning on line 54 in src/v0/destinations/user/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/user/transform.js#L54

Added line #L54 was not covered by tests
} else {
const { id } = user;
builder = createOrUpdateUserPayloadBuilder(message, destination, id);
builder = createOrUpdateUserPayloadBuilder(event, id);
}
const { payload, endpoint, method } = builder;
return responseBuilder(payload, endpoint, method, apiKey);
};

const trackResponseBuilder = async (message, destination) => {
const trackResponseBuilder = async ({ message, destination, metadata }) => {
if (!message.event) {
throw new InstrumentationError('Parameter event is required');
}
Expand All @@ -68,7 +69,7 @@ const trackResponseBuilder = async (message, destination) => {
let endpoint;
let method;
let builder;
const user = await retrieveUserFromLookup(message, destination);
const user = await retrieveUserFromLookup({ message, destination, metadata });
const { Config } = destination;
const { apiKey, appSubdomain } = Config;
if (user) {
Expand All @@ -85,12 +86,12 @@ const trackResponseBuilder = async (message, destination) => {
);
};

const pageResponseBuilder = async (message, destination) => {
const pageResponseBuilder = async ({ message, destination, metadata }) => {
let payload;
let endpoint;
let method;
let builder;
const user = await retrieveUserFromLookup(message, destination);
const user = await retrieveUserFromLookup({ message, destination, metadata });
const { Config } = destination;
const { apiKey, appSubdomain } = Config;
if (user) {
Expand All @@ -106,26 +107,26 @@ const pageResponseBuilder = async (message, destination) => {
);
};

const groupResponseBuilder = async (message, destination) => {
const groupResponseBuilder = async ({ message, destination, metadata }) => {
validateGroupPayload(message);

let payload;
let endpoint;
let method;
let builder;
const user = await getUserByCustomId(message, destination);
const user = await getUserByCustomId(message, destination, metadata);
const { Config } = destination;
const { apiKey, appSubdomain } = Config;
/*
* user exist -> create or update the company -> add user to company
* user does not exist -> throw an error
*/
if (user) {
let company = await getCompanyByCustomId(message, destination);
let company = await getCompanyByCustomId(message, destination, metadata);
if (!company) {
company = await createCompany(message, destination);
company = await createCompany(message, destination, metadata);

Check warning on line 127 in src/v0/destinations/user/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/user/transform.js#L127

Added line #L127 was not covered by tests
} else {
company = await updateCompany(message, destination, company);
company = await updateCompany(message, destination, company, metadata);
}
builder = addUserToCompanyPayloadBuilder(user, company);
payload = builder.payload;
Expand All @@ -137,7 +138,8 @@ const groupResponseBuilder = async (message, destination) => {
throw new NetworkInstrumentationError('No user found with given userId');
};

const processEvent = async (message, destination) => {
const processEvent = async (event) => {
const { message } = event;
// Validating if message type is even given or not
if (!message.type) {
throw new InstrumentationError('Event type is required');
Expand All @@ -146,24 +148,24 @@ const processEvent = async (message, destination) => {
let response;
switch (messageType) {
case EventType.IDENTIFY:
response = await identifyResponseBuilder(message, destination);
response = await identifyResponseBuilder(event);
break;
case EventType.GROUP:
response = await groupResponseBuilder(message, destination);
response = await groupResponseBuilder(event);
break;
case EventType.TRACK:
response = await trackResponseBuilder(message, destination);
response = await trackResponseBuilder(event);
break;
case EventType.PAGE:
response = await pageResponseBuilder(message, destination);
response = await pageResponseBuilder(event);
break;
default:
throw new InstrumentationError(`Event type ${messageType} is not supported`);
}
return response;
};

const process = async (event) => processEvent(event.message, event.destination);
const process = async (event) => processEvent(event);

const processRouterDest = async (inputs, reqMetadata) => {
const respList = await simpleProcessRouterDest(inputs, process, reqMetadata);
Expand Down
35 changes: 21 additions & 14 deletions src/v0/destinations/user/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ const validateGroupPayload = (message) => {
* @param {*} destination
* @returns
*/
const createCompany = async (message, destination) => {
const createCompany = async (message, destination, metadata) => {
const commonCompanyPropertiesPayload = constructPayload(
message,
MAPPING_CONFIG[CONFIG_CATEGORIES.CREATE_COMPANY.name],
Expand Down Expand Up @@ -240,6 +240,7 @@ const createCompany = async (message, destination) => {
endpointPath: `/companies/`,
requestMethod: 'POST',
module: 'router',
metadata,
});
const data = processAxiosResponse(response);
return data.response;
Expand All @@ -253,7 +254,7 @@ const createCompany = async (message, destination) => {
* @param {*} company
* @returns
*/
const updateCompany = async (message, destination, company) => {
const updateCompany = async (message, destination, company, metadata) => {
const commonCompanyPropertiesPayload = constructPayload(
message,
MAPPING_CONFIG[CONFIG_CATEGORIES.UPDATE_COMPANY.name],
Expand Down Expand Up @@ -283,6 +284,7 @@ const updateCompany = async (message, destination, company) => {
endpointPath: `/companies/`,
requestMethod: 'PUT',
module: 'router',
metadata,
});
const data = processAxiosResponse(response);
return data.response;
Expand All @@ -296,7 +298,7 @@ const updateCompany = async (message, destination, company) => {
* @param {*} appSubdomain
* @returns
*/
const getUserByUserKey = async (apiKey, userKey, appSubdomain) => {
const getUserByUserKey = async (apiKey, userKey, appSubdomain, metadata) => {
const endpoint = prepareUrl(`${BASE_ENDPOINT}/users/search/?key=${userKey}`, appSubdomain);
const requestOptions = {
headers: {
Expand All @@ -312,6 +314,7 @@ const getUserByUserKey = async (apiKey, userKey, appSubdomain) => {
endpointPath: `/users/search`,
requestMethod: 'GET',
module: 'router',
metadata,
});
const processedUserResponse = processAxiosResponse(userResponse);
if (processedUserResponse.status === 200) {
Expand All @@ -328,7 +331,7 @@ const getUserByUserKey = async (apiKey, userKey, appSubdomain) => {
* @param {*} appSubdomain
* @returns
*/
const getUserByEmail = async (apiKey, email, appSubdomain) => {
const getUserByEmail = async (apiKey, email, appSubdomain, metadata) => {
if (!email) {
throw new InstrumentationError('Lookup field : email value is not present');
}
Expand All @@ -348,6 +351,7 @@ const getUserByEmail = async (apiKey, email, appSubdomain) => {
endpointPath: `/users/search/?email`,
requestMethod: 'GET',
module: 'router',
metadata,
});
const processedUserResponse = processAxiosResponse(userResponse);

Expand All @@ -366,7 +370,7 @@ const getUserByEmail = async (apiKey, email, appSubdomain) => {
* @param {*} appSubdomain
* @returns
*/
const getUserByPhoneNumber = async (apiKey, phoneNumber, appSubdomain) => {
const getUserByPhoneNumber = async (apiKey, phoneNumber, appSubdomain, metadata) => {
if (!phoneNumber) {
throw new InstrumentationError('Lookup field : phone value is not present');
}
Expand All @@ -389,6 +393,7 @@ const getUserByPhoneNumber = async (apiKey, phoneNumber, appSubdomain) => {
endpointPath: `/users/search/?phone_number`,
requestMethod: 'GET',
module: 'router',
metadata,
});
const processedUserResponse = processAxiosResponse(userResponse);

Expand All @@ -415,7 +420,7 @@ const getUserByPhoneNumber = async (apiKey, phoneNumber, appSubdomain) => {
* @param {*} destination
* @returns
*/
const getUserByCustomId = async (message, destination) => {
const getUserByCustomId = async (message, destination, metadata) => {
const { Config } = destination;
const { appSubdomain, apiKey } = Config;
const userCustomId = getFieldValueFromMessage(message, 'userId');
Expand All @@ -436,6 +441,7 @@ const getUserByCustomId = async (message, destination) => {
endpointPath: `/users-by-id/`,
requestMethod: 'GET',
module: 'router',
metadata,
});
const processedUserResponse = processAxiosResponse(userResponse);

Expand All @@ -453,7 +459,7 @@ const getUserByCustomId = async (message, destination) => {
* @param {*} destination
* @returns
*/
const getCompanyByCustomId = async (message, destination) => {
const getCompanyByCustomId = async (message, destination, metadata) => {
const { Config } = destination;
const { appSubdomain, apiKey } = Config;
const companyCustomId = getFieldValueFromMessage(message, 'groupId');
Expand All @@ -474,6 +480,7 @@ const getCompanyByCustomId = async (message, destination) => {
endpointPath: `/companies-by-id/`,
requestMethod: 'GET',
module: 'router',
metadata,
});
const processedUserResponse = processAxiosResponse(response);
if (processedUserResponse.status === 200) {
Expand All @@ -490,12 +497,12 @@ const getCompanyByCustomId = async (message, destination) => {
* @param {*} destination
* @returns
*/
const retrieveUserFromLookup = async (message, destination) => {
const retrieveUserFromLookup = async ({ message, destination, metadata }) => {
const { Config } = destination;
const { appSubdomain, apiKey } = Config;
const userKey = getDestinationExternalID(message, 'userKey');
if (isDefinedAndNotNullAndNotEmpty(userKey)) {
return getUserByUserKey(apiKey, userKey, appSubdomain);
return getUserByUserKey(apiKey, userKey, appSubdomain, metadata);
}

const integrationsObj = getIntegrationsObj(message, 'user');
Expand All @@ -504,11 +511,11 @@ const retrieveUserFromLookup = async (message, destination) => {
const lookupFieldValue = getFieldValueFromMessage(message, lookupField);

if (lookupField === 'email') {
return getUserByEmail(apiKey, lookupFieldValue, appSubdomain);
return getUserByEmail(apiKey, lookupFieldValue, appSubdomain, metadata);
}

if (lookupField === 'phone') {
return getUserByPhoneNumber(apiKey, lookupFieldValue, appSubdomain);
return getUserByPhoneNumber(apiKey, lookupFieldValue, appSubdomain, metadata);

Check warning on line 518 in src/v0/destinations/user/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/user/utils.js#L518

Added line #L518 was not covered by tests
}

throw new InstrumentationError(
Expand All @@ -517,11 +524,11 @@ const retrieveUserFromLookup = async (message, destination) => {
} else {
const userId = getValueFromMessage(message, 'userId');
if (userId) {
return getUserByCustomId(message, destination);
return getUserByCustomId(message, destination, metadata);
}
const email = getFieldValueFromMessage(message, 'email');
if (isDefinedAndNotNullAndNotEmpty(email)) {
return getUserByEmail(apiKey, email, appSubdomain);
return getUserByEmail(apiKey, email, appSubdomain, metadata);

Check warning on line 531 in src/v0/destinations/user/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/user/utils.js#L531

Added line #L531 was not covered by tests
}

throw new InstrumentationError('Default lookup field : email value is empty');
Expand All @@ -535,7 +542,7 @@ const retrieveUserFromLookup = async (message, destination) => {
* @param {*} id
* @returns
*/
const createOrUpdateUserPayloadBuilder = (message, destination, id = null) => {
const createOrUpdateUserPayloadBuilder = ({ message, destination }, id = null) => {
const { appSubdomain } = destination.Config;
const commonUserPropertiesPayload = constructPayload(
message,
Expand Down

0 comments on commit 0c4ce7c

Please sign in to comment.