-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Command and routes for invite codes (#360)
* ➕ Add command for invite codes generation * ➕ Add basic repo functions for invite codes * ❌ remove default value, add is_used property * ➕ Add basic routes for invite codes * ➕ Include invite router and update docs * 📜 Update docs
- Loading branch information
Showing
12 changed files
with
256 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import os | ||
|
||
from ..database import repo | ||
from ..dependencies.database import get_engine_and_session | ||
|
||
|
||
def run(n: int): | ||
print(f"Generating {n} new invite codes...") | ||
|
||
_, session = get_engine_and_session() | ||
db = session() | ||
|
||
codes = repo.generate_invite_codes(db, n) | ||
|
||
db.close() | ||
|
||
print(f"Successfull added {len(codes)} shiny new invite codes to the database.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
.../src/appointment/migrations/versions/2024_04_16_1241-fadd0d1ef438_create_invites_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"""create invites table | ||
Revision ID: fadd0d1ef438 | ||
Revises: c5b9fc31b555 | ||
Create Date: 2024-04-16 12:41:53.550102 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from database.models import InviteStatus | ||
from sqlalchemy import func, ForeignKey | ||
from sqlalchemy_utils import StringEncryptedType | ||
from sqlalchemy_utils.types.encrypted.encrypted_type import AesEngine | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'fadd0d1ef438' | ||
down_revision = 'c5b9fc31b555' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def secret(): | ||
return os.getenv("DB_SECRET") | ||
|
||
|
||
def upgrade() -> None: | ||
op.create_table( | ||
'invites', | ||
sa.Column('id', sa.Integer, primary_key=True, index=True), | ||
sa.Column('subscriber_id', sa.Integer, ForeignKey("subscribers.id")), | ||
sa.Column('code', StringEncryptedType(sa.String, secret, AesEngine, "pkcs5", length=255), index=False), | ||
sa.Column('status', sa.Enum(InviteStatus), index=True), | ||
sa.Column('time_created', sa.DateTime, server_default=func.now()), | ||
sa.Column('time_updated', sa.DateTime, server_default=func.now()), | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_table('invites') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
from fastapi import APIRouter, Depends | ||
|
||
from sqlalchemy.orm import Session | ||
|
||
from ..database import repo, schemas, models | ||
from ..dependencies.auth import get_subscriber, get_subscriber_from_signed_url | ||
from ..dependencies.database import get_db | ||
|
||
from ..exceptions import validation | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/generate/{n}", response_model=list[schemas.Invite]) | ||
def generate_invite_codes(n: int, db: Session = Depends(get_db)): | ||
"""endpoint to generate n invite codes""" | ||
return repo.generate_invite_codes(db, n) | ||
|
||
|
||
@router.put("/redeem/{code}") | ||
def use_invite_code(code: str, db: Session = Depends(get_db)): | ||
"""endpoint to create a new subscriber and update the corresponding invite""" | ||
if not repo.invite_code_exists(db, code): | ||
raise validation.InviteCodeNotFoundException() | ||
if not repo.invite_code_is_available(db, code): | ||
raise validation.InviteCodeNotAvailableException() | ||
# TODO: get email from admin panel | ||
email = '[email protected]' | ||
subscriber = repo.create_subscriber(db, schemas.SubscriberBase(email=email, username=email)) | ||
return repo.use_invite_code(db, code, subscriber.id) | ||
|
||
|
||
@router.put("/revoke/{code}") | ||
def use_invite_code(code: str, db: Session = Depends(get_db)): | ||
"""endpoint to revoke a given invite code and mark in unavailable""" | ||
if not repo.invite_code_exists(db, code): | ||
raise validation.InviteCodeNotFoundException() | ||
if not repo.invite_code_is_available(db, code): | ||
raise validation.InviteCodeNotAvailableException() | ||
return repo.revoke_invite_code(db, code) |