-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unique Resident IDs #200
Unique Resident IDs #200
Changes from 9 commits
595a274
81e7b4f
7dc12cf
6434f10
cd32647
292fac6
12175d9
e5f410b
446034c
43c4bd5
d5aeb80
6591ed1
63eaf16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,16 @@ def add_resident(): | |
400, | ||
) | ||
|
||
# Check for the existence of a resident prior to adding them | ||
fmt_resident_id = resident.get("initial") + str(resident.get("room_num")) | ||
try: | ||
res = residents_service.get_residents(False, 1, 10, fmt_resident_id) | ||
if len(res["residents"]) > 0: | ||
return jsonify({"error": "Resident already 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 | ||
|
@@ -41,6 +51,17 @@ def update_resident(resident_id): | |
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 = updated_resident.get("initial") + str(updated_resident.get("room_num")) | ||
try: | ||
res = residents_service.get_residents(False, 1, 10, fmt_resident_id) | ||
if len(res["residents"]) == 1 and res["residents"][0]["id"] != resident_id: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When editing a resident, there are two cases for which the result of get_residents could be
We want case #2 to be marked as INVALID but case #1 to be VALID. As such, our checking logic must check for:
|
||
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( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import { | |
CountResidentsResponse, | ||
CreateResidentParams, | ||
EditResidentParams, | ||
ErrorResponse, | ||
} from "../types/ResidentTypes"; | ||
import { getLocalStorageObjProperty } from "../utils/LocalStorageUtils"; | ||
import baseAPIClient from "./BaseAPIClient"; | ||
|
@@ -60,7 +61,7 @@ const createResident = async ({ | |
roomNum, | ||
dateJoined, | ||
buildingId, | ||
}: CreateResidentParams): Promise<boolean> => { | ||
}: CreateResidentParams): Promise<boolean | ErrorResponse> => { | ||
try { | ||
const bearerToken = `Bearer ${getLocalStorageObjProperty( | ||
AUTHENTICATED_USER_KEY, | ||
|
@@ -73,6 +74,13 @@ const createResident = async ({ | |
); | ||
return true; | ||
} catch (error) { | ||
const axiosErr = (error as any) as AxiosError; | ||
|
||
if (axiosErr.response && axiosErr.response.status === 409) { | ||
return { | ||
errMessage: "Resident with the specified user ID already exists." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, we're propagating the error message displayed in the toast through the API client |
||
}; | ||
} | ||
return false; | ||
} | ||
}; | ||
|
@@ -103,7 +111,7 @@ const editResident = async ({ | |
dateJoined, | ||
buildingId, | ||
dateLeft, | ||
}: EditResidentParams): Promise<boolean> => { | ||
}: EditResidentParams): Promise<boolean | ErrorResponse> => { | ||
try { | ||
const bearerToken = `Bearer ${getLocalStorageObjProperty( | ||
AUTHENTICATED_USER_KEY, | ||
|
@@ -116,6 +124,13 @@ const editResident = async ({ | |
); | ||
return true; | ||
} catch (error) { | ||
const axiosErr = (error as any) as AxiosError; | ||
|
||
if (axiosErr.response && axiosErr.response.status === 409) { | ||
return { | ||
errMessage: "Resident with the specified user ID already exists." | ||
}; | ||
} | ||
return false; | ||
} | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,11 +24,13 @@ import { | |
import { AddIcon } from "@chakra-ui/icons"; | ||
import { SingleDatepicker } from "chakra-dayzed-datepicker"; | ||
import { Col, Row } from "react-bootstrap"; | ||
import CreateToast from "../common/Toasts"; | ||
|
||
import selectStyle from "../../theme/forms/selectStyles"; | ||
import { singleDatePickerStyle } from "../../theme/forms/datePickerStyles"; | ||
import ResidentAPIClient from "../../APIClients/ResidentAPIClient"; | ||
import { convertToString } from "../../helper/dateHelpers"; | ||
import isResidentErrorResponse from "../../helper/residentError"; | ||
|
||
type Props = { | ||
getRecords: (pageNumber: number) => Promise<void>; | ||
|
@@ -61,17 +63,30 @@ const CreateResident = ({ | |
const [isOpen, setIsOpen] = useState(false); | ||
const [showAlert, setShowAlert] = useState(false); | ||
|
||
const newToast = CreateToast(); | ||
|
||
const ROOM_ERROR_TEXT = `Room Number is required and must only contain numbers.`; | ||
const addResident = async () => { | ||
await ResidentAPIClient.createResident({ | ||
const res = await ResidentAPIClient.createResident({ | ||
initial: initials.toUpperCase(), | ||
roomNum: parseInt(roomNumber, 10), | ||
dateJoined: convertToString(moveInDate), | ||
buildingId, | ||
}); | ||
getRecords(1); | ||
countResidents(); | ||
setUserPageNum(1); | ||
|
||
if (isResidentErrorResponse(res)) { | ||
newToast( | ||
"Error creating resident", | ||
res.errMessage, | ||
"error" | ||
) | ||
} | ||
else if (res) { | ||
getRecords(1); | ||
countResidents(); | ||
setUserPageNum(1); | ||
setShowAlert(true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We move the call to here since the showAlert state controls the visibility of a |
||
} | ||
}; | ||
|
||
const handleInitialsChange = (e: { target: { value: unknown } }) => { | ||
|
@@ -146,7 +161,6 @@ const CreateResident = ({ | |
|
||
addResident(); | ||
setIsOpen(false); | ||
setShowAlert(true); | ||
}; | ||
|
||
// Timer to remove alert | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { | ||
ErrorResponse, | ||
} from "../types/ResidentTypes"; | ||
|
||
const isResidentErrorResponse = (res: boolean | ErrorResponse) : res is ErrorResponse => { | ||
return (typeof res !== 'boolean' && 'errMessage' in res); | ||
} | ||
|
||
export default isResidentErrorResponse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When adding a new resident, we simply want to check for a result length greater than 0 (if there's even one other resident that has the same ID, then this is a duplicate resident => reject)