Created
August 5, 2024 15:37
-
-
Save alphanetEX/99c2325e3bd6257823dfab74bc3bfb9e to your computer and use it in GitHub Desktop.
validation de inscripciones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validate (ardBase, ardUser) { | |
let counter=0; | |
let valCount=0; | |
let lengs = { | |
lengBase :ardBase.length, | |
lengUser : ardUser.length | |
}; | |
while(lengs.lengBase > counter){ | |
// console.log(`array user : ${ardUser[counter]} array Base: ${ardBase[counter]}`); | |
if(ardUser[counter] === ardBase[counter]){ | |
valCount ++; | |
}; | |
counter ++; | |
}; | |
return lengs.lengBase === valCount ? true : false; | |
} | |
function ValidateDependencies(objdep){ | |
let counter =0; | |
let zeros=0, notzeros=0; | |
while (counter < objdep.length) { | |
if(objdep[counter].depends_of !== 'ZERO'){ | |
notzeros++; | |
} else{ | |
zeros++; | |
} | |
counter ++; | |
} | |
//if its present lent of zeros or notzeros return true for validate dependencies | |
if(zeros > 0 || notzeros >0){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
async function valInscription(req, res, next){ | |
const payloadjwt = ValUserBodyJwt(req); | |
const userInscriptionCourse = new inscriptionService(`inscriptions_vow`) | |
const cnsAvailableCursadas = new inscriptionService(`availablecursadas_front_vow`); | |
const courseDepedencies = new insValService(`crsdependings_vow`); | |
const userCompletedCourses = new insValService(`completedcourses_dash_vow`); | |
let crsBasesArrd, crsusrCompleted; | |
//how to resolve this path using try/catch | |
const [ veryfycursadaDisp ] = await cnsAvailableCursadas.findByCod(req.body.codename); | |
//added cursada_direct in the middleware for the optimization progress | |
if (veryfycursadaDisp === undefined){ | |
next(boom.notFound("this inscription wasn't available")); | |
}else { | |
req.body.cursada_id = veryfycursadaDisp.cursada_id; | |
const [ InscriptionCourseBycodename ] = await userInscriptionCourse.findByCodEmail(veryfycursadaDisp.codename, payloadjwt.email); | |
if (InscriptionCourseBycodename !== undefined) { | |
next(boom.conflict("this user was already registered on this course")); | |
} | |
const Coursedependencie = await courseDepedencies.courseDependenciesBycourseId(veryfycursadaDisp.course_id); | |
//[record a video of validation] | |
//validate in both cases if present zeros crsdependings_vow or dependencies | |
let resultVal = ValidateDependencies(courseDepedencies); | |
if (!resultVal){ | |
//obtain only coursebase_id and transform to arraid | |
crsBasesArrd = Coursedependencie.map((course)=> course.coursebase_id); | |
//remove nulls this part was added validate function | |
let arraywithoutnulls = crsBasesArrd.filter((filnull) => { | |
return filnull != null; | |
}); | |
//ordenate of minor to mayor | |
arraywithoutnulls.sort((a,b) => a-b); | |
const completedCourses = await userCompletedCourses.completeCrsDsVowByUserId(payloadjwt.email); | |
//same ordenarte to minor to mayor | |
crsusrCompleted = completedCourses.map((course) => course.course_id); | |
crsusrCompleted.sort((a,b) => a-b); | |
//validate the completed courses with certification user with de dependencies of course | |
if(!validate(arraywithoutnulls, crsusrCompleted)){ | |
next(boom.conflict("the user not complish de course dependencies")); | |
}else{ | |
next(); | |
} | |
}else{ | |
//just send a boom conflict if in the case retrun false ? | |
next() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment