-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4be2997
commit 9558e3b
Showing
3 changed files
with
97 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"sourceKeys": "ts", | ||
"destKeys": ["timestamp", "originalTimestamp"] | ||
}, | ||
{ | ||
"sourceKeys": "type", | ||
"destKeys": "event" | ||
} | ||
] |
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,72 @@ | ||
const sha256 = require('sha256'); | ||
const { TransformationError } = require('@rudderstack/integrations-lib'); | ||
const Message = require('../message'); | ||
const { mapping, formEventName } = require('./util'); | ||
const { generateUUID, removeUndefinedAndNullValues } = require('../../util'); | ||
const { JSON_MIME_TYPE } = require('../../util/constant'); | ||
|
||
function processNormalEvent(slackPayload) { | ||
const message = new Message(`SLACK`); | ||
// we are setting event type as track always | ||
message.setEventType('track'); | ||
message.setEventName(formEventName(slackPayload.type)); | ||
if (slackPayload.user) { | ||
const stringifiedUserId = | ||
typeof slackPayload.user === 'string' ? slackPayload.user : slackPayload.user.id; | ||
message.setProperty( | ||
'anonymousId', | ||
stringifiedUserId ? sha256(stringifiedUserId).toString().substring(0, 36) : generateUUID(), | ||
); | ||
// setting the userId got from Monday into externalId | ||
message.context.externalId = [ | ||
{ | ||
type: 'slackUserId', | ||
id: stringifiedUserId, | ||
}, | ||
]; | ||
} else { | ||
throw new TransformationError('UserId not found'); | ||
} | ||
|
||
message.setPropertiesV2(slackPayload, mapping); | ||
/* deleting properties already mapped in | ||
the payload's root */ | ||
delete message.properties.triggerTime; | ||
delete message.properties.userId; | ||
return message; | ||
} | ||
|
||
// the payload for the challenge event will be as follows: | ||
// { | ||
// challenge : "some_key" | ||
// } | ||
// this will be sent when the webhook is added to an item in monday. | ||
|
||
function isChallengeEvent(event) { | ||
return !!event?.challenge; | ||
} | ||
|
||
// sending challenge event object back to Monday | ||
function processChallengeEvent(event) { | ||
return { | ||
outputToSource: { | ||
body: Buffer.from(JSON.stringify(event)).toString('base64'), | ||
contentType: JSON_MIME_TYPE, | ||
}, | ||
statusCode: 200, | ||
}; | ||
} | ||
|
||
// we will check here if the event is a challenge event or not | ||
// and process accordingly. | ||
// For challenge event the recieved challenge object is sent back | ||
// to Monday to verify the webhook url. | ||
// Ref: https://developer.monday.com/api-reference/docs/webhooks-1#how-to-verify-a-webhook-url | ||
function process(event) { | ||
const response = isChallengeEvent(event) | ||
? processChallengeEvent(event) | ||
: processNormalEvent(event); | ||
return removeUndefinedAndNullValues(response); | ||
} | ||
|
||
exports.process = process; |
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,15 @@ | ||
/* eslint-disable no-restricted-syntax */ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
const mapping = JSON.parse(fs.readFileSync(path.resolve(__dirname, './mapping.json'), 'utf-8')); | ||
|
||
// turning underscore-seperated Monday events into Rudder format | ||
function formEventName(evtName) { | ||
return evtName | ||
.split('_') | ||
.map((s) => s.charAt(0).toUpperCase() + s.slice(1)) | ||
.join(' '); | ||
} | ||
|
||
module.exports = { mapping, formEventName }; |