1"""Discord role-reset outbox: a site account was deleted and must be stripped of every manageable Discord role."""
2
3from datetime import UTC, datetime # SQLAlchemy resolves Mapped[] annotations at runtime
4
5from advanced_alchemy.base import UUIDAuditBase
6from advanced_alchemy.types import DateTimeUTC
7from sqlalchemy import BigInteger
8from sqlalchemy.orm import Mapped, mapped_column
9
10
[docs]
11class MemberRoleReset(UUIDAuditBase):
12 """One Discord user who deleted their site account and still needs their Discord roles stripped.
13
14 Written by ``MembersService.delete_account`` in the same transaction as the account deletion, so the outbox
15 row survives even though the ``Member`` row it's about is gone. The bot's ``link_roles`` plugin removes every
16 role it can manage from ``discord_id`` (keeping ``@everyone``, managed/integration roles, and anything at or
17 above its own top role, which Discord would reject anyway) and immediately re-grants the gate role, since the
18 member is now unlinked; it then acks via ``POST .../role-resets/{id}/ack``, which sets ``done_at``. A member
19 no longer in the guild is acked as done without any role changes. ``requested_at`` orders the outbox
20 oldest-first; rows that go unacked past the API's retention window (e.g. the member left before the bot could
21 act) are dropped by the listing route rather than sitting forever.
22 """
23
24 __tablename__ = "member_role_reset"
25 __table_args__ = {"comment": "Outbox: Discord role resets owed after a site account deletion."}
26
27 discord_id: Mapped[int] = mapped_column(BigInteger, index=True)
28 requested_at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True), default=lambda: datetime.now(UTC))
29 done_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)