Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for subscribing for RETL flow #3195

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions src/v0/destinations/klaviyo/klaviyoUtil.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
const { addSubscribeFlagToTraits } = require('./util');

describe('addSubscribeFlagToTraits', () => {
// The function should add a 'subscribe' property to the 'properties' object if it exists in the input 'traitsInfo'.
it('should add subscribe property to properties object when it exists in traitsInfo', () => {
// Arrange
const traitsInfo = {
properties: {
name: 'John',
age: 30,
},
};

// Act
const result = addSubscribeFlagToTraits(traitsInfo);

// Assert
expect(result.properties.subscribe).toBe(true);
});

// The input 'traitsInfo' object is null.
it('should create a new traitsInfo object with properties object containing subscribe property when traitsInfo is null', () => {
// Arrange
const traitsInfo = null;

// Act
const result = addSubscribeFlagToTraits(traitsInfo);

// Assert
expect(result).toEqual(null);
});

// The function should create a new 'properties' object with a 'subscribe' property and assign it to the 'traitsInfo' if 'properties' does not exist in the input 'traitsInfo'.
it("should create a new 'properties' object with a 'subscribe' property when 'properties' does not exist in traitsInfo", () => {
// Arrange
const traitsInfo = {};

// Act
const result = addSubscribeFlagToTraits(traitsInfo);

// Assert
expect(result.properties).toBeDefined();
expect(result.properties.subscribe).toBe(true);
});

// The function should return the modified 'traitsInfo' object with the 'subscribe' property added.
it('should add subscribe property to properties object when it exists in traitsInfo', () => {
// Arrange
const traitsInfo = {
properties: {
name: 'John',
age: 30,
},
};

// Act
const result = addSubscribeFlagToTraits(traitsInfo);

// Assert
expect(result.properties.subscribe).toBe(true);
});

// The input 'traitsInfo' object has an empty 'properties' object.
it('should add subscribe property to properties object when it exists in traitsInfo', () => {
// Arrange
const traitsInfo = {
properties: {},
};

// Act
const result = addSubscribeFlagToTraits(traitsInfo);

// Assert
expect(result.properties.subscribe).toBe(true);
});

// The input 'traitsInfo' object has a 'properties' object with a 'subscribe' property already present.
it('should add subscribe property to properties object when it exists in traitsInfo', () => {
// Arrange
const traitsInfo = {
properties: {
name: 'John',
age: 30,
subscribe: false,
},
};

// Act
const result = addSubscribeFlagToTraits(traitsInfo);

// Assert
expect(result.properties.subscribe).toBe(false);
});

// The function should not modify any other properties of the 'traitsInfo' object.
it('should not modify any other properties of the traitsInfo object', () => {
// Arrange
const traitsInfo = {
properties: {
name: 'John',
age: 30,
},
};

// Act
const result = addSubscribeFlagToTraits(traitsInfo);

// Assert
expect(result.properties.subscribe).toBe(true);
expect(result.properties.name).toBe('John');
expect(result.properties.age).toBe(30);
});

// The function should handle cases where the input 'traitsInfo' object has nested objects.
it('should add subscribe property to nested properties object when it exists in traitsInfo', () => {
// Arrange
const traitsInfo = {
properties: {
name: 'John',
age: 30,
address: {
street: '123 Main St',
city: 'New York',
},
},
};

// Act
const result = addSubscribeFlagToTraits(traitsInfo);

// Assert
expect(result.properties.subscribe).toBe(true);
expect(result.properties.address).toMatchObject({
street: '123 Main St',
city: 'New York',
});
});

// The function should handle cases where the input 'traitsInfo' object has properties with null or undefined values.
it('should add subscribe property to properties object when it exists in traitsInfo', () => {
// Arrange
const traitsInfo = {
properties: {
name: 'John',
age: 30,
address: null,
},
};

// Act
const result = addSubscribeFlagToTraits(traitsInfo);

// Assert
expect(result.properties.subscribe).toBe(true);
});
});
5 changes: 4 additions & 1 deletion src/v0/destinations/klaviyo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
batchSubscribeEvents,
getIdFromNewOrExistingProfile,
profileUpdateResponseBuilder,
addSubcribeFlagToTraits,
} = require('./util');
const {
defaultRequestConfig,
Expand Down Expand Up @@ -55,11 +56,13 @@
// If listId property is present try to subscribe/member user in list
const { privateApiKey, enforceEmailAsPrimary, listId, flattenProperties } = destination.Config;
const mappedToDestination = get(message, MappedToDestinationKey);
let traitsInfo = getFieldValueFromMessage(message, 'traits');
if (mappedToDestination) {
addExternalIdToTraits(message);
adduserIdFromExternalId(message);
traitsInfo = addSubcribeFlagToTraits(traitsInfo);
}
const traitsInfo = getFieldValueFromMessage(message, 'traits');

Check warning on line 65 in src/v0/destinations/klaviyo/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/klaviyo/transform.js#L65

Added line #L65 was not covered by tests
let propertyPayload = constructPayload(message, MAPPING_CONFIG[category.name]);
// Extract other K-V property from traits about user custom properties
let customPropertyPayload = {};
Expand Down
29 changes: 28 additions & 1 deletion src/v0/destinations/klaviyo/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const { defaultRequestConfig } = require('rudder-transformer-cdk/build/utils');
const lodash = require('lodash');
const { NetworkError, InstrumentationError } = require('@rudderstack/integrations-lib');
const {
NetworkError,
InstrumentationError,
isDefinedAndNotNull,
} = require('@rudderstack/integrations-lib');
const { WhiteListedTraits } = require('../../../constants');

const {
Expand Down Expand Up @@ -305,6 +309,28 @@ const batchSubscribeEvents = (subscribeRespList) => {
return batchedResponseList;
};

const addSubscribeFlagToTraits = (traitsInfo) => {
let traits = traitsInfo;
if (!isDefinedAndNotNull(traits)) {
return traits;
}
// check if properties already contains subscribe flag

if (traits.properties) {
if (traits.properties.subscribe === undefined) {
traits.properties.subscribe = true;
} else {
// return if subscribe flag is already present
return traits;
}
} else {
traits = {
properties: { subscribe: true },
utsabc marked this conversation as resolved.
Show resolved Hide resolved
};
}
return traits;
};

module.exports = {
subscribeUserToList,
createCustomerProperties,
Expand All @@ -313,4 +339,5 @@ module.exports = {
batchSubscribeEvents,
profileUpdateResponseBuilder,
getIdFromNewOrExistingProfile,
addSubscribeFlagToTraits,
};
Loading