@@ -4,7 +4,10 @@ import {
44 listdepartment ,
55 deletedepartment ,
66} from "#services/department" ;
7-
7+ import { isEntityIdValid } from "#middleware/entityIdValidation" ;
8+ import Accreditation from "#models/accreditation" ;
9+ import Infrastructure from "#models/infrastructure" ;
10+ import Organization from "#models/organization" ;
811import { logger } from "#util" ;
912
1013async function addDepartment ( req , res ) {
@@ -16,7 +19,21 @@ async function addDepartment(req, res) {
1619 infrastructures,
1720 organization,
1821 } = req . body ;
22+
23+ const isAccredationValid = await isEntityIdValid ( accreditations , Accreditation ) ;
24+ const isInfrastructureValid = await isEntityIdValid ( infrastructures , Infrastructure ) ;
25+ const isOrganizationValid = await isEntityIdValid ( organization , Organization ) ;
26+
1927 try {
28+
29+ if ( ! isAccredationValid && ! isInfrastructureValid && ! isOrganizationValid ) {
30+
31+ const error = "" ;
32+ if ( ! isBranchValid ) error . concat ( "Invalid branch" ) ;
33+ if ( ! isCourseValid ) error . concat ( " Invalid course opted" ) ;
34+ res . status ( 400 ) . json ( { err : error } ) ;
35+ }
36+ else {
2037 const department = await createnewdepartment (
2138 name ,
2239 acronym ,
@@ -29,6 +46,7 @@ async function addDepartment(req, res) {
2946 res : `added Department successfully ${ department . name } ` ,
3047 id : department . id ,
3148 } ) ;
49+ }
3250 } catch ( error ) {
3351 logger . error ( "Error while inserting" , error ) ;
3452 res . status ( 500 ) ;
0 commit comments