diff --git a/deployment/config.dev.py b/deployment/config.dev.py index 76f7ace..1c43cfa 100644 --- a/deployment/config.dev.py +++ b/deployment/config.dev.py @@ -8,7 +8,9 @@ DB_COLLECTIONS = { "users": "users", "magic links": "magicLinks", - "slack messages": "slackMessages" + "slack messages": "slackMessages", + "houses": "houses", + "teams": "teams" } SPARKPOST_KEY = os.getenv("DEVELOP_SPARKPOST_KEY", "") @@ -58,6 +60,7 @@ class GOOGLE_CAL: RESUME_BUCKET = os.getenv("DEVELOP_RESUME_BUCKET", "") WAIVER_BUCKET = os.getenv("DEVELOP_WAIVER_BUCKET", "") VACCINE_BUCKET = os.getenv("DEVELOP_VACCINE_BUCKET", "") +SPONSOR_BUCKET = os.getenv("DEVELOP_SPONSOR_BUCKET", "") # Json webtoken JWT_SECRET = os.getenv("DEVELOP_JWT_SECRET", "") JWT_ALGO = os.getenv("DEVELOP_JWT_ALGO", "") diff --git a/deployment/config.prod.py b/deployment/config.prod.py index 4aa0b7b..9b1587a 100644 --- a/deployment/config.prod.py +++ b/deployment/config.prod.py @@ -9,7 +9,9 @@ DB_COLLECTIONS = { "users": "users", "magic links": "magicLinks", - "slack messages": "slackMessages" + "slack messages": "slackMessages", + "houses": "houses", + "teams": "teams" } SPARKPOST_KEY = os.getenv("PRODUCTION_SPARKPOST_KEY", "") @@ -61,6 +63,7 @@ class GOOGLE_CAL: RESUME_BUCKET = os.getenv("PRODUCTION_RESUME_BUCKET", "") WAIVER_BUCKET = os.getenv("PRODUCTION_WAIVER_BUCKET", "") VACCINE_BUCKET = os.getenv("PRODUCTION_VACCINE_BUCKET", "") +SPONSOR_BUCKET = os.getenv("PRODUCTION_SPONSOR_BUCKET", "") # Json webtoken JWT_SECRET = os.getenv("PRODUCTION_JWT_SECRET", "") JWT_ALGO = os.getenv("PRODUCTION_JWT_ALGO", "") diff --git a/serverless.yml b/serverless.yml index 9c5b49c..d9a1bd0 100644 --- a/serverless.yml +++ b/serverless.yml @@ -302,3 +302,15 @@ functions: request: template: application/json: '$input.body' + + get-house-points: + handler: src/houses.get_all_points + events: + - http: + path: get-house_points + integration: lambda + method: get + cors: true + request: + template: + application/json: '$input.body' diff --git a/src/houses.py b/src/houses.py new file mode 100644 index 0000000..2207117 --- /dev/null +++ b/src/houses.py @@ -0,0 +1,18 @@ +from src.util import coll + +def get_all_points(event, context): + + # get house collection + houses = coll('houses') + + data = houses.find({}) + + results = { + "status": 200, + "houses": {} + } + + for doc in data: + results['houses'][doc['name']] = doc['points'] + + return results \ No newline at end of file diff --git a/src/qrscan.py b/src/qrscan.py index 4089f3e..32d587d 100644 --- a/src/qrscan.py +++ b/src/qrscan.py @@ -40,6 +40,7 @@ def qr_match(event, context, user=None): 'token': {'type': 'string'}, 'qr': {'type': 'string'}, 'event': {'type': 'string'}, + 'point': {'type': 'integer'}, 'again': {'type': 'boolean'} }, 'required': ['token', 'qr', 'event'] @@ -55,12 +56,17 @@ def attend_event(aws_event, context, user=None): qr = aws_event['qr'] event = aws_event['event'] again = aws_event.get('again', False) + point = aws_event.get('point', 0) + + # in case user accidentally put a negative number + if point < 0: + point = 0 def attend(user): # if the user has already attended the event and is not an event that can be re-attended, complain if not again and user['day_of'].get(event, 0) > 0: return {'statusCode': 402, 'body': 'user already checked into event'} - # update the user data to reflect event attendance by incrementing the count of the event by 1 + # update the user data to reflect event attendance by incrementing the count of the event by the variable 'point' new_user = users.find_one_and_update({'email': user['email']}, { '$inc': {'day_of.' + event: 1}, @@ -68,10 +74,10 @@ def attend(user): }, return_document=pymongo.ReturnDocument.AFTER) - # update the user's house points to reflect event attendance by incrementing it by 1 + # update the user's house points to reflect event attendance by incrementing it by the variable 'point' update_points = houses.find_one_and_update({"name": new_user['house']}, { - '$inc': {'points': 1} + '$inc': {'points': point} }, return_document=pymongo.ReturnDocument.AFTER) diff --git a/src/teams.py b/src/teams.py index ceb28e5..7d3fa61 100644 --- a/src/teams.py +++ b/src/teams.py @@ -3,26 +3,24 @@ from src.util import coll import json -# the request body will include: -# 1. token (for auth) -# 2. team_leader: email (string) -# 3. team_members: [strings] +# the request body will require: +# 1. team_leader: email (string) +# 2. team_members: [strings] # procedure: -# 1. check to see if team_leader is teamless (must be true before proceed) -# 2. check to see if ALL team_members are teamless (must be true before proceed) -# 3. create a new team in collection "Team", assign a team_id by: getting the total no. of teams in collection + 1 -# 4. assign team leader and team_members inside Team object (maybe?) +# 1. Team leader and team members need to be distinct +# 2. check to see if team_leader exists and is teamless (must be true before proceed) +# 3. check to see if ALL team_members exist and are teamless (must be true before proceed) +# 4. create a new team in collection "Team", assign a team_id by: getting the total no. of teams in collection + 1 # 5. Go find each user, including team leader and member, attach team_id into their user profile @ensure_schema({ "type": "object", "properties": { - "token": {"type": "string"}, "team_leader": {"type": "string"}, "team_members": {"type": "array"} }, - "required": ["token", "team_leader", "team_members"] + "required": ["team_leader", "team_members"] }) def make_teams(event, context): team_leader_email = event['team_leader'] @@ -31,40 +29,59 @@ def make_teams(event, context): users = coll('users') teams = coll('teams') - # check to see if team leader is teamless (consider changing to checking from teams instead of using users) + # Team leader and team members need to be distinct + if team_leader_email in team_members_emails: + return {"status": 400, "error": "team leader email is the same as one of the members"} + + # team members (array of strings) need to be distinct among themselves + if len(team_members_emails) != len(set(team_members_emails)): + return {"status": 400, "error": "team member emails need to be distinct"} + + + # check to see if team leader exists and is teamless team_leader = users.find_one({"email": team_leader_email}) - if "team_id" in team_leader and team_leader['team_id'] > 0: - return {"status": 400, "body": "team leader already has a team"} + if team_leader is not None: + if "team_id" in team_leader and team_leader['team_id'] > 0: + return {"status": 400, "error": "team leader already has a team"} + else: + return {"status": 400, "error": "team leader email does not exist"} - # check to see if all team members are teamless + # check to see if all team members exist and are teamless for member_email in team_members_emails: team_member = users.find_one({"email": member_email}) - if "team_id" in team_member and team_member['team_id'] > 0: - return {"status": 400, "bodys": "team member already has a team"} + if team_member is not None: + if "team_id" in team_member and team_member['team_id'] > 0: + return {"status": 400, "error": "team member already has a team"} + else: + return {"status": 400, "error": "this team member email does not exist", "email": member_email} # create a team - teams_count = teams.count_documents({}) - new_team = teams.insert_one({ - "team_leader": team_leader_email, - "team_members": team_members_emails, - "team_id": teams_count + 1 - }) + try: + teams_count = teams.count_documents({}) + new_team = teams.insert_one({ + "team_leader": team_leader_email, + "team_members": team_members_emails, + "team_id": teams_count + 1 + }) - # assigning team_id to team leader and members in Users collection - team_id = teams_count + 1 - print(team_id) - update_team_leader_id = users.find_one_and_update({"email": team_leader_email}, - { - '$set': {'team_id': team_id} - }) - - for member_email in team_members_emails: - update_team_member_id = users.find_one_and_update({"email": member_email}, - { - '$set': {'team_id': team_id} - }) + # assigning team_id to team leader and members in Users collection + team_id = teams_count + 1 + + update_team_leader_id = users.find_one_and_update({"email": team_leader_email}, + { + '$set': {'team_id': team_id} + }) + + for member_email in team_members_emails: + update_team_member_id = users.find_one_and_update({"email": member_email}, + { + '$set': {'team_id': team_id} + }) + except Exception as e: + return {"status": 500, 'error': e} + # success response return { "status": 200, "body": {