Skip to content

Commit

Permalink
clean up duplicate resident code and update warning message
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor Bechthold committed Feb 5, 2024
1 parent 6468545 commit 9461e67
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 60 deletions.
48 changes: 8 additions & 40 deletions backend/app/rest/residents_routes.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
50 changes: 31 additions & 19 deletions backend/app/services/implementations/residents_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
Expand Down

0 comments on commit 9461e67

Please sign in to comment.