Skip to content

Commit

Permalink
chore: adjust shopify upgrade to v2 (#3924)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinayteki95 authored Jan 16, 2025
2 parents 0e0944d + eacfdf1 commit 99b7034
Show file tree
Hide file tree
Showing 11 changed files with 752 additions and 632 deletions.
5 changes: 5 additions & 0 deletions src/controllers/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export class SourceController {
version,
events,
);
logger.debug('Native(Source-Transform):: Controller Input Adapter::', {
implementationVersion,
inputVersion: version,
source,
});

const resplist = await integrationService.sourceTransformRoutine(
input,
Expand Down
17 changes: 16 additions & 1 deletion src/controllers/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getValueFromMessage } from '../../v0/util';
import genericFieldMap from '../../v0/util/data/GenericFieldMapping.json';
import { EventType, MappedToDestinationKey } from '../../constants';
import { versionConversionFactory } from './versionConversion';
import defaultFeaturesConfig from '../../features';

export class ControllerUtility {
private static sourceVersionMap: Map<string, string> = new Map();
Expand All @@ -29,15 +30,29 @@ export class ControllerUtility {
[EventType.TRACK]: [`properties.${RETL_TIMESTAMP}`, ...genericFieldMap.timestamp],
};

private static getSourceDirPath(version: string): string {
if (version === 'v2') {
return path.resolve(__dirname, '../../sources');
}
return path.resolve(__dirname, `../../${version}/sources`);
}

private static getSourceVersionsMap(): Map<string, any> {
if (this.sourceVersionMap?.size > 0) {
return this.sourceVersionMap;
}

const versions = ['v0', 'v1'];
if (defaultFeaturesConfig.upgradedToSourceTransformV2) {
// this makes it easy to revert to v0,v1 spec if something doesn't work out using ENV variables
versions.push('v2');
}

versions.forEach((version) => {
const files = fs.readdirSync(path.resolve(__dirname, `../../${version}/sources`), {
const files = fs.readdirSync(this.getSourceDirPath(version), {
withFileTypes: true,
});

const sources = files.filter((file) => file.isDirectory()).map((folder) => folder.name);
sources.forEach((source) => {
this.sourceVersionMap.set(source, version);
Expand Down
3 changes: 3 additions & 0 deletions src/services/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export class MiscService {
}

public static getSourceHandler(source: string, version: string) {
if (version === 'v2') {
return require(`../sources/${source}/transform`);
}
return require(`../${version}/sources/${source}/transform`);
}

Expand Down
18 changes: 18 additions & 0 deletions src/sources/adjust/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { processEvent: processV0Event } = require('../../v0/sources/adjust/transform');
const { CommonUtils } = require('../../util/common');

const convertV2ToV0 = (sourceEvent) => {
const v0Event = JSON.parse(sourceEvent.request.body);
if (sourceEvent.request.query_parameters) {
v0Event.query_parameters = sourceEvent.request.query_parameters;
}
return v0Event;
};

const process = (requests) => {
const requestsArray = CommonUtils.toArray(requests);
const v0Events = requestsArray.map(convertV2ToV0);
return v0Events.map(processV0Event);
};

module.exports = { process };
19 changes: 19 additions & 0 deletions src/sources/shopify/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { process: processV1 } = require('../../v1/sources/shopify/transform');

const convertV2ToV1 = (inputRequest) => {
const { body: bodyString, query_parameters: qParams } = inputRequest.request;
const requestBody = JSON.parse(bodyString);

if (qParams) {
requestBody.query_parameters = qParams;
}

return {
event: requestBody,
source: inputRequest.source,
};
};

const process = async (inputEvent) => processV1(convertV2ToV1(inputEvent));

module.exports = { process };
2 changes: 1 addition & 1 deletion src/v0/sources/adjust/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ const process = (events) => {
return eventsArray.map(processEvent);
};

module.exports = { process };
module.exports = { process, processEvent };
22 changes: 0 additions & 22 deletions test/apitests/service.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,28 +543,6 @@ describe('Destination api tests', () => {
});

describe('Source api tests', () => {
test('(shopify) successful source transform', async () => {
const data = getDataFromPath('./data_scenarios/source/v0/successful.json');
const response = await request(server)
.post('/v0/sources/shopify')
.set('Accept', 'application/json')
.send(data.input);
const parsedResp = JSON.parse(response.text);
delete parsedResp[0].output.batch[0].anonymousId;
expect(response.status).toEqual(200);
expect(parsedResp).toEqual(data.output);
});

test('(shopify) failure source transform (shopify)', async () => {
const data = getDataFromPath('./data_scenarios/source/v0/failure.json');
const response = await request(server)
.post('/v0/sources/shopify')
.set('Accept', 'application/json')
.send(data.input);
expect(response.status).toEqual(200);
expect(JSON.parse(response.text)).toEqual(data.output);
});

test('(shopify) success source transform (monday)', async () => {
const data = getDataFromPath('./data_scenarios/source/v0/response_to_caller.json');
const response = await request(server)
Expand Down
69 changes: 51 additions & 18 deletions test/integrations/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import axios from 'axios';
import bodyParser from 'koa-bodyparser';
import { Command } from 'commander';
import { createHttpTerminator } from 'http-terminator';
import { MockHttpCallsData, TestCaseData } from './testTypes';
import { ExtendedTestCaseData, MockHttpCallsData, TestCaseData } from './testTypes';
import { applicationRoutes } from '../../src/routes/index';
import MockAxiosAdapter from 'axios-mock-adapter';
import {
Expand All @@ -24,6 +24,9 @@ import { appendFileSync } from 'fs';
import { assertRouterOutput, responses } from '../testHelper';
import { generateTestReport, initaliseReport } from '../test_reporter/reporter';
import _ from 'lodash';
import defaultFeaturesConfig from '../../src/features';
import { ControllerUtility } from '../../src/controllers/util';
import { FetchHandler } from '../../src/helpers/fetchHandlers';
import { enhancedTestUtils } from '../test_reporter/allureReporter';

// To run single destination test cases
Expand Down Expand Up @@ -230,23 +233,53 @@ describe.each(allTestDataFilePaths)('%s Tests', (testDataPath) => {
return false;
});
}

const extendedTestData: ExtendedTestCaseData[] = testData.flatMap((tcData) => {
if (tcData.module === tags.MODULES.SOURCE) {
return [
{
tcData,
sourceTransformV2Flag: false,
descriptionSuffix: ' (sourceTransformV2Flag: false)',
},
{
tcData,
sourceTransformV2Flag: true,
descriptionSuffix: ' (sourceTransformV2Flag: true)',
},
];
}
return [{ tcData }];
});

describe(`${testData[0].name} ${testData[0].module}`, () => {
test.each(testData)('$feature -> $description (index: $#)', async (tcData) => {
tcData?.mockFns?.(mockAdapter);

switch (tcData.module) {
case tags.MODULES.DESTINATION:
await destinationTestHandler(tcData);
break;
case tags.MODULES.SOURCE:
await sourceTestHandler(tcData);
break;
default:
console.log('Invalid module');
// Intentionally fail the test case
expect(true).toEqual(false);
break;
}
});
test.each(extendedTestData)(
'$feature -> $description$descriptionSuffix (index: $#)',
async ({ tcData, sourceTransformV2Flag }) => {
tcData?.mockFns?.(mockAdapter);

switch (tcData.module) {
case tags.MODULES.DESTINATION:
await destinationTestHandler(tcData);
break;
case tags.MODULES.SOURCE:
tcData?.mockFns?.(mockAdapter);
testSetupSourceTransformV2(sourceTransformV2Flag);
await sourceTestHandler(tcData);
break;
default:
console.log('Invalid module');
// Intentionally fail the test case
expect(true).toEqual(false);
break;
}
},
);
});
});

const testSetupSourceTransformV2 = (flag) => {
defaultFeaturesConfig.upgradedToSourceTransformV2 = flag;
ControllerUtility['sourceVersionMap'] = new Map();
FetchHandler['sourceHandlerMap'] = new Map();
};
65 changes: 37 additions & 28 deletions test/integrations/sources/adjust/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@ export const data = [
name: 'adjust',
description: 'Simple track call',
module: 'source',
version: 'v0',
version: 'v1',
input: {
request: {
body: [
{
id: 'adjust',
query_parameters: {
gps_adid: ['38400000-8cf0-11bd-b23e-10b96e40000d'],
adid: ['18546f6171f67e29d1cb983322ad1329'],
tracker_token: ['abc'],
custom: ['custom'],
tracker_name: ['dummy'],
created_at: ['1404214665'],
event_name: ['Click'],
event: {
id: 'adjust',
query_parameters: {
gps_adid: ['38400000-8cf0-11bd-b23e-10b96e40000d'],
adid: ['18546f6171f67e29d1cb983322ad1329'],
tracker_token: ['abc'],
custom: ['custom'],
tracker_name: ['dummy'],
created_at: ['1404214665'],
event_name: ['Click'],
},
updated_at: '2023-02-10T12:16:07.251Z',
created_at: '2023-02-10T12:05:04.402Z',
},
updated_at: '2023-02-10T12:16:07.251Z',
created_at: '2023-02-10T12:05:04.402Z',
source: {},
},
],
method: 'POST',
Expand Down Expand Up @@ -85,15 +88,18 @@ export const data = [
name: 'adjust',
description: 'Simple track call with no query parameters',
module: 'source',
version: 'v0',
version: 'v1',
skipGo: 'FIXME',
input: {
request: {
body: [
{
id: 'adjust',
updated_at: '2023-02-10T12:16:07.251Z',
created_at: '2023-02-10T12:05:04.402Z',
event: {
id: 'adjust',
updated_at: '2023-02-10T12:16:07.251Z',
created_at: '2023-02-10T12:05:04.402Z',
},
source: {},
},
],
method: 'POST',
Expand Down Expand Up @@ -129,24 +135,27 @@ export const data = [
name: 'adjust',
description: 'Simple track call with wrong created at',
module: 'source',
version: 'v0',
version: 'v1',
skipGo: 'FIXME',
input: {
request: {
body: [
{
id: 'adjust',
query_parameters: {
gps_adid: ['38400000-8cf0-11bd-b23e-10b96e40000d'],
adid: ['18546f6171f67e29d1cb983322ad1329'],
tracker_token: ['abc'],
custom: ['custom'],
tracker_name: ['dummy'],
created_at: ['test'],
event_name: ['Click'],
event: {
id: 'adjust',
query_parameters: {
gps_adid: ['38400000-8cf0-11bd-b23e-10b96e40000d'],
adid: ['18546f6171f67e29d1cb983322ad1329'],
tracker_token: ['abc'],
custom: ['custom'],
tracker_name: ['dummy'],
created_at: ['test'],
event_name: ['Click'],
},
updated_at: '2023-02-10T12:16:07.251Z',
created_at: 'test',
},
updated_at: '2023-02-10T12:16:07.251Z',
created_at: 'test',
source: {},
},
],
method: 'POST',
Expand Down
Loading

0 comments on commit 99b7034

Please sign in to comment.