From 3584256e6da1de2244c153f116bc661a1008a32c Mon Sep 17 00:00:00 2001 From: MohammadHoseinPaymard Date: Tue, 27 May 2025 12:19:45 +0330 Subject: [PATCH] update index.js and question.js --- index.js | 40 ++++++++- question.js | 238 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 277 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 694db9e..284256b 100644 --- a/index.js +++ b/index.js @@ -159,8 +159,46 @@ require(path.join(process.cwd(), "..", "server", "dist", "database.js")); // console.log("data",data); } + + let formQuestions = await bpmsDB.bpms_formQuestion.findAll({ + where: { + processId:process.id, + }, + raw: true + }); + + const formQuestionMap = new Map(formQuestions.map((fQ) => ([+fQ.questionId, fQ]))); + + let questions = await bpmsDB.bpms_question.findAll({ + where: { + id: [...formQuestions.map(e => +e.questionId), ...checkListQuestions.map(e => +e.questionId)] + }, + raw: true, + }); + + questions = questions.map((q) => { + let fq = formQuestionMap.get(+q.id); + // let fch = fq || checkListMap.get(+q.id); + let fch = fq || formQuestionMap.get(+q.id); + q.condition = fch.condition || q.condition; + q.required = returnBooleanOfArguments([fch.isRequired, q.required]); + if (fch.requiredCondition) + q.requiredCondition = fch.requiredCondition; + else if (fch.isRequired !== null) { + q.required = fch.isRequired; + q.requiredCondition = null; + } + q.ord = fq ? fq.ord || q.ord : 1e6 + (fch.ord || q.ord); + return q; + }).sort((a, b) => a.ord - b.ord) + + + + console.log("questions", questions) + + return; + } - return; } return; diff --git a/question.js b/question.js index e69de29..4c98311 100644 --- a/question.js +++ b/question.js @@ -0,0 +1,238 @@ + +const checkCondition = ({ data, condition}, conditionArray = new Set()) => { + let ands = true; + for (const key in condition) { + const conditionValue = condition[key]; + if (key == 'or') { + let ors = false; + for (const cond of conditionValue) { + // const orConditionChecking = checkCondition({ data, condition: cond }, conditionArray); + // ors = ors || orConditionChecking.ok; + ors = ors || checkCondition({data,condition: cond}); + // conditionArray = new Set([...conditionArray, ...orConditionChecking.conditionArray]) + if (ors) break; + } + ands = ands && ors; + } else { + if (data[key] == 'true') data[key] = true; + if (data[key] == 'false') data[key] = false; + switch (key) { + case [Op.gt]: + ands = ands && data[key] > conditionValue; + // if (!conditionArray.has(key)) conditionArray.add(key); + break; + default: + ands = ands && data[key] == conditionValue; + // if (!conditionArray.has(key)) conditionArray.add(key); + } + } + if (!ands) { + // return { ok: false, conditionArray }; + return false; + } + } + + // return { ok: true, conditionArray }; + return true; +}; + +function checkingOptions({ question, answer }) { + let options = convertStringToJson(question?.options) || [], + inputType = +question?.inputType || -1; + + // console.log("options", options) + if (!Array.isArray(options)) options = [options]; + const OptionsValidValue = options.map((option) => { + return parseValue(option?.value); + }) + const questionAnswer = answer[question.id]; + if (questionAnswer === null || questionAnswer === undefined) return { + ok: false + } + + let min = question?.min, + max = question?.max, + length = question?.length, + validation = question?.validation; + + switch (inputType) { + case 1: //TEXT + case 3: + if (typeof questionAnswer !== "string") { + return { + statusCode:400, + message:ErrorMessages.InvalidData + } + } + if(length && isNumber(length) && questionAnswer.length>+length){ + return { + statusCode:400, + message:ErrorMessages.InvalidTextLength(question.id,length) + } + } + if(validation && isValidRegex(validation)){ + let validationRegex = new RegExp(validation); + if(!validationRegex.test(questionAnswer)) return { + statusCode:400, + message:ErrorMessages.InvalidTextPattern + } + } + + break; + case 2: //NUMBER + if (!isNumber(questionAnswer)) { + return { + statusCode:400, + message:ErrorMessages.InvalidNumber(question.id) + } + } + if(min && isNumber(min) && +min>+questionAnswer) return { + statusCode:400, + message:ErrorMessages.InvalidMinNumber(question.id,min) + } + if(max && isNumber(max) && +max<+questionAnswer) return { + statusCode:400, + message:ErrorMessages.InvalidMaxNumber(question.id,max) + } + break; + case 4: // tak entekhab + case 6: + if (!OptionsValidValue.includes(questionAnswer)) { + return { + statusCode:400, + message:ErrorMessages.InvalidSelectedOption(question.id) + } + } + break; + case 5: // chand entekhab + case 7: + if (Array.isArray(questionAnswer)) { + for (let ans of questionAnswer) { + if (!OptionsValidValue.includes(ans)) return { statusCode: 400, message:ErrorMessages.InvalidAdditionalData(question.id) }; + } + } else if (typeof questionAnswer === "string") { + for (let ans of questionAnswer.split(",")) { + if (!OptionsValidValue.includes(ans)) return { statusCode: 400, message:ErrorMessages.InvalidAdditionalData(question.id) }; + } + } else { + return { statusCode: 400, message:ErrorMessages.InvalidData(question.id) }; + } + break; + case 8: + if (!isValidDateStrict(questionAnswer)) return { statusCode:400, message:ErrorMessages.InvalidDate(question.id)}; + const questionDate = new Date(questionAnswer); + if(min){ + if(isValidDateStrict(min)){ + if(new Date(min) > questionDate) return { + statusCode: 400, + message: ErrorMessages.InvalidMinDate(question.id) + } + }else if(min==="now()"){ + if(new Date() > questionDate) return { + statusCode: 400, + message: ErrorMessages.InvalidMinDate(question.id) + } + }else if(min.includes("day")){ + let days = min.match(/day(s)?\(([0-9]{1,2})\)/)[2]; + if( + !isNaN(+days) + // && + // Date.now() < questionDate + && + Math.abs(Date.now() - questionDate.getTime()) > convertDaysToSeconds(days) + ){ + return { + statusCode: 400, + message:ErrorMessages.InvalidDayMinDate(question.id,days) + } + } + } + } + if(max){ + if(isValidDateStrict(max)){ + if(new Date(max) < questionDate) return { + statusCode: 400, + message: ErrorMessages.InvalidMaxDate(question.id) + } + }else if(max==="now()"){ + if(new Date() < questionDate) return { + statusCode: 400, + message: ErrorMessages.InvalidMaxDate(question.id) + } + }else if(max.includes("day")){ + let days = max.match(/day(s)?\(([0-9]{1,2})\)/)[2]; + if( + !isNaN(+days) + // && + // Date.now() < questionDate + && + Math.abs(Date.now() - questionDate.getTime()) < convertDaysToSeconds(days) + ){ + return { + statusCode: 400, + message:ErrorMessages.InvalidDayMaxDate(question.id,days) + } + } + } + } + break; + case 10: + if (!isBoolean(questionAnswer)) return { statusCode:400, message: ErrorMessages.InvalidBooleanData(question.id) } + break; + default: + return { + statusCode:400, + message:ErrorMessages.InvalidInputType(question.id) + } + } + return true; +} +async function checkingConditionTypes({ fQ, question, mojazData = [], answer }) { + let check = false, checkOption = false, inputType = 10; + let condition = {}, isRequired = null; + let requiredCondition = {}, validation = null; + + requiredCondition = question?.requiredCondition || {}; + validation = question?.validation || null; + + condition = convertStringToJson(fQ?.condition) || convertStringToJson(question?.condition) || null; + isRequired = returnBooleanOfArguments([fQ?.isRequired, question?.required]); + + inputType = question?.inputType || 10; + + console.log("question id:", question.id) + console.log("requiredCondition", requiredCondition); + // console.log("validation",validation); + console.log("condition", condition); + console.log("isRequired", isRequired); + // console.log("options",options); + console.log("inputType", inputType); + + //1.check requiredConditions + //2.check Conditions and check they required or not + //3.check Options and check they required or not + //4.check validation + //5.remove extra data + + //1.check requiredConditions + check = checkCondition({ data: answer, condition: requiredCondition }) + if (check.ok === false) throw `${defects[question.id]}`; + mojazData = [...new Set([...mojazData, ...check.conditionArray])]; + console.log("DONE SOLVE requiredConditions") + + //2.check Conditions and check they required or not + check = checkCondition({ data: answer, condition: condition }) + checkOption = checkingOptions({ question, answer }); + if ((check.ok === false || checkOption.ok === false) && isRequired === true) throw `${defects[question.id]}`; + if (checkOption.ok === true) mojazData.push(`${question.id}`); + mojazData = [...new Set([...mojazData, ...check.conditionArray])]; + console.log("DONE SOLVE condition") + + //3.check Validation (ro bayad beporsam az aghaye sadeghi) + + console.log("mojazDataAfter-requiredCondition-condition", mojazData); + + return { + mojazData + } +} \ No newline at end of file