diff --git a/src/schema.py b/src/schema.py index 95657cf..1a314f4 100644 --- a/src/schema.py +++ b/src/schema.py @@ -2,6 +2,7 @@ import graphene import base64 +import gspread import os from flask_jwt_extended import create_access_token, create_refresh_token, get_jwt_identity, get_jwt, jwt_required from functools import wraps @@ -28,7 +29,12 @@ from src.models.report import Report as ReportModel from src.models.hourly_average_capacity import HourlyAverageCapacity as HourlyAverageCapacityModel from src.models.user_workout_goal_history import UserWorkoutGoalHistory as UserWorkoutGoalHistoryModel -from src.utils.constants import get_digital_ocean_s3_endpoint_url +from src.utils.constants import ( + SERVICE_ACCOUNT_PATH, + SHEET_KEY, + SHEET_REPORTS, + get_digital_ocean_s3_endpoint_url, +) from src.database import db_session import requests from firebase_admin import messaging @@ -38,6 +44,11 @@ import boto3 from botocore.config import Config +# Configure the spreadsheet used to mirror user-submitted reports. SHEET_KEY +# selects the development or production spreadsheet based on FLASK_ENV. +gc = gspread.service_account(filename=SERVICE_ACCOUNT_PATH) +sh = gc.open_by_key(SHEET_KEY) + local_tz = ZoneInfo("America/New_York") def resolve_enum_value(entry): @@ -1187,13 +1198,15 @@ def mutate(self, info, description, issue, created_at, gym_id): "OTHER", ]: raise GraphQLError("Issue is not a valid enumeration.") - created_at_utc = ensure_utc(created_at) - report = ReportModel(description=description, issue=issue, created_at=created_at_utc, gym_id=gym_id) + # Keep accepting the client timestamp for backwards compatibility, but + # use the time this request reached the server as the source of truth. + submitted_at = datetime.now(timezone.utc) + report = ReportModel(description=description, issue=issue, created_at=submitted_at, gym_id=gym_id) db_session.add(report) db_session.commit() try: - sh.worksheet(SHEET_REPORTS).append_row([report.id, issue, gym.name, description, created_at.isoformat()]) + sh.worksheet(SHEET_REPORTS).append_row([report.id, issue, gym.name, description, submitted_at.isoformat()]) except Exception as e: print(f"Error logging report to sheet: {e}")