|
| 1 | +import md5 from 'md5'; |
| 2 | +import { hashToSha256, InstrumentationError, formatZodError } from '@rudderstack/integrations-lib'; |
| 3 | +import { groupBy } from 'lodash'; |
| 4 | +import type { TiktokAudienceRecordRequest } from './recordTypes'; |
| 5 | +import { TiktokAudienceRecordRouterRequestSchema } from './recordTypes'; |
| 6 | +import { SHA256_TRAITS, ENDPOINT, ENDPOINT_PATH, ACTION_RECORD_MAP } from './config'; |
| 7 | +import { defaultRequestConfig, getSuccessRespEvents, handleRtTfSingleEventError } from '../../util'; |
| 8 | +import { RouterTransformationResponse } from '../../../types'; |
| 9 | + |
| 10 | +type Identifier = { |
| 11 | + id: string; |
| 12 | + audience_ids: string[]; |
| 13 | +}; |
| 14 | + |
| 15 | +type Payload = { |
| 16 | + event: TiktokAudienceRecordRequest; |
| 17 | + batchIdentifiers: Identifier[]; |
| 18 | + idSchema: string[]; |
| 19 | + advertiserId: string; |
| 20 | + action: string; |
| 21 | +}; |
| 22 | + |
| 23 | +function prepareIdentifiersPayload(event: TiktokAudienceRecordRequest): Payload { |
| 24 | + const { message, connection, destination } = event; |
| 25 | + const { isHashRequired, audienceId } = connection.config.destination; |
| 26 | + const { advertiserId } = destination.Config; |
| 27 | + const { action, identifiers } = message; |
| 28 | + |
| 29 | + const hashIdentifier = (fieldName: string, value: string) => { |
| 30 | + if (isHashRequired) { |
| 31 | + if (SHA256_TRAITS.includes(fieldName)) { |
| 32 | + return hashToSha256(value); |
| 33 | + } |
| 34 | + return md5(value); |
| 35 | + } |
| 36 | + return value; |
| 37 | + }; |
| 38 | + |
| 39 | + const identifiersList: Identifier[] = []; |
| 40 | + for (const [fieldName, value] of Object.entries(identifiers)) { |
| 41 | + if (value) { |
| 42 | + identifiersList.push({ |
| 43 | + id: hashIdentifier(fieldName, value), |
| 44 | + audience_ids: [audienceId], |
| 45 | + }); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + const payload: Payload = { |
| 50 | + event, |
| 51 | + batchIdentifiers: identifiersList, |
| 52 | + idSchema: Object.keys(identifiers).sort(), |
| 53 | + advertiserId, |
| 54 | + action: ACTION_RECORD_MAP[action], |
| 55 | + }; |
| 56 | + return payload; |
| 57 | +} |
| 58 | + |
| 59 | +function buildResponseForProcessTransformation( |
| 60 | + payload: Record<string, any>, |
| 61 | + event: TiktokAudienceRecordRequest, |
| 62 | +) { |
| 63 | + const accessToken = event.metadata?.secret?.accessToken; |
| 64 | + const userId = event.message?.userId; |
| 65 | + |
| 66 | + const response = defaultRequestConfig(); |
| 67 | + response.body.JSON = payload; |
| 68 | + response.userId = userId; |
| 69 | + response.endpoint = ENDPOINT; |
| 70 | + response.endpointPath = ENDPOINT_PATH; |
| 71 | + response.headers = { |
| 72 | + 'Access-Token': accessToken, |
| 73 | + 'Content-Type': 'application/json', |
| 74 | + }; |
| 75 | + return response; |
| 76 | +} |
| 77 | + |
| 78 | +function validateAudienceRecordEvent(event: unknown) { |
| 79 | + const result = TiktokAudienceRecordRouterRequestSchema.safeParse(event); |
| 80 | + if (!result.success) { |
| 81 | + throw new InstrumentationError(formatZodError(result.error)); |
| 82 | + } |
| 83 | + return result.data; |
| 84 | +} |
| 85 | + |
| 86 | +function processTiktokAudienceRecords(events: unknown[]): { |
| 87 | + recordFailedResponses: RouterTransformationResponse[]; |
| 88 | + recordSuccessfulResponses: RouterTransformationResponse[]; |
| 89 | +} { |
| 90 | + const recordSuccessfulResponses: RouterTransformationResponse[] = []; |
| 91 | + const recordFailedResponses: RouterTransformationResponse[] = []; |
| 92 | + |
| 93 | + const payloads: Payload[] = []; |
| 94 | + |
| 95 | + for (const event of events) { |
| 96 | + try { |
| 97 | + const tiktokEvent = validateAudienceRecordEvent(event); |
| 98 | + const payload = prepareIdentifiersPayload(tiktokEvent); |
| 99 | + payloads.push(payload); |
| 100 | + } catch (error) { |
| 101 | + recordFailedResponses.push(handleRtTfSingleEventError(event, error, {})); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + const groupedPayloads = groupBy( |
| 106 | + payloads, |
| 107 | + (payload) => `${payload.advertiserId}-${payload.action}-${payload.idSchema.join(',')}`, |
| 108 | + ); |
| 109 | + |
| 110 | + for (const [key, payloadArray] of Object.entries(groupedPayloads)) { |
| 111 | + try { |
| 112 | + const [advertiserId, action, idSchema] = key.split('-'); |
| 113 | + const idSchemaList = idSchema.split(','); |
| 114 | + const batchData = payloadArray.map((payload) => payload.batchIdentifiers); |
| 115 | + const metadataList = payloadArray.map((payload) => payload.event.metadata); |
| 116 | + |
| 117 | + const payload: Record<string, any> = { |
| 118 | + batch_data: batchData, |
| 119 | + id_schema: idSchemaList, |
| 120 | + advertiser_ids: [advertiserId], |
| 121 | + action, |
| 122 | + }; |
| 123 | + const response = buildResponseForProcessTransformation(payload, payloadArray[0].event); |
| 124 | + recordSuccessfulResponses.push( |
| 125 | + getSuccessRespEvents(response, metadataList, payloadArray[0].event.destination, true), |
| 126 | + ); |
| 127 | + } catch (error) { |
| 128 | + recordFailedResponses.push( |
| 129 | + ...payloadArray.map((payload) => handleRtTfSingleEventError(payload.event, error, {})), |
| 130 | + ); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return { recordFailedResponses, recordSuccessfulResponses }; |
| 135 | +} |
| 136 | + |
| 137 | +export { processTiktokAudienceRecords }; |
0 commit comments