Skip to content

Commit 53bfac5

Browse files
committed
feat: did too many things, Don't know which message it should be
1 parent 0ced7cc commit 53bfac5

6 files changed

Lines changed: 354 additions & 18 deletions

File tree

RBAC_SPEC.md

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
# RBAC Specification – HMS Backend
2+
3+
This document defines the Role-Based Access Control (RBAC) model for the HMS Backend system.
4+
5+
The objective of this specification is to clearly define:
6+
7+
* System modules
8+
* Allowed actions per module
9+
* Access boundaries per role
10+
* Enforcement expectations at middleware level
11+
12+
---
13+
14+
# 1. Roles
15+
16+
The system currently supports the following roles:
17+
18+
* **Admin**
19+
* **Doctor**
20+
* **Reception**
21+
* **Billing Staff**
22+
23+
Each role has defined authority boundaries to ensure operational control and data protection.
24+
25+
---
26+
27+
# 2. System Modules
28+
29+
The backend exposes the following logical modules:
30+
31+
1. Dashboard
32+
2. Doctor Management
33+
3. Patient Management
34+
4. Department Management
35+
5. Schedule Management
36+
6. Appointment Management
37+
7. Billing / Payment
38+
8. Reports
39+
9. Human Resources
40+
10. System Settings & Security
41+
42+
---
43+
44+
# 3. Action Definitions
45+
46+
Standard action types used across modules:
47+
48+
* **View** – Read access
49+
* **Create** – Add new records
50+
* **Update** – Modify existing records
51+
* **Delete** – Remove records
52+
* **Approve** – Authorize specific actions (if applicable)
53+
* **Export** – Generate downloadable data
54+
55+
---
56+
57+
# 4. Role Permission Matrix
58+
59+
## 4.1 Admin
60+
61+
Administrative superuser with full system authority.
62+
63+
Permissions:
64+
65+
* Full access (View / Create / Update / Delete) across all modules
66+
* Manage user roles and permissions
67+
* Access system configuration and security settings
68+
* View and export all reports
69+
* Manage HR records
70+
* Verify and review billing/payment records
71+
72+
Admin has unrestricted operational and configuration-level control.
73+
74+
---
75+
76+
## 4.2 Doctor
77+
78+
Clinical authority with access restricted to assigned operational data.
79+
80+
Permissions:
81+
82+
Dashboard:
83+
84+
* View personal dashboard
85+
86+
Patient Management:
87+
88+
* View assigned patients
89+
* Update consultation notes
90+
* Cannot delete patient records
91+
* Cannot create new patient profiles (unless explicitly allowed)
92+
93+
Appointment Management:
94+
95+
* View assigned appointments
96+
* Update appointment status (e.g., Completed, Cancelled)
97+
98+
Schedule:
99+
100+
* View own schedule
101+
102+
Reports:
103+
104+
* View reports related to own patients
105+
* Cannot access system-wide financial reports
106+
107+
Restrictions:
108+
109+
* No access to HR module
110+
* No access to system settings
111+
* No authority to manage other doctors
112+
113+
---
114+
115+
## 4.3 Reception
116+
117+
Front-desk workflow management role.
118+
119+
Permissions:
120+
121+
Patient Management:
122+
123+
* Create patient registration
124+
* Update patient demographic details
125+
* View patient records
126+
* Cannot delete patient records
127+
128+
Appointment Management:
129+
130+
* Create appointments
131+
* Assign doctors
132+
* Update appointment details
133+
* Cancel appointments
134+
135+
Schedule:
136+
137+
* View and manage available slots
138+
139+
Billing:
140+
141+
* Create initial billing entries
142+
* View payment status
143+
* Cannot finalize or override billing records (if Billing Staff exists)
144+
145+
Reports:
146+
147+
* View basic operational reports
148+
149+
Restrictions:
150+
151+
* No access to HR
152+
* No access to system configuration
153+
* No access to medical consultation notes editing
154+
155+
---
156+
157+
## 4.4 Billing Staff
158+
159+
Financial operations authority.
160+
161+
Permissions:
162+
163+
Billing:
164+
165+
* Create billing records
166+
* Update billing records
167+
* Mark payment status
168+
* Generate invoices
169+
* Export billing reports
170+
171+
Patient Management:
172+
173+
* View patient billing-related data
174+
* Cannot modify medical or demographic information
175+
176+
Reports:
177+
178+
* View and export financial reports
179+
180+
Restrictions:
181+
182+
* No access to HR
183+
* No access to system settings
184+
* No access to medical consultation editing
185+
186+
---
187+
188+
# 5. Consolidated Permission Matrix
189+
190+
| Module | Admin | Doctor | Reception | Billing Staff |
191+
| ------------------- | ----- | --------------- | --------------- | ------------------- |
192+
| Dashboard | Full | Own | Limited | Limited |
193+
| Doctor Management | CRUD | Self | View | No Access |
194+
| Patient Management | CRUD | View Assigned | Create / Update | View (Billing Only) |
195+
| Appointment | CRUD | Update Assigned | Create / Update | View |
196+
| Schedule | CRUD | View Own | Manage Slots | No Access |
197+
| Billing | Full | View | Create | CRUD |
198+
| Reports | Full | Own Patients | Operational | Financial Only |
199+
| Human Resources | CRUD | No Access | No Access | No Access |
200+
| Settings & Security | Full | No Access | No Access | No Access |
201+
202+
Legend:
203+
204+
* **Full / CRUD** = View, Create, Update, Delete
205+
* **Own** = Limited to assigned records
206+
* **Limited** = Restricted data view
207+
* **No Access** = Access denied
208+
209+
---
210+
211+
# 6. Enforcement Strategy
212+
213+
RBAC enforcement must occur at:
214+
215+
* Middleware level (route protection)
216+
* Service-level ownership validation (e.g., doctor accessing only assigned patients)
217+
218+
Example enforcement layers:
219+
220+
1. Authentication middleware (JWT verification)
221+
2. Role validation middleware
222+
3. Resource ownership validation inside services
223+
224+
Security must not rely solely on frontend role checks.
225+
226+
---
227+
228+
# 7. Future Expansion
229+
230+
The RBAC model is designed to support:
231+
232+
* Fine-grained permission mapping (role → permissions table)
233+
* Permission-based system instead of hardcoded roles
234+
* Audit logging per action
235+
* Multi-hospital tenant architecture (if required)
236+
237+
---
238+
239+
# 8. Design Principles
240+
241+
* Least privilege access
242+
* Clear authority boundaries
243+
* Separation between operational and financial control
244+
* Role enforcement independent of UI
245+
246+
---
247+
248+
This document serves as the official access contract between backend and frontend teams.
249+
250+
All new features must align with this specification unless formally updated.
251+
252+
---
253+

src/app.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import cors from 'cors';
33
import morgan from 'morgan';
44

55
import { globalLimiter } from './middleware/rateLimiter.js';
6+
import { errorHandler } from './middleware/errorHandler.js';
67

78
import authRoutes from './routes/auth.routes.js';
89
import paymentRoutes from './routes/payment.routes.js';
@@ -60,11 +61,13 @@ app.use('/api/patients', patientRoutes);
6061
app.use('/api/departments', deptRoutes);
6162
app.use('/api/appointments', appointmentRoutes);
6263

63-
app.use((err, _req, res, _next) => {
64-
console.error(err);
65-
res.status(err.status || 500).json({
66-
error: err.message || 'Internal Server Error'
64+
app.use((req, res, _next) => {
65+
res.status(404).json({
66+
success: false,
67+
message: "Route not found"
6768
});
6869
});
6970

70-
export default app;
71+
app.use(errorHandler);
72+
73+
export default app;

src/middleware/authmiddleware.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,41 @@ const protect = (req, res, next) => {
1414

1515
req.user = {
1616
id: decoded.id,
17-
role: decoded.role,
17+
role: decoded.role.toLowerCase(),
1818
email: decoded.email,
1919
};
2020

2121
return next();
2222
} catch (error) {
23-
if (error.name === "TokenExpiredError") {
24-
return res.status(401).json({ message: "Token has expired" });
25-
}
26-
return res.status(401).json({ message: "Not authorized, token failed" });
23+
return res.status(401).json({
24+
success: false,
25+
message:
26+
error.name === "TokenExpiredError"
27+
? "Token has expired"
28+
: "Invalid token",
29+
});
2730
}
2831
}
2932

30-
return res.status(401).json({ message: "Not authorized, no token" });
33+
return res.status(401).json({
34+
success: false,
35+
message: "Not authorized, no token",
36+
});
3137
};
3238

33-
34-
const authorize = (...roles) => {
39+
export const authorize = (...roles) => {
3540
return (req, res, next) => {
36-
if (!roles.includes(req.user.role)) {
37-
return res
38-
.status(403)
39-
.json({ message: "You do not have permission to perform this action" });
41+
const allowedRoles = Array.isArray(roles[0]) ? roles[0] : roles;
42+
43+
if (!req.user || !allowedRoles.includes(req.user.role)) {
44+
return res.status(403).json({
45+
success: false,
46+
message: "Access denied",
47+
});
4048
}
49+
4150
next();
4251
};
4352
};
4453

45-
4654
export { protect, authorize };

src/middleware/errorHandler.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const errorHandler = (err, req, res, next) => {
2+
console.error(err);
3+
4+
const statusCode = err.statusCode || 500;
5+
6+
res.status(statusCode).json({
7+
success: false,
8+
message: err.message || "Internal Server Error",
9+
});
10+
};

src/models/billing.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import mongoose from "mongoose";
2+
3+
const billingSchema = new mongoose.Schema(
4+
{
5+
patient: {
6+
type: mongoose.Schema.Types.ObjectId,
7+
ref: "Patient",
8+
required: true,
9+
},
10+
11+
appointment: {
12+
type: mongoose.Schema.Types.ObjectId,
13+
ref: "Appointment",
14+
},
15+
16+
amount: {
17+
type: Number,
18+
required: true,
19+
},
20+
21+
status: {
22+
type: String,
23+
enum: ["pending", "paid"],
24+
default: "pending",
25+
},
26+
27+
paymentMethod: {
28+
type: String,
29+
enum: ["cash", "online"],
30+
},
31+
32+
transactionId: {
33+
type: String,
34+
},
35+
36+
createdBy: {
37+
type: mongoose.Schema.Types.ObjectId,
38+
ref: "User",
39+
},
40+
41+
paidAt: {
42+
type: Date,
43+
},
44+
},
45+
{ timestamps: true }
46+
);
47+
48+
export default mongoose.model("Billing", billingSchema);

0 commit comments

Comments
 (0)