Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions chalicelib/modules/google_sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions chalicelib/services/EventService.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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"]
Expand All @@ -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:
Expand All @@ -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.",
Expand Down