diff --git a/backend/README.md b/backend/README.md index be2d8a1eb..bfb291f4e 100644 --- a/backend/README.md +++ b/backend/README.md @@ -45,4 +45,4 @@ run-command main --help * `download-legal` is an internal command to process privacy policy and terms of service files that will be served by the frontend. * `update-db` runs on docker container entry, and ensures the latest db migration has run, or if it's a new db then to kickstart that. -* `create-invite-codes --n 50` is an internal command to create invite codes which can be used for registrations. The `--n` argument specifies the amount of codes generated, defaults to 50 if left empty. +* `create-invite-codes n` is an internal command to create invite codes which can be used for user registrations. The `n` argument is an integer that specifies the amount of codes to be generated. diff --git a/backend/src/appointment/commands/create_invite_codes.py b/backend/src/appointment/commands/create_invite_codes.py index 7d8a7ab8a..34b4a7684 100644 --- a/backend/src/appointment/commands/create_invite_codes.py +++ b/backend/src/appointment/commands/create_invite_codes.py @@ -5,7 +5,7 @@ from ..dependencies.database import get_engine_and_session -def run(n: int = 50): +def run(n: int): print(f"Generating {n} new invite codes...") codes = [str(uuid.uuid4()) for _ in range(n)] diff --git a/backend/src/appointment/database/models.py b/backend/src/appointment/database/models.py index 3ab281ec0..49db1b255 100644 --- a/backend/src/appointment/database/models.py +++ b/backend/src/appointment/database/models.py @@ -284,3 +284,8 @@ class Invite(Base): status = Column(Enum(InviteStatus), index=True) subscriber = relationship("Subscriber", back_populates="invite") + + @property + def is_used(self) -> bool: + """True if the invite code is assigned to a subscriber""" + return self.subscriber_id is not None diff --git a/backend/src/appointment/database/repo.py b/backend/src/appointment/database/repo.py index 086572287..94c49233f 100644 --- a/backend/src/appointment/database/repo.py +++ b/backend/src/appointment/database/repo.py @@ -564,7 +564,7 @@ def schedule_has_slot(db: Session, schedule_id: int, slot_id: int): def use_invite_code(db: Session, code: str, subscriber_id: int): """set existing invite code status to revoked""" db_invite = db.query(models.Invite).filter(models.Invite.code == code).first() - if db_invite.status == models.InviteStatus.active: + if db_invite.status == models.InviteStatus.active and not db_invite.is_used: db_invite.subscriber_id = subscriber_id db.commit() db.refresh(db_invite) diff --git a/backend/src/appointment/routes/commands.py b/backend/src/appointment/routes/commands.py index 4e168f3fb..2c87e4585 100644 --- a/backend/src/appointment/routes/commands.py +++ b/backend/src/appointment/routes/commands.py @@ -36,5 +36,5 @@ def download_legal_docs(): @router.command('create-invite-codes') -def create_app_invite_codes(n: int = 50): +def create_app_invite_codes(n: int): create_invite_codes.run(n)