Skip to content

Commit

Permalink
chore: update trengo httpGet call with metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsSudip committed Jun 20, 2024
1 parent 36990ea commit 9e9651f
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/v0/destinations/trengo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const validate = (email, phone, channelIdentifier) => {
*
* In case no contact is founf for a particular identifer it returns -1
*/
const lookupContact = async (term, destination) => {
const lookupContact = async (term, destination, metadata) => {
let res;
try {
res = await httpGET(
Expand All @@ -91,6 +91,7 @@ const lookupContact = async (term, destination) => {
},
},
{
metadata,
destType: 'trengo',
feature: 'transformation',
endpointPath: '/contacts',
Expand Down Expand Up @@ -142,6 +143,7 @@ const contactBuilderTrengo = async (
destination,
identifier,
extIds,
metadata,
createScope = true,
) => {
let result;
Expand Down Expand Up @@ -176,7 +178,7 @@ const contactBuilderTrengo = async (
let contactId = get(extIds, 'contactId');
if (!contactId) {
// If we alrady dont have contactId in our message we do lookup
contactId = await lookupContact(identifier, destination);
contactId = await lookupContact(identifier, destination, metadata);
if (!contactId) {
// In case contactId is returned null we throw error (This indicates and search API issue in trengo end)
throw new NetworkInstrumentationError(
Expand All @@ -203,13 +205,13 @@ const contactBuilderTrengo = async (
return result;
};

const ticketBuilderTrengo = async (message, destination, identifer, extIds) => {
const ticketBuilderTrengo = async (message, destination, identifer, extIds, metadata) => {
let subjectLine;
const template = getTemplate(message, destination);
const externalId = get(extIds, 'externalId');
let contactId = get(extIds, 'contactId');
if (!contactId) {
contactId = await lookupContact(identifer, destination);
contactId = await lookupContact(identifer, destination, metadata);
if (!contactId) {
throw new InstrumentationError(
`LookupContact failed for term:${identifer} track event failed`,
Expand Down Expand Up @@ -264,7 +266,7 @@ const ticketBuilderTrengo = async (message, destination, identifer, extIds) => {
* based on type of event and the destination configurations the
* payloads are generated.
*/
const responseBuilderSimple = async (message, messageType, destination) => {
const responseBuilderSimple = async (message, messageType, destination, metadata) => {
let trengoPayload;
// ChannelId is a mandatory field if it is not present in destination config
// we will abort events.
Expand Down Expand Up @@ -294,6 +296,7 @@ const responseBuilderSimple = async (message, messageType, destination) => {
destination,
channelIdentifier === 'email' ? email : phone,
extIds,
metadata,
false,
);
if (trengoPayload === -1) {
Expand All @@ -307,6 +310,7 @@ const responseBuilderSimple = async (message, messageType, destination) => {
destination,
channelIdentifier === 'email' ? email : phone,
extIds,
metadata,
true,
);
}
Expand All @@ -321,6 +325,7 @@ const responseBuilderSimple = async (message, messageType, destination) => {
destination,
channelIdentifier === 'email' ? email : phone,
extIds,
metadata,
true,
);
}
Expand All @@ -332,6 +337,7 @@ const responseBuilderSimple = async (message, messageType, destination) => {
destination,
channelIdentifier === 'email' ? email : phone,
extIds,
metadata,
);
}
// Wrapped payload with structure
Expand Down Expand Up @@ -371,20 +377,20 @@ const responseBuilderSimple = async (message, messageType, destination) => {
* If event type is not identify or track it will discard
* the event
*/
const processEvent = async (message, destination) => {
const processEvent = async (message, destination, metadata) => {
if (!message.type) {
throw new InstrumentationError('Event type is required');
}
const messageType = message.type.toLowerCase();
if (messageType !== EventType.IDENTIFY && messageType !== EventType.TRACK) {
throw new InstrumentationError(`Event type ${messageType} is not supported`);
}
const resp = await responseBuilderSimple(message, messageType, destination);
const resp = await responseBuilderSimple(message, messageType, destination, metadata);
return resp;
};

const process = async (event) => {
const response = await processEvent(event.message, event.destination);
const response = await processEvent(event.message, event.destination, event.metadata);
return response;
};

Expand Down

0 comments on commit 9e9651f

Please sign in to comment.