fix(responses): invalidate changed options safely

This commit is contained in:
2026-07-20 18:30:04 +02:00
parent e0de54b756
commit 12b3175b07
3 changed files with 122 additions and 2 deletions

View File

@@ -732,6 +732,44 @@ def _lock_poll_for_response(session: Session, *, tenant_id: str, poll_id: str) -
return poll
def get_poll_response_for_respondents(
session: Session,
*,
tenant_id: str,
poll_id: str,
respondent_ids: tuple[str, ...],
invitation_id: str | None = None,
) -> PollResponse | None:
"""Resolve a response from server-trusted respondent identities."""
get_poll(session, tenant_id=tenant_id, poll_id=poll_id)
ids = tuple(dict.fromkeys(value for value in respondent_ids if value))
if ids:
response = (
session.query(PollResponse)
.filter(
PollResponse.tenant_id == tenant_id,
PollResponse.poll_id == poll_id,
PollResponse.respondent_id.in_(ids),
PollResponse.deleted_at.is_(None),
)
.order_by(PollResponse.submitted_at.desc(), PollResponse.id.desc())
.first()
)
if response is not None:
return response
if invitation_id is None:
return None
return next(
(
response
for response in reversed(list_poll_responses(session, tenant_id=tenant_id, poll_id=poll_id))
if (response.metadata_ or {}).get("invitation_id") == invitation_id
),
None,
)
def update_poll_option(
session: Session,
*,
@@ -793,6 +831,8 @@ def update_poll_option(
.with_for_update()
.all()
)
if responses and not poll.allow_response_update:
raise PollError("Poll options cannot be edited after responses when response updates are disabled")
option.label = label
option.description = description
option.value = normalized_value
@@ -805,6 +845,8 @@ def update_poll_option(
]
if retained_answers != (response.answers or []):
response.answers = retained_answers
if not retained_answers:
response.deleted_at = _now()
session.flush()
return option