Skip to content

Commit

Permalink
feat: added backward compatability to support both old and new VDM
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepdsvs committed Sep 5, 2024
1 parent 9a9340f commit 9589ab7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/v0/destinations/fb_custom_audience/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ function getEndPoint(audienceId) {
return `${BASE_URL}/${audienceId}/users`;
}

const VDM_V2_SCHEMA_VERSION = '1.1';

const schemaFields = [
'EXTERN_ID',
'EMAIL',
Expand Down Expand Up @@ -105,4 +107,5 @@ module.exports = {
typeFields,
subTypeFields,
maxPayloadSize,
VDM_V2_SCHEMA_VERSION,
};
20 changes: 16 additions & 4 deletions src/v0/destinations/fb_custom_audience/recordTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const lodash = require('lodash');
const get = require('get-value');
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib');
const { schemaFields } = require('./config');
const { schemaFields, VDM_V2_SCHEMA_VERSION } = require('./config');
const { MappedToDestinationKey } = require('../../../constants');
const stats = require('../../../util/stats');
const {
Expand All @@ -17,6 +17,7 @@ const {
ensureApplicableFormat,
getUpdatedDataElement,
getSchemaForEventMappedToDest,
getSchemaForEventMappedToDestForVDMv2,
batchingWithPayloadSize,
responseBuilderSimple,
getDataSource,
Expand Down Expand Up @@ -103,8 +104,15 @@ async function processRecordInputs(groupedRecordInputs) {
const { destination, connection } = groupedRecordInputs[0];
const { message } = groupedRecordInputs[0];
const { accessToken, disableFormat, type, subType, isRaw, maxUserCount } = destination.Config;
const audienceId = connection?.config?.destination?.audienceId;
const isHashRequired = connection?.config?.destination?.isHashRequired;
let audienceId;
let isHashRequired;
if (connection?.config?.destination?.schemaVersion === VDM_V2_SCHEMA_VERSION) {
audienceId = connection?.config?.destination?.audienceId;
isHashRequired = connection?.config?.destination?.isHashRequired;
} else {
audienceId = destination.Config?.audienceId;
isHashRequired = destination.Config?.isHashRequired;
}
const prepareParams = {
access_token: accessToken,
};
Expand All @@ -129,7 +137,11 @@ async function processRecordInputs(groupedRecordInputs) {
// user schema validation
let { userSchema } = destination.Config;
if (mappedToDestination) {
userSchema = getSchemaForEventMappedToDest(message);
if (connection?.config?.destination?.schemaVersion === VDM_V2_SCHEMA_VERSION) {
userSchema = getSchemaForEventMappedToDestForVDMv2(message);
} else {
userSchema = getSchemaForEventMappedToDest(message);
}
}
if (!Array.isArray(userSchema)) {
userSchema = [userSchema];
Expand Down
21 changes: 17 additions & 4 deletions src/v0/destinations/fb_custom_audience/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ const {
const {
prepareDataField,
getSchemaForEventMappedToDest,
getSchemaForEventMappedToDestForVDMv2,
batchingWithPayloadSize,
generateAppSecretProof,
responseBuilderSimple,
getDataSource,
} = require('./util');
const { schemaFields, USER_ADD, USER_DELETE } = require('./config');
const { schemaFields, USER_ADD, USER_DELETE, VDM_V2_SCHEMA_VERSION } = require('./config');

const { MappedToDestinationKey } = require('../../../constants');
const { processRecordInputs } = require('./recordTransform');
Expand Down Expand Up @@ -157,8 +158,16 @@ const processEvent = (message, destination, connection) => {
const respList = [];
let toSendEvents = [];
let { userSchema } = destination.Config;
const { isHashRequired, maxUserCount } = destination.Config;
const audienceId = get(connection, 'config.destination.audienceId');
const { maxUserCount } = destination.Config;
let audienceId;
let isHashRequired;
if (connection?.config?.destination?.schemaVersion === VDM_V2_SCHEMA_VERSION) {
audienceId = connection?.config?.destination?.audienceId;
isHashRequired = connection?.config?.destination?.isHashRequired;
} else {
audienceId = destination.Config?.audienceId;
isHashRequired = destination.Config?.isHashRequired;
}
if (!message.type) {
throw new InstrumentationError('Message Type is not present. Aborting message.');
}
Expand All @@ -182,7 +191,11 @@ const processEvent = (message, destination, connection) => {

// If mapped to destination, use the mapped fields instead of destination userschema
if (mappedToDestination) {
userSchema = getSchemaForEventMappedToDest(message);
if (connection?.config?.destination?.schemaVersion === VDM_V2_SCHEMA_VERSION) {
userSchema = getSchemaForEventMappedToDestForVDMv2(message);
} else {
userSchema = getSchemaForEventMappedToDest(message);
}
}

// When one single schema field is added in the webapp, it does not appear to be an array
Expand Down
15 changes: 15 additions & 0 deletions src/v0/destinations/fb_custom_audience/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const lodash = require('lodash');
const sha256 = require('sha256');
const crypto = require('crypto');
const get = require('get-value');
const jsonSize = require('json-size');
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib');
const { TransformationError } = require('@rudderstack/integrations-lib');
Expand Down Expand Up @@ -48,6 +49,19 @@ const batchingWithPayloadSize = (payload) => {
};

const getSchemaForEventMappedToDest = (message) => {
const mappedSchema = get(message, 'context.destinationFields');
if (!mappedSchema) {
throw new InstrumentationError(
'context.destinationFields is required property for events mapped to destination ',
);
}
// context.destinationFields has 2 possible values. An Array of fields or Comma seperated string with field names
let userSchema = Array.isArray(mappedSchema) ? mappedSchema : mappedSchema.split(',');
userSchema = userSchema.map((field) => field.trim());
return userSchema;
};

const getSchemaForEventMappedToDestForVDMv2 = (message) => {
const mappedSchema = message.fields;
if (!mappedSchema) {
throw new InstrumentationError(
Expand Down Expand Up @@ -259,6 +273,7 @@ const responseBuilderSimple = (payload, audienceId) => {
module.exports = {
prepareDataField,
getSchemaForEventMappedToDest,
getSchemaForEventMappedToDestForVDMv2,
batchingWithPayloadSize,
ensureApplicableFormat,
getUpdatedDataElement,
Expand Down

0 comments on commit 9589ab7

Please sign in to comment.