fix(grist): Don't send request to grist of nothing is written

This commit is contained in:
2025-12-02 21:54:59 +01:00
parent 491a7154e2
commit 78066b77ae
2 changed files with 16 additions and 5 deletions

View File

@@ -14,7 +14,11 @@ config = {
}
def sync_with_grist(event):
def sync_with_grist(event) -> int:
"""Writes the event's registrations to Grist.
If a registration already exists for a given household, it is not overwritten.
Returns the number of new records.
"""
grist = GristApi(config=config)
status_code, grist_response = grist.list_records(
@@ -53,4 +57,8 @@ def sync_with_grist(event):
"meal_manager_event_id": event.id,
}
)
if new_records:
grist.add_records("Transactions", new_records)
return len(new_records)
else:
return 0

View File

@@ -4,6 +4,7 @@ from contextlib import asynccontextmanager
from datetime import datetime, timedelta
from functools import partial
from typing import Annotated
from urllib.parse import quote_plus
import starlette.status as status
from fastapi import Depends, FastAPI, HTTPException, Request, Response
@@ -446,9 +447,11 @@ def sync_with_grist_route(event_id: int, session: SessionDep, user: StrictUserDe
statement = select(Event).where(Event.id == event_id)
event = session.scalars(statement).one()
sync_with_grist(event)
entries_written = sync_with_grist(event)
message = "Es wurden keine Einträge geschrieben."
if entries_written > 0:
message = f"Erfolgreich {entries_written} Einträge geschrieben."
return RedirectResponse(
url=f"/event/{event_id}?message=Erfolgreich%20an%20Abrechnung%20%C3%BCbertragen",
url=f"/event/{event_id}?message={quote_plus(message)}",
status_code=status.HTTP_302_FOUND,
)