File size: 3,299 Bytes
b7ed26f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ea4a10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
CRUD operations for student Clearance Statuses.
"""
from sqlalchemy.orm import Session
from fastapi import HTTPException, status

from src import models

def get_clearance_statuses_by_student_id(db: Session, student_id: str) -> list[models.ClearanceStatus]:
    """
    Fetches all existing clearance status records for a given student.
    """
    return db.query(models.ClearanceStatus).filter(models.ClearanceStatus.student_id == student_id).all()

def update_clearance_status(
    db: Session,
    student_id: str,
    department: models.ClearanceDepartment,
    new_status: models.ClearanceStatusEnum,
    remarks: str,
    cleared_by_user_id: int
) -> models.ClearanceStatus:
    """
    Updates or creates a clearance status for a student in a specific department.
    """
    student = db.query(models.Student).filter(models.Student.student_id == student_id).first()
    if not student:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"Student with ID '{student_id}' not found."
        )

    existing_status = db.query(models.ClearanceStatus).filter(
        models.ClearanceStatus.student_id == student_id,
        models.ClearanceStatus.department == department
    ).first()

    if existing_status:
        existing_status.status = new_status
        existing_status.remarks = remarks
        existing_status.cleared_by = cleared_by_user_id
        db_status = existing_status
    else:
        db_status = models.ClearanceStatus(
            student_id=student_id,
            department=department,
            status=new_status,
            remarks=remarks,
            cleared_by=cleared_by_user_id
        )
        db.add(db_status)

    db.commit()
    db.refresh(db_status)
    return db_status

def delete_clearance_status(
    db: Session,
    student_id: str,
    department: models.ClearanceDepartment
) -> models.ClearanceStatus | None:
    """
    Deletes a specific clearance status record for a student.

    This effectively resets the status for that department to the default state.
    Returns the deleted object if found, otherwise returns None.
    """
    status_to_delete = db.query(models.ClearanceStatus).filter(
        models.ClearanceStatus.student_id == student_id,
        models.ClearanceStatus.department == department
    ).first()

    if status_to_delete:
        db.delete(status_to_delete)
        db.commit()

    return status_to_delete


def get_all_clearance_status(db: Session) -> list[models.ClearanceStatus]:
    """
    Retrieves all clearance statuses from the database.
    
    This function is useful for administrative purposes, allowing staff to view
    all clearance records across all students and departments.
    """
    return db.query(models.ClearanceStatus).all()

def get_student_clearance_status(
    db: Session,
    student_id: str,
    department: models.ClearanceDepartment
) -> models.ClearanceStatus | None:
    """
    Retrieves the clearance status for a specific student in a specific department.
    
    Returns None if no status exists for that student and department.
    """
    return db.query(models.ClearanceStatus).filter(
        models.ClearanceStatus.student_id == student_id,
        models.ClearanceStatus.department == department
    ).first()