Skip to content

Commit

Permalink
Merge branch 'develop' into feat/movable-ink
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauravudia authored Mar 5, 2024
2 parents ca8d695 + d1102a2 commit 13a13d9
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/prepare-for-staging-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:
cd rudder-devops
BRANCH_NAME="shared-transformer-$TAG_NAME"
echo $BRANCH_NAME
if [ `git ls-remote --heads origin $BRANCH_NAME 2>/dev/null` ]
if [ -n `git ls-remote --heads origin $BRANCH_NAME 2>/dev/null` ]
then
echo "Staging deployment branch already exists!"
else
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [1.57.1](https://github.com/rudderlabs/rudder-transformer/compare/v1.57.0...v1.57.1) (2024-03-04)


### Bug Fixes

* amplitude fix for user operations ([7f2364e](https://github.com/rudderlabs/rudder-transformer/commit/7f2364e41167611c41003559de65cee1fece5464))
* amplitude fix for user operations ([#3153](https://github.com/rudderlabs/rudder-transformer/issues/3153)) ([31869fb](https://github.com/rudderlabs/rudder-transformer/commit/31869fb114bb141d545de01c56f57b97e5aa54a6))

## [1.57.0](https://github.com/rudderlabs/rudder-transformer/compare/v1.56.1...v1.57.0) (2024-02-29)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rudder-transformer",
"version": "1.57.0",
"version": "1.57.1",
"description": "",
"homepage": "https://github.com/rudderlabs/rudder-transformer#readme",
"bugs": {
Expand Down
13 changes: 11 additions & 2 deletions src/v0/destinations/am/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ const updateConfigProperty = (message, payload, mappingJson, validatePayload, Co
}
});
};
const identifyBuilder = (message, destination, rawPayload) => {
const userPropertiesHandler = (message, destination, rawPayload) => {
// update payload user_properties from userProperties/traits/context.traits/nested traits of Rudder message
// traits like address converted to top level user properties (think we can skip this extra processing as AM supports nesting upto 40 levels)
let traits = getFieldValueFromMessage(message, 'traits');
Expand Down Expand Up @@ -335,14 +335,16 @@ const getDefaultResponseData = (message, rawPayload, evType, groupInfo) => {
const groups = groupInfo && cloneDeep(groupInfo);
return { groups, rawPayload };
};


const getResponseData = (evType, destination, rawPayload, message, groupInfo) => {
let groups;

switch (evType) {
case EventType.IDENTIFY:
// event_type for identify event is $identify
rawPayload.event_type = IDENTIFY_AM;
rawPayload = identifyBuilder(message, destination, rawPayload);
rawPayload = userPropertiesHandler(message, destination, rawPayload);
break;
case EventType.GROUP:
// event_type for identify event is $identify
Expand All @@ -357,8 +359,15 @@ const getResponseData = (evType, destination, rawPayload, message, groupInfo) =>
case EventType.ALIAS:
break;
default:
if (destination.Config.enableEnhancedUserOperations) {
// handle all other events like track, page, screen for user properties
rawPayload = userPropertiesHandler(message, destination, rawPayload);
}
({ groups, rawPayload } = getDefaultResponseData(message, rawPayload, evType, groupInfo));
}
if (destination.Config.enableEnhancedUserOperations) {
rawPayload = AMUtils.userPropertiesPostProcess(rawPayload);
}
return { rawPayload, groups };
};

Expand Down
69 changes: 68 additions & 1 deletion src/v0/destinations/am/util.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getUnsetObj, validateEventType } = require('./utils');
const { getUnsetObj, validateEventType, userPropertiesPostProcess } = require('./utils');

describe('getUnsetObj', () => {
it("should return undefined when 'message.integrations.Amplitude.fieldsToUnset' is not array", () => {
Expand Down Expand Up @@ -97,3 +97,70 @@ describe('validateEventType', () => {
);
});
});

describe('userPropertiesPostProcess', () => {
// The function correctly removes duplicate keys found in both operation keys and root level keys.
it('should remove duplicate keys from user_properties', () => {
// Arrange
const rawPayload = {
user_properties: {
$setOnce: {
key1: 'value1',
},
$add: {
key2: 'value2',
},
key3: 'value3',
key1: 'value4',
},
};

// Act
const result = userPropertiesPostProcess(rawPayload);

// Assert
expect(result.user_properties).toEqual({
$setOnce: {
key1: 'value1',
},
$add: {
key2: 'value2',
},
$set: {
key3: 'value3',
},
});
});

// The function correctly moves root level properties to $set operation.
it('should move root level properties to $set operation when they dont belong to any other operation', () => {
// Arrange
const rawPayload = {
user_properties: {
$setOnce: {
key1: 'value1',
},
$add: {
key2: 'value2',
},
key3: 'value3',
},
};

// Act
const result = userPropertiesPostProcess(rawPayload);

// Assert
expect(result.user_properties).toEqual({
$set: {
key3: 'value3',
},
$setOnce: {
key1: 'value1',
},
$add: {
key2: 'value2',
},
});
});
});
56 changes: 56 additions & 0 deletions src/v0/destinations/am/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,61 @@ const validateEventType = (evType) => {
);
}
};


const userPropertiesPostProcess = (rawPayload) => {
const operationList = [
'$setOnce',
'$add',
'$unset',
'$append',
'$prepend',
'$preInsert',
'$postInsert',
'$remove',
];
// eslint-disable-next-line @typescript-eslint/naming-convention
const { user_properties } = rawPayload;
const userPropertiesKeys = Object.keys(user_properties).filter(
(key) => !operationList.includes(key),
);
const duplicatekeys = new Set();
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const key of userPropertiesKeys) {
// check if any of the keys are present in the user_properties $setOnce, $add, $unset, $append, $prepend, $preInsert, $postInsert, $remove keys as well as root level

if (
operationList.some(
(operation) => user_properties[operation] && user_properties[operation][key],
)
) {
duplicatekeys.add(key);
}
}
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const key of duplicatekeys) {
delete user_properties[key];
}

// Moving root level properties that doesn't belong to any operation under $set
const setProps = {};
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of Object.entries(user_properties)) {
if (!operationList.includes(key)) {
setProps[key] = value;
delete user_properties[key];
}
}

if (Object.keys(setProps).length > 0) {
user_properties.$set = setProps;
}

// eslint-disable-next-line no-param-reassign
rawPayload.user_properties = user_properties;
return rawPayload;
};

module.exports = {
getOSName,
getOSVersion,
Expand All @@ -132,4 +187,5 @@ module.exports = {
getEventId,
getUnsetObj,
validateEventType,
userPropertiesPostProcess
};

0 comments on commit 13a13d9

Please sign in to comment.