UNPKG

@causalfoundry/js-sdk

Version:

Causal Foundry WEB SDK (JS/TS)

153 lines (137 loc) 3.62 kB
import { ELearningTypes, ExamAction, ExamProperties, ModuleProperties, QuestionProperties, } from "./typings"; import { injectEvent } from "../../core/injector"; import CfExceptionHandler from "../../core/CfExceptionHandler"; const logIngestEvent = async ( eventType: ELearningTypes, properties: ModuleProperties | ExamProperties | QuestionProperties, sendNow = false, eventTime?: string ) => { let checkProps = true; switch (eventType) { case ELearningTypes.Module: checkProps = validateModuleEvent(properties as ModuleProperties); break; case ELearningTypes.Exam: checkProps = validateExamEvent(properties as ExamProperties); break; case ELearningTypes.Question: checkProps = validateQuestionEvent(properties as QuestionProperties); break; default: return; } if (!checkProps) { return; } injectEvent( eventType, properties.action, properties, sendNow, "", eventTime ); }; const validateModuleEvent = (properties: ModuleProperties): boolean => { if (properties.module_id == "") { new CfExceptionHandler().throwAndLogError( ELearningTypes.Module, "empty module id" ); return false; } else if (properties.action == undefined) { new CfExceptionHandler().throwAndLogError( ELearningTypes.Module, "invalid module action" ); return false; } else if (properties.progress == undefined) { new CfExceptionHandler().throwAndLogError( ELearningTypes.Module, "invalid module progress" ); return false; } return true; }; const validateExamEvent = (properties: ExamProperties): boolean => { if (properties.exam_id == "") { new CfExceptionHandler().throwAndLogError( ELearningTypes.Exam, "empty exam id" ); return false; } else if (properties.action == undefined) { new CfExceptionHandler().throwAndLogError( ELearningTypes.Exam, "invalid exam action" ); return false; } else if ( properties.action == ExamAction.Submit && properties.duration < 0 ) { new CfExceptionHandler().throwAndLogError( ELearningTypes.Exam, "exam duration value required for submit action" ); return false; } else if ( properties.action == ExamAction.Result && properties.score == undefined ) { new CfExceptionHandler().throwAndLogError( ELearningTypes.Exam, "exam score required for result action" ); return false; } else if ( properties.action == ExamAction.Result && properties.is_passed == undefined ) { new CfExceptionHandler().throwAndLogError( ELearningTypes.Exam, "exam is_passed required for result action" ); return false; } return true; }; const validateQuestionEvent = (properties: QuestionProperties) => { if (properties.question_id == "") { new CfExceptionHandler().throwAndLogError( ELearningTypes.Question, "empty question id" ); return false; } else if (properties.action == undefined) { new CfExceptionHandler().throwAndLogError( ELearningTypes.Question, "invalid question action" ); return false; } else if (properties.answer_id == "") { new CfExceptionHandler().throwAndLogError( ELearningTypes.Question, "empty question answer id" ); return false; } else if (properties.exam_id == "") { new CfExceptionHandler().throwAndLogError( ELearningTypes.Question, "empty question exam id" ); return false; } return true; }; export default { logIngestEvent, };