Skip to content

Commit

Permalink
Update food_request_group and food_request model (#82)
Browse files Browse the repository at this point in the history
* updated schema for food request models and corresponding GraphQL mutation

---------

Co-authored-by: HeetShah <[email protected]>
Co-authored-by: Colin Toft <[email protected]>
Co-authored-by: Shahan Neda <[email protected]>
  • Loading branch information
4 people authored Jul 22, 2023
1 parent 3601cb6 commit 7e9f431
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 157 deletions.
51 changes: 40 additions & 11 deletions backend/app/graphql/food_request.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import graphene

from .types import (
ContactInput,
Mutation,
MutationList,
)
Expand All @@ -14,45 +15,73 @@ class MealRequestTypeInput(graphene.InputObjectType):
portions = graphene.Int(required=True)


class CreateFoodRequestDatesInput(graphene.InputObjectType):
date = graphene.DateTime(required=True)
meal_types = graphene.List(MealRequestTypeInput, required=True)


# Response Types
class MealRequestTypeResponse(graphene.ObjectType):
tags = graphene.List(graphene.String, required=True)
portions = graphene.Int(required=True)
dietary_restrictions = graphene.String(required=True)
meal_suggestions = graphene.String(required=True)


class CreateFoodRequestResponse(graphene.ObjectType):
id = graphene.ID()
target_fulfillment_date = graphene.DateTime()
meal_types = graphene.List(MealRequestTypeResponse)
donation_date = graphene.Date()
status = graphene.String()


class CreateFoodRequestGroupResponse(graphene.ObjectType):
id = graphene.ID()
description = graphene.String()
requests = graphene.List(CreateFoodRequestResponse)
meal_info = graphene.Field(MealRequestTypeResponse)
status = graphene.String()


class MealTypeInput(graphene.InputObjectType):
portions = graphene.Int(required=True)
dietary_restrictions = graphene.String(required=True)
meal_suggestions = graphene.String(required=True)


# Mutations
class CreateFoodRequestGroup(Mutation):
class Arguments:
description = graphene.String(required=True)
requestor = graphene.ID(required=True)
commitments = graphene.List(CreateFoodRequestDatesInput, required=True)
# request_dates is a list of dates
request_dates = graphene.List(graphene.Date, required=True)

meal_info = MealTypeInput(required=True)
drop_off_time = graphene.Time(required=True)
drop_off_location = graphene.String(required=True)
delivery_instructions = graphene.String()
onsite_staff = graphene.List(ContactInput, required=True)

# return values
food_request_group = graphene.Field(CreateFoodRequestGroupResponse)

def mutate(self, info, description, requestor, commitments):
def mutate(
self,
info,
description,
requestor,
request_dates,
meal_info,
drop_off_time,
drop_off_location,
delivery_instructions,
onsite_staff,
):
result = services["food_request_service"].create_food_request_group(
description=description, requestor=requestor, commitments=commitments
description=description,
requestor=requestor,
request_dates=request_dates,
meal_info=meal_info,
drop_off_time=drop_off_time,
drop_off_location=drop_off_location,
delivery_instructions=delivery_instructions,
onsite_staff=onsite_staff,
)

return CreateFoodRequestGroup(food_request_group=result)


Expand Down
13 changes: 6 additions & 7 deletions backend/app/models/food_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@


class MealType(mg.EmbeddedDocument):
tags = mg.ListField(mg.StringField(required=True))
portions = mg.IntField(required=True)
portions_fulfilled = mg.IntField(required=True, default=0)
dietary_restrictions = mg.StringField(required=True, default="No restrictions")
meal_suggestions = mg.StringField(required=True)


class FoodRequest(mg.EmbeddedDocument):
_id = mg.ObjectIdField(required=True, default=ObjectId)
donor = mg.ObjectIdField()
target_fulfillment_date = mg.DateTimeField(required=True)
actual_fulfillment_date = mg.DateTimeField()
meal_types = mg.EmbeddedDocumentListField(MealType, default=list)

# The date that the food is being
donation_date = mg.DateField(required=True)
"""
Open: Request has not been completely fulfilled
Fulfilled: All meal types have been fulfilled
Expand All @@ -23,6 +20,8 @@ class FoodRequest(mg.EmbeddedDocument):
status = mg.StringField(
choices=["Open", "Fulfilled", "Cancelled"], required=True, default="Open"
)
donor_id = mg.ObjectIdField(required=False)
commitment_date = mg.DateTimeField(required=False)

def to_serializable_dict(self):
"""
Expand Down
21 changes: 16 additions & 5 deletions backend/app/models/food_request_group.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import mongoengine as mg
from .food_request import FoodRequest
from .food_request import FoodRequest, MealType
from datetime import datetime
from .user_info import Contact
from bson.objectid import ObjectId


class FoodRequestGroup(mg.Document):
_id = mg.ObjectIdField(required=True, default=ObjectId)
description = mg.StringField(required=True)
requestor = mg.ObjectIdField() # The ASP making the request

# TODO: make this required=True when we have users populated
requestor = mg.ObjectIdField()
requests = mg.EmbeddedDocumentListField(FoodRequest, default=list)

date_created = mg.DateTimeField(required=True, default=datetime.utcnow)
date_updated = mg.DateTimeField(required=True, default=datetime.utcnow)

"""
Open: At least one FoodRequest is open
Fulfilled: All FoodRequests are fulfilled
Expand All @@ -22,6 +22,17 @@ class FoodRequestGroup(mg.Document):
choices=["Open", "Fulfilled", "Cancelled"], required=True, default="Open"
)

# Donation Details
meal_info = mg.EmbeddedDocumentField(MealType, required=True)
drop_off_time = mg.DateTimeField(required=True)
drop_off_location = mg.StringField(required=True)
delivery_instructions = mg.StringField(required=True)
onsite_staff = mg.EmbeddedDocumentListField(Contact, required=True)

# Timestamps
date_created = mg.DateTimeField(required=True, default=datetime.utcnow)
date_updated = mg.DateTimeField(required=True, default=datetime.utcnow)

def to_serializable_dict(self):
"""
Returns a dict representation of the document that is JSON serializable
Expand Down
44 changes: 25 additions & 19 deletions backend/app/services/implementations/food_request_service.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
from ...models.food_request_group import FoodRequestGroup
from ...models.food_request import FoodRequest, MealType
from ...models.food_request import FoodRequest
from ..interfaces.food_request_service import IFoodRequestService
from datetime import datetime


class FoodRequestService(IFoodRequestService):
def __init__(self, logger):
self.logger = logger

def create_food_request_group(self, description, requestor, commitments):
def create_food_request_group(
self,
description,
requestor,
request_dates,
meal_info,
drop_off_time,
drop_off_location,
delivery_instructions,
onsite_staff,
):
try:
# Create FoodRequestGroup
new_food_request_group = FoodRequestGroup(
description=description,
# TODO: uncomment when we have users populated
# requestor=requestor,
status="Open",
requestor=requestor,
requests=[
FoodRequest(donation_date=request_date)
for request_date in request_dates
],
meal_info=meal_info,
# Convert the time into a datetime object (date does not matter here)
drop_off_time=datetime.combine(datetime.today().date(), drop_off_time),
drop_off_location=drop_off_location,
delivery_instructions=delivery_instructions,
onsite_staff=onsite_staff,
)

# Create FoodRequests
for commitment in commitments:
meal_types = [
MealType(tags=meal_type.tags, portions=meal_type.portions)
for meal_type in commitment.meal_types
]
new_food_request = FoodRequest(
target_fulfillment_date=commitment.date,
status="Open",
meal_types=meal_types,
)
new_food_request_group.requests.append(new_food_request)

new_food_request_group.save()
print(new_food_request_group.to_serializable_dict())
except Exception as error:
self.logger.error(str(error))
raise error
Expand Down
Loading

0 comments on commit 7e9f431

Please sign in to comment.