Skip to content

Commit

Permalink
Merge branch 'user-dashboard' of https://github.com/uwblueprint/feedi…
Browse files Browse the repository at this point in the history
…ng-canadian-kids into user-dashboard
  • Loading branch information
sophie-wjyang committed Jul 18, 2023
2 parents 5ffb29d + 76c9fda commit 884fd2d
Show file tree
Hide file tree
Showing 35 changed files with 2,623 additions and 1,430 deletions.
4 changes: 3 additions & 1 deletion backend/app/graphql/error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def do_resolve(*args, **kwargs):
except Exception as error:
logger.error(error, exc_info=error)
raise GraphQLError(
message="Unexpected error.",
# Returning the error the the FE helps with development,
# but may be a security vulnerability
message="Unexpected error: " + str(error),
original_error=None,
)

Expand Down
32 changes: 32 additions & 0 deletions backend/app/graphql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,27 @@ class Contact(graphene.ObjectType):
phone = graphene.String()


class ASPInfo(graphene.ObjectType):
num_kids = graphene.Int()


class DonorInfo(graphene.ObjectType):
type = graphene.String()
tags = graphene.List(graphene.String)


class RoleInfo(graphene.ObjectType):
asp_info = graphene.Field(ASPInfo)
donor_info = graphene.Field(DonorInfo)


class UserInfo(graphene.ObjectType):
email = graphene.String()
organization_address = graphene.String()
organization_name = graphene.String()
organization_desc = graphene.String()
role = graphene.String()
role_info = graphene.Field(RoleInfo)
primary_contact = graphene.Field(Contact)
onsite_contacts = graphene.List(Contact)

Expand All @@ -49,11 +65,27 @@ class ContactInput(graphene.InputObjectType):
phone = graphene.String(required=True)


class ASPInfoInput(graphene.InputObjectType):
num_kids = graphene.Int()


class DonorInfoInput(graphene.InputObjectType):
type = graphene.String()
tags = graphene.List(graphene.String)


class RoleInfoInput(graphene.InputObjectType):
asp_info = graphene.Field(ASPInfoInput)
donor_info = graphene.Field(DonorInfoInput)


class UserInfoInput(graphene.InputObjectType):
email = graphene.String(required=True)
organization_address = graphene.String(required=True)
organization_name = graphene.String(required=True)
organization_desc = graphene.String(required=True)
role = graphene.String(required=True)
role_info = graphene.Field(RoleInfoInput)
primary_contact = graphene.Field(ContactInput, required=True)
onsite_contacts = graphene.List(ContactInput, required=True)

Expand Down
25 changes: 20 additions & 5 deletions backend/app/models/user_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@
USERINFO_ROLE_ASP = "ASP"
USERINFO_ROLES = [USERINFO_ROLE_ADMIN, USERINFO_ROLE_DONOR, USERINFO_ROLE_ASP]

DONOR_TYPE_RESTAURANT = "Restaurant"
DONOR_TYPE_INDIVIDUAL = "Individual"
DONOR_TYPES = [DONOR_TYPE_RESTAURANT, DONOR_TYPE_INDIVIDUAL]


class ASPInfo(mg.EmbeddedDocument):
num_kids = mg.IntField(required=True)


class DonorInfo(mg.EmbeddedDocument):
type = mg.StringField(choices=DONOR_TYPES)
tags = mg.ListField(mg.StringField())


class RoleInfo(mg.EmbeddedDocument):
asp_info = mg.EmbeddedDocumentField(ASPInfo)
donor_info = mg.EmbeddedDocumentField(DonorInfo)


class Contact(mg.EmbeddedDocument):
name = mg.StringField(required=True)
Expand All @@ -17,13 +35,10 @@ class UserInfo(mg.EmbeddedDocument):
email = mg.StringField(required=True, unique=True)
organization_address = mg.StringField(required=True)
organization_name = mg.StringField(required=True)
organization_desc = mg.StringField(required=True)
role = mg.StringField(choices=USERINFO_ROLES, required=True)
role_info = mg.EmbeddedDocumentField(RoleInfo)
primary_contact = mg.EmbeddedDocumentField(Contact, required=True)
onsite_contacts = mg.EmbeddedDocumentListField(Contact, required=True)

meta = {"allow_inheritance": True}


class ASPInfo(UserInfo):
priority: mg.IntField(required=True)
locations = mg.ListField(mg.PointField(required=True))
40 changes: 38 additions & 2 deletions backend/app/resources/validate_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from ..models.user_info import USERINFO_ROLES
from ..models.user_info import (
USERINFO_ROLES,
USERINFO_ROLE_ASP,
USERINFO_ROLE_ADMIN,
)


def validate_contact(contact, contact_str, error_list):
Expand All @@ -20,12 +24,38 @@ def validate_contact(contact, contact_str, error_list):
return error_list


def validate_role_info(role, role_info, role_info_str, error_list):
if not isinstance(role_info, dict) and role != USERINFO_ROLE_ADMIN:
error_list.append(f"The {role_info_str} supplied is not a dict.")
return error_list

asp_info_fields = ["num_kids"]
if role == USERINFO_ROLE_ASP:
for field in asp_info_fields:
role_info = role_info["asp_info"]
print(f"validating role_info {role_info} for field {field}")
if field not in role_info:
error_list.append(
f'The {role_info_str} supplied does not have field "{field}".'
)
elif type(role_info[field]) is not int:
error_list.append(
f'The field "{field}" in {role_info_str} is not a string.'
)
elif field == "num_kids" and role_info["num_kids"] < 0:
error_list.append("num_kids must be greater than or equal to zero.")
# TODO: Add donor info validation once meal donor schema is finalized
return error_list


def validate_userinfo(userinfo, error_list):
userinfo_fields = [
"email",
"organization_address",
"organization_name",
"organization_desc",
"role",
"role_info",
"primary_contact",
"onsite_contacts",
]
Expand All @@ -34,7 +64,9 @@ def validate_userinfo(userinfo, error_list):
return error_list

for field in userinfo_fields:
if field not in userinfo:
if field not in userinfo and (
field != "role_info" or userinfo["role"] != USERINFO_ROLE_ADMIN
):
error_list.append(f'The info supplied does not have field "{field}".')
for key, val in userinfo.items():
if key == "primary_contact":
Expand All @@ -47,6 +79,10 @@ def validate_userinfo(userinfo, error_list):
error_list = validate_contact(
val[i], f"index {i} of info.onsite_contacts", error_list
)
elif key == "role_info":
error_list = validate_role_info(
userinfo["role"], val, "info.role_info", error_list
)
elif type(val) is not str:
error_list.append(f"The field info.{key} supplied is not a string.")
elif val == "":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def create_onboarding_request(self, userInfo):
email=userInfo.email,
organization_address=userInfo.organization_address,
organization_name=userInfo.organization_name,
organization_desc=userInfo.organization_desc,
role=userInfo.role,
role_info=userInfo.role_info,
primary_contact=userInfo.primary_contact,
onsite_contacts=userInfo.onsite_contacts,
)
Expand Down
34 changes: 34 additions & 0 deletions backend/tests/graphql/mock_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
"email": "[email protected]",
"organization_address": "123 Anywhere Street",
"organization_name": "Test1 Org",
"organization_desc": "Testing123",
"role": "ASP",
"role_info": {
"asp_info": {
"num_kids": 4,
},
"donor_info": None,
},
"primary_contact": {
"name": "Jessie",
"phone": "123456",
Expand All @@ -18,7 +25,14 @@
"email": "[email protected]",
"organizationAddress": "123 Anywhere Street",
"organizationName": "Test1 Org",
"organizationDesc": "Testing123",
"role": "ASP",
"roleInfo": {
"aspInfo": {
"numKids": 4,
},
"donorInfo": None,
},
"primaryContact": {
"name": "Jessie",
"phone": "123456",
Expand All @@ -34,7 +48,15 @@
"email": "[email protected]",
"organization_address": "456 Anywhere Street",
"organization_name": "Test2 Org",
"organization_desc": "Testing123",
"role": "Donor",
"role_info": {
"asp_info": None,
"donor_info": {
"type": "Restaurant",
"tags": ["Halal", "Vegan"],
},
},
"primary_contact": {
"name": "Mr. Goose",
"phone": "98765",
Expand All @@ -50,7 +72,15 @@
"email": "[email protected]",
"organizationAddress": "456 Anywhere Street",
"organizationName": "Test2 Org",
"organizationDesc": "Testing123",
"role": "Donor",
"roleInfo": {
"aspInfo": None,
"donorInfo": {
"type": "Restaurant",
"tags": ["Halal", "Vegan"],
},
},
"primaryContact": {
"name": "Mr. Goose",
"phone": "98765",
Expand All @@ -66,7 +96,9 @@
"email": "[email protected]",
"organization_address": "789 Anywhere Street",
"organization_name": "Test3 Org",
"organization_desc": "Testing 123",
"role": "Admin",
"role_info": None,
"primary_contact": {
"name": "Anon ymous",
"phone": "13579",
Expand All @@ -82,7 +114,9 @@
"email": "[email protected]",
"organizationAddress": "789 Anywhere Street",
"organizationName": "Test3 Org",
"organizationDesc": "Testing 123",
"role": "Admin",
"roleInfo": None,
"primaryContact": {
"name": "Anon ymous",
"phone": "13579",
Expand Down
Loading

0 comments on commit 884fd2d

Please sign in to comment.