diff --git a/CHANGELOG.md b/CHANGELOG.md index e8edf77..3911ec2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## v1.2.2 - 2024-08-17 + +### Fix + +- Fix late bytes 422 error by adding optional param session_id to late bytes endpoint + + ## v1.2.1 - 2024-07-21 ### Fixed diff --git a/masterbase/app.py b/masterbase/app.py index a7c642e..2287f99 100644 --- a/masterbase/app.py +++ b/masterbase/app.py @@ -129,7 +129,8 @@ def late_bytes(request: Request, api_key: str, data: LateBytesBody) -> dict[str, current_time = datetime.now().astimezone(timezone.utc) steam_id = steam_id_from_api_key(engine, api_key) converted_late_bytes = bytes.fromhex(data.late_bytes) - error = late_bytes_helper(engine, steam_id, converted_late_bytes, current_time) + session_id = data.session_id + error = late_bytes_helper(engine, steam_id, converted_late_bytes, current_time, session_id) if error is None: return {"late_bytes": True} else: diff --git a/masterbase/lib.py b/masterbase/lib.py index 9aec222..468ec8a 100644 --- a/masterbase/lib.py +++ b/masterbase/lib.py @@ -539,40 +539,57 @@ def late_bytes_helper( steam_id: str, late_bytes: bytes, current_time: datetime, + session_id: str | None = None, ) -> str | None: """Add late bytes to the database and blob storage. No-ops and returns an error message if late bytes are found or there are no active sessions. """ with engine.connect() as conn: - try: - session_id, old_late_bytes = conn.execute( + if session_id is not None: + conn.execute( sa.text( - """SELECT session_id, late_bytes FROM demo_sessions - WHERE active = True - AND steam_id = :steam_id; - """, + """UPDATE demo_sessions + SET + late_bytes = :late_bytes, + updated_at = :updated_at + WHERE session_id = session_id;""" ), - {"steam_id": steam_id}, - ).one() - except NoResultFound: - return "no active session" - if session_id and old_late_bytes: - return "already submitted" - conn.execute( - sa.text( - """UPDATE demo_sessions - SET - late_bytes = :late_bytes, - updated_at = :updated_at - WHERE session_id = session_id;""" - ), - { - "session_id": session_id, - "late_bytes": late_bytes, - "updated_at": current_time.isoformat(), - }, - ) + { + "session_id": session_id, + "late_bytes": late_bytes, + "updated_at": current_time.isoformat(), + }, + ) + else: + try: + session_id, old_late_bytes = conn.execute( + sa.text( + """SELECT session_id, late_bytes FROM demo_sessions + WHERE active = True + AND steam_id = :steam_id; + """, + ), + {"steam_id": steam_id}, + ).one() + except NoResultFound: + return "no active session" + if session_id and old_late_bytes: + return "already submitted" + conn.execute( + sa.text( + """UPDATE demo_sessions + SET + late_bytes = :late_bytes, + updated_at = :updated_at + WHERE session_id = session_id;""" + ), + { + "session_id": session_id, + "late_bytes": late_bytes, + "updated_at": current_time.isoformat(), + }, + ) conn.commit() return None diff --git a/masterbase/models.py b/masterbase/models.py index 2ff131c..bdf77ef 100644 --- a/masterbase/models.py +++ b/masterbase/models.py @@ -1,6 +1,7 @@ """Module of pydantic models.""" from enum import Enum +from typing import Optional from pydantic import BaseModel @@ -31,3 +32,4 @@ class LateBytesBody(BaseModel): """Report model for late_bytes post request body.""" late_bytes: str + session_id: Optional[str] = None