diff --git a/src/v0/destinations/customerio_audience/transform.ts b/src/v0/destinations/customerio_audience/transform.ts index e2411260f08..27a997cb11d 100644 --- a/src/v0/destinations/customerio_audience/transform.ts +++ b/src/v0/destinations/customerio_audience/transform.ts @@ -1,6 +1,6 @@ import { ConfigurationError, isDefinedAndNotNullAndNotEmpty } from '@rudderstack/integrations-lib'; import { SegmentAction } from './config'; -import { EventStructure, RespList } from './type'; +import { CustomerIORouterRequestType, RespList } from './type'; const { InstrumentationError } = require('@rudderstack/integrations-lib'); const { batchResponseBuilder, getEventAction } = require('./utils'); @@ -11,7 +11,7 @@ interface ProcessedEvent extends RespList { eventAction: keyof typeof SegmentAction; } -const createEventChunk = (event: EventStructure): ProcessedEvent => { +const createEventChunk = (event: CustomerIORouterRequestType): ProcessedEvent => { const eventAction = getEventAction(event); const { identifiers } = event?.message || {}; const id: string | number = Object.values(identifiers)[0]; @@ -23,7 +23,7 @@ const createEventChunk = (event: EventStructure): ProcessedEvent => { }; }; -const validateEvent = (event: EventStructure): boolean => { +const validateEvent = (event: CustomerIORouterRequestType): boolean => { const eventType = getEventType(event?.message); if (eventType !== EventType.RECORD) { throw new InstrumentationError(`message type ${eventType} is not supported`); @@ -52,7 +52,7 @@ const validateEvent = (event: EventStructure): boolean => { return true; }; -const processRouterDest = async (inputs: any[], reqMetadata: any) => { +const processRouterDest = async (inputs: CustomerIORouterRequestType[], reqMetadata: any) => { if (!inputs?.length) return []; const { destination, connection } = inputs[0]; diff --git a/src/v0/destinations/customerio_audience/type.ts b/src/v0/destinations/customerio_audience/type.ts index 5a597474c7a..1c5aee81b3f 100644 --- a/src/v0/destinations/customerio_audience/type.ts +++ b/src/v0/destinations/customerio_audience/type.ts @@ -1,38 +1,11 @@ +import { Connection, Destination, Metadata } from '../../../types'; +import { FixMe } from '../../../util/types'; + export type RespList = { payload: { ids: (string | number)[]; }; - metadata: Record; -}; - -export type EventStructure = { - message: { - action: string; - identifiers: Record; - }; - metadata: Record; - destination: DestinationStructure; - connection: ConnectionStructure; -}; - -export type ConnectionStructure = { - config: { - destination: { - audienceId: string | number; - identifierMappings: { - from: string; - to: string; - }[]; - }; - }; -}; - -export type DestinationStructure = { - Config: { - apiKey: string; - appApiKey: string; - siteId: string; - }; + metadata: CustomerIOMetadataType; }; export type SegmentationPayloadType = { @@ -47,3 +20,62 @@ export type SegmentationHeadersType = { 'Content-Type': string; Authorization: string; }; + +type CIOConnectionConfigType = { + destination: { + audienceId: string | number; + identifierMappings: { + from: string; + to: string; + }[]; + [key: string]: any; + }; + [key: string]: any; +}; + +type CIODestinationConfigType = { + apiKey: string; + appApiKey: string; + siteId: string; + [key: string]: any; +}; + +type GenericRouterRequestData< + CIOMessage = object, + CIODestination = Destination, + CIOConnection = Connection, +> = { + message: CIOMessage; + metadata: Metadata; + destination: CIODestination; + connection: CIOConnection; + [key: string]: any; +}; + +type CIODestination = { + Config: CIODestinationConfig; + [key: string]: any; +}; + +type CIOConnection> = { + config: CIOConnectionConfig; + [key: string]: any; +}; + +export type CustomerIOMessageType = { + action: string; + identifiers: Record; + [key: string]: any; +}; + +export type CustomerIOMetadataType = Metadata; + +export type CustomerIODestinationType = CIODestination; + +export type CustomerIOConnectionType = CIOConnection; + +export type CustomerIORouterRequestType = GenericRouterRequestData< + CustomerIOMessageType, + CustomerIODestinationType, + CustomerIOConnectionType +>; diff --git a/src/v0/destinations/customerio_audience/utils.ts b/src/v0/destinations/customerio_audience/utils.ts index 84a539145f0..17c3bcabe09 100644 --- a/src/v0/destinations/customerio_audience/utils.ts +++ b/src/v0/destinations/customerio_audience/utils.ts @@ -2,27 +2,28 @@ import { base64Convertor } from '@rudderstack/integrations-lib'; import { BatchUtils } from '@rudderstack/workflow-engine'; import { BASE_ENDPOINT, MAX_ITEMS } from './config'; import { - ConnectionStructure, - DestinationStructure, - EventStructure, + CustomerIOConnectionType, + CustomerIODestinationType, + CustomerIOMetadataType, + CustomerIORouterRequestType, RespList, SegmentationHeadersType, SegmentationParamType, SegmentationPayloadType, } from './type'; -const getIdType = (connection: ConnectionStructure): string => +const getIdType = (connection: CustomerIOConnectionType): string => connection.config.destination.identifierMappings[0]?.to || 'id'; -const getSegmentId = (connection: ConnectionStructure): string | number => +const getSegmentId = (connection: CustomerIOConnectionType): string | number => connection.config.destination.audienceId; -const getHeaders = (destination: DestinationStructure): SegmentationHeadersType => ({ +const getHeaders = (destination: CustomerIODestinationType): SegmentationHeadersType => ({ 'Content-Type': 'application/json', Authorization: `Basic ${base64Convertor(`${destination.Config.siteId}:${destination.Config.apiKey}`)}`, }); -const getParams = (connection: ConnectionStructure): SegmentationParamType => ({ +const getParams = (connection: CustomerIOConnectionType): SegmentationParamType => ({ id_type: getIdType(connection), }); @@ -30,7 +31,7 @@ const getMergedPayload = (batch: RespList[]): SegmentationPayloadType => ({ ids: batch.flatMap((input) => input.payload.ids), }); -const getMergedMetadata = (batch: RespList[]): Record[] => +const getMergedMetadata = (batch: RespList[]): CustomerIOMetadataType[] => batch.map((input) => input.metadata); const buildBatchedResponse = ( @@ -38,8 +39,8 @@ const buildBatchedResponse = ( endpoint: string, headers: SegmentationHeadersType, params: SegmentationParamType, - metadata: Record[], - destination: DestinationStructure, + metadata: CustomerIOMetadataType[], + destination: CustomerIODestinationType, ) => ({ batchedRequest: { body: { @@ -65,8 +66,8 @@ const buildBatchedResponse = ( const processBatch = ( respList: RespList[], endpoint: string, - destination: DestinationStructure, - connection: ConnectionStructure, + destination: CustomerIODestinationType, + connection: CustomerIOConnectionType, ): any[] => { if (!respList?.length) { return []; @@ -93,8 +94,8 @@ const processBatch = ( const batchResponseBuilder = ( insertOrUpdateRespList: RespList[], deleteRespList: RespList[], - destination: DestinationStructure, - connection: ConnectionStructure, + destination: CustomerIODestinationType, + connection: CustomerIOConnectionType, ): any[] => { const segmentId = getSegmentId(connection); @@ -115,7 +116,7 @@ const batchResponseBuilder = ( return [...insertResponses, ...deleteResponses]; }; -const getEventAction = (event: EventStructure): string => +const getEventAction = (event: CustomerIORouterRequestType): string => event?.message?.action?.toLowerCase() || ''; export { batchResponseBuilder, getEventAction };