-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1592 lines (1306 loc) · 42.2 KB
/
Copy pathapp.py
File metadata and controls
1592 lines (1306 loc) · 42.2 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import secrets
from flask import Flask, request, jsonify, render_template, session, url_for
from database import init_db, get_db_connection
app = Flask(__name__)
app.secret_key = os.environ.get("FLASK_SECRET_KEY") or secrets.token_hex(32)
UPLOAD_FOLDER = "uploads"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
def is_debug_enabled():
return os.environ.get("FLASK_DEBUG", "").strip().lower() in {
"1",
"true",
"yes",
"on"
}
# Demo user table
USERS = {
"manager": {
"password": "1234",
"role": "manager",
"display_name": "Manager"
},
"staffa": {
"password": "1234",
"role": "staff",
"display_name": "Staff A"
},
"staffb": {
"password": "1234",
"role": "staff",
"display_name": "Staff B"
}
}
STATUS_PENDING = "pending"
STATUS_ASSIGNED = "assigned"
STATUS_IN_PROGRESS = "in_progress"
STATUS_SUBMITTED = "submitted"
STATUS_APPROVED = "approved"
STATUS_REJECTED = "rejected"
STATUS_CLOSED = "closed"
STATUS_VOIDED = "voided"
DEMO_TICKETS = [
{
"ticket_type": "pos_store_system",
"category": "pos_store_system",
"priority": "urgent",
"title": "Front counter printer cannot print labels",
"description": "Fake demo ticket: label printer stopped during a busy service period. Staff need escalation notes and a clear workaround.",
"reported_by": "Staff A",
"reported_to": "Manager",
"assigned_to": "Staff B",
"visibility": "public",
"status": STATUS_SUBMITTED,
"proof_type": "photo",
"proof_path": "uploads/demo-printer-label-proof.jpg",
"staff_note": "Checked paper path, restarted workstation, attached printer queue screenshot.",
"manager_comment": None,
"created_at": "2026-07-08 08:15:00",
"updated_at": "2026-07-08 09:05:00"
},
{
"ticket_type": "customer_complaint",
"category": "customer_complaint",
"priority": "urgent",
"title": "Customer complaint about refund over AUD 100",
"description": "Fake demo ticket: customer is requesting a refund above the staff approval threshold.",
"reported_by": "Staff B",
"reported_to": "Manager",
"assigned_to": "Staff A",
"visibility": "manager_only",
"status": STATUS_REJECTED,
"proof_type": "receipt",
"proof_path": "uploads/demo-refund-receipt.pdf",
"staff_note": "Customer asked for immediate decision. Receipt reference recorded for manager review.",
"manager_comment": "Please add final resolution options before customer follow-up.",
"created_at": "2026-07-08 08:35:00",
"updated_at": "2026-07-08 09:20:00"
},
{
"ticket_type": "repair",
"category": "repair",
"priority": "normal",
"title": "Student iPad screen check",
"description": "Fake demo ticket: iPad screen touch response is inconsistent after customer drop-off.",
"reported_by": "Manager",
"reported_to": None,
"assigned_to": "Staff A",
"visibility": "public",
"status": STATUS_IN_PROGRESS,
"proof_type": "note",
"proof_path": None,
"staff_note": "Initial diagnostics started. Waiting for second test after restart.",
"manager_comment": None,
"created_at": "2026-07-08 09:00:00",
"updated_at": "2026-07-08 09:40:00"
},
{
"ticket_type": "warranty_return",
"category": "warranty_return",
"priority": "high",
"title": "Warranty return needs manager approval",
"description": "Fake demo ticket: accessory return is inside warranty period but needs condition check.",
"reported_by": "Staff A",
"reported_to": "Manager",
"assigned_to": "Staff B",
"visibility": "public",
"status": STATUS_APPROVED,
"proof_type": "photo",
"proof_path": "uploads/demo-warranty-condition.jpg",
"staff_note": "Photos uploaded. Product condition is consistent with warranty claim.",
"manager_comment": "Approved for warranty exchange after proof review.",
"created_at": "2026-07-08 09:25:00",
"updated_at": "2026-07-08 10:10:00"
},
{
"ticket_type": "staff_report",
"category": "staff_report",
"priority": "high",
"title": "Staff report: SOP discount question",
"description": "Fake demo ticket: staff asked which discount rule applies when manager is unavailable.",
"reported_by": "Staff B",
"reported_to": "Manager",
"assigned_to": "Staff A, Staff B",
"visibility": "public",
"status": STATUS_ASSIGNED,
"proof_type": "note",
"proof_path": None,
"staff_note": None,
"manager_comment": "Use SOP guidance first, then escalate if the customer asks for an exception.",
"created_at": "2026-07-08 10:00:00",
"updated_at": "2026-07-08 10:05:00"
},
{
"ticket_type": "customer_feedback",
"category": "customer_feedback",
"priority": "low",
"title": "Customer feedback about pickup communication",
"description": "Fake demo ticket: customer suggested clearer repair pickup timing messages.",
"reported_by": "Staff A",
"reported_to": "Manager",
"assigned_to": "Staff B",
"visibility": "public",
"status": STATUS_CLOSED,
"proof_type": "note",
"proof_path": "uploads/demo-feedback-note.txt",
"staff_note": "Customer feedback recorded and template wording updated.",
"manager_comment": "Closed after message template was updated.",
"created_at": "2026-07-08 10:20:00",
"updated_at": "2026-07-08 10:55:00"
},
{
"ticket_type": "stock_inventory",
"category": "stock_inventory",
"priority": "normal",
"title": "Stock count mismatch for charging cables",
"description": "Fake demo ticket: inventory count does not match shelf quantity.",
"reported_by": "Manager",
"reported_to": None,
"assigned_to": None,
"visibility": "public",
"status": STATUS_PENDING,
"proof_type": "note",
"proof_path": None,
"staff_note": None,
"manager_comment": None,
"created_at": "2026-07-08 11:00:00",
"updated_at": "2026-07-08 11:00:00"
},
{
"ticket_type": "general_task",
"category": "general_task",
"priority": "low",
"title": "Duplicate cleaning checklist task",
"description": "Fake demo ticket: duplicate task was created during workflow review and should stay visible as voided history.",
"reported_by": "Manager",
"reported_to": None,
"assigned_to": None,
"visibility": "public",
"status": STATUS_VOIDED,
"proof_type": "note",
"proof_path": None,
"staff_note": None,
"manager_comment": "Voided because the task was duplicated during demo setup.",
"created_at": "2026-07-08 11:15:00",
"updated_at": "2026-07-08 11:25:00"
},
{
"ticket_type": "repair",
"category": "repair",
"priority": "high",
"title": "Phone battery replacement quality check",
"description": "Fake demo ticket: battery replacement needs final quality check before customer pickup.",
"reported_by": "Manager",
"reported_to": None,
"assigned_to": "Staff A",
"visibility": "public",
"status": STATUS_SUBMITTED,
"proof_type": "photo",
"proof_path": "uploads/demo-battery-test.jpg",
"staff_note": "Battery replaced, charging tested, final photo attached.",
"manager_comment": None,
"created_at": "2026-07-08 11:30:00",
"updated_at": "2026-07-08 12:05:00"
},
{
"ticket_type": "pos_store_system",
"category": "pos_store_system",
"priority": "urgent",
"title": "EFTPOS terminal intermittent connection",
"description": "Fake demo ticket: payment terminal intermittently disconnects and may affect customer checkout.",
"reported_by": "Staff A",
"reported_to": "Manager",
"assigned_to": "Staff A, Staff B",
"visibility": "public",
"status": STATUS_IN_PROGRESS,
"proof_type": "note",
"proof_path": None,
"staff_note": "Checked cable, restarted terminal, monitoring next transaction window.",
"manager_comment": "Escalate to provider if the next disconnect happens.",
"created_at": "2026-07-08 12:10:00",
"updated_at": "2026-07-08 12:35:00"
}
]
init_db()
def require_login():
return "username" in session
def require_role(role):
return session.get("role") == role
def current_user_role():
return session.get("role")
def current_user_display_name():
return session.get("display_name", "Unknown")
def split_assignees(assigned_to):
if not assigned_to:
return []
return [
name.strip()
for name in assigned_to.split(",")
if name.strip()
]
def normalize_assigned_to(assigned_to):
if not assigned_to:
return None
names = split_assignees(assigned_to)
unique_names = []
for name in names:
if name not in unique_names:
unique_names.append(name)
if not unique_names:
return None
return ", ".join(unique_names)
def next_status_after_assignment(current_status, assigned_to):
if current_status == STATUS_PENDING:
if assigned_to:
return STATUS_ASSIGNED
return STATUS_PENDING
if current_status == STATUS_ASSIGNED:
if assigned_to:
return STATUS_ASSIGNED
return STATUS_PENDING
return current_status
def is_terminal_status(status):
return status in [STATUS_CLOSED, STATUS_VOIDED]
def serialize_ticket(ticket):
return {
"id": ticket["id"],
"ticket_type": ticket["ticket_type"],
"category": ticket["category"],
"priority": ticket["priority"],
"title": ticket["title"],
"description": ticket["description"],
"reported_by": ticket["reported_by"],
"reported_to": ticket["reported_to"],
"assigned_to": ticket["assigned_to"],
"visibility": ticket["visibility"],
"status": ticket["status"],
"proof_required": ticket["proof_required"],
"proof_type": ticket["proof_type"],
"proof_path": ticket["proof_path"],
"staff_note": ticket["staff_note"],
"manager_comment": ticket["manager_comment"],
"is_demo": ticket["is_demo"],
"created_at": ticket["created_at"],
"updated_at": ticket["updated_at"]
}
def error_response(message, status_code):
return jsonify({
"success": False,
"error": message
}), status_code
def add_audit_log(ticket_id, action, actor, details=None):
conn = get_db_connection()
conn.execute("""
INSERT INTO audit_logs (
ticket_id,
action,
actor,
details
)
VALUES (?, ?, ?, ?)
""", (
ticket_id,
action,
actor,
details
))
conn.commit()
conn.close()
@app.route("/dashboard")
def dashboard_page():
return render_template("index.html")
@app.route("/")
def home():
dashboard_url = url_for("dashboard_page")
return f"""
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Task Ticket System</title>
<style>
body {{
font-family: Arial, sans-serif;
margin: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: #f4f6f8;
color: #1f2937;
}}
main {{
width: min(420px, 90vw);
padding: 32px;
border: 1px solid #d8dee4;
border-radius: 8px;
background: #ffffff;
text-align: center;
}}
h1 {{
margin: 0 0 12px;
font-size: 28px;
}}
p {{
margin: 0 0 24px;
color: #4b5563;
}}
a {{
display: inline-block;
padding: 12px 18px;
border-radius: 6px;
background: #2563eb;
color: #ffffff;
text-decoration: none;
font-weight: 600;
}}
</style>
</head>
<body>
<main>
<h1>Task Ticket System</h1>
<p>The application is running.</p>
<a href="{dashboard_url}">Open Dashboard</a>
</main>
</body>
</html>
"""
@app.route("/login", methods=["POST"])
def login():
data = request.get_json() or {}
username = data.get("username")
password = data.get("password")
user = USERS.get(username)
if user is None or user["password"] != password:
return error_response("Invalid username or password", 401)
session["username"] = username
session["role"] = user["role"]
session["display_name"] = user.get("display_name", username)
return jsonify({
"message": "Login successful",
"username": username,
"role": user["role"],
"display_name": session["display_name"]
}), 200
@app.route("/logout", methods=["POST"])
def logout():
session.clear()
return jsonify({
"message": "Logged out successfully"
}), 200
@app.route("/me", methods=["GET"])
def me():
if "username" not in session:
return jsonify({
"logged_in": False
}), 200
return jsonify({
"logged_in": True,
"username": session["username"],
"role": session["role"],
"display_name": session["display_name"]
}), 200
@app.route("/tickets", methods=["POST"])
def create_ticket():
if not require_login():
return error_response("Please login first to create a task or report an issue", 401)
data = request.get_json() or {}
ticket_type = data.get("ticket_type", "task")
category = data.get("category", ticket_type or "general_task")
priority = data.get("priority", "normal")
title = data.get("title")
description = data.get("description")
reported_by = current_user_display_name()
role = current_user_role()
if role == "staff":
reported_to = "Manager"
assigned_to = None
else:
reported_to = None
assigned_to = normalize_assigned_to(data.get("assigned_to"))
visibility = data.get("visibility", "public")
proof_required = data.get("proof_required", 1)
proof_type = data.get("proof_type", "photo")
if not title or not description:
return error_response("Title and description are required", 400)
conn = get_db_connection()
cursor = conn.execute("""
INSERT INTO tickets (
ticket_type,
category,
priority,
title,
description,
reported_by,
reported_to,
assigned_to,
visibility,
proof_required,
proof_type
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
ticket_type,
category,
priority,
title,
description,
reported_by,
reported_to,
assigned_to,
visibility,
proof_required,
proof_type
))
conn.commit()
ticket_id = cursor.lastrowid
conn.close()
if role == "manager":
details = f"Ticket created by {reported_by} and assigned to {assigned_to}"
else:
details = f"Ticket reported by {reported_by} to {reported_to}"
add_audit_log(
ticket_id=ticket_id,
action="created",
actor=reported_by,
details=details
)
return jsonify({
"message": "Ticket created successfully",
"ticket_id": ticket_id
}), 201
@app.route("/tickets", methods=["GET"])
def get_tickets():
conn = get_db_connection()
role = current_user_role()
display_name = current_user_display_name()
if role == "manager":
tickets = conn.execute("""
SELECT *
FROM tickets
ORDER BY created_at DESC
""").fetchall()
elif role == "staff":
tickets = conn.execute("""
SELECT *
FROM tickets
WHERE visibility = 'public'
OR reported_by = ?
OR assigned_to = ?
OR assigned_to LIKE ?
ORDER BY created_at DESC
""", (
display_name,
display_name,
f"%{display_name}%"
)).fetchall()
else:
tickets = conn.execute("""
SELECT *
FROM tickets
WHERE visibility = 'public'
ORDER BY created_at DESC
""").fetchall()
conn.close()
ticket_list = []
for ticket in tickets:
ticket_list.append(serialize_ticket(ticket))
return jsonify(ticket_list), 200
@app.route("/tickets/<int:ticket_id>", methods=["GET"])
def get_ticket(ticket_id):
conn = get_db_connection()
ticket = conn.execute("""
SELECT *
FROM tickets
WHERE id = ?
""", (ticket_id,)).fetchone()
if ticket is None:
conn.close()
return error_response("Ticket not found", 404)
role = current_user_role()
display_name = current_user_display_name()
if role != "manager":
if (
ticket["visibility"] == "manager_only"
and ticket["reported_by"] != display_name
and ticket["assigned_to"] != display_name
):
conn.close()
return error_response("You do not have permission to view this ticket", 403)
conn.close()
return jsonify(serialize_ticket(ticket)), 200
@app.route("/tickets/<int:ticket_id>", methods=["PATCH"])
def update_ticket(ticket_id):
if current_user_role() != "manager":
return error_response("Only manager can edit tickets", 403)
data = request.get_json() or {}
conn = get_db_connection()
ticket = conn.execute("""
SELECT *
FROM tickets
WHERE id = ?
""", (ticket_id,)).fetchone()
if ticket is None:
conn.close()
return error_response("Ticket not found", 404)
if is_terminal_status(ticket["status"]):
conn.close()
return error_response("Closed or voided tickets cannot be edited", 400)
ticket_type = data.get("ticket_type", ticket["ticket_type"])
category = data.get("category", ticket["category"])
priority = data.get("priority", ticket["priority"])
title = data.get("title", ticket["title"])
description = data.get("description", ticket["description"])
visibility = data.get("visibility", ticket["visibility"])
proof_required = data.get("proof_required", ticket["proof_required"])
proof_type = data.get("proof_type", ticket["proof_type"])
if "assigned_to" in data:
assigned_to = normalize_assigned_to(data.get("assigned_to"))
else:
assigned_to = ticket["assigned_to"]
if not title or not description:
conn.close()
return error_response("Title and description are required", 400)
new_status = next_status_after_assignment(
current_status=ticket["status"],
assigned_to=assigned_to
)
conn.execute("""
UPDATE tickets
SET ticket_type = ?,
category = ?,
priority = ?,
title = ?,
description = ?,
assigned_to = ?,
visibility = ?,
proof_required = ?,
proof_type = ?,
status = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""", (
ticket_type,
category,
priority,
title,
description,
assigned_to,
visibility,
proof_required,
proof_type,
new_status,
ticket_id
))
conn.commit()
updated_ticket = conn.execute("""
SELECT *
FROM tickets
WHERE id = ?
""", (ticket_id,)).fetchone()
conn.close()
add_audit_log(
ticket_id=ticket_id,
action="updated",
actor=current_user_display_name(),
details="Ticket details updated"
)
return jsonify({
"message": "Ticket updated successfully",
"ticket": serialize_ticket(updated_ticket)
}), 200
@app.route("/tickets/<int:ticket_id>/assign", methods=["PATCH"])
def assign_ticket(ticket_id):
if current_user_role() != "manager":
return error_response("Only manager can assign tickets", 403)
data = request.get_json() or {}
assigned_to = normalize_assigned_to(data.get("assigned_to"))
conn = get_db_connection()
ticket = conn.execute("""
SELECT *
FROM tickets
WHERE id = ?
""", (ticket_id,)).fetchone()
if ticket is None:
conn.close()
return error_response("Ticket not found", 404)
if ticket["status"] == STATUS_APPROVED:
conn.close()
return error_response("Approved tickets cannot be reassigned", 400)
if is_terminal_status(ticket["status"]):
conn.close()
return error_response("Closed or voided tickets cannot be reassigned", 400)
new_status = next_status_after_assignment(
current_status=ticket["status"],
assigned_to=assigned_to
)
conn.execute("""
UPDATE tickets
SET assigned_to = ?,
status = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""", (
assigned_to,
new_status,
ticket_id
))
conn.commit()
conn.close()
add_audit_log(
ticket_id=ticket_id,
action="assigned",
actor=current_user_display_name(),
details=f"Assignment updated to {assigned_to or 'Unassigned'}"
)
return jsonify({
"message": f"Assignment updated to {assigned_to or 'Unassigned'}",
"assigned_to": assigned_to,
"status": new_status
}), 200
@app.route("/tickets/<int:ticket_id>/start", methods=["PATCH"])
def start_ticket(ticket_id):
if current_user_role() != "staff":
return error_response("Only staff can start assigned tickets", 403)
conn = get_db_connection()
ticket = conn.execute("""
SELECT *
FROM tickets
WHERE id = ?
""", (ticket_id,)).fetchone()
if ticket is None:
conn.close()
return error_response("Ticket not found", 404)
if ticket["status"] not in [STATUS_PENDING, STATUS_ASSIGNED]:
conn.close()
return error_response("Only pending or assigned tickets can be started", 400)
conn.execute("""
UPDATE tickets
SET status = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""", (
STATUS_IN_PROGRESS,
ticket_id
))
conn.commit()
conn.close()
add_audit_log(
ticket_id=ticket_id,
action="started",
actor=current_user_display_name(),
details="Ticket started"
)
return jsonify({
"message": "Ticket started successfully"
}), 200
@app.route("/tickets/<int:ticket_id>/submit", methods=["PATCH"])
def submit_ticket(ticket_id):
if current_user_role() != "staff":
return error_response("Only staff can submit ticket proof", 403)
data = request.get_json() or {}
proof_type = data.get("proof_type") or "photo"
proof_path = data.get("proof_path")
staff_note = data.get("staff_note")
if not proof_path:
return error_response("proof_path is required", 400)
conn = get_db_connection()
ticket = conn.execute("""
SELECT *
FROM tickets
WHERE id = ?
""", (ticket_id,)).fetchone()
if ticket is None:
conn.close()
return error_response("Ticket not found", 404)
if ticket["status"] not in [STATUS_PENDING, STATUS_IN_PROGRESS]:
conn.close()
return error_response("Only pending or in progress tickets can be submitted", 400)
conn.execute("""
UPDATE tickets
SET status = ?,
proof_type = ?,
proof_path = ?,
staff_note = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""", (
STATUS_SUBMITTED,
proof_type,
proof_path,
staff_note,
ticket_id
))
conn.commit()
conn.close()
add_audit_log(
ticket_id=ticket_id,
action="submitted",
actor=current_user_display_name(),
details=f"Proof submitted ({proof_type}): {proof_path}"
)
return jsonify({
"message": "Ticket submitted successfully",
"ticket_id": ticket_id,
"status": STATUS_SUBMITTED,
"proof_type": proof_type,
"proof_path": proof_path,
"staff_note": staff_note
}), 200
@app.route("/tickets/<int:ticket_id>/approve", methods=["PATCH"])
def approve_ticket(ticket_id):
if current_user_role() != "manager":
return error_response("Only manager can approve tickets", 403)
conn = get_db_connection()
ticket = conn.execute("""
SELECT *
FROM tickets
WHERE id = ?
""", (ticket_id,)).fetchone()
if ticket is None:
conn.close()
return error_response("Ticket not found", 404)
if ticket["status"] != STATUS_SUBMITTED:
conn.close()
return error_response("Only submitted tickets can be approved", 400)
conn.execute("""
UPDATE tickets
SET status = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""", (
STATUS_APPROVED,
ticket_id
))
conn.commit()
conn.close()
add_audit_log(
ticket_id=ticket_id,
action="approved",
actor=current_user_display_name(),
details="Ticket approved by manager"
)
return jsonify({
"message": "Ticket approved successfully",
"ticket_id": ticket_id,
"status": STATUS_APPROVED
}), 200
@app.route("/tickets/<int:ticket_id>/close", methods=["PATCH"])
def close_ticket(ticket_id):
if current_user_role() != "manager":
return error_response("Only manager can close tickets", 403)
data = request.get_json() or {}
manager_comment = data.get("manager_comment")
conn = get_db_connection()
ticket = conn.execute("""
SELECT *
FROM tickets
WHERE id = ?
""", (ticket_id,)).fetchone()
if ticket is None:
conn.close()
return error_response("Ticket not found", 404)
if ticket["status"] != STATUS_APPROVED:
conn.close()
return error_response("Only approved tickets can be closed", 400)
final_comment = manager_comment or ticket["manager_comment"]
conn.execute("""
UPDATE tickets
SET status = ?,
manager_comment = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""", (
STATUS_CLOSED,
final_comment,
ticket_id
))
conn.commit()
conn.close()
add_audit_log(
ticket_id=ticket_id,
action="closed",
actor=current_user_display_name(),
details=final_comment or "Ticket closed by manager"
)
return jsonify({
"message": "Ticket closed successfully",
"ticket_id": ticket_id,