Skip to content

Instantly share code, notes, and snippets.

@alphanetEX
Created October 2, 2023 21:19
Show Gist options
  • Save alphanetEX/c9e276d0ae4ae68067062029bd4a59d1 to your computer and use it in GitHub Desktop.
Save alphanetEX/c9e276d0ae4ae68067062029bd4a59d1 to your computer and use it in GitHub Desktop.
Analisys
const { Router } = require('express');
const boom = require("@hapi/boom");
const userService = require('../../../services/userService');
const inscriptionService = require('../../../services/inscriptionsService');
const insValService = require('../../../services/insValidationService');
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;
}
module.exports ={
findByEmail: async (req, res, next) => {
try{
const consult = new inscriptionService(`inscriptions_vow`);
const payloadjwt = req.user;
const userInscriptions = await consult.findByAllinsEmail(payloadjwt.email);
if(userInscriptions.length === 0){
throw boom.notFound("the inscriptions wasn't found");
}
return res.status(200).json({
status: `200`,
userInscriptions
})
}catch(err){
next(err);
}
},
fvowFindbyCodename: async (req, res, next) => {
try{
const payloadjwt = req.user;
const cnsAvailableCursadas = new inscriptionService(`inscriptions_vow`);
const findInscriptionByCodename = await cnsAvailableCursadas.findByCodUuid(req.params.codename, payloadjwt.email);
if(findInscriptionByCodename.length === 0){
throw boom.notFound("the inscription wasn't found");
};
return res.status(200).json({
status: `200`,
findInscriptionByCodename
});
}catch(err){
next(err);
}
},
create: async (req, res, next) =>{
try{
const payloadjwt = req.user;
const userinfo = new userService(`users`);
const cnsinscriptions = new inscriptionService(`inscriptions`);
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`);
const [ userData ] = await userinfo.findByEmail(payloadjwt.email);
const veryfycursadaDisp = await cnsAvailableCursadas.findByCod(req.body.codename);
// const Coursedependencie = await courseDepedencies.courseDependenciesBycourseId(veryfycursadaDisp[0].course_id);
// const completedCourses = await userCompletedCourses.completeCrsDsVowByUserId(userData.user_id);
if (veryfycursadaDisp[0] === undefined){
throw boom.notFound("this inscription wasn't available");
}
//change method to findByCodEmail
const InscriptionCourseBycodename = await userInscriptionCourse.findByCodUuid(veryfycursadaDisp[0].codename, payloadjwt.email);
if (InscriptionCourseBycodename[0] !== undefined) {
throw boom.conflict("this user was already registered on this course");
}
const Coursedependencie = await courseDepedencies.courseDependenciesBycourseId(veryfycursadaDisp[0].course_id);
let crsBasesArrd, crsusrCompleted;
if ( Coursedependencie[0].depends_of !== "ZERO"){
crsBasesArrd = Coursedependencie.map((course)=> course.coursebase_id);
crsBasesArrd.sort((a,b) => a-b);
const completedCourses = await userCompletedCourses.completeCrsDsVowByUserId(userData.user_id);
crsusrCompleted = completedCourses.map((course) => course.course_id);
crsusrCompleted.sort((a,b) => a-b);
if(!validate(crsBasesArrd, crsusrCompleted)){
throw boom.conflict("the user not complish de course dependencies");
}
}
let obj = {
cursada_id: veryfycursadaDisp[0].cursada_id,
user_id : userData.user_id
};
const result = await cnsinscriptions.Create(obj);
return res.status(200).json({
status: '200',
result
})
}catch(err){
next(err);
}
},
//not implemented to next sprint
update: async (req, res, next) => {
try{
const query = new inscriptionService('inscriptions');
let obj = {
cursada_id: veryfycursadaDisp.cursada_id,
user_id : data.user_id
};
const reqdb = await query.update(objparams);
return res.status(200).json({
status: 200,
reqdb
})
}catch(err){
next(err)
}
},
delete: async (req, res, next) =>{
try {
const payloadjwt = req.user;
const cnsAvailableCursadas = new inscriptionService(`inscriptions_vow`);
const [ veryfycursadaDisp ] = await cnsAvailableCursadas.findByCodUuid(req.params.codename, payloadjwt.email);
if(veryfycursadaDisp === undefined){
throw boom.notFound("this inscription wasn't exist");
};
const query = new inscriptionService(`inscriptions`);
const reqdb = await query.Delete({ inscription_id: veryfycursadaDisp.inscription_id});
return res.status(200).json({
status: '200',
reqdb
})
}catch(err){
next(err);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment