Skip to content

Commit

Permalink
fix: add originalTimestamp from slack ts property
Browse files Browse the repository at this point in the history
  • Loading branch information
gitcommitshow committed Mar 2, 2024
1 parent 42d6010 commit b19eb8c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
8 changes: 0 additions & 8 deletions src/v0/sources/slack/mapping.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
[
{
"sourceKeys": "ts",
"destKeys": ["timestamp", "originalTimestamp"]
},
{
"sourceKeys": "type",
"destKeys": "event"
},
{
"sourceKeys": "channel",
"destKeys": "properties.slackChannel"
}
]
13 changes: 7 additions & 6 deletions src/v0/sources/slack/transform.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const sha256 = require('sha256');
const { TransformationError } = require('@rudderstack/integrations-lib');
const Message = require('../message');
const { mapping, formEventName } = require('./util');
const { mapping, tsToISODate, formEventName } = require('./util');
const { generateUUID, removeUndefinedAndNullValues } = require('../../util');
const { JSON_MIME_TYPE } = require('../../util/constant');

Expand All @@ -27,12 +27,13 @@ function processNormalEvent(slackPayload) {
} else {
throw new TransformationError('UserId not found');
}

// Set the standard event property originalTimestamp
message.setProperty('originalTimestamp', tsToISODate(slackPayload.ts || slackPayload.event_ts));
// Map the remaining standard event properties according to mappings for the payload properties
message.setPropertiesV2(slackPayload, mapping);
/* deleting properties already mapped in
the payload's root */
delete message.properties?.ts;
delete message.properties?.type;
// Copy the complete Slack payload to message.properties
if (!message.properties) message.properties = {};
Object.assign(message.properties, slackPayload);
return message;
}

Expand Down
35 changes: 34 additions & 1 deletion src/v0/sources/slack/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,39 @@ const fs = require('fs');

const mapping = JSON.parse(fs.readFileSync(path.resolve(__dirname, './mapping.json'), 'utf-8'));

/**
* Converts a Slack timestamp to RudderStack's standard timestamp format - ISO 8601 date string.
* The Slack timestamp is a string that represents unix timestamp (seconds since the Unix Epoch)
* with fractional seconds for millisecond precision.
* If the timestamp is not provided, the function returns the current date and time in ISO 8601 format.
*
* @param {string} [slackTs] - The Slack timestamp to be converted.
* @returns {string} The ISO 8601 formatted date string corresponding to the given Slack timestamp
* or the current date and time if no timestamp is provided.
*
* @example
* // Convert a Slack timestamp to an ISO 8601 date string
* const slackTimestamp = "1609459200.123000";
* const isoDate = tsToISODate(slackTimestamp);
* console.log(isoDate); // Output: "2021-01-01T00:00:00.123Z" (depending on your timezone)
*/
function tsToISODate(slackTs) {
// Default to current date if slackTs is not provided
if (!slackTs) return new Date().toISOString();

// Convert slackTs string into unix timestamp in milliseconds
const msTimestamp = parseFloat(slackTs) * 1000;
// Convert to a date object
if (Number.isNaN(msTimestamp)) {
// If timestamp was not a valid float, the parser will return NaN, stop processing the timestamp further and return null
return null;
}
const date = new Date(msTimestamp);

// Return the date in ISO 8601 format
return date.toISOString();
}

// turning underscore-seperated Slack events into Rudder format
function formEventName(evtName) {
return evtName
Expand All @@ -12,4 +45,4 @@ function formEventName(evtName) {
.join(' ');
}

module.exports = { mapping, formEventName };
module.exports = { mapping, tsToISODate, formEventName };

0 comments on commit b19eb8c

Please sign in to comment.