feat: Sprint 1 foundation - FastAPI scaffold, DB schema, auth, seed data, mock WhatsApp, Docker

This commit is contained in:
root
2026-07-23 12:04:14 +00:00
parent 8c755f3db7
commit 32a4c50b23
34 changed files with 1390 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
Generic single-database configuration with an async dbapi.
+70
View File
@@ -0,0 +1,70 @@
import asyncio
from logging.config import fileConfig
from sqlalchemy import pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import async_engine_from_config
from alembic import context
# Alembic Config object
config = context.config
# Set up logging
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# ── Import all models so Alembic can detect them ─────────────────────
from app.core.database import Base # noqa: E402
from app.models.user import User # noqa: E402, F401
from app.models.unit import Unit # noqa: E402, F401
from app.models.category import Category # noqa: E402, F401
from app.models.ticket import Ticket, TicketTimeline, TicketPhoto, Escalation # noqa: E402, F401
from app.models.whatsapp_log import WhatsAppLog # noqa: E402, F401
target_metadata = Base.metadata
# ── Database URL from our settings ──────────────────────────────────
from app.core.config import settings as app_settings # noqa: E402
config.set_main_option("sqlalchemy.url", app_settings.DATABASE_URL)
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode."""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection: Connection) -> None:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
async def run_async_migrations() -> None:
connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
def run_migrations_online() -> None:
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
+28
View File
@@ -0,0 +1,28 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
"""Upgrade schema."""
${upgrades if upgrades else "pass"}
def downgrade() -> None:
"""Downgrade schema."""
${downgrades if downgrades else "pass"}
@@ -0,0 +1,135 @@
"""initial schema
Revision ID: 795c6b95f637
Revises:
Create Date: 2026-07-23 12:01:33.135357
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '795c6b95f637'
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('categories',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('type', sa.String(length=20), nullable=False, comment='maintenance, cs, emergency'),
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('parent_id', sa.Integer(), nullable=True),
sa.Column('sla_urgency', sa.String(length=10), nullable=True, comment='urgent, high, medium, low'),
sa.ForeignKeyConstraint(['parent_id'], ['categories.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('units',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('property', sa.String(length=10), nullable=False, comment='East or West'),
sa.Column('apartment_code', sa.String(length=20), nullable=False),
sa.Column('building', sa.String(length=100), nullable=True),
sa.Column('floor', sa.Integer(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_units_apartment_code'), 'units', ['apartment_code'], unique=True)
op.create_table('users',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('email', sa.String(length=255), nullable=False),
sa.Column('password_hash', sa.String(length=255), nullable=False),
sa.Column('full_name', sa.String(length=255), nullable=False),
sa.Column('phone', sa.String(length=50), nullable=True),
sa.Column('role', sa.String(length=50), nullable=False, comment='CS Rep, CS Manager, FM Dispatcher, Admin/Jerome, Admin/Wahab, Tech, CEO, Director'),
sa.Column('active', sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
op.create_table('whatsapp_log',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('command', sa.Text(), nullable=False),
sa.Column('from_number', sa.String(length=50), nullable=True),
sa.Column('ticket_id', sa.Integer(), nullable=True),
sa.Column('received_at', sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('tickets',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('ticket_number', sa.String(length=20), nullable=False, comment='Format: PAV-YYYY-NNNNN'),
sa.Column('status', sa.String(length=30), nullable=False, comment='New, Logged, Triage, Assigned, Accepted, Travelling, On Site, In Progress, Waiting Parts, Escalated, Completed, On-Field Verification, Wahab Review, Closed, Reopened'),
sa.Column('unit_id', sa.Integer(), nullable=True),
sa.Column('category_id', sa.Integer(), nullable=True),
sa.Column('priority', sa.String(length=10), nullable=True, comment='urgent, high, medium, low'),
sa.Column('reporter', sa.String(length=255), nullable=True),
sa.Column('reported_via', sa.String(length=20), nullable=True, comment='whatsapp, phone, walk-in, qr, agent'),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('assigned_to', sa.Integer(), nullable=True),
sa.Column('sla_deadline', sa.DateTime(), nullable=True),
sa.Column('eta', sa.DateTime(), nullable=True),
sa.Column('cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('parts_used', sa.Text(), nullable=True),
sa.Column('customer_rating', sa.Integer(), nullable=True),
sa.Column('reopen_count', sa.Integer(), nullable=False),
sa.Column('closed_at', sa.DateTime(), nullable=True),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
sa.ForeignKeyConstraint(['assigned_to'], ['users.id'], ),
sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ),
sa.ForeignKeyConstraint(['unit_id'], ['units.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_tickets_ticket_number'), 'tickets', ['ticket_number'], unique=True)
op.create_table('escalations',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('ticket_id', sa.Integer(), nullable=False),
sa.Column('escalated_to', sa.Integer(), nullable=False),
sa.Column('reason', sa.Text(), nullable=True),
sa.Column('resolved_at', sa.DateTime(), nullable=True),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
sa.ForeignKeyConstraint(['escalated_to'], ['users.id'], ),
sa.ForeignKeyConstraint(['ticket_id'], ['tickets.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('ticket_photos',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('ticket_id', sa.Integer(), nullable=False),
sa.Column('photo_url', sa.String(length=500), nullable=False),
sa.Column('is_before', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['ticket_id'], ['tickets.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('ticket_timeline',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('ticket_id', sa.Integer(), nullable=False),
sa.Column('from_status', sa.String(length=30), nullable=True),
sa.Column('to_status', sa.String(length=30), nullable=True),
sa.Column('note', sa.Text(), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
sa.ForeignKeyConstraint(['ticket_id'], ['tickets.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('ticket_timeline')
op.drop_table('ticket_photos')
op.drop_table('escalations')
op.drop_index(op.f('ix_tickets_ticket_number'), table_name='tickets')
op.drop_table('tickets')
op.drop_table('whatsapp_log')
op.drop_index(op.f('ix_users_email'), table_name='users')
op.drop_table('users')
op.drop_index(op.f('ix_units_apartment_code'), table_name='units')
op.drop_table('units')
op.drop_table('categories')
# ### end Alembic commands ###