Skip to content

Commit

Permalink
chore: updated test cases and function
Browse files Browse the repository at this point in the history
  • Loading branch information
aanshi07 committed Aug 21, 2024
1 parent ecab59f commit 5b659a0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/cdk/v2/destinations/smartly/procWorkflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ steps:
template: |
const payload = $.removeUndefinedAndNullValues($.constructPayload(.message, $.TRACK_CONFIG));
$.verifyAdInteractionTime(payload.ad_interaction_time);
$.context.payloadList = $.getPayloads(.message.event_name, .destination.Config, payload)
$.context.payloadList = $.getPayloads(.message.event, .destination.Config, payload)
- name: buildResponse
template: |
const response = $.buildResponseList($.context.payloadList)
Expand Down
30 changes: 7 additions & 23 deletions src/cdk/v2/destinations/smartly/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,15 @@ const getPayloads = (event, Config, payload) => {
return payloadLists;
};

// ad_interaction_time must be within one year in the future and three years in the past from the current date
// Example : "1735680000"
const verifyAdInteractionTime = (adInteractionTime) => {
if (isDefinedAndNotNull(adInteractionTime)) {
let adInteractionTimeMoment;

// Handle both UNIX timestamps and UTC date strings
if (typeof adInteractionTime === 'number') {
adInteractionTimeMoment = moment.unix(adInteractionTime); // Parse as UNIX timestamp
} else {
adInteractionTimeMoment = moment.utc(adInteractionTime); // Parse as UTC date string
}

const currentMoment = moment.utc(); // Current time in UTC

// Calculate the time difference in days
const diffDaysPast = currentMoment.diff(adInteractionTimeMoment, 'days');
const diffDaysFuture = adInteractionTimeMoment.diff(currentMoment, 'days');

// Define the day range: 3 years (1095 days) in the past and 1 year (365 days) in the future
const maxDaysPast = 3 * 365 + 1; // 1095 days + 1 day for a leap year
const maxDaysFuture = 1 * 365 + 1; // 365 days + 1 day for a leap year

// Check if adInteractionTime is within the allowed range
const isWithinAllowedRange = diffDaysPast <= maxDaysPast && diffDaysFuture <= maxDaysFuture;

if (!isWithinAllowedRange) {
const now = moment();
const threeYearAgo = now.clone().subtract(3, 'year');
const oneYearFromNow = now.clone().add(1, 'year');
const inputMoment = moment(adInteractionTime * 1000); // Convert to milliseconds
if (!inputMoment.isAfter(threeYearAgo) || !inputMoment.isBefore(oneYearFromNow)) {
throw new InstrumentationError(
'ad_interaction_time must be within one year in the future and three years in the past.',
);
Expand Down
9 changes: 1 addition & 8 deletions src/cdk/v2/destinations/smartly/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const moment = require('moment');
const { InstrumentationError } = require('@rudderstack/workflow-engine');
const { verifyAdInteractionTime } = require('./utils');

describe('verifyAdInteractionTime', () => {
Expand Down Expand Up @@ -31,16 +30,10 @@ describe('verifyAdInteractionTime', () => {
);
});

it('should pass when adInteractionTime is exactly 3 years in the past (UTC date string)', () => {
// Exactly 3 years ago from now
const adInteractionTime = moment.utc().subtract(3, 'years').toISOString();
expect(() => verifyAdInteractionTime(adInteractionTime)).not.toThrow();
});

it('should pass when adInteractionTime is exactly 1 year in the future (UTC date string)', () => {
// Exactly 1 year in the future from now
const adInteractionTime = moment.utc().add(1, 'year').toISOString();
expect(() => verifyAdInteractionTime(adInteractionTime)).not.toThrow();
expect(() => verifyAdInteractionTime(adInteractionTime)).toThrow();
});

it('should fail when adInteractionTime is 4 years in the past (UTC date string)', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/integrations/destinations/smartly/processor/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export const trackTestData = [
{
destination,
message: {
event_name: 'Add to cart',
event: 'Add to cart',
properties: {
platform: 'meta',
ad_unit_id: '228287',
ad_interaction_time: 1612137600,
ad_interaction_time: 1735680000,
email: '[email protected]',
},
type: 'track',
Expand Down Expand Up @@ -51,7 +51,7 @@ export const trackTestData = [
JSON: {
platform: 'meta',
ad_unit_id: '228287',
ad_interaction_time: 1612137600,
ad_interaction_time: 1735680000,
conversions: '1',
event_name: 'Add to cart',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const validationFailures = [
destination,
message: {
type: 'track',
event_name: 'product purchased',
event: 'product purchased',
sentAt: '2021-01-25T16:12:02.048Z',
userId: 'john123',
properties: {
Expand Down
12 changes: 6 additions & 6 deletions test/integrations/destinations/smartly/router/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const data = [
{
message: {
type: 'track',
event_name: 'product list viewed',
event: 'product list viewed',
properties: {
platform: 'meta',
conversions: 1,
Expand All @@ -32,7 +32,7 @@ export const data = [
{
message: {
type: 'track',
event_name: 'add to cart',
event: 'add to cart',
properties: {
conversions: 3,
platform: 'snapchat',
Expand Down Expand Up @@ -135,7 +135,7 @@ export const data = [
{
message: {
type: 'track',
event_name: 'product list viewed',
event: 'product list viewed',
properties: {
platform: 'meta',
conversions: 1,
Expand All @@ -149,7 +149,7 @@ export const data = [
{
message: {
type: 'track',
event_name: 'purchase',
event: 'purchase',
userId: 'testuserId1',
integrations: { All: true },
properties: {
Expand Down Expand Up @@ -180,7 +180,7 @@ export const data = [
{
message: {
type: 'track',
event_name: 'random event',
event: 'random event',
userId: 'testuserId1',
integrations: { All: true },
properties: {
Expand All @@ -195,7 +195,7 @@ export const data = [
{
message: {
type: 'track',
event_name: 'add to cart',
event: 'add to cart',
userId: 'testuserId1',
integrations: { All: true },
properties: {
Expand Down

0 comments on commit 5b659a0

Please sign in to comment.