@@ -22,6 +22,19 @@ const getDoctors = async (req, res) => {
2222 }
2323} ;
2424
25+ // Fetch doctors based on user agency
26+ const getCategory = async ( req , res ) => {
27+ try {
28+ const user = await getUserById ( req . userId ) ;
29+ const agency_id = user . agency_id ;
30+ const agencyDBDetails = await getAgencyDBDetails ( agency_id ) ;
31+ const doctors = await Category . getAllCategories ( agencyDBDetails ) ;
32+ res . json ( doctors ) ;
33+ } catch ( error ) {
34+ res . status ( 500 ) . json ( { message : error . message } ) ;
35+ }
36+ } ;
37+
2538const err_getDoctors = async ( req , res ) => {
2639 const user = req . user ;
2740 logger . info ( 'Accessing getDoctors route' , { user } ) ;
@@ -60,6 +73,10 @@ const getDoctorById = async (req, res) => {
6073
6174const createDoctor = async ( req , res ) => {
6275 const data = req . body ;
76+ const user = await getUserById ( req . userId ) ;
77+ const agency_id = user . agency_id ;
78+ const agencyDBDetails = await getAgencyDBDetails ( agency_id ) ;
79+
6380 logger . info ( 'Accessing createDoctor route' , { data } ) ;
6481
6582 // Validate input data
@@ -69,19 +86,19 @@ const createDoctor = async (req, res) => {
6986 }
7087
7188 // Validate speciality, category, and town existence
72- const specialityExists = await Speciality . checkSpecialityExists ( data . speciality_id ) ;
89+ const specialityExists = await Speciality . checkSpecialityExists ( data . speciality_id , agencyDBDetails ) ;
7390 if ( ! specialityExists ) {
7491 logger . warn ( 'Speciality does not exist' ) ;
7592 return res . status ( 400 ) . json ( { error : 'Speciality ID does not exist' } ) ;
7693 }
7794
78- const categoryExists = await Category . checkCategoryExists ( data . category_id ) ;
95+ const categoryExists = await Category . checkCategoryExists ( data . category_id , agencyDBDetails ) ;
7996 if ( ! categoryExists ) {
8097 logger . warn ( 'Category does not exist' ) ;
8198 return res . status ( 400 ) . json ( { error : 'Category ID does not exist' } ) ;
8299 }
83100
84- const townExists = await Town . checkTownExists ( data . town_id ) ;
101+ const townExists = await Town . checkTownExists ( data . town_id , agencyDBDetails ) ;
85102 if ( ! townExists ) {
86103 logger . warn ( 'Town does not exist' , { town_id } ) ;
87104 return res . status ( 400 ) . json ( { error : 'Town ID does not exist' } ) ;
@@ -91,7 +108,10 @@ const createDoctor = async (req, res) => {
91108 try {
92109
93110 // Insert doctor if both checks pass
94- const result = await Doctor . create ( data ) ;
111+ const user = await getUserById ( req . userId ) ;
112+ const agency_id = user . agency_id ;
113+ const agencyDBDetails = await getAgencyDBDetails ( agency_id ) ;
114+ const result = await Doctor . create ( data , agencyDBDetails ) ;
95115 logger . info ( 'Doctor created successfully' , { doctorId : result . insertId } ) ;
96116 res . status ( 201 ) . json ( { message : 'Doctor created' , doctorId : result . insertId } ) ;
97117 } catch ( err ) {
@@ -148,4 +168,4 @@ const deleteDoctor = async (req, res) => {
148168 }
149169} ;
150170
151- module . exports = { getDoctors, getDoctorById, createDoctor, updateDoctor, deleteDoctor } ;
171+ module . exports = { getDoctors, getDoctorById, getCategory , createDoctor, updateDoctor, deleteDoctor } ;
0 commit comments