Skip to content

Commit

Permalink
Fix issues from deleted onsite contacts in old meal requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
shahanneda committed Sep 1, 2024
1 parent fefdb5a commit f43d94d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 34 deletions.
2 changes: 1 addition & 1 deletion backend/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

def create_app(config_name):
print("Environment is", config_name)
if config_name != "testing":
if config_name != "testing" and config_name != "development":
sentry_sdk.init(
dsn="https://85a9bf2fc71b287cc4e60cb9f918f034@o4507682847850496.ingest.us.sentry.io/4507801405227008",
# Set traces_sample_rate to 1.0 to capture 100%
Expand Down
17 changes: 11 additions & 6 deletions backend/app/models/meal_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ def validate_onsite_contacts(self):
f"onsite contact {contact.id} not found or not associated with the donor organization"
)

def _get_expanded_contacts_list(self, contacts):
expanded_contacts = []
for contact in contacts:
# If a contact is deleted, it won't have the `to_serializable_dict` method, so we won't add it to the results
if not hasattr(contact, "to_serializable_dict"):
continue
expanded_contacts.append(contact.to_serializable_dict())
return expanded_contacts

def to_serializable_dict(self):
"""
Returns a dict representation of the document that is JSON serializable
Expand All @@ -98,15 +107,11 @@ def to_serializable_dict(self):
meal_request_dict = self.to_mongo().to_dict()
id = meal_request_dict.pop("_id", None)
meal_request_dict["id"] = str(id)

contacts = [contact.to_serializable_dict() for contact in self.onsite_contacts]
contacts = self._get_expanded_contacts_list(self.onsite_contacts)
meal_request_dict["onsite_contacts"] = contacts

if self.donation_info and self.donation_info.donor_onsite_contacts:
contacts = [
contact.to_serializable_dict()
for contact in self.donation_info.donor_onsite_contacts
]
contacts = self._get_expanded_contacts_list(self.donation_info.donor_onsite_contacts)
meal_request_dict["donation_info"]["donor_onsite_contacts"] = contacts

return meal_request_dict
Expand Down
6 changes: 3 additions & 3 deletions backend/app/services/implementations/meal_request_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def commit_to_meal_request(

meal_request_dtos.append(meal_request.to_dto())
meal_request.save()
donor.info.involved_meal_requests += 1
donor.info.involved_meal_requests += len(meal_request_dtos)
donor.save()

return meal_request_dtos
Expand All @@ -238,7 +238,7 @@ def cancel_donation(self, meal_request_id: str) -> MealRequestDTO:
)

donor = User.objects(id=meal_request.donation_info.donor.id).first()
donor.info.involved_meal_requests -= 1
donor.info.involved_meal_requests = max(donor.info.involved_meal_requests - 1, 0)
donor.save()

meal_request.donation_info = None
Expand All @@ -260,7 +260,7 @@ def delete_meal_request(self, meal_request_id: str) -> MealRequestDTO:
raise Exception(f'Meal request "{meal_request_id}" not found')

requestor = User.objects(id=meal_request.requestor.id).first()
requestor.info.involved_meal_requests -= 1
requestor.info.involved_meal_requests = max(requestor.info.involved_meal_requests - 1, 0)
requestor.save()

meal_request.delete()
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/components/common/OnsiteContactSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ const OnsiteContactSection = ({
}: OnsiteContactSectionProps): React.ReactElement => {
const isWebView = useIsWebView();

const [showCreateModal, setShowCreateModal] = useState(false);

if (isWebView) {
return (
Expand Down Expand Up @@ -554,10 +553,7 @@ const OnsiteContactSection = ({
email: "",
},
]);
return;
}

setShowCreateModal(true);
}}
>
+ Add another contact
Expand Down
40 changes: 20 additions & 20 deletions frontend/src/pages/EditMealRequestForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -268,27 +268,29 @@ const EditMealRequestForm = ({

// For validation
const validateData = () => {
if (
numberOfMeals <= 0 ||
onsiteContacts.length === 0 ||
onsiteContacts.some(
(contact) =>
!contact ||
contact.name === "" ||
contact.email === "" ||
contact.phone === "",
)
) {
setAttemptedSubmit(true);
return false;
}

if (isEditDonation) {
if (mealDescription === "") {
setAttemptedSubmit(true);
return false;
}
}
if (mealDonorOnsiteContacts.length < 0) {
setAttemptedSubmit(true);
return false;
}
} else if (
numberOfMeals <= 0 ||
onsiteContacts.length < 0 ||
onsiteContacts.some(
(contact) =>
!contact ||
contact.name === "" ||
contact.email === "" ||
contact.phone === "",
)
) {
setAttemptedSubmit(true);
return false;
}

setAttemptedSubmit(false);
return true;
Expand Down Expand Up @@ -426,12 +428,10 @@ const EditMealRequestForm = ({
Food Description
</FormLabel>
<FormHelperText fontSize="xs" my={2}>
Please describe a typical meal you can provide (this can be
modified later)
Please describe a typical meal you can provide:
</FormHelperText>
<Input
// TODO should we change this placeholder?
placeholder="Ex. 40 mac and cheeses with 9 gluten free ones. Also will donate 30 bags of cheetos."
placeholder="Ex. Will be bringing 5 large cheese pizza's."
value={mealDescription}
onChange={(e) => setMealDescription(e.target.value)}
ref={initialFocusRef}
Expand Down

0 comments on commit f43d94d

Please sign in to comment.