Skip to content

Commit

Permalink
Merge branch 'develop' into feat.sprig-cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-4116 authored Dec 11, 2023
2 parents dc5b248 + 36d3f25 commit a7bfce5
Show file tree
Hide file tree
Showing 36 changed files with 6,725 additions and 4,715 deletions.
10 changes: 8 additions & 2 deletions src/adapters/networkHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ SUPPORTED_VERSIONS.forEach((version) => {
});

const getNetworkHandler = (type, version) => {
const NetworkHandler = handlers[version][type] || handlers.generic;
return new NetworkHandler();
let handlerVersion = version;
let NetworkHandler = handlers[version][type] || handlers.generic;
if (version === 'v1' && NetworkHandler === handlers.generic) {
NetworkHandler = handlers.v0[type] || handlers.generic;
handlerVersion = 'v0';
}
const networkHandler = new NetworkHandler();
return { networkHandler, handlerVersion };
};

module.exports = {
Expand Down
29 changes: 20 additions & 9 deletions src/adapters/networkHandlerFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,34 @@ const { networkHandler: GenericNetworkHandler } = require('./networkhandler/gene

describe(`Network Handler Tests`, () => {
it('Should return v0 networkhandler', () => {
let proxyHandler = getNetworkHandler('campaign_manager', `v0`);
let { networkHandler, handlerVersion } = getNetworkHandler('campaign_manager', `v0`);
const cmProxy = require(`../v0/destinations/campaign_manager/networkHandler`).networkHandler;
expect(proxyHandler).toEqual(new cmProxy());
expect(networkHandler).toEqual(new cmProxy());
});

proxyHandler = getNetworkHandler('braze', `v0`);
it('Should return v0 networkhandler braze', () => {
let { networkHandler, handlerVersion } = getNetworkHandler('braze', `v0`);
const brazeProxy = require(`../v0/destinations/braze/networkHandler`).networkHandler;
expect(proxyHandler).toEqual(new brazeProxy());
expect(networkHandler).toEqual(new brazeProxy());
});

it('Should return v1 networkhandler', () => {
let proxyHandler = getNetworkHandler('campaign_manager', `v1`);
let { networkHandler, handlerVersion } = getNetworkHandler('campaign_manager', `v1`);
const cmProxy = require(`../v1/destinations/campaign_manager/networkHandler`).networkHandler;
expect(proxyHandler).toEqual(new cmProxy());
expect(networkHandler).toEqual(new cmProxy());
});

it('Should return genericHandler if v1 proxy and handler is not present for destination', () => {
let proxyHandler = getNetworkHandler('braze', `v1`);
expect(proxyHandler).toEqual(new GenericNetworkHandler());
it('Should return v0 handler if v1 version and handler is present for destination in v0', () => {
const { networkHandler, handlerVersion } = getNetworkHandler('braze', `v1`);
const brazeProxy = require(`../v0/destinations/braze/networkHandler`).networkHandler;
console.log(networkHandler);
expect(networkHandler).toEqual(new brazeProxy());
});

it('Should return generic handler', () => {
const { networkHandler, handlerVersion } = getNetworkHandler('abc', `v1`);
const brazeProxy = require(`../v0/destinations/braze/networkHandler`).networkHandler;
console.log(networkHandler);
expect(networkHandler).toEqual(new GenericNetworkHandler());
});
});
68 changes: 58 additions & 10 deletions src/controllers/delivery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/* eslint-disable prefer-destructuring */
/* eslint-disable sonarjs/no-duplicate-string */
import { Context } from 'koa';
import { MiscService } from '../services/misc';
import { DeliveryResponse, ProcessorTransformationOutput } from '../types/index';
import {
DeliveriesResponse,
DeliveryResponse,
ProcessorTransformationOutput,
ProxyDeliveriesRequest,
ProxyDeliveryRequest,
} from '../types/index';
import { ServiceSelector } from '../helpers/serviceSelector';
import { DeliveryTestService } from '../services/delivertTest/deliveryTest';
import { ControllerUtility } from './util';
Expand All @@ -9,37 +17,75 @@ import { DestinationPostTransformationService } from '../services/destination/po
import tags from '../v0/util/tags';
import { FixMe } from '../util/types';

const NON_DETERMINABLE = 'Non-determinable';

export class DeliveryController {
public static async deliverToDestination(ctx: Context) {
logger.debug('Native(Delivery):: Request to transformer::', JSON.stringify(ctx.request.body));
let deliveryResponse: DeliveryResponse;
const requestMetadata = MiscService.getRequestMetadata(ctx);
const event = ctx.request.body as ProcessorTransformationOutput;
const deliveryRequest = ctx.request.body as ProxyDeliveryRequest;
const { destination }: { destination: string } = ctx.params;
const { version }: { version: string } = ctx.params;
const integrationService = ServiceSelector.getNativeDestinationService();
try {
deliveryResponse = await integrationService.deliver(
event,
deliveryResponse = (await integrationService.deliver(
deliveryRequest,
destination,
requestMetadata,
version,
);
'v0',
)) as DeliveryResponse;
} catch (error: any) {
const { metadata } = deliveryRequest;
const metaTO = integrationService.getTags(
destination,
event.metadata?.destinationId || 'Non-determininable',
event.metadata?.workspaceId || 'Non-determininable',
metadata?.destinationId || NON_DETERMINABLE,
metadata?.workspaceId || NON_DETERMINABLE,
tags.FEATURES.DATA_DELIVERY,
);
metaTO.metadata = event.metadata;
metaTO.metadata = metadata;
deliveryResponse = DestinationPostTransformationService.handleDeliveryFailureEvents(
error,
metaTO,
);
}
ctx.body = { output: deliveryResponse };
ControllerUtility.deliveryPostProcess(ctx, deliveryResponse.status);

logger.debug('Native(Delivery):: Response from transformer::', JSON.stringify(ctx.body));
return ctx;
}

public static async deliverToDestinationV1(ctx: Context) {
logger.debug('Native(Delivery):: Request to transformer::', JSON.stringify(ctx.request.body));
let deliveryResponse: DeliveriesResponse;
const requestMetadata = MiscService.getRequestMetadata(ctx);
const deliveryRequest = ctx.request.body as ProxyDeliveriesRequest;
const { destination }: { destination: string } = ctx.params;
const integrationService = ServiceSelector.getNativeDestinationService();
try {
deliveryResponse = (await integrationService.deliver(
deliveryRequest,
destination,
requestMetadata,
'v1',
)) as DeliveriesResponse;
} catch (error: any) {
const { metadata } = deliveryRequest;
const metaTO = integrationService.getTags(
destination,
metadata[0].destinationId || NON_DETERMINABLE,
metadata[0].workspaceId || NON_DETERMINABLE,
tags.FEATURES.DATA_DELIVERY,
);
metaTO.metadatas = metadata;
deliveryResponse = DestinationPostTransformationService.handlevV1DeliveriesFailureEvents(
error,
metaTO,
);
}
ctx.body = { output: deliveryResponse };
ControllerUtility.deliveryPostProcess(ctx);

logger.debug('Native(Delivery):: Response from transformer::', JSON.stringify(ctx.body));
return ctx;
}
Expand All @@ -50,6 +96,7 @@ export class DeliveryController {
JSON.stringify(ctx.request.body),
);
const { destination }: { destination: string } = ctx.params;
const { version }: { version: string } = ctx.params;
const {
deliveryPayload,
destinationRequestPayload,
Expand All @@ -61,6 +108,7 @@ export class DeliveryController {
destination,
destinationRequestPayload,
deliveryPayload,
version,
);
ctx.body = { output: response };
ControllerUtility.postProcess(ctx);
Expand Down
2 changes: 1 addition & 1 deletion src/features.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@
"REDDIT": true
},
"supportSourceTransformV1": true,
"supportTransformerProxyV1": false
"supportTransformerProxyV1": true
}
7 changes: 4 additions & 3 deletions src/interfaces/DestinationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import {
ProcessorTransformationResponse,
RouterTransformationRequestData,
RouterTransformationResponse,
ProcessorTransformationOutput,
UserDeletionRequest,
UserDeletionResponse,
ProxyRequest,
DeliveriesResponse,
} from '../types/index';

export interface DestinationService {
Expand Down Expand Up @@ -44,11 +45,11 @@ export interface DestinationService {
): RouterTransformationResponse[];

deliver(
event: ProcessorTransformationOutput,
event: ProxyRequest,
destinationType: string,
requestMetadata: NonNullable<unknown>,
version: string,
): Promise<DeliveryResponse>;
): Promise<DeliveryResponse | DeliveriesResponse>;

processUserDeletion(
requests: UserDeletionRequest[],
Expand Down
9 changes: 8 additions & 1 deletion src/routes/delivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import { RouteActivationMiddleware } from '../middlewares/routeActivation';
const router = new Router();

router.post(
'/:version/destinations/:destination/proxy',
'/v0/destinations/:destination/proxy',
RouteActivationMiddleware.isDeliveryRouteActive,
RouteActivationMiddleware.destinationDeliveryFilter,
DeliveryController.deliverToDestination,
);

router.post(
'/v1/destinations/:destination/proxy',
RouteActivationMiddleware.isDeliveryRouteActive,
RouteActivationMiddleware.destinationDeliveryFilter,
DeliveryController.deliverToDestinationV1,
);

router.post(
'/:version/destinations/:destination/proxyTest',
RouteActivationMiddleware.isDeliveryTestRouteActive,
Expand Down
8 changes: 4 additions & 4 deletions src/services/comparator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* eslint-disable class-methods-use-this */
import { DestinationService } from '../interfaces/DestinationService';
import {
DeliveriesResponse,
DeliveryResponse,
Destination,
ErrorDetailer,
MetaTransferObject,
ProcessorTransformationOutput,
ProcessorTransformationRequest,
ProcessorTransformationResponse,
ProxyRequest,
RouterTransformationRequestData,
RouterTransformationResponse,
UserDeletionRequest,
Expand Down Expand Up @@ -365,19 +366,18 @@ export class ComparatorService implements DestinationService {
}

public async deliver(
event: ProcessorTransformationOutput,
event: ProxyRequest,
destinationType: string,
requestMetadata: NonNullable<unknown>,
version: string,
): Promise<DeliveryResponse> {
): Promise<DeliveryResponse | DeliveriesResponse> {
const primaryResplist = await this.primaryService.deliver(
event,
destinationType,
requestMetadata,
version,
);
logger.error('[LIVE_COMPARE_TEST] not implemented for delivery routine');

return primaryResplist;
}

Expand Down
5 changes: 3 additions & 2 deletions src/services/delivertTest/deliveryTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ export class DeliveryTestService {
destination: string,
routerDestReqPayload: any,
routerDeliveryPayload: any,
version,
) {
let response: any;
try {
const destNetworkHandler = networkHandlerFactory.getNetworkHandler(destination);
const { networkHandler } = networkHandlerFactory.getNetworkHandler(destination, version);

const proxyDestReqPayload = destNetworkHandler.prepareProxy(routerDeliveryPayload);
const proxyDestReqPayload = networkHandler.prepareProxy(routerDeliveryPayload);
response = {
destinationRequestPayload: proxyDestReqPayload,
};
Expand Down
7 changes: 4 additions & 3 deletions src/services/destination/cdkV1Integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import {
ProcessorTransformationResponse,
RouterTransformationRequestData,
RouterTransformationResponse,
ProcessorTransformationOutput,
UserDeletionRequest,
UserDeletionResponse,
ProxyRequest,
DeliveriesResponse,
} from '../../types/index';
import { DestinationPostTransformationService } from './postTransformation';
import tags from '../../v0/util/tags';
Expand Down Expand Up @@ -117,10 +118,10 @@ export class CDKV1DestinationService implements DestinationService {
}

public deliver(
_event: ProcessorTransformationOutput,
_event: ProxyRequest,
_destinationType: string,
_requestMetadata: NonNullable<unknown>,
): Promise<DeliveryResponse> {
): Promise<DeliveryResponse | DeliveriesResponse> {
throw new TransformationError('CDV1 Does not Implement Delivery Routine');
}

Expand Down
15 changes: 11 additions & 4 deletions src/services/destination/cdkV2Integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
ProcessorTransformationOutput,
UserDeletionRequest,
UserDeletionResponse,
ProxyRequest,
DeliveriesResponse,
} from '../../types/index';
import tags from '../../v0/util/tags';
import { DestinationPostTransformationService } from './postTransformation';
Expand Down Expand Up @@ -64,7 +66,7 @@ export class CDKV2DestinationService implements DestinationService {
destinationType,
event,
tags.FEATURES.PROCESSOR,
requestMetadata
requestMetadata,
);

stats.increment('event_transform_success', {
Expand Down Expand Up @@ -127,7 +129,12 @@ export class CDKV2DestinationService implements DestinationService {
metaTo.metadata = destInputArray[0].metadata;
try {
const doRouterTransformationResponse: RouterTransformationResponse[] =
await processCdkV2Workflow(destinationType, destInputArray, tags.FEATURES.ROUTER, requestMetadata);
await processCdkV2Workflow(
destinationType,
destInputArray,
tags.FEATURES.ROUTER,
requestMetadata,
);
return DestinationPostTransformationService.handleRouterTransformSuccessEvents(
doRouterTransformationResponse,
undefined,
Expand Down Expand Up @@ -160,10 +167,10 @@ export class CDKV2DestinationService implements DestinationService {
}

public deliver(
_event: ProcessorTransformationOutput,
_event: ProxyRequest,
_destinationType: string,
_requestMetadata: NonNullable<unknown>,
): Promise<DeliveryResponse> {
): Promise<DeliveryResponse | DeliveriesResponse> {
throw new TransformationError('CDKV2 Does not Implement Delivery Routine');
}

Expand Down
Loading

0 comments on commit a7bfce5

Please sign in to comment.