Skip to content

Commit

Permalink
fix: reduce cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
anantjain45823 committed Aug 19, 2024
1 parent b8d6be0 commit f2ddc92
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/v0/destinations/klaviyo/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,24 @@ const constructProfile = (message, destination, isIdentifyCall) => {
return { data };
};

/** This function update profile with consents for subscribing to email and/or phone
* @param {*} profileAttributes
* @param {*} subscribeConsent
* @param {*} email
* @param {*} phone
*/
const updateProfileWithConsents = (profileAttributes, subscribeConsent, email, phone) => {
let consent = subscribeConsent;
if (!Array.isArray(consent)) {
consent = [consent];

Check warning on line 466 in src/v0/destinations/klaviyo/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/klaviyo/util.js#L466

Added line #L466 was not covered by tests
}
if (consent.includes('email') && email) {
set(profileAttributes, 'subscriptions.email.marketing.consent', 'SUBSCRIBED');
}
if (consent.includes('sms') && phone) {
set(profileAttributes, 'subscriptions.sms.marketing.consent', 'SUBSCRIBED');
}
};
/**
* This function is used for creating profile response for subscribing users to a particular list for V2
* DOCS: https://developers.klaviyo.com/en/reference/subscribe_profiles
Expand All @@ -463,7 +481,7 @@ const subscribeOrUnsubscribeUserToListV2 = (message, traitsInfo, destination, op
// listId from message properties are preferred over Config listId
const { consent } = destination.Config;
let { listId } = destination.Config;
let subscribeConsent = traitsInfo.consent || traitsInfo.properties?.consent || consent;
const subscribeConsent = traitsInfo.consent || traitsInfo.properties?.consent || consent;
const email = getFieldValueFromMessage(message, 'email');
const phone = getFieldValueFromMessage(message, 'phone');
const profileAttributes = {
Expand All @@ -473,15 +491,7 @@ const subscribeOrUnsubscribeUserToListV2 = (message, traitsInfo, destination, op

// used only for subscription and not for unsubscription
if (operation === 'subscribe' && subscribeConsent) {
if (!Array.isArray(subscribeConsent)) {
subscribeConsent = [subscribeConsent];
}
if (subscribeConsent.includes('email') && email) {
set(profileAttributes, 'subscriptions.email.marketing.consent', 'SUBSCRIBED');
}
if (subscribeConsent.includes('sms') && phone) {
set(profileAttributes, 'subscriptions.sms.marketing.consent', 'SUBSCRIBED');
}
updateProfileWithConsents(profileAttributes, subscribeConsent, email, phone);
}

const profile = removeUndefinedAndNullValues({
Expand Down

0 comments on commit f2ddc92

Please sign in to comment.