Skip to content

Commit

Permalink
fix: refactor adjust using optional chaining (#2671)
Browse files Browse the repository at this point in the history
fix: refactor adj using opitonal chaining
  • Loading branch information
anantjain45823 authored Sep 29, 2023
1 parent b784800 commit f63b493
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 23 deletions.
16 changes: 10 additions & 6 deletions src/v0/destinations/adj/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ const rejectParams = ['revenue', 'currency'];

function responseBuilderSimple(message, category, destination) {
const payload = constructPayload(message, MAPPING_CONFIG[category.name]);
const { appToken, customMappings, environment } = destination.Config;
const { appToken, customMappings, environment, partnerParamsKeys } = destination.Config;
if (!appToken) {
throw new ConfigurationError(
'App Token is not present. Please configure your app token from config dashbaord',
);
}
const platform = get(message, 'context.device.type');
const id = get(message, 'context.device.id');
if (typeof platform !== 'string' || !platform || !id) {
Expand All @@ -37,7 +42,7 @@ function responseBuilderSimple(message, category, destination) {
throw new InstrumentationError('Device type not valid');
}
if (payload.revenue) {
payload.currency = message.properties.currency || 'USD';
payload.currency = message?.properties?.currency || 'USD';
}
const hashMap = getHashFromArray(customMappings, 'from', 'to', false);
if (payload && message.event && hashMap[message.event]) {
Expand All @@ -46,11 +51,11 @@ function responseBuilderSimple(message, category, destination) {
Accept: '*/*',
};

const partnerParamsKeysMap = getHashFromArray(destination?.Config?.partnerParamsKeys);
const partnerParamsKeysMap = getHashFromArray(partnerParamsKeys);
if (partnerParamsKeysMap) {
payload.partner_params = {};
Object.keys(partnerParamsKeysMap).forEach((key) => {
if (message.properties[key]) {
if (message?.properties?.[key]) {
payload.partner_params[partnerParamsKeysMap[key]] = message.properties[key].toString();
}
});
Expand Down Expand Up @@ -81,9 +86,8 @@ function responseBuilderSimple(message, category, destination) {
// fail-safety for developer error
if (!message.event || !hashMap[message.event]) {
throw new ConfigurationError('No event token mapped for this event');
} else {
throw new TransformationError('Payload could not be constructed');
}
throw new TransformationError('Payload could not be constructed');
}

const processEvent = (message, destination) => {
Expand Down
115 changes: 98 additions & 17 deletions test/integrations/destinations/adj/processor/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2084,7 +2084,7 @@ export const data = [
},
{
name: 'adj',
description: 'Test 17',
description: 'Test 18 -> No adverstisingId present',
feature: 'processor',
module: 'destination',
version: 'v0',
Expand All @@ -2104,7 +2104,6 @@ export const data = [
},
device: {
adTrackingEnabled: true,
advertisingId: '3f034872-5e28-45a1-9eda-ce22a3e36d1a',
id: '3f034872-5e28-45a1-9eda-ce22a3e36d1a',
manufacturer: 'Google',
model: 'AOSP on IA Emulator',
Expand Down Expand Up @@ -2160,12 +2159,101 @@ export const data = [
},
},
Config: {
appToken: 'testAppToken',
customMappings: [
{ from: 'Application Installed', to: '3fdmll' },
{ from: 'First Investment', to: 'testEventToken' },
],
partnerParamsKeys: [],
environment: true,
},
Enabled: true,
Transformations: [],
IsProcessorEnabled: true,
},
},
],
method: 'POST',
},
},
output: {
response: {
status: 200,
body: [
{
error: 'App Token is not present. Please configure your app token from config dashbaord',
statTags: {
destType: 'ADJ',
errorCategory: 'dataValidation',
errorType: 'configuration',
feature: 'processor',
implementation: 'native',
module: 'destination',
},
statusCode: 400,
},
],
},
},
},
{
name: 'adj',
description: 'Test 19 -> No properties object in message body',
feature: 'processor',
module: 'destination',
version: 'v0',
input: {
request: {
body: [
{
message: {
"type": "track",
"event": "Application Installed",
"sentAt": "2022-09-28T20:14:44.995Z",
"userId": "sample_user_id",
"context": {
"device": {
"id": "sample_device_id",
"type": "android",
"advertisingId": "_sample"
},
"traits": {
"userId": "_sample_uid",
"anonymousId": "_sample_anonid"
}
},
"timestamp": "2022-09-28T20:14:43.314Z",
"request_ip": "71.189.106.156",
"originalTimestamp": "2022-09-28T20:14:44.995Z"
},
destination: {
ID: '1i3Em7GMU9xVEiDlZUN8c88BMS9',
Name: 'ADJ',
DestinationDefinition: {
ID: '1i3DeZo6eSUKrS3KzDUqjbBPCDJ',
Name: 'ADJ',
DisplayName: 'Adjust',
Config: {
destConfig: {
android: ['useNativeSDK'],
defaultConfig: ['appToken', 'customMappings', 'delay', 'environment'],
ios: ['useNativeSDK'],
reactnative: ['useNativeSDK'],
},
excludeKeys: [],
includeKeys: ['appToken', 'customMappings', 'delay'],
supportedSourceTypes: ['android', 'ios', 'reactnative', 'cloud'],
},
},
Config: {
appToken: 'testAppToken',
customMappings: [
{ from: 'Application Installed', to: '3fdmll' }],
partnerParamsKeys: [
{ from: 'key1', to: 'partnerParamKey-1' },
{ from: 'key2', to: 'partnerParamKey-2' },
{ from: 'revenue', to: 'price' },
{ from: 'novalue', to: 'check' },
{ from: 'Custom_Val_NA', to: 'Custom_dest' },
],
environment: true,
},
Enabled: true,
Expand All @@ -2189,28 +2277,21 @@ export const data = [
endpoint: 'https://s2s.adjust.com/event',
headers: { Accept: '*/*' },
params: {
callback_params:
'{"key1":"value1","key2":"value2","key3.k4":"v4","key3.k5.k6":"v6","key5[0].k":"v1","key5[1].k":"v2"}',
revenue: 20.38,
android_id: '3f034872-5e28-45a1-9eda-ce22a3e36d1a',
gps_adid: '3f034872-5e28-45a1-9eda-ce22a3e36d1a',
att_status: 3,
tracking_enabled: true,
currency: 'EUR',
ip_address: '[::1]',
event_token: "3fdmll",
ip_address: "71.189.106.156",
android_id: 'sample_device_id',
gps_adid: "_sample",
s2s: 1,
app_token: 'testAppToken',
event_token: 'testEventToken',
environment: 'production',
},
body: { JSON: {}, XML: {}, JSON_ARRAY: {}, FORM: {} },
files: {},
userId: '21e13f4bc7ceddad',
userId: '',
},
statusCode: 200,
},
],
},
},
},
];
},];

0 comments on commit f63b493

Please sign in to comment.