@@ -31,6 +31,10 @@ import {
3131} from "#services/employee/empPersonal" ;
3232import { logger } from "#util" ;
3333
34+ import { isEntityIdValid } from "#middleware/entityIdValidation" ;
35+ import Department from "#models/department" ;
36+ import Course from "#models/course" ;
37+
3438async function addFaculty ( req , res ) {
3539 const {
3640 dateOfJoining,
@@ -52,48 +56,62 @@ async function addFaculty(req, res) {
5256 employeeCurrentDetails,
5357 employeeEducationDetails,
5458 } = req . body ;
59+
60+ const isDepartmentValid = await isEntityIdValid ( department , Department ) ;
61+ const isPreferredSubjectsValid = await isEntityIdValid ( preferredSubjects , Course ) ;
62+
5563 const session = await mongoose . startSession ( ) ;
5664 session . startTransaction ( ) ;
5765 try {
58- const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
59- const randomIndex = Math . floor ( Math . random ( ) * alphabet . length ) ;
60- const randomLetter = alphabet [ randomIndex ] ;
61- let randomNumber = Math . floor ( Math . random ( ) * 1000 ) . toString ( ) ;
62- if ( randomNumber . length === 2 ) {
63- randomNumber = `0${ randomNumber } ` ;
66+
67+ if ( ! isDepartmentValid || ! isPreferredSubjectsValid ) {
68+ res . status ( 400 ) . json ( {
69+ error : `Invalid IDs: Department: ${ isDepartmentValid } , PreferredSubjects: ${ isPreferredSubjectsValid } }` ,
70+ } ) ;
71+ } else {
72+ const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
73+ const randomIndex = Math . floor ( Math . random ( ) * alphabet . length ) ;
74+ const randomLetter = alphabet [ randomIndex ] ;
75+ let randomNumber = Math . floor ( Math . random ( ) * 1000 ) . toString ( ) ;
76+ if ( randomNumber . length === 2 ) {
77+ randomNumber = `0${ randomNumber } ` ;
78+ }
79+ const ERPID = `F${ randomLetter } ${ randomNumber } ` ;
80+
81+
82+ const newFaculty = await createFaculty (
83+ ERPID ,
84+ dateOfJoining ,
85+ dateOfLeaving ,
86+ profileLink ,
87+ qualifications ,
88+ totalExperience ,
89+ achievements ,
90+ areaOfSpecialization ,
91+ papersPublishedPG ,
92+ papersPublishedUG ,
93+ department ,
94+ preferredSubjects ,
95+ designation ,
96+ natureOfAssociation ,
97+ additionalResponsibilities ,
98+ session ,
99+ ) ;
100+ await Promise . all ( [
101+ addNewEmployeePersonal ( employeePersonalDetails , session ) ,
102+ createEmployeeEducationHistory ( employeeEducationDetails , session ) ,
103+ addNewEmployeeCurrent ( employeeCurrentDetails , session ) ,
104+ createEmployeeBank ( employeeBankDetails , session ) ,
105+ ] ) ;
106+ res . json ( {
107+ res : `added faculty ${ newFaculty . ERPID } ` ,
108+ id : newFaculty . ERPID ,
109+ } ) ;
110+ await session . commitTransaction ( ) ;
111+ session . endSession ( ) ;
64112 }
65- const ERPID = `F${ randomLetter } ${ randomNumber } ` ;
66113
67- const newFaculty = await createFaculty (
68- ERPID ,
69- dateOfJoining ,
70- dateOfLeaving ,
71- profileLink ,
72- qualifications ,
73- totalExperience ,
74- achievements ,
75- areaOfSpecialization ,
76- papersPublishedPG ,
77- papersPublishedUG ,
78- department ,
79- preferredSubjects ,
80- designation ,
81- natureOfAssociation ,
82- additionalResponsibilities ,
83- session ,
84- ) ;
85- await Promise . all ( [
86- addNewEmployeePersonal ( employeePersonalDetails , session ) ,
87- createEmployeeEducationHistory ( employeeEducationDetails , session ) ,
88- addNewEmployeeCurrent ( employeeCurrentDetails , session ) ,
89- createEmployeeBank ( employeeBankDetails , session ) ,
90- ] ) ;
91- res . json ( {
92- res : `added faculty ${ newFaculty . ERPID } ` ,
93- id : newFaculty . ERPID ,
94- } ) ;
95- await session . commitTransaction ( ) ;
96- session . endSession ( ) ;
114+
97115 } catch ( error ) {
98116 await session . abortTransaction ( ) ;
99117 session . endSession ( ) ;
0 commit comments