diff --git a/chalicelib/modules/google_sheets.py b/chalicelib/modules/google_sheets.py index 4034d6c..1af341c 100644 --- a/chalicelib/modules/google_sheets.py +++ b/chalicelib/modules/google_sheets.py @@ -89,6 +89,23 @@ def find_matching_name( return idx return -1 + + def find_matching_email( + self, spreadsheet_id: str, sheet_name: str, col: str, email_to_match: str + ): + range = f"{sheet_name}!{col}:{col}" + response = ( + self.service.spreadsheets() + .values() + .get(spreadsheetId=spreadsheet_id, range=range) + .execute() + ) + + rows = list(response["values"]) + for idx, row in enumerate(rows): + if email_to_match in row: + return idx + return -1 def update_row( self, spreadsheet_id: str, sheet_name: str, col: str, row: int, data: list diff --git a/chalicelib/services/EventService.py b/chalicelib/services/EventService.py index bd0c01f..a0f63f2 100644 --- a/chalicelib/services/EventService.py +++ b/chalicelib/services/EventService.py @@ -116,7 +116,7 @@ def checkin(self, event_id: str, user: dict) -> dict: Returns: dict -- Dictionary containing status and message. """ - user_id = user["userId"] + user_id, user_email = user["id"], user["email"] member = mongo_module.get_document_by_id(f"users", user_id) if member is None: raise NotFoundError(f"User with ID {user_id} does not exist.") @@ -136,13 +136,6 @@ def checkin(self, event_id: str, user: dict) -> dict: "dateCheckedIn": datetime.datetime.now(), } - # Update event collection with checkin data - mongo_module.update_document( - f"{self.collection_prefix}event", - event_id, - {"$push": {"usersAttended": checkin_data}}, - ) - # Get timeframe document to get Google Sheets info timeframe = mongo_module.get_document_by_id( f"{self.collection_prefix}timeframe", event["timeframeId"] @@ -154,13 +147,12 @@ def checkin(self, event_id: str, user: dict) -> dict: # Initialize Google Sheets Module gs = GoogleSheetsModule() - # Find row in Google Sheets that matches user's name - row_num = gs.find_matching_name( + # Find row in Google Sheets that matches user's email + row_num = gs.find_matching_email( spreadsheet_id=ss_id, sheet_name=event["sheetTab"], - cols=["A", "B"], - name_to_match=user_name, - use_similarity=True, + col="C", + email_to_match=user_email, ) if row_num == -1: @@ -178,6 +170,14 @@ def checkin(self, event_id: str, user: dict) -> dict: data=[["1"]], ) + # Update event collection with checkin data + mongo_module.update_document( + f"{self.collection_prefix}event", + event_id, + {"$push": {"usersAttended": checkin_data}}, + ) + + return { "status": True, "message": f"{user_name} has successfully been checked in.",