diff --git a/src/helpers/geoLocation.ts b/src/helpers/geoLocation.ts index 1a9b6a3939..710c02c5ef 100644 --- a/src/helpers/geoLocation.ts +++ b/src/helpers/geoLocation.ts @@ -25,7 +25,7 @@ export default class GeoLocationHelper { public static getGeoLocationData(message: Record): Record { const msg = cloneDeep(message || {}); - if (isEmpty(msg.context.geo)) { + if (isEmpty(msg?.context?.geo || {})) { // geo-location data was not sent return {}; } diff --git a/src/middlewares/enricher.ts b/src/middlewares/enricher.ts index eff69a155e..0b84b19ab0 100644 --- a/src/middlewares/enricher.ts +++ b/src/middlewares/enricher.ts @@ -1,4 +1,4 @@ -import { Context } from 'koa'; +import { Context, Next } from 'koa'; import { ProcessorTransformationRequest, RouterTransformationRequest, @@ -24,7 +24,7 @@ export default class Enricher { }); } - public static enrichGeoLocation(ctx: Context) { + public static async enrichGeoLocation(ctx: Context, next: Next) { const transformationRequest = ctx.request.body; let transformationReq: DTRequest; let reqBody: unknown; @@ -47,5 +47,6 @@ export default class Enricher { reqBody = Enricher.enrichWithGeoInfo(transformationReq); } ctx.request.body = reqBody; + await next(); } } diff --git a/src/routes/destination.ts b/src/routes/destination.ts index 3d4be42ff3..e8caaf99e7 100644 --- a/src/routes/destination.ts +++ b/src/routes/destination.ts @@ -3,6 +3,7 @@ import DestinationController from '../controllers/destination'; import RegulationController from '../controllers/regulation'; import FeatureFlagController from '../middlewares/featureFlag'; import RouteActivationController from '../middlewares/routeActivation'; +import Enricher from '../middlewares/enricher'; const router = new Router(); @@ -11,6 +12,7 @@ router.post( RouteActivationController.isDestinationRouteActive, RouteActivationController.destinationProcFilter, FeatureFlagController.handle, + Enricher.enrichGeoLocation, DestinationController.destinationTransformAtProcessor, ); router.post( @@ -18,6 +20,7 @@ router.post( RouteActivationController.isDestinationRouteActive, RouteActivationController.destinationRtFilter, FeatureFlagController.handle, + Enricher.enrichGeoLocation, DestinationController.destinationTransformAtRouter, ); router.post( @@ -25,6 +28,7 @@ router.post( RouteActivationController.isDestinationRouteActive, RouteActivationController.destinationBatchFilter, FeatureFlagController.handle, + Enricher.enrichGeoLocation, DestinationController.batchProcess, ); diff --git a/test/middleware/enricher.test.ts b/test/middleware/enricher.test.ts index cdf2a33cd1..23fb3ee924 100644 --- a/test/middleware/enricher.test.ts +++ b/test/middleware/enricher.test.ts @@ -2,7 +2,7 @@ import { ProcessorTransformationRequest, RouterTransformationRequest } from '../ import Enricher from '../../src/middlewares/enricher'; describe('[GeoLocation Enrichment] Processor transformation tests', () => { - test('should enrich when context.geo is populated correctly', () => { + test('should enrich when context.geo is populated correctly', async () => { const inputData: ProcessorTransformationRequest[] = [ { destination: { @@ -95,7 +95,7 @@ describe('[GeoLocation Enrichment] Processor transformation tests', () => { ]; const ctx = { request: { body: inputData } }; // @ts-ignore - Enricher.enrichGeoLocation(ctx); + await Enricher.enrichGeoLocation(ctx, () => {}); expect(ctx.request.body[0].message.traits).toMatchObject( expect.objectContaining({ age: 23, @@ -111,7 +111,7 @@ describe('[GeoLocation Enrichment] Processor transformation tests', () => { ); }); - test('should not enrich when address is already enhanced', () => { + test('should not enrich when address is already enhanced', async () => { const inputData: ProcessorTransformationRequest[] = [ { destination: { @@ -211,7 +211,7 @@ describe('[GeoLocation Enrichment] Processor transformation tests', () => { ]; const ctx = { request: { body: inputData } }; // @ts-ignore - Enricher.enrichGeoLocation(ctx); + await Enricher.enrichGeoLocation(ctx, () => {}); expect(ctx.request.body[0].message.traits).toMatchObject( expect.objectContaining({ age: 23, @@ -228,7 +228,7 @@ describe('[GeoLocation Enrichment] Processor transformation tests', () => { ); }); - test('should enrich only those fields that are not already enriched when address already contains partial data', () => { + test('should enrich only those fields that are not already enriched when address already contains partial data', async () => { const inputData: ProcessorTransformationRequest[] = [ { destination: { @@ -327,7 +327,7 @@ describe('[GeoLocation Enrichment] Processor transformation tests', () => { ]; const ctx = { request: { body: inputData } }; // @ts-ignore - Enricher.enrichGeoLocation(ctx); + await Enricher.enrichGeoLocation(ctx, () => {}); expect(ctx.request.body[0].message.traits).toMatchObject( expect.objectContaining({ age: 23, @@ -345,7 +345,7 @@ describe('[GeoLocation Enrichment] Processor transformation tests', () => { ); }); - test('should not enrich when context.geo is not populated', () => { + test('should not enrich when context.geo is not populated', async () => { const inputData: ProcessorTransformationRequest[] = [ { destination: { @@ -429,13 +429,13 @@ describe('[GeoLocation Enrichment] Processor transformation tests', () => { ]; const ctx = { request: { body: inputData } }; // @ts-ignore - Enricher.enrichGeoLocation(ctx); + await Enricher.enrichGeoLocation(ctx, () => {}); // @ts-ignore expect(ctx.request.body[0].message.traits?.address).toBe(undefined); }); - test('should not contain address object, when context.geo & address are not present', () => { + test('should not contain address object, when context.geo & address are not present', async () => { const inputData: ProcessorTransformationRequest[] = [ { destination: { @@ -596,12 +596,12 @@ describe('[GeoLocation Enrichment] Processor transformation tests', () => { ]; const ctx = { request: { body: inputData } }; // @ts-ignore - Enricher.enrichGeoLocation(ctx); + await Enricher.enrichGeoLocation(ctx, () => {}); expect(ctx.request.body[0].message.traits).not.toHaveProperty('address'); expect(ctx.request.body[1].message.traits).not.toHaveProperty('address'); }); - test('should enrich when context.geo is populated correctly for multiple payloads with their own geolocation data', () => { + test('should enrich when context.geo is populated correctly for multiple payloads with their own geolocation data', async () => { const inputData: ProcessorTransformationRequest[] = [ { destination: { @@ -782,7 +782,7 @@ describe('[GeoLocation Enrichment] Processor transformation tests', () => { ]; const ctx = { request: { body: inputData } }; // @ts-ignore - Enricher.enrichGeoLocation(ctx); + await Enricher.enrichGeoLocation(ctx, () => {}); expect(ctx.request.body[0].message.traits).toMatchObject( expect.objectContaining({ age: 23, @@ -811,7 +811,7 @@ describe('[GeoLocation Enrichment] Processor transformation tests', () => { }); describe('[GeoLocation Enrichment] Router/Batch transformation tests', () => { - test('should enrich with geo information when context.geo is present', () => { + test('should enrich with geo information when context.geo is present', async () => { const inputData: RouterTransformationRequest = { input: [ { @@ -998,7 +998,7 @@ describe('[GeoLocation Enrichment] Router/Batch transformation tests', () => { }; const ctx = { request: { body: inputData } }; // @ts-ignore - Enricher.enrichGeoLocation(ctx); + await Enricher.enrichGeoLocation(ctx, () => {}); expect(ctx.request.body.input[0].message.traits).toMatchObject( expect.objectContaining({ age: 23,