From 7359fad03a2ff8eac82b7a7057d985906f864db4 Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Fri, 10 Jan 2025 17:47:25 +0530 Subject: [PATCH] fix: fixing mismatch of schedule field value in clicksend destination due to different timezone --- src/cdk/v2/destinations/clicksend/utils.js | 10 ++++++--- .../v2/destinations/clicksend/utils.test.js | 21 ++++++++++++++----- .../destinations/clicksend/processor/track.ts | 4 ++-- .../destinations/clicksend/router/data.ts | 12 +++++------ 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/cdk/v2/destinations/clicksend/utils.js b/src/cdk/v2/destinations/clicksend/utils.js index d0671df45c..3b28f752fd 100644 --- a/src/cdk/v2/destinations/clicksend/utils.js +++ b/src/cdk/v2/destinations/clicksend/utils.js @@ -33,13 +33,17 @@ const deduceSchedule = (eventLevelSchedule, timestamp, destConfig) => { if (isDefinedAndNotNull(eventLevelSchedule) && !Number.isNaN(eventLevelSchedule)) { return eventLevelSchedule; } - const { defaultCampaignScheduleUnit = 'minute', defaultCampaignSchedule = 0 } = destConfig; + const { defaultCampaignScheduleUnit = 'minute', defaultCampaignSchedule = '0' } = destConfig; const date = new Date(timestamp); + let defaultCampaignScheduleInt = parseInt(defaultCampaignSchedule, 10); + if (Number.isNaN(defaultCampaignScheduleInt)) { + defaultCampaignScheduleInt = 0; + } if (defaultCampaignScheduleUnit === 'day') { - date.setDate(date.getDate() + defaultCampaignSchedule); + date.setDate(date.getDate() + defaultCampaignScheduleInt); } else if (defaultCampaignScheduleUnit === 'minute') { - date.setMinutes(date.getMinutes() + defaultCampaignSchedule); + date.setMinutes(date.getMinutes() + defaultCampaignScheduleInt); } else { throw new Error("Invalid delta unit. Use 'day' or 'minute'."); } diff --git a/src/cdk/v2/destinations/clicksend/utils.test.js b/src/cdk/v2/destinations/clicksend/utils.test.js index 4999f55678..37e371354d 100644 --- a/src/cdk/v2/destinations/clicksend/utils.test.js +++ b/src/cdk/v2/destinations/clicksend/utils.test.js @@ -46,7 +46,7 @@ describe('deduceSchedule', () => { it('should return eventLevelSchedule when it is defined, not null, and not empty', () => { const eventLevelSchedule = 1234567890; const timestamp = '2023-10-01T00:00:00Z'; - const destConfig = { defaultCampaignScheduleUnit: 'minute', defaultCampaignSchedule: 5 }; + const destConfig = { defaultCampaignScheduleUnit: 'minute', defaultCampaignSchedule: '5' }; const result = deduceSchedule(eventLevelSchedule, timestamp, destConfig); @@ -57,7 +57,7 @@ describe('deduceSchedule', () => { it('should throw error when defaultCampaignScheduleUnit is invalid', () => { const eventLevelSchedule = null; const timestamp = '2023-10-01T00:00:00Z'; - const destConfig = { defaultCampaignScheduleUnit: 'hour', defaultCampaignSchedule: 5 }; + const destConfig = { defaultCampaignScheduleUnit: 'hour', defaultCampaignSchedule: '5' }; expect(() => { deduceSchedule(eventLevelSchedule, timestamp, destConfig); @@ -68,7 +68,7 @@ describe('deduceSchedule', () => { it('should calculate future timestamp correctly when defaultCampaignScheduleUnit is minute', () => { const eventLevelSchedule = null; const timestamp = '2023-10-01T00:00:00Z'; - const destConfig = { defaultCampaignScheduleUnit: 'minute', defaultCampaignSchedule: 5 }; + const destConfig = { defaultCampaignScheduleUnit: 'minute', defaultCampaignSchedule: '5' }; const result = deduceSchedule(eventLevelSchedule, timestamp, destConfig); @@ -81,7 +81,7 @@ describe('deduceSchedule', () => { it('should calculate future timestamp correctly when defaultCampaignScheduleUnit is day', () => { const eventLevelSchedule = null; const timestamp = '2023-10-01T00:00:00Z'; - const destConfig = { defaultCampaignScheduleUnit: 'day', defaultCampaignSchedule: 1 }; + const destConfig = { defaultCampaignScheduleUnit: 'day', defaultCampaignSchedule: '1' }; const result = deduceSchedule(eventLevelSchedule, timestamp, destConfig); @@ -90,11 +90,22 @@ describe('deduceSchedule', () => { expect(result).toBe(expectedTimestamp); }); + it('should calculate timestamp when defaultCampaignSchedule in some invalid string', () => { + const eventLevelSchedule = null; + const timestamp = '2023-10-01T00:00:00Z'; + const destConfig = { defaultCampaignScheduleUnit: 'day', defaultCampaignSchedule: 'inValid' }; + + const result = deduceSchedule(eventLevelSchedule, timestamp, destConfig); + const expectedTimestamp = new Date('2023-10-01T00:00:00Z').getTime() / 1000; + + expect(result).toBe(expectedTimestamp); + }); + // returns UNIX timestamp in seconds it('should return UNIX timestamp in seconds', () => { const eventLevelSchedule = null; const timestamp = '2023-10-01T00:00:00Z'; - const destConfig = { defaultCampaignScheduleUnit: 'minute', defaultCampaignSchedule: 5 }; + const destConfig = { defaultCampaignScheduleUnit: 'minute', defaultCampaignSchedule: '5' }; const result = deduceSchedule(eventLevelSchedule, timestamp, destConfig); diff --git a/test/integrations/destinations/clicksend/processor/track.ts b/test/integrations/destinations/clicksend/processor/track.ts index e4a8c5be97..eee7c573e9 100644 --- a/test/integrations/destinations/clicksend/processor/track.ts +++ b/test/integrations/destinations/clicksend/processor/track.ts @@ -62,7 +62,7 @@ export const track = [ body: 'abcd', from: 'abc@gmail.com', name: 'new campaign', - schedule: 1631201576, + schedule: 1611761576, }, userId: '', }), @@ -227,7 +227,7 @@ export const track = [ from: 'abc@gmail.com', from_email: 'dummy@gmail.com', custom_string: 'test string', - schedule: 1631201576, + schedule: 1611761576, source: 'php', to: '+9182XXXX068', }, diff --git a/test/integrations/destinations/clicksend/router/data.ts b/test/integrations/destinations/clicksend/router/data.ts index 54018787b0..5a9da7e7a6 100644 --- a/test/integrations/destinations/clicksend/router/data.ts +++ b/test/integrations/destinations/clicksend/router/data.ts @@ -121,7 +121,7 @@ export const data = [ from: 'abc@gmail.com', list_id: 123345, name: 'new campaign', - schedule: 1631201576, + schedule: 1611761576, }, JSON_ARRAY: {}, XML: {}, @@ -160,7 +160,7 @@ export const data = [ email: 'abc@gmail.com', from: 'abc@gmail.com', from_email: 'dummy@gmail.com', - schedule: 1631201576, + schedule: 1611761576, source: 'php', to: '+9182XXXX068', }, @@ -373,7 +373,7 @@ export const data = [ from: 'abc@gmail.com', list_id: 123345, name: 'new campaign', - schedule: 1631201576, + schedule: 1611761576, }, JSON_ARRAY: {}, XML: {}, @@ -412,7 +412,7 @@ export const data = [ email: 'abc@gmail.com', from: 'abc@gmail.com', from_email: 'dummy@gmail.com', - schedule: 1631201576, + schedule: 1611761576, source: 'php', to: '+9182XXXX068', }, @@ -422,7 +422,7 @@ export const data = [ email: 'abc@gmail.com', from: 'abc@gmail.com', from_email: 'dummy@gmail.com', - schedule: 1631201576, + schedule: 1611761576, source: 'php', to: '+9182XXXX068', }, @@ -432,7 +432,7 @@ export const data = [ email: 'abc@gmail.com', from: 'abc@gmail.com', from_email: 'dummy@gmail.com', - schedule: 1631201576, + schedule: 1611761576, source: 'php', to: '+9182XXXX068', },