parent
d6509a018a
commit
3584256e6d
2 changed files with 277 additions and 1 deletions
@ -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 |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue