diff --git a/src/v0/destinations/bqstream/transform.js b/src/v0/destinations/bqstream/transform.js index 631689cc75..fd8d0adfd5 100644 --- a/src/v0/destinations/bqstream/transform.js +++ b/src/v0/destinations/bqstream/transform.js @@ -136,7 +136,7 @@ const processRouterDest = (inputs) => { let eachUserBatchedResponse = []; orderedEventsList.forEach((eventList) => { // no error event list will have more than one items in the list - if(eventList[0].error) { + if (eventList[0].error) { finalResp.push([...eventList]); } else { // batch the successful events diff --git a/src/v0/destinations/bqstream/util.js b/src/v0/destinations/bqstream/util.js index df00b55779..f5e97c9209 100644 --- a/src/v0/destinations/bqstream/util.js +++ b/src/v0/destinations/bqstream/util.js @@ -172,7 +172,7 @@ const filterAndSplitEvents = (sortedEvents) => { // if error is present, then push the previous successfulEventsChunk // and then push the error event if (isDefinedAndNotNull(item.error)) { - if(successfulEventsChunk.length > 0) { + if (successfulEventsChunk.length > 0) { resultArray.push(successfulEventsChunk); successfulEventsChunk = []; } @@ -180,9 +180,9 @@ const filterAndSplitEvents = (sortedEvents) => { } else { // if error is not present, then push the event to successfulEventsChunk successfulEventsChunk.push(item); - } + } } - // Push the last successfulEventsChunk to resultArray + // Push the last successfulEventsChunk to resultArray if (successfulEventsChunk.length > 0) { resultArray.push(successfulEventsChunk); } @@ -190,7 +190,7 @@ const filterAndSplitEvents = (sortedEvents) => { }; -const convertMetadataToArray = (eventList ) => { +const convertMetadataToArray = (eventList) => { const processedEvents = eventList.map((event) => ({ ...event, metadata: Array.isArray(event.metadata) ? event.metadata : [event.metadata], @@ -198,43 +198,43 @@ const convertMetadataToArray = (eventList ) => { return processedEvents; } - /** - * Takes in two arrays, eachUserSuccessEventslist and eachUserErrorEventsList, and returns an ordered array of events. - * If there are no error events, it returns the array of transformed events. - * If there are no successful responses, it returns the error events. - * If there are both successful and erroneous events, it orders them based on the jobId property of the events' metadata array. - * considering error responses are built with @handleRtTfSingleEventError - * - * @param {Array} eachUserSuccessEventslist - An array of events representing successful responses for a user. - * @param {Array} eachUserErrorEventsList - An array of events representing error responses for a user. - * @returns {Array} - An ordered array of events. - * - * @example - * const eachUserSuccessEventslist = [{track, jobId: 1}, {track, jobId: 2}, {track, jobId: 5}]; - * const eachUserErrorEventsList = [{identify, jobId: 3}, {identify, jobId: 4}]; - * Output: [[{track, jobId: 1}, {track, jobId: 2}],[{identify, jobId: 3}],[{identify, jobId: 4}], {track, jobId: 5}]] - */ - const HandleEventOrdering = (eachUserSuccessEventslist, eachUserErrorEventsList) => { - // Convert 'metadata' to an array if it's not already - const processedSuccessfulEvents = convertMetadataToArray(eachUserSuccessEventslist); - const processedErrorEvents = convertMetadataToArray(eachUserErrorEventsList); - - // if there are no error events, then return the batched response - if (eachUserErrorEventsList.length === 0) { - return [processedSuccessfulEvents]; - } - // if there are no batched response, then return the error events - if (eachUserSuccessEventslist.length === 0) { - return [processedErrorEvents]; - } +/** + * Takes in two arrays, eachUserSuccessEventslist and eachUserErrorEventsList, and returns an ordered array of events. + * If there are no error events, it returns the array of transformed events. + * If there are no successful responses, it returns the error events. + * If there are both successful and erroneous events, it orders them based on the jobId property of the events' metadata array. + * considering error responses are built with @handleRtTfSingleEventError + * + * @param {Array} eachUserSuccessEventslist - An array of events representing successful responses for a user. + * @param {Array} eachUserErrorEventsList - An array of events representing error responses for a user. + * @returns {Array} - An ordered array of events. + * + * @example + * const eachUserSuccessEventslist = [{track, jobId: 1}, {track, jobId: 2}, {track, jobId: 5}]; + * const eachUserErrorEventsList = [{identify, jobId: 3}, {identify, jobId: 4}]; + * Output: [[{track, jobId: 1}, {track, jobId: 2}],[{identify, jobId: 3}],[{identify, jobId: 4}], {track, jobId: 5}]] + */ +const HandleEventOrdering = (eachUserSuccessEventslist, eachUserErrorEventsList) => { + // Convert 'metadata' to an array if it's not already + const processedSuccessfulEvents = convertMetadataToArray(eachUserSuccessEventslist); + const processedErrorEvents = convertMetadataToArray(eachUserErrorEventsList); + + // if there are no error events, then return the batched response + if (eachUserErrorEventsList.length === 0) { + return [processedSuccessfulEvents]; + } + // if there are no batched response, then return the error events + if (eachUserSuccessEventslist.length === 0) { + return [processedErrorEvents]; + } - // if there are both batched response and error events, then order them - const combinedTransformedEventList = [...processedSuccessfulEvents, ...processedErrorEvents].flat(); - - const sortedEvents = _.sortBy(combinedTransformedEventList, (event) => event.metadata[0].jobId); - const finalResp = filterAndSplitEvents(sortedEvents); + // if there are both batched response and error events, then order them + const combinedTransformedEventList = [...processedSuccessfulEvents, ...processedErrorEvents].flat(); - return finalResp; - } + const sortedEvents = _.sortBy(combinedTransformedEventList, (event) => event.metadata[0].jobId); + const finalResp = filterAndSplitEvents(sortedEvents); + + return finalResp; +} -module.exports = { networkHandler, generateUserJourneys, HandleEventOrdering }; +module.exports = { networkHandler, generateUserJourneys, HandleEventOrdering }; diff --git a/src/v0/destinations/bqstream/utils.test.js b/src/v0/destinations/bqstream/utils.test.js index 8a8273cdea..707e547cd5 100644 --- a/src/v0/destinations/bqstream/utils.test.js +++ b/src/v0/destinations/bqstream/utils.test.js @@ -1,42 +1,40 @@ -const {HandleEventOrdering} = require('./util') +const { HandleEventOrdering } = require('./util') describe('HandleEventOrdering', () => { - // Tests that the function returns an array of transformed events when there are no error events - it('should return an array of transformed events when there are no error events', () => { - const eachUserSuccessEventslist = [{message: { type: "track"}, metadata: {jobId: 1}}, {message: { type: "track"}, metadata: {jobId: 3}}, {message: { type: "track"}, metadata: {jobId: 5}}]; - const eachUserErrorEventsList = []; - const expected = [[{"message":{"type":"track"},"metadata":[{"jobId":1}]},{"message":{"type":"track"},"metadata":[{"jobId":3}]},{"message":{"type":"track"},"metadata":[{"jobId":5}]}]]; - const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList); - console.log(JSON.stringify(result)) - expect(result).toEqual(expected); - }); + // Tests that the function returns an array of transformed events when there are no error events + it('should return an array of transformed events when there are no error events', () => { + const eachUserSuccessEventslist = [{ message: { type: "track" }, metadata: { jobId: 1 } }, { message: { type: "track" }, metadata: { jobId: 3 } }, { message: { type: "track" }, metadata: { jobId: 5 } }]; + const eachUserErrorEventsList = []; + const expected = [[{ "message": { "type": "track" }, "metadata": [{ "jobId": 1 }] }, { "message": { "type": "track" }, "metadata": [{ "jobId": 3 }] }, { "message": { "type": "track" }, "metadata": [{ "jobId": 5 }] }]]; + const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList); + expect(result).toEqual(expected); + }); - // Tests that the function returns an empty array when both input arrays are empty - it('should return an empty array when both input arrays are empty', () => { - const eachUserSuccessEventslist = []; - const eachUserErrorEventsList = []; - const expected = [[]]; - const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList); - expect(result).toEqual(expected); - }); + // Tests that the function returns an empty array when both input arrays are empty + it('should return an empty array when both input arrays are empty', () => { + const eachUserSuccessEventslist = []; + const eachUserErrorEventsList = []; + const expected = [[]]; + const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList); + expect(result).toEqual(expected); + }); - // Tests that the function returns an array with only error events when all events are erroneous - it('should return an array with only error events when all events are erroneous', () => { - const eachUserSuccessEventslist = []; - const eachUserErrorEventsList = [{batched: false,destination: {},error: 'Message Type not supported: identify',metadata: [{jobId: 3,userId: 'user12345'}]}, {batched: false,destination: {},error: 'Message Type not supported: identify',metadata: [{jobId: 4,userId: 'user12345'}]}]; - const expected = [[{batched: false,destination: {},error: 'Message Type not supported: identify',metadata: [{jobId: 3,userId: 'user12345'}]}, {batched: false,destination: {},error: 'Message Type not supported: identify',metadata: [{jobId: 4,userId: 'user12345'}]}]]; - const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList); - expect(result).toEqual(expected); - }); + // Tests that the function returns an array with only error events when all events are erroneous + it('should return an array with only error events when all events are erroneous', () => { + const eachUserSuccessEventslist = []; + const eachUserErrorEventsList = [{ batched: false, destination: {}, error: 'Message Type not supported: identify', metadata: [{ jobId: 3, userId: 'user12345' }] }, { batched: false, destination: {}, error: 'Message Type not supported: identify', metadata: [{ jobId: 4, userId: 'user12345' }] }]; + const expected = [[{ batched: false, destination: {}, error: 'Message Type not supported: identify', metadata: [{ jobId: 3, userId: 'user12345' }] }, { batched: false, destination: {}, error: 'Message Type not supported: identify', metadata: [{ jobId: 4, userId: 'user12345' }] }]]; + const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList); + expect(result).toEqual(expected); + }); - // Tests that the function returns an ordered array of events with both successful and erroneous events, ordered based on the jobId property of the events' metadata array - it('should return an ordered array of events with both successful and erroneous events', () => { - const eachUserSuccessEventslist = [{batched: false,destination: {},error: 'Message Type not supported: identify',metadata: [{jobId: 3,userId: 'user12345'}]}, {batched: false,destination: {},error: 'Message Type not supported: identify',metadata: [{jobId: 4,userId: 'user12345'}]}]; - const eachUserErrorEventsList = [{message: { type: "track"}, metadata: {jobId: 1}}, {message: { type: "track"}, metadata: {jobId: 2}}, {message: { type: "track"}, metadata: {jobId: 5}}]; - const expected = [[{"message":{"type":"track"},"metadata":[{"jobId":1}]},{"message":{"type":"track"},"metadata":[{"jobId":2}]}],[{"batched":false,"destination":{},"error":"Message Type not supported: identify","metadata":[{"jobId":3,"userId":"user12345"}]}],[{"batched":false,"destination":{},"error":"Message Type not supported: identify","metadata":[{"jobId":4,"userId":"user12345"}]}],[{"message":{"type":"track"},"metadata":[{"jobId":5}]}]]; - const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList); - console.log(JSON.stringify(result)) - expect(result).toEqual(expected); - }); + // Tests that the function returns an ordered array of events with both successful and erroneous events, ordered based on the jobId property of the events' metadata array + it('should return an ordered array of events with both successful and erroneous events', () => { + const eachUserSuccessEventslist = [{ batched: false, destination: {}, error: 'Message Type not supported: identify', metadata: [{ jobId: 3, userId: 'user12345' }] }, { batched: false, destination: {}, error: 'Message Type not supported: identify', metadata: [{ jobId: 4, userId: 'user12345' }] }]; + const eachUserErrorEventsList = [{ message: { type: "track" }, metadata: { jobId: 1 } }, { message: { type: "track" }, metadata: { jobId: 2 } }, { message: { type: "track" }, metadata: { jobId: 5 } }]; + const expected = [[{ "message": { "type": "track" }, "metadata": [{ "jobId": 1 }] }, { "message": { "type": "track" }, "metadata": [{ "jobId": 2 }] }], [{ "batched": false, "destination": {}, "error": "Message Type not supported: identify", "metadata": [{ "jobId": 3, "userId": "user12345" }] }], [{ "batched": false, "destination": {}, "error": "Message Type not supported: identify", "metadata": [{ "jobId": 4, "userId": "user12345" }] }], [{ "message": { "type": "track" }, "metadata": [{ "jobId": 5 }] }]]; + const result = HandleEventOrdering(eachUserSuccessEventslist, eachUserErrorEventsList); + expect(result).toEqual(expected); + }); });