-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: garl record event support (#3403)
- Loading branch information
1 parent
546f2c1
commit 60fee0e
Showing
11 changed files
with
1,329 additions
and
473 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
src/v0/destinations/google_adwords_remarketing_lists/recordTransform.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* eslint-disable no-const-assign */ | ||
const lodash = require('lodash'); | ||
const { InstrumentationError } = require('@rudderstack/integrations-lib'); | ||
const { | ||
getValueFromMessage, | ||
getAccessToken, | ||
constructPayload, | ||
returnArrayOfSubarrays, | ||
getSuccessRespEvents, | ||
} = require('../../util'); | ||
const { populateConsentFromConfig } = require('../../util/googleUtils'); | ||
const { populateIdentifiers, responseBuilder } = require('./util'); | ||
const { getErrorResponse, createFinalResponse } = require('../../util/recordUtils'); | ||
const { offlineDataJobsMapping, consentConfigMap } = require('./config'); | ||
|
||
const processRecordEventArray = ( | ||
records, | ||
message, | ||
destination, | ||
accessToken, | ||
developerToken, | ||
operationType, | ||
) => { | ||
let outputPayloads = {}; | ||
// ** only send it if identifier > 0 | ||
|
||
const fieldsArray = []; | ||
const metadata = []; | ||
records.forEach((record) => { | ||
fieldsArray.push(record.message.fields); | ||
metadata.push(record.metadata); | ||
}); | ||
|
||
const userIdentifiersList = populateIdentifiers(fieldsArray, destination); | ||
|
||
const outputPayload = constructPayload(message, offlineDataJobsMapping); | ||
outputPayload.operations = []; | ||
// breaking the userIdentiFier array in chunks of 20 | ||
const userIdentifierChunks = returnArrayOfSubarrays(userIdentifiersList, 20); | ||
// putting each chunk in different create/remove operations | ||
switch (operationType) { | ||
case 'add': | ||
// for add operation | ||
userIdentifierChunks.forEach((element) => { | ||
const operations = { | ||
create: {}, | ||
}; | ||
operations.create.userIdentifiers = element; | ||
outputPayload.operations.push(operations); | ||
}); | ||
outputPayloads = { ...outputPayloads, create: outputPayload }; | ||
break; | ||
case 'remove': | ||
// for remove operation | ||
userIdentifierChunks.forEach((element) => { | ||
const operations = { | ||
remove: {}, | ||
}; | ||
operations.remove.userIdentifiers = element; | ||
outputPayload.operations.push(operations); | ||
}); | ||
outputPayloads = { ...outputPayloads, remove: outputPayload }; | ||
break; | ||
default: | ||
} | ||
|
||
const toSendEvents = []; | ||
Object.values(outputPayloads).forEach((data) => { | ||
const consentObj = populateConsentFromConfig(destination.Config, consentConfigMap); | ||
toSendEvents.push( | ||
responseBuilder(accessToken, developerToken, data, destination, message, consentObj), | ||
); | ||
}); | ||
|
||
const successResponse = getSuccessRespEvents(toSendEvents, metadata, destination, true); | ||
|
||
return successResponse; | ||
}; | ||
|
||
async function processRecordInputs(groupedRecordInputs) { | ||
const { destination, message, metadata } = groupedRecordInputs[0]; | ||
const accessToken = getAccessToken(metadata, 'accessToken'); | ||
const developerToken = getValueFromMessage(metadata, 'secret.developer_token'); | ||
|
||
const groupedRecordsByAction = lodash.groupBy(groupedRecordInputs, (record) => | ||
record.message.action?.toLowerCase(), | ||
); | ||
|
||
let insertResponse; | ||
let deleteResponse; | ||
let updateResponse; | ||
|
||
if (groupedRecordsByAction.delete) { | ||
deleteResponse = processRecordEventArray( | ||
groupedRecordsByAction.delete, | ||
message, | ||
destination, | ||
accessToken, | ||
developerToken, | ||
'remove', | ||
); | ||
} | ||
|
||
if (groupedRecordsByAction.insert) { | ||
insertResponse = processRecordEventArray( | ||
groupedRecordsByAction.insert, | ||
message, | ||
destination, | ||
accessToken, | ||
developerToken, | ||
'add', | ||
); | ||
} | ||
|
||
if (groupedRecordsByAction.update) { | ||
updateResponse = processRecordEventArray( | ||
groupedRecordsByAction.update, | ||
message, | ||
destination, | ||
accessToken, | ||
developerToken, | ||
'add', | ||
); | ||
} | ||
|
||
const errorResponse = getErrorResponse(groupedRecordsByAction); | ||
const finalResponse = createFinalResponse( | ||
deleteResponse, | ||
insertResponse, | ||
updateResponse, | ||
errorResponse, | ||
); | ||
if (finalResponse.length === 0) { | ||
throw new InstrumentationError( | ||
'Missing valid parameters, unable to generate transformed payload', | ||
); | ||
} | ||
|
||
return finalResponse; | ||
} | ||
|
||
module.exports = { | ||
processRecordInputs, | ||
}; |
Oops, something went wrong.