|
| 1 | +import { BaseStrategy } from './base'; |
| 2 | +import { GenericProxyHandlerInput, IterableBulkProxyInput } from '../types'; |
| 3 | +import { checkIfEventIsAbortableAndExtractErrorMessage } from '../utils'; |
| 4 | +import { DeliveryJobState, DeliveryV1Response } from '../../../../types'; |
| 5 | +import { TransformerProxyError } from '../../../../v0/util/errorTypes'; |
| 6 | +import { getDynamicErrorType } from '../../../../adapters/utils/networkUtils'; |
| 7 | +import { TAG_NAMES } from '../../../../v0/util/tags'; |
| 8 | + |
| 9 | +class TrackIdentifyStrategy extends BaseStrategy { |
| 10 | + handleSuccess(responseParams: IterableBulkProxyInput): DeliveryV1Response { |
| 11 | + const { destinationResponse, rudderJobMetadata, destinationRequest } = responseParams; |
| 12 | + const { status } = destinationResponse; |
| 13 | + const responseWithIndividualEvents: DeliveryJobState[] = []; |
| 14 | + |
| 15 | + const { events, users } = destinationRequest?.body.JSON || {}; |
| 16 | + const finalData = events || users; |
| 17 | + |
| 18 | + if (finalData) { |
| 19 | + finalData.forEach((event, idx) => { |
| 20 | + const parsedOutput = { |
| 21 | + statusCode: 200, |
| 22 | + metadata: rudderJobMetadata[idx], |
| 23 | + error: 'success', |
| 24 | + }; |
| 25 | + |
| 26 | + const { isAbortable, errorMsg } = checkIfEventIsAbortableAndExtractErrorMessage( |
| 27 | + event, |
| 28 | + destinationResponse, |
| 29 | + ); |
| 30 | + if (isAbortable) { |
| 31 | + parsedOutput.statusCode = 400; |
| 32 | + parsedOutput.error = errorMsg; |
| 33 | + } |
| 34 | + responseWithIndividualEvents.push(parsedOutput); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + return { |
| 39 | + status, |
| 40 | + message: '[ITERABLE Response Handler] - Request Processed Successfully', |
| 41 | + destinationResponse, |
| 42 | + response: responseWithIndividualEvents, |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + handleError(responseParams: GenericProxyHandlerInput): void { |
| 47 | + const { destinationResponse, rudderJobMetadata } = responseParams; |
| 48 | + const { response, status } = destinationResponse; |
| 49 | + const responseMessage = response.params || response.msg || response.message; |
| 50 | + const errorMessage = JSON.stringify(responseMessage) || 'unknown error format'; |
| 51 | + |
| 52 | + const responseWithIndividualEvents = rudderJobMetadata.map((metadata) => ({ |
| 53 | + statusCode: status, |
| 54 | + metadata, |
| 55 | + error: errorMessage, |
| 56 | + })); |
| 57 | + |
| 58 | + throw new TransformerProxyError( |
| 59 | + `ITERABLE: Error transformer proxy during ITERABLE response transformation. ${errorMessage}`, |
| 60 | + status, |
| 61 | + { [TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status) }, |
| 62 | + destinationResponse, |
| 63 | + '', |
| 64 | + responseWithIndividualEvents, |
| 65 | + ); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +export { TrackIdentifyStrategy }; |
0 commit comments