Skip to content

Commit

Permalink
Merge branch 'develop' into feat.intercom
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-4116 authored Jan 31, 2024
2 parents 851e0e2 + 64ab555 commit 8ca8822
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 6 deletions.
19 changes: 19 additions & 0 deletions src/v0/destinations/marketo_bulk_upload/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ const FETCH_FAILURE_JOB_STATUS_ERR_MSG = 'Could not fetch failure job status';
const FETCH_WARNING_JOB_STATUS_ERR_MSG = 'Could not fetch warning job status';
const ACCESS_TOKEN_FETCH_ERR_MSG = 'Error during fetching access token';

const SCHEMA_DATA_TYPE_MAP = {
string: 'string',
number: 'number',
boolean: 'boolean',
undefined: 'undefined',
float: 'number',
text: 'string',
currency: 'string',
integer: 'number',
reference: 'string',
datetime: 'string',
date: 'string',
email: 'string',
phone: 'string',
url: 'string',
object: 'object',
};

module.exports = {
ABORTABLE_CODES,
RETRYABLE_CODES,
Expand All @@ -33,4 +51,5 @@ module.exports = {
FETCH_FAILURE_JOB_STATUS_ERR_MSG,
FETCH_WARNING_JOB_STATUS_ERR_MSG,
ACCESS_TOKEN_FETCH_ERR_MSG,
SCHEMA_DATA_TYPE_MAP,
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
handlePollResponse,
handleFileUploadResponse,
getAccessToken,
checkEventStatusViaSchemaMatching,
} = require('./util');

const {
Expand Down Expand Up @@ -353,3 +354,187 @@ describe('getAccessToken', () => {
await expect(getAccessToken(config)).rejects.toThrow(TransformationError);
});
});

describe('checkEventStatusViaSchemaMatching', () => {
// The function correctly identifies fields with expected data types.
it('if event data types match with expected data types we send no field as mismatch', () => {
const event = {
input: [
{
message: {
email: 'value1',
id: 123,
isLead: true,
},
metadata: {
job_id: 'job1',
},
},
],
};
const fieldSchemaMapping = {
email: 'string',
id: 'integer',
isLead: 'boolean',
};

const result = checkEventStatusViaSchemaMatching(event, fieldSchemaMapping);

expect(result).toEqual({});
});

// The function correctly identifies fields with unexpected data types.
it('if event data types do not match with expected data types we send that field as mismatch', () => {
const event = {
input: [
{
message: {
email: 123,
city: '123',
islead: true,
},
metadata: {
job_id: 'job1',
},
},
],
};
const fieldSchemaMapping = {
email: 'string',
city: 'number',
islead: 'boolean',
};

const result = checkEventStatusViaSchemaMatching(event, fieldSchemaMapping);

expect(result).toEqual({
job1: 'invalid email',
});
});

// The function correctly handles events with multiple fields.
it('For array of events the mismatch object fills up with each event errors', () => {
const event = {
input: [
{
message: {
id: 'value1',
testCustomFieldScore: 123,
isLead: true,
},
metadata: {
job_id: 'job1',
},
},
{
message: {
email: 'value2',
id: 456,
testCustomFieldScore: false,
},
metadata: {
job_id: 'job2',
},
},
],
};
const fieldSchemaMapping = {
email: 'email',
id: 'integer',
testCustomFieldScore: 'integer',
isLead: 'boolean',
};

const result = checkEventStatusViaSchemaMatching(event, fieldSchemaMapping);

expect(result).toEqual({
job1: 'invalid id',
job2: 'invalid testCustomFieldScore',
});
});

// The function correctly handles events with missing fields.
it('it is not mandatory to send all the fields present in schema', () => {
const event = {
input: [
{
message: {
email: 'value1',
isLead: true,
},
metadata: {
job_id: 'job1',
},
},
],
};
const fieldSchemaMapping = {
email: 'string',
id: 'number',
isLead: 'boolean',
};

const result = checkEventStatusViaSchemaMatching(event, fieldSchemaMapping);

expect(result).toEqual({});
});

// The function correctly handles events with additional fields. But this will not happen in our use case
it('for any field beyond schema fields will be mapped as invalid', () => {
const event = {
input: [
{
message: {
email: 'value1',
id: 124,
isLead: true,
abc: 'value2',
},
metadata: {
job_id: 'job1',
},
},
],
};
const fieldSchemaMapping = {
email: 'string',
id: 'number',
isLead: 'boolean',
};

const result = checkEventStatusViaSchemaMatching(event, fieldSchemaMapping);

expect(result).toEqual({
job1: 'invalid abc',
});
});

// The function correctly handles events with null values.
it('should correctly handle events with null values', () => {
const event = {
input: [
{
message: {
email: 'value1',
id: null,
isLead: true,
},
metadata: {
job_id: 'job1',
},
},
],
};
const fieldSchemaMapping = {
email: 'string',
id: 'number',
isLead: 'boolean',
};

const result = checkEventStatusViaSchemaMatching(event, fieldSchemaMapping);

expect(result).toEqual({
job1: 'invalid id',
});
});
});
8 changes: 2 additions & 6 deletions src/v0/destinations/marketo_bulk_upload/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
POLL_STATUS_ERR_MSG,
FILE_UPLOAD_ERR_MSG,
ACCESS_TOKEN_FETCH_ERR_MSG,
SCHEMA_DATA_TYPE_MAP,
} = require('./config');
const logger = require('../../../logger');

Expand Down Expand Up @@ -401,14 +402,9 @@ const checkEventStatusViaSchemaMatching = (event, fieldMap) => {
const { job_id } = metadata;

Object.entries(message).forEach(([paramName, paramValue]) => {
let expectedDataType = fieldMap[paramName];
const expectedDataType = SCHEMA_DATA_TYPE_MAP[fieldMap[paramName]];
const actualDataType = typeof paramValue;

// If expectedDataType is not one of the primitive data types, treat it as a string
if (!['string', 'number', 'boolean', 'undefined'].includes(expectedDataType)) {
expectedDataType = 'string';
}

if (!mismatchedFields[job_id] && actualDataType !== expectedDataType) {
mismatchedFields[job_id] = `invalid ${paramName}`;
}
Expand Down

0 comments on commit 8ca8822

Please sign in to comment.