Skip to content

Commit fb12695

Browse files
committed
doc routes added
1 parent 387a681 commit fb12695

27 files changed

+977
-86
lines changed

backend/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const PORT = process.env.PORT || 3000;
1919

2020

2121

22+
2223
// Configure CORS
2324
const corsOptions = {
2425
origin: 'https://localhost:3000' // Update with your frontend URL
@@ -44,6 +45,8 @@ const credentials = { key: privateKey, cert: certificate };
4445

4546
const httpsServer = https.createServer(credentials, app);
4647

48+
49+
4750
httpsServer.listen(parseInt(PORT), () => {
4851
logger.info(`HTTPS Server running on port ${PORT}`);
4952
});

backend/config/db.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ dotenv.config();
66
const createMainDBConnection = () => {
77
return new Sequelize('mris', 'saw1993', '1234', {
88
host: 'localhost',
9-
dialect: 'mysql'
9+
dialect: 'mysql',
10+
logging:false,
1011
});
1112
};
1213

1314
// Create a Sequelize instance for the agency database connection
1415
const createAgencyDBConnection = (agencyDetails) => {
1516
return new Sequelize(agencyDetails.db_name, agencyDetails.db_user, agencyDetails.db_password, {
1617
host: agencyDetails.db_host,
17-
dialect: 'mysql'
18+
dialect: 'mysql',
19+
logging:false,
1820
});
1921
};
2022

backend/constants/PermissionData.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,14 @@ module.exports = {
33
EDIT_USER: 2,
44
DELETE_USER: 3,
55
CREATE_USER: 4,
6-
VIEW_DOCTORS: 2
6+
7+
VIEW_DOCTORS: 5,
8+
EDIT_DOCTOR: 6,
9+
DELETE_DOCTOR: 7,
10+
CREATE_DOCTOR: 8,
11+
12+
VIEW_CHEMISTS: 9,
13+
EDIT_CHEMIST: 10,
14+
DELETE_CHEMIST: 11,
15+
CREATE_CHEMIST: 12
716
};

backend/controllers/doctorController.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,26 @@ const Town = require('../models/townModel');
33
const Category = require('../models/doctorCategoryModel');
44
const Speciality = require('../models/specialityModel');
55
const logger = require('../config/logger');
6+
const { createAgencyDBConnection } = require('../config/db');
67

8+
//const { getAllDoctors } = require('../repositories/doctorRepository');
9+
const getAllDoctors = require('../usecases/doctor/getAllDoctors');
10+
const getRepDoctors = require('../usecases/doctor/getRepDoctors')
711
const { getUserById } = require('../repositories/userRepository');
812
const { getAgencyDBDetails } = require('../models/agencyModel');
913
const { getDoctorsByAgency } = require('../models/doctorModel');
1014

1115

16+
1217
// Fetch doctors based on user agency
1318
const getDoctors = async (req, res) => {
1419
try {
1520
const user = await getUserById(req.userId);
1621
const agency_id = user.agency_id;
1722
const agencyDBDetails = await getAgencyDBDetails(agency_id);
18-
const doctors = await getDoctorsByAgency(agencyDBDetails);
23+
const agency_DB = createAgencyDBConnection(agencyDBDetails)
24+
//const doctors = await getAllDoctors(agency_DB);
25+
const doctors = await getRepDoctors(user.user_id,agency_DB)
1926
res.json(doctors);
2027
} catch (error) {
2128
res.status(500).json({ message: error.message });

backend/logs/combined.log

Lines changed: 399 additions & 0 deletions
Large diffs are not rendered by default.

backend/logs/error.log

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,3 +557,19 @@
557557
2024-11-11 07:16:29 error: Error in checkPermission middleware: Error fetching permissions
558558
2024-11-11 07:17:46 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
559559
2024-11-11 07:17:46 error: Error in checkPermission middleware: Error fetching permissions
560+
2024-11-11 07:56:19 error: Invalid token: jwt expired
561+
2024-11-12 07:22:02 error: Invalid token: jwt expired
562+
2024-11-13 18:15:05 error: Invalid token: jwt expired
563+
2024-11-15 13:35:21 error: Invalid token: jwt expired
564+
2024-11-16 07:03:11 error: Error finding user by email [email protected]: Unknown column 'rep_id' in 'field list'
565+
2024-11-16 07:06:53 error: Error finding user by email [email protected]: Unknown column 'User.rep_id' in 'field list'
566+
2024-11-16 07:13:13 error: Error in checkPermission middleware: Cannot read properties of undefined (reading 'role_id')
567+
2024-11-16 07:13:47 error: Error in checkPermission middleware: Cannot read properties of undefined (reading 'role_id')
568+
2024-11-16 07:14:06 error: Error in checkPermission middleware: Cannot read properties of undefined (reading 'role_id')
569+
2024-11-16 07:16:26 error: Error fetching user by ID 8: Argument passed to findByPk is invalid: [object Object]
570+
2024-11-16 07:16:26 error: Error in checkPermission middleware: Argument passed to findByPk is invalid: [object Object]
571+
2024-11-16 07:18:45 error: Error fetching user by ID 8: UserHierarchy is associated to User using an alias. You've included an alias (user_hierarchy), but it does not match the alias(es) defined in your association (role).
572+
2024-11-16 07:18:45 error: Error in checkPermission middleware: UserHierarchy is associated to User using an alias. You've included an alias (user_hierarchy), but it does not match the alias(es) defined in your association (role).
573+
2024-11-16 07:22:51 error: Error fetching user by ID 8: Unknown column 'User.role_id' in 'field list'
574+
2024-11-16 07:22:51 error: Error in checkPermission middleware: Unknown column 'User.role_id' in 'field list'
575+
2024-11-16 08:54:34 error: Token verification failed: jwt expired

backend/middlewares/authMiddleware.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const authMiddleware = (req, res, next) => {
1212
try {
1313
const decoded = verifyToken(token);
1414
req.userId = decoded.id; // Attach user ID to request
15-
logger.info(`Token request verified for authMiddleware: ${decoded.toString}`);
1615
next();
1716
} catch (err) {
1817
logger.error(`Invalid token: ${err.message}`);

backend/middlewares/checkPermissionMiddleware.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,24 @@ const checkPermission = (permissionID) => {
2020
return res.status(404).json({ message: 'User not found' });
2121
}
2222

23-
24-
// Check if the required permission exists in any of the roles
23+
// Check if the required permission exists in any of the roles
2524
let hasPermission = false;
26-
const permissions = await permissionRepository.getPermissionsForRole(user.role_id);
25+
const permissions = await permissionRepository.getPermissionsForRole(user.user_hierarchy.role_id);
2726

27+
hasPermission = permissions.some(permission => {
2828

29-
hasPermission = permissions.some(permission => permission.permission_id === permissionID);
30-
31-
if (hasPermission) {
32-
next();
33-
}else {
34-
return res.status(403).json({ message: 'Permission denied' });
29+
if (permission.permission_id === permissionID) {
30+
logger.info(`Token verified and permission granted for : ${permission.permission_name}`); // Log the permission name
31+
return true;
3532
}
33+
return false;
34+
});
35+
36+
if (hasPermission) {
37+
next();
38+
} else {
39+
return res.status(403).json({ message: 'Permission denied' });
40+
}
3641

3742
} catch (error) {
3843
logger.error(`Error in checkPermission middleware: ${error.message}`);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { tableName } = require("./DoctorSpeciality");
2+
3+
const ChemistRoute = sequelize.define('ChemistRouteMapping', {
4+
chemist_id: {
5+
type: DataTypes.INTEGER,
6+
references: { model: 'Chemist', key: 'chemist_id' },
7+
},
8+
route_id: {
9+
type: DataTypes.INTEGER,
10+
references: { model: 'Route', key: 'route_id' },
11+
}
12+
}, {
13+
tableName:"chemist_route",
14+
timestamps: false });
15+
16+
module.exports = ChemistRoute
Lines changed: 70 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,76 @@
1-
// models/Doctor.js
1+
// src/models/Doctor.js
22
const { Model, DataTypes } = require('sequelize');
3+
const DoctorSpeciality = require('./DoctorSpeciality'); // Import the speciality model
4+
const Town = require('./Towns'); // Import the town model
5+
const DoctorCategory = require('./DoctorCategory'); // Import the doctor category model
36

47
class Doctor extends Model {
5-
static initModel(sequelize) {
6-
Doctor.init({
7-
doctor_id: {
8-
type: DataTypes.INTEGER,
9-
primaryKey: true,
10-
autoIncrement: true,
11-
},
12-
category_id: {
13-
type: DataTypes.INTEGER,
14-
},
15-
name: {
16-
type: DataTypes.STRING,
17-
allowNull: false,
18-
},
19-
telephone: {
20-
type: DataTypes.STRING,
21-
},
22-
email: {
23-
type: DataTypes.STRING,
24-
},
25-
slmc_no: {
26-
type: DataTypes.STRING,
27-
},
28-
birthday: {
29-
type: DataTypes.DATE,
30-
},
31-
remarks: {
32-
type: DataTypes.TEXT,
33-
},
34-
frequency: {
35-
type: DataTypes.INTEGER,
36-
},
37-
speciality_id: {
38-
type: DataTypes.INTEGER,
39-
},
40-
town_id: {
41-
type: DataTypes.INTEGER,
42-
},
43-
}, {
44-
sequelize,
45-
modelName:'Doctors',
46-
tableName: 'doctors',
47-
timestamps: false,
48-
});
49-
}
8+
// Static method to initialize the model with a dynamic sequelize instance
9+
static initModel(sequelize) {
10+
Doctor.init({
11+
doctor_id: {
12+
type: DataTypes.INTEGER,
13+
primaryKey: true,
14+
autoIncrement: true,
15+
},
16+
category_id: {
17+
type: DataTypes.INTEGER,
18+
},
19+
name: {
20+
type: DataTypes.STRING,
21+
allowNull: false,
22+
},
23+
telephone: {
24+
type: DataTypes.STRING,
25+
},
26+
email: {
27+
type: DataTypes.STRING,
28+
},
29+
slmc_no: {
30+
type: DataTypes.STRING,
31+
},
32+
birthday: {
33+
type: DataTypes.DATE,
34+
},
35+
remarks: {
36+
type: DataTypes.TEXT,
37+
},
38+
frequency: {
39+
type: DataTypes.INTEGER,
40+
},
41+
speciality_id: {
42+
type: DataTypes.INTEGER,
43+
},
44+
town_id: {
45+
type: DataTypes.INTEGER,
46+
},
47+
}, {
48+
sequelize, // Dynamically injected sequelize instance
49+
modelName: "Doctor", // Class name (can be pluralized if needed)
50+
tableName: 'doctors', // Explicit table name
51+
timestamps: false, // Disable automatic timestamp fields
52+
});
53+
54+
// Doctor belongs to a DoctorCategory (many-to-one relationship)
55+
Doctor.belongsTo(DoctorCategory, {
56+
foreignKey: 'category_id',
57+
as: 'category',
58+
});
59+
60+
// Doctor belongs to a DoctorSpeciality (many-to-one relationship)
61+
Doctor.belongsTo(DoctorSpeciality, {
62+
foreignKey: 'speciality_id',
63+
as: 'speciality',
64+
});
65+
66+
// Doctor belongs to a Town (many-to-one relationship)
67+
Doctor.belongsTo(Town, {
68+
foreignKey: 'town_id',
69+
as: 'town',
70+
});
71+
}
72+
73+
5074
}
5175

5276
module.exports = Doctor;

0 commit comments

Comments
 (0)