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/responseStrategy.ts b/src/v1/destinations/iterable/strategies/base.ts similarity index 51% rename from src/v1/destinations/iterable/responseStrategy.ts rename to src/v1/destinations/iterable/strategies/base.ts index 9c2d1eeb40..348bfe7fc7 100644 --- a/src/v1/destinations/iterable/responseStrategy.ts +++ b/src/v1/destinations/iterable/strategies/base.ts @@ -1,14 +1,12 @@ -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'; + +// Base strategy is the base class for all strategies in Iterable destination +class BaseStrategy { + handleResponse(responseParams: { destinationResponse: DestinationResponse }): void { const { destinationResponse } = responseParams; const { status } = destinationResponse; @@ -22,14 +20,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 `response.message` 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 +36,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 +48,4 @@ class ResponseStrategy { } } -export { ResponseStrategy }; +export { BaseStrategy }; 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..2702875610 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 { BaseStrategy } from './base'; +import { DestinationResponse, SuccessResponse } from '../types'; -const { ResponseStrategy } = require('./responseStrategy'); - -class CommonStrategy extends ResponseStrategy { +class GenericStrategy extends BaseStrategy { 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/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..7c242f3ac7 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 { BaseStrategy } from './base'; +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 BaseStrategy { 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,