File size: 10,277 Bytes
ed0b977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273

from flask import Blueprint, render_template, request, redirect, url_for, flash, session, jsonify
from utils.logging.logger_setup import logger
from utils.db.db import mongo
import bcrypt
from datetime import datetime

admin_bp = Blueprint('admin', __name__, url_prefix='/admin')

@admin_bp.route('/')
def selection():
    return render_template('admin/selection.html')

@admin_bp.route('/login', methods=['GET', 'POST'])
def login():
    # Helper to redirect logged-in users
    if session.get('user_type'):
        user_type = session.get('user_type')
        if user_type == 'admin':
             return redirect(url_for('admin.dashboard'))
        elif user_type == 'assistant':
             # Redirect to generic assistant dashboard which handles role splitting
             return redirect(url_for('assistant.dashboard'))

    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        
        # Check Admin
        admin = mongo.db.admin.find_one({"username": username})
        if admin:
            # Check password
            stored_password = admin.get('password')
            is_valid = False
            try:
                if stored_password:
                    is_valid = bcrypt.checkpw(password.encode('utf-8'), stored_password.encode('utf-8'))
            except ValueError:
                is_valid = (stored_password == password)

            if is_valid:
                session['admin_id'] = admin.get('sql_id', admin.get('id'))
                session['user_type'] = 'admin'
                logger.info({"action": "admin_login", "status": "success", "username": username})
                return redirect(url_for('admin.dashboard'))
        
        # Check Assistant
        assistant = mongo.db.administrativeassistant.find_one({"username": username})
        if assistant:
            stored_password = assistant.get('password')
            is_valid = False
            try:
                if stored_password:
                    is_valid = bcrypt.checkpw(password.encode('utf-8'), stored_password.encode('utf-8'))
            except ValueError:
                is_valid = (stored_password == password)

            if is_valid:
                session['assistant_id'] = assistant.get('sql_id', assistant.get('id'))
                session['user_type'] = 'assistant'
                session['role'] = assistant.get('role')
                session['user_name'] = assistant.get('username')
                
                if assistant.get('role') == 'account_creation':
                    return redirect(url_for('assistant.account_creation_dashboard'))
                elif assistant.get('role') == 'data_entry':
                    return redirect(url_for('assistant.data_entry_dashboard'))
                else:
                     return redirect(url_for('admin.dashboard')) 
        
        logger.warning({"action": "admin_login", "status": "failure", "username": username})
        
        flash('Invalid credentials')
        return redirect(url_for('admin.login'))
    
    return render_template('admin/admin_login.html')

@admin_bp.route('/dashboard')
def dashboard():
    if 'user_type' not in session:
        return redirect(url_for('admin.login'))
    
    # If an assistant tries to access admin dashboard, send them to their place
    if session.get('user_type') == 'assistant':
         return redirect(url_for('assistant.dashboard'))
    
    return render_template('admin/admin_dashboard.html')

@admin_bp.route('/logout')
def logout():
    session.clear()
    logger.info({"action": "logout", "user_type": "admin/assistant"})
    return redirect(url_for('admin.login'))

@admin_bp.route('/manage/doctors')
def manage_doctors():
    if session.get('user_type') != 'admin':
        return redirect(url_for('admin.login'))
    doctors = list(mongo.db.doctor.find())
    return render_template('admin/manage_doctors.html', doctors=doctors)

@admin_bp.route('/manage/patients')
def manage_patients():
    if session.get('user_type') != 'admin':
        return redirect(url_for('admin.login'))
    patients = list(mongo.db.patient.find())
    return render_template('admin/manage_patients.html', patients=patients)

@admin_bp.route('/manage/assistants')
def manage_assistants():
    if session.get('user_type') != 'admin':
        return redirect(url_for('admin.login'))
    assistants = list(mongo.db.administrativeassistant.find())
    return render_template('admin/manage_assistants.html', assistants=assistants)

def get_next_id(collection_name):
    """Helper to simulate auto-increment ID for compatibility"""
    last_item = mongo.db[collection_name].find_one(sort=[("sql_id", -1)])
    if last_item and 'sql_id' in last_item:
        return last_item['sql_id'] + 1
    return 1

@admin_bp.route('/add/doctor', methods=['POST'])
def add_doctor():
    """
    Create a new doctor account.
    """
    if session.get('user_type') != 'admin':
        return redirect(url_for('admin.login'))
    
    try:
        name = request.form['name']
        specialization = request.form['specialization']
        email = request.form['email']
        phone = request.form['phone']
        password = request.form['password']
        
        hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
        new_id = get_next_id('doctor')
        
        new_doctor = {
            "sql_id": new_id,
            "id": new_id,
            "name": name,
            "specialization": specialization,
            "email": email,
            "phone": phone,
            "password": hashed_password,
            "bio": None
        }

        mongo.db.doctor.insert_one(new_doctor)
        logger.info({"action": "add_doctor", "performed_by": "admin", "doctor_email": email})
        flash("Doctor added successfully.")
    except Exception as e:
        logger.error(f"Error adding doctor: {e}")
        flash("Error adding doctor. Please check logs.")
    
    return redirect(url_for('admin.manage_doctors'))

@admin_bp.route('/add/patient', methods=['POST'])
def add_patient():
    """
    Create a new patient account with a structured ID.
    """
    if session.get('user_type') != 'admin':
        return redirect(url_for('admin.login'))
    
    try:
        name = request.form['name']
        email = request.form['email']
        password = request.form['password']
        
        hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
        new_id = get_next_id('patient')

        # Generate Custom Patient ID: SQ-YYYY-COUNT
        current_year = datetime.now().year
        # Count existing patients with IDs starting with SQ-YYYY
        count = mongo.db.patient.count_documents({"patient_id": {"$regex": f"^SQ-{current_year}-"}})
        new_patient_id = f"SQ-{current_year}-{count + 1:03d}"

        new_patient = {
            "sql_id": new_id,
            "id": new_id,
            "patient_id": new_patient_id, # New Structured ID
            "name": name,
            "email": email,
            "password": hashed_password,
            "created_at": datetime.now()
        }

        mongo.db.patient.insert_one(new_patient)
        logger.info({"action": "add_patient", "performed_by": "admin", "patient_email": email, "new_patient_id": new_patient_id})
        flash(f"Patient added successfully! ID: {new_patient_id}")
    except Exception as e:
        logger.error(f"Error adding patient: {e}")
        flash("Error adding patient.")
    
    return redirect(url_for('admin.manage_patients'))

@admin_bp.route('/add/assistant', methods=['POST'])
def add_assistant():
    """
    Create a new administrative assistant account.
    """
    if session.get('user_type') != 'admin':
        return redirect(url_for('admin.login'))
    
    try:
        username = request.form['username']
        password = request.form['password']
        role = request.form['role']
        
        hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
        new_id = get_next_id('administrativeassistant')

        new_assistant = {
            "sql_id": new_id,
            "id": new_id,
            "username": username,
            "password": hashed_password,
            "role": role
        }

        mongo.db.administrativeassistant.insert_one(new_assistant)
        logger.info({"action": "add_assistant", "performed_by": "admin", "username": username, "role": role})
        flash("Assistant added successfully.")
    except Exception as e:
        logger.error(f"Error adding assistant: {e}")
        flash("Error adding assistant.")
    
    return redirect(url_for('admin.manage_assistants'))

@admin_bp.route('/delete/<string:user_type>/<int:user_id>')
def delete_user(user_type, user_id):
    """
    Delete a user (doctor, patient, or assistant) by ID.
    """
    if session.get('user_type') != 'admin':
        return redirect(url_for('admin.login'))

    collection_name = None
    redirect_url = 'admin.dashboard'

    if user_type == 'doctor':
        collection_name = 'doctor'
        redirect_url = 'admin.manage_doctors'
    elif user_type == 'patient':
        collection_name = 'patient'
        redirect_url = 'admin.manage_patients'
    elif user_type == 'assistant':
        collection_name = 'administrativeassistant'
        redirect_url = 'admin.manage_assistants'
    
    if collection_name:
        try:
            # Delete by sql_id or id
            result = mongo.db[collection_name].delete_one({"$or": [{"sql_id": user_id}, {"id": user_id}]})
            if result.deleted_count > 0:
                logger.info({"action": "delete_user", "target_type": user_type, "target_id": user_id, "status": "success"})
                flash(f"{user_type.capitalize()} deleted successfully.")
            else:
                 logger.warning({"action": "delete_user", "target_type": user_type, "target_id": user_id, "status": "not_found"})
                 flash("User not found.")
        except Exception as e:
            logger.error(f"Error deleting user: {e}")
            flash("Error deleting user.")
        
        return redirect(url_for(redirect_url))

    return redirect(url_for('admin.dashboard'))