Advertisement
shopnilSS

Untitled

Apr 29th, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. const bcrypt = require("bcrypt")
  2. const studentValidator = require("../../../validation/student") //get the student validator
  3. const Student = require("../../model/user/student")
  4. const User = require("../../model/user/user")
  5. const Class = require("../../model/academic/class")
  6.  
  7. //creat a student
  8. const newStudentCreatController = async (req, res) => {
  9. try{
  10. const {error} = studentValidator.validate(req.body)
  11. if(error){
  12. console.log(error);
  13. res.status(406).json({
  14. message: "joi validation error",
  15. error
  16. })
  17. }else{
  18. const {password, userId, personalInfo, userType} = req.body //get the expected data from req body
  19.  
  20. //start the procedure of creat a unique user id
  21. const splitedDateOfBirth = personalInfo.dateOfBirth.split("-");
  22. const birthYear = splitedDateOfBirth[0]
  23. const birthDate = splitedDateOfBirth[splitedDateOfBirth.length - 1]
  24.  
  25. //format of user id is (BirthDate - userID - BirthYear)
  26. const generateUserId = `${birthDate}${userId}${birthYear}` //get the new user id
  27.  
  28. const hashedPassword = await bcrypt.hash(password, 10)//hashed the password
  29. // const hashedPasswordRetype = bcrypt.hash(retypePassword, 10)//hashed the retype one
  30. const profilePic = req.file.filename
  31. //creat the student
  32. const student = new Student({
  33. ...req.body,
  34. userId: generateUserId,
  35. password: hashedPassword,
  36. "personalInfo.profilePic": profilePic
  37. })//creat a new student
  38. // console.log(generateUserId);
  39.  
  40. //save the new student
  41. const saveData = await student.save() //save new student
  42. if(saveData){
  43. res.status(201).json({
  44. message: "student has created successfully",
  45. saveData
  46. })
  47. // await Class.updateOne(
  48. // {
  49. // className: saveData.academicInfo.class
  50. // }, //query
  51. // {
  52. // $inc: {
  53. // studentNumber: 1
  54. // }
  55. // }, //update part
  56. // {} //option
  57. // )
  58. }
  59.  
  60. //store the data in recovery model
  61. const {email} = personalInfo //get the email from body
  62. const user = new User({
  63. userType,
  64. email
  65. }) //creat a new user in User model
  66. await user.save() //save that data
  67. }
  68. }
  69. catch(err){
  70. console.log(err);
  71. res.status(400).json({
  72. err
  73. })
  74. }
  75. }
  76.  
  77.  
  78. //export part
  79. module.exports = {
  80. newStudentCreatController
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement