diff --git a/backend/app/rest/residents_routes.py b/backend/app/rest/residents_routes.py index 4c0b940d..f6d282a3 100644 --- a/backend/app/rest/residents_routes.py +++ b/backend/app/rest/residents_routes.py @@ -1,7 +1,7 @@ from flask import Blueprint, current_app, jsonify, request from ..middlewares.auth import require_authorization_by_role from ..services.implementations.residents_service import ResidentsService -from datetime import datetime +from ..utilities.exceptions.duplicate_entity_exceptions import DuplicateResidentException import json residents_service = ResidentsService(current_app.logger) @@ -20,29 +20,13 @@ def add_resident(): jsonify({"date_left_error": "date_left cannot be less than date_joined"}), 400, ) - - # Check for the existence of a resident prior to adding them - fmt_resident_id = resident.get("initial") + resident.get("room_num") - try: - existing_resident = residents_service.get_resident_by_id(fmt_resident_id) - if existing_resident: - return ( - jsonify( - { - "error": "Resident with ID {fmt_resident_id} already exists.".format( - fmt_resident_id=fmt_resident_id - ) - } - ), - 409, - ) - except Exception as e: - error_message = getattr(e, "message", None) - return jsonify({"error": (error_message if error_message else str(e))}), 500 - try: created_resident = residents_service.add_resident(resident) return jsonify(created_resident), 201 + + except DuplicateResidentException as e: + error_message = getattr(e, "message", None) + return jsonify({"error": (error_message if error_message else str(e))}), 409 except Exception as e: error_message = getattr(e, "message", None) return jsonify({"error": (error_message if error_message else str(e))}), 500 @@ -61,25 +45,6 @@ def update_resident(resident_id): 400, ) - # Check for the existence of a resident prior to adding them - fmt_resident_id = updated_resident.get("initial") + updated_resident.get("room_num") - try: - existing_resident = residents_service.get_resident_by_id(fmt_resident_id) - if existing_resident and existing_resident["id"] != resident_id: - return ( - jsonify( - { - "error": "Resident with ID {fmt_resident_id} already exists.".format( - fmt_resident_id=fmt_resident_id - ) - } - ), - 409, - ) - except Exception as e: - error_message = getattr(e, "message", None) - return jsonify({"error": (error_message if error_message else str(e))}), 500 - try: updated_resident = residents_service.update_resident( resident_id, updated_resident @@ -94,6 +59,9 @@ def update_resident(resident_id): ), 201, ) + except DuplicateResidentException as e: + error_message = getattr(e, "message", None) + return jsonify({"error": (error_message if error_message else str(e))}), 409 except Exception as e: error_message = getattr(e, "message", None) return jsonify({"error": (error_message if error_message else str(e))}), 500 diff --git a/backend/app/services/implementations/residents_service.py b/backend/app/services/implementations/residents_service.py index 9436c913..4c85915c 100644 --- a/backend/app/services/implementations/residents_service.py +++ b/backend/app/services/implementations/residents_service.py @@ -4,6 +4,9 @@ from ...models.log_record_residents import LogRecordResidents from ...models.buildings import Buildings from ...models import db +from ...utilities.exceptions.duplicate_entity_exceptions import ( + DuplicateResidentException, +) from datetime import datetime from sqlalchemy.sql.expression import or_, and_ from pytz import timezone @@ -135,28 +138,37 @@ def add_resident(self, resident): db.session.add(new_resident) db.session.commit() return resident - except Exception as postgres_error: - raise postgres_error + except Exception as e: + if type(e).__name__ == "IntegrityError": + raise DuplicateResidentException(resident["initial"] + resident["room_num"]) + else: + raise e def update_resident(self, resident_id, updated_resident): - if "date_left" in updated_resident: - create_update_resident = Residents.query.filter_by(id=resident_id).update( - { - Residents.date_left: updated_resident["date_left"], - **updated_resident, - } - ) - else: - create_update_resident = Residents.query.filter_by(id=resident_id).update( - {Residents.date_left: None, **updated_resident} - ) - if not create_update_resident: - raise Exception( - "Resident with id {resident_id} not found".format( - resident_id=resident_id + try: + if "date_left" in updated_resident: + create_update_resident = Residents.query.filter_by(id=resident_id).update( + { + Residents.date_left: updated_resident["date_left"], + **updated_resident, + } ) - ) - db.session.commit() + else: + create_update_resident = Residents.query.filter_by(id=resident_id).update( + {Residents.date_left: None, **updated_resident} + ) + if not create_update_resident: + raise Exception( + "Resident with id {resident_id} not found".format( + resident_id=resident_id + ) + ) + db.session.commit() + except Exception as e: + if type(e).__name__ == "IntegrityError": + raise DuplicateResidentException(updated_resident["initial"] + updated_resident["room_num"]) + else: + raise e def delete_resident(self, resident_id): resident_log_records = LogRecordResidents.query.filter_by( diff --git a/backend/app/utilities/exceptions/duplicate_entity_exceptions.py b/backend/app/utilities/exceptions/duplicate_entity_exceptions.py index 02334dc6..d6f86d25 100644 --- a/backend/app/utilities/exceptions/duplicate_entity_exceptions.py +++ b/backend/app/utilities/exceptions/duplicate_entity_exceptions.py @@ -16,3 +16,12 @@ class DuplicateUserException(Exception): def __init__(self, email): message = f"User with email {email} already exists." super().__init__(message) + +class DuplicateResidentException(Exception): + """ + Raised when an duplicate resident is encountered + """ + + def __init__(self, resident_id): + message = f"Resident with ID {resident_id} already exists." + super().__init__(message) diff --git a/frontend/src/components/pages/AdminControls/EmployeeDirectoryTable.tsx b/frontend/src/components/pages/AdminControls/EmployeeDirectoryTable.tsx index daa0baf0..86ce58ce 100644 --- a/frontend/src/components/pages/AdminControls/EmployeeDirectoryTable.tsx +++ b/frontend/src/components/pages/AdminControls/EmployeeDirectoryTable.tsx @@ -117,7 +117,7 @@ const EmployeeDirectoryTable = ({ const deactivateWarningMessage = (employeeId: number) => { if (authenticatedUser?.id === employeeId) { - return "Note: Deactivating your account will require you to login to the application again." + return "Note: Deactivating your account will log you out of the application." } return "" }