From c67c9d1acd38ef157ae6b55d687e71c13cf5e166 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Fri, 6 Dec 2024 20:03:29 +0530 Subject: [PATCH] chore: improve iterable network handler --- .../destinations/iterable/networkHandler.ts | 12 ++-- .../generic.ts} | 18 ++---- .../principal.ts} | 33 +++++------ .../track-identify.ts} | 33 +++-------- src/v1/destinations/iterable/type.ts | 26 --------- src/v1/destinations/iterable/types.ts | 57 +++++++++++++++++++ .../iterable/dataDelivery/business.ts | 4 +- 7 files changed, 93 insertions(+), 90 deletions(-) rename src/v1/destinations/iterable/{commonStrategy.ts => strategies/generic.ts} (54%) rename src/v1/destinations/iterable/{responseStrategy.ts => strategies/principal.ts} (54%) rename src/v1/destinations/iterable/{trackIdentifyStrategy.ts => strategies/track-identify.ts} (58%) delete mode 100644 src/v1/destinations/iterable/type.ts create mode 100644 src/v1/destinations/iterable/types.ts diff --git a/src/v1/destinations/iterable/networkHandler.ts b/src/v1/destinations/iterable/networkHandler.ts index b409facc4f..06ab77b317 100644 --- a/src/v1/destinations/iterable/networkHandler.ts +++ b/src/v1/destinations/iterable/networkHandler.ts @@ -1,25 +1,25 @@ import { prepareProxyRequest, proxyRequest } from '../../../adapters/network'; import { processAxiosResponse } from '../../../adapters/utils/networkUtils'; import { BULK_ENDPOINTS } from '../../../v0/destinations/iterable/config'; -import { CommonStrategy } from './commonStrategy'; -import { TrackIdentifyStrategy } from './trackIdentifyStrategy'; +import { GenericStrategy } from './strategies/generic'; +import { TrackIdentifyStrategy } from './strategies/track-identify'; -interface ResponseParams { +type ResponseParams = { destinationRequest: { endpoint: string; }; -} +}; const strategyRegistry: { [key: string]: any } = { [TrackIdentifyStrategy.name]: new TrackIdentifyStrategy(), - [CommonStrategy.name]: new CommonStrategy(), + [GenericStrategy.name]: new GenericStrategy(), }; const getResponseStrategy = (endpoint: string) => { if (BULK_ENDPOINTS.some((path) => endpoint.includes(path))) { return strategyRegistry[TrackIdentifyStrategy.name]; } - return strategyRegistry[CommonStrategy.name]; + return strategyRegistry[GenericStrategy.name]; }; const responseHandler = (responseParams: ResponseParams) => { diff --git a/src/v1/destinations/iterable/commonStrategy.ts b/src/v1/destinations/iterable/strategies/generic.ts similarity index 54% rename from src/v1/destinations/iterable/commonStrategy.ts rename to src/v1/destinations/iterable/strategies/generic.ts index 5d6aa2a892..fa13f71a97 100644 --- a/src/v1/destinations/iterable/commonStrategy.ts +++ b/src/v1/destinations/iterable/strategies/generic.ts @@ -1,17 +1,11 @@ -import { CommonResponse } from './type'; +import { PrincipalStrategy } from './principal'; +import { DestinationResponse, SuccessResponse } from '../types'; -const { ResponseStrategy } = require('./responseStrategy'); - -class CommonStrategy extends ResponseStrategy { +class GenericStrategy extends PrincipalStrategy { handleSuccess(responseParams: { - destinationResponse: { status: number; response: CommonResponse }; + destinationResponse: DestinationResponse; rudderJobMetadata: any[]; - }): { - status: number; - message: string; - destinationResponse: { status: number; response: CommonResponse }; - response: { statusCode: number; metadata: any; error: string }[]; - } { + }): SuccessResponse { const { destinationResponse, rudderJobMetadata } = responseParams; const { status } = destinationResponse; @@ -30,4 +24,4 @@ class CommonStrategy extends ResponseStrategy { } } -export { CommonStrategy }; +export { GenericStrategy }; diff --git a/src/v1/destinations/iterable/responseStrategy.ts b/src/v1/destinations/iterable/strategies/principal.ts similarity index 54% rename from src/v1/destinations/iterable/responseStrategy.ts rename to src/v1/destinations/iterable/strategies/principal.ts index 9c2d1eeb40..f27e706e4b 100644 --- a/src/v1/destinations/iterable/responseStrategy.ts +++ b/src/v1/destinations/iterable/strategies/principal.ts @@ -1,14 +1,11 @@ -import { CommonResponse } from './type'; - -const { isHttpStatusSuccess } = require('../../../v0/util/index'); -const { TransformerProxyError } = require('../../../v0/util/errorTypes'); -const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils'); -const tags = require('../../../v0/util/tags'); - -class ResponseStrategy { - handleResponse(responseParams: { - destinationResponse: { status: number; response: CommonResponse }; - }): void { +import { TAG_NAMES } from '@rudderstack/integrations-lib'; +import { getDynamicErrorType } from '../../../../adapters/utils/networkUtils'; +import { isHttpStatusSuccess } from '../../../../v0/util'; +import { TransformerProxyError } from '../../../../v0/util/errorTypes'; +import { DestinationResponse, ResponseParams } from '../types'; + +class PrincipalStrategy { + handleResponse(responseParams: { destinationResponse: DestinationResponse }): void { const { destinationResponse } = responseParams; const { status } = destinationResponse; @@ -22,14 +19,12 @@ class ResponseStrategy { return this.handleSuccess(responseParams); } - handleError(responseParams): void { + handleError(responseParams: ResponseParams): void { const { destinationResponse, rudderJobMetadata } = responseParams; const { response, status } = destinationResponse; - const errorMessage = - JSON.stringify(response.params) || - JSON.stringify(response.msg) || - JSON.stringify(response.message) || - 'unknown error format'; + // @ts-expect-error: not sure if this is correct or needed + const responseMessage = response.params || response.msg || response.message; + const errorMessage = JSON.stringify(responseMessage) || 'unknown error format'; const responseWithIndividualEvents = rudderJobMetadata.map((metadata) => ({ statusCode: status, @@ -40,7 +35,7 @@ class ResponseStrategy { throw new TransformerProxyError( `ITERABLE: Error transformer proxy during ITERABLE response transformation. ${errorMessage}`, status, - { [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status) }, + { [TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status) }, destinationResponse, '', responseWithIndividualEvents, @@ -52,4 +47,4 @@ class ResponseStrategy { } } -export { ResponseStrategy }; +export { PrincipalStrategy }; diff --git a/src/v1/destinations/iterable/trackIdentifyStrategy.ts b/src/v1/destinations/iterable/strategies/track-identify.ts similarity index 58% rename from src/v1/destinations/iterable/trackIdentifyStrategy.ts rename to src/v1/destinations/iterable/strategies/track-identify.ts index 5ca5d4ae84..170d1e4130 100644 --- a/src/v1/destinations/iterable/trackIdentifyStrategy.ts +++ b/src/v1/destinations/iterable/strategies/track-identify.ts @@ -1,36 +1,19 @@ -import { ResponseStrategy } from './responseStrategy'; -import { checkIfEventIsAbortableAndExtractErrorMessage } from '../../../v0/destinations/iterable/util'; -import { CommonResponse } from './type'; +import { PrincipalStrategy } from './principal'; +import { DestinationResponse, ResponseParams, Response } from '../types'; +import { checkIfEventIsAbortableAndExtractErrorMessage } from '../../../../v0/destinations/iterable/util'; -interface ResponseParams { - destinationResponse: { status: number; response: CommonResponse }; - rudderJobMetadata: any[]; - destinationRequest: { - body: { - JSON: { - events?: any[]; - users?: any[]; - }; - }; - }; -} - -class TrackIdentifyStrategy extends ResponseStrategy { +class TrackIdentifyStrategy extends PrincipalStrategy { handleSuccess(responseParams: ResponseParams): { status: number; message: string; - destinationResponse: { status: number; response: CommonResponse }; - response: Array<{ statusCode: number; metadata: any; error: string }>; + destinationResponse: DestinationResponse; + response: Response[]; } { const { destinationResponse, rudderJobMetadata, destinationRequest } = responseParams; const { status } = destinationResponse; - const responseWithIndividualEvents: Array<{ - statusCode: number; - metadata: any; - error: string; - }> = []; + const responseWithIndividualEvents: Response[] = []; - const { events, users } = destinationRequest.body.JSON; + const { events, users } = destinationRequest?.body.JSON || {}; const finalData = events || users; if (finalData) { diff --git a/src/v1/destinations/iterable/type.ts b/src/v1/destinations/iterable/type.ts deleted file mode 100644 index e29e6236e6..0000000000 --- a/src/v1/destinations/iterable/type.ts +++ /dev/null @@ -1,26 +0,0 @@ -interface FailedUpdates { - invalidEmails?: string[]; - invalidUserIds?: string[]; - notFoundEmails?: string[]; - notFoundUserIds?: string[]; - invalidDataEmails?: string[]; - invalidDataUserIds?: string[]; - conflictEmails?: string[]; - conflictUserIds?: string[]; - forgottenEmails?: string[]; - forgottenUserIds?: string[]; -} - -export interface CommonResponse { - msg?: string; - code?: string; - params?: Record; - successCount?: number; - failCount?: number; - invalidEmails?: string[]; - invalidUserIds?: string[]; - filteredOutFields?: string[]; - createdFields?: string[]; - disallowedEventNames?: string[]; - failedUpdates?: FailedUpdates; -} diff --git a/src/v1/destinations/iterable/types.ts b/src/v1/destinations/iterable/types.ts new file mode 100644 index 0000000000..65f5a3416f --- /dev/null +++ b/src/v1/destinations/iterable/types.ts @@ -0,0 +1,57 @@ +type FailedUpdates = { + invalidEmails?: string[]; + invalidUserIds?: string[]; + notFoundEmails?: string[]; + notFoundUserIds?: string[]; + invalidDataEmails?: string[]; + invalidDataUserIds?: string[]; + conflictEmails?: string[]; + conflictUserIds?: string[]; + forgottenEmails?: string[]; + forgottenUserIds?: string[]; +}; + +export type GeneralApiResponse = { + msg?: string; + code?: string; + params?: Record; + successCount?: number; + failCount?: number; + invalidEmails?: string[]; + invalidUserIds?: string[]; + filteredOutFields?: string[]; + createdFields?: string[]; + disallowedEventNames?: string[]; + failedUpdates?: FailedUpdates; +}; + +export type DestinationResponse = { + status: number; + response: GeneralApiResponse; +}; + +export type ResponseParams = { + destinationResponse: DestinationResponse; + rudderJobMetadata: any[]; + destinationRequest?: { + body: { + JSON: { + events?: any[]; + users?: any[]; + }; + }; + }; +}; + +export type Response = { + statusCode: number; + metadata: any; + error: string; +}; + +export type SuccessResponse = { + status: number; + message: string; + destinationResponse: DestinationResponse; + response: Response[]; +}; diff --git a/test/integrations/destinations/iterable/dataDelivery/business.ts b/test/integrations/destinations/iterable/dataDelivery/business.ts index ec40c29b40..df75c8fd1d 100644 --- a/test/integrations/destinations/iterable/dataDelivery/business.ts +++ b/test/integrations/destinations/iterable/dataDelivery/business.ts @@ -499,7 +499,7 @@ export const testScenariosForV1API: ProxyV1TestData[] = [ status: 400, statTags, message: - 'ITERABLE: Error transformer proxy during ITERABLE response transformation. null', + 'ITERABLE: Error transformer proxy during ITERABLE response transformation. "Invalid currentEmail sayan"', response: [ { statusCode: 400, @@ -596,7 +596,7 @@ export const testScenariosForV1API: ProxyV1TestData[] = [ status: 400, statTags, message: - 'ITERABLE: Error transformer proxy during ITERABLE response transformation. null', + 'ITERABLE: Error transformer proxy during ITERABLE response transformation. "Invalid email: sayan"', response: [ { statusCode: 400,