UNPKG

@causalfoundry/js-sdk

Version:

Causal Foundry WEB SDK (JS/TS)

103 lines (91 loc) 4.08 kB
import {injectEvent} from '../../core/injector' import {BreakProperties, CallCenterEventType, ContactProperties, OpsScorecardProperties,} from './typings' import CfExceptionHandler from "../../core/CfExceptionHandler"; import {isISOLocal} from "../../utils"; const logIngestEvent = async (eventType: CallCenterEventType, properties: BreakProperties | ContactProperties | OpsScorecardProperties, sendNow = false, eventTime?: string) => { let checkProps = true; let property: string = null; switch (eventType) { case CallCenterEventType.Break: checkProps = validateBreakEvent(properties as BreakProperties); if (checkProps) { property = (properties as BreakProperties).action } break; case CallCenterEventType.Contact: checkProps = validateContactEvent(properties as ContactProperties); if (checkProps) { property = (properties as ContactProperties).action } break; case CallCenterEventType.OpsScorecard: checkProps = validateOpsScorecardEvent(properties as OpsScorecardProperties); if (checkProps) { property = (properties as OpsScorecardProperties).performance_group } break; default: return; // Exit early if eventType is unrecognized } if (!checkProps) { return; // Exit if the properties validation failed } injectEvent( eventType, property, properties, sendNow, '', eventTime, ) } const validateBreakEvent = (properties: BreakProperties): boolean => { if (properties.type == undefined) { new CfExceptionHandler().throwAndLogError(CallCenterEventType.Break, 'invalid break type') return false; } else if (properties.action == undefined) { new CfExceptionHandler().throwAndLogError(CallCenterEventType.Break, 'invalid break action') return false; } else if (properties.total_time < 0) { new CfExceptionHandler().throwAndLogError(CallCenterEventType.Break, 'invalid break total_time') return false; } else if (properties.consumed_time < 0) { new CfExceptionHandler().throwAndLogError(CallCenterEventType.Break, 'invalid break consumed_time') return false; } return true } const validateContactEvent = (properties: ContactProperties): boolean => { if (properties.action == undefined) { new CfExceptionHandler().throwAndLogError(CallCenterEventType.Contact, 'invalid contact action') return false; } else if (!properties.contact_id) { new CfExceptionHandler().throwAndLogError(CallCenterEventType.Contact, 'invalid contact details id') return false; } else if (!properties.contact_channel) { new CfExceptionHandler().throwAndLogError(CallCenterEventType.Contact, 'invalid contact details channel') return false; } else if (!properties.contact_type) { new CfExceptionHandler().throwAndLogError(CallCenterEventType.Contact, 'invalid contact details type') return false; } else if (properties.contact_start_time && !isISOLocal(properties.contact_start_time)) { new CfExceptionHandler().throwAndLogError(CallCenterEventType.Contact, 'invalid contact start_time: should be in ISO8601 format e.g. 2025-01-01T12:00:27.87+02:00') return false; } return true } const validateOpsScorecardEvent = (properties: OpsScorecardProperties): boolean => { if (properties.audit_count == undefined) { new CfExceptionHandler().throwAndLogError(CallCenterEventType.OpsScorecard, 'invalid quality_traits audit_count') return false; } else if (!properties.performance_group) { new CfExceptionHandler().throwAndLogError(CallCenterEventType.OpsScorecard, 'invalid quality_traits performance_group') return false; } return true } export default { logIngestEvent }