From b19eb8c699b60e90e02b39e3a448e6aac721e37f Mon Sep 17 00:00:00 2001 From: gitcommitshow Date: Sat, 2 Mar 2024 19:08:27 +0530 Subject: [PATCH] fix: add originalTimestamp from slack ts property --- src/v0/sources/slack/mapping.json | 8 ------- src/v0/sources/slack/transform.js | 13 ++++++------ src/v0/sources/slack/util.js | 35 ++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/v0/sources/slack/mapping.json b/src/v0/sources/slack/mapping.json index 304473180e..02f31482d4 100644 --- a/src/v0/sources/slack/mapping.json +++ b/src/v0/sources/slack/mapping.json @@ -1,14 +1,6 @@ [ - { - "sourceKeys": "ts", - "destKeys": ["timestamp", "originalTimestamp"] - }, { "sourceKeys": "type", "destKeys": "event" - }, - { - "sourceKeys": "channel", - "destKeys": "properties.slackChannel" } ] diff --git a/src/v0/sources/slack/transform.js b/src/v0/sources/slack/transform.js index d9460b7d23..6c244a1d78 100644 --- a/src/v0/sources/slack/transform.js +++ b/src/v0/sources/slack/transform.js @@ -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'); @@ -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; } diff --git a/src/v0/sources/slack/util.js b/src/v0/sources/slack/util.js index 6f8270cd8d..712a75bb6c 100644 --- a/src/v0/sources/slack/util.js +++ b/src/v0/sources/slack/util.js @@ -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 @@ -12,4 +45,4 @@ function formEventName(evtName) { .join(' '); } -module.exports = { mapping, formEventName }; +module.exports = { mapping, tsToISODate, formEventName };