M1: backend core + proposal engine

- FastAPI skeleton, SQLAlchemy models (§13), Alembic initial migration
- SchedulingProvider interface with google_calendar (free/busy read-only),
  partner_api (Appendix B client) and mock implementations
- Proposal engine: create → provider-routed delivery → owner actions
  (resolve/confirm+SMS/reject) → expiry + reminders (§9)
- Signed single-use action links, .ics METHOD:REQUEST attachment
- Partner outcome webhook with HMAC verification + polling fallback
- SmsProvider (console) with Bosnian templates (§5.5), EmailProvider (console/SMTP)
- Fake partner API server in tests/ — Appendix B reference implementation
- 43 tests: slot math, proposal lifecycle, action links, partner contract

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-11 09:45:06 +02:00
commit e855650f09
48 changed files with 4941 additions and 0 deletions

50
alembic/env.py Normal file
View File

@@ -0,0 +1,50 @@
import re
from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config, pool
from gogo.config import get_settings
from gogo.db import Base
from gogo import models # noqa: F401 — register all tables
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = Base.metadata
def _sync_url() -> str:
# alembic runs sync; strip async driver suffixes
url = get_settings().database_url
return re.sub(r"\+(asyncpg|aiosqlite)", "", url)
def run_migrations_offline() -> None:
context.configure(
url=_sync_url(),
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
connectable = engine_from_config(
{"sqlalchemy.url": _sync_url()},
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

25
alembic/script.py.mako Normal file
View File

@@ -0,0 +1,25 @@
"""${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: str = ${repr(up_revision)}
down_revision: Union[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:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,352 @@
"""initial schema
Revision ID: 62fcb10345fa
Revises:
Create Date: 2026-07-11 09:43:04.542280
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
revision: str = '62fcb10345fa'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('admins',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('email', sa.String(length=200), nullable=False),
sa.Column('password_hash', sa.String(length=200), nullable=False),
sa.Column('active', sa.Boolean(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('email')
)
op.create_table('prompt_templates',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('version', sa.Integer(), nullable=False),
sa.Column('body', sa.Text(), nullable=False),
sa.Column('notes', sa.Text(), nullable=False),
sa.Column('published', sa.Boolean(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('version')
)
op.create_table('tenants',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('name', sa.String(length=200), nullable=False),
sa.Column('slug', sa.String(length=80), nullable=False),
sa.Column('address', sa.String(length=300), nullable=False),
sa.Column('city', sa.String(length=100), nullable=False),
sa.Column('phone', sa.String(length=40), nullable=False),
sa.Column('website', sa.String(length=200), nullable=False),
sa.Column('locale', sa.String(length=20), nullable=False),
sa.Column('timezone', sa.String(length=50), nullable=False),
sa.Column('currency', sa.String(length=10), nullable=False),
sa.Column('working_hours', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=False),
sa.Column('scheduling_provider', sa.String(length=30), nullable=False),
sa.Column('plan', sa.String(length=40), nullable=False),
sa.Column('included_minutes', sa.Integer(), nullable=False),
sa.Column('hard_cutoff', sa.Boolean(), nullable=False),
sa.Column('paid_until', sa.DateTime(timezone=True), nullable=True),
sa.Column('status', sa.String(length=20), nullable=False),
sa.Column('tts_voice', sa.String(length=80), nullable=False),
sa.Column('price_mode', sa.String(length=20), nullable=False),
sa.Column('agent_notes', sa.Text(), nullable=False),
sa.Column('llm_model', sa.String(length=80), nullable=False),
sa.Column('proposal_ttl_hours', sa.Integer(), nullable=False),
sa.Column('notify_emails', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=False),
sa.Column('sms_request_received', sa.Boolean(), nullable=False),
sa.Column('sms_templates', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=False),
sa.Column('min_notice_hours', sa.Integer(), nullable=False),
sa.Column('max_days_ahead', sa.Integer(), nullable=False),
sa.Column('ring_first_enabled', sa.Boolean(), nullable=False),
sa.Column('ring_timeout_s', sa.Integer(), nullable=False),
sa.Column('ring_strategy', sa.String(length=20), nullable=False),
sa.Column('widget_public_key', sa.String(length=64), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('slug'),
sa.UniqueConstraint('widget_public_key')
)
op.create_table('calendar_connections',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=False),
sa.Column('google_account', sa.String(length=200), nullable=False),
sa.Column('scopes', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=False),
sa.Column('token_data_encrypted', sa.Text(), nullable=False),
sa.Column('status', sa.String(length=20), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('tenant_id')
)
op.create_table('calls',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=False),
sa.Column('started_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('caller_msisdn', sa.String(length=30), nullable=False),
sa.Column('duration_s', sa.Integer(), nullable=False),
sa.Column('outcome', sa.String(length=30), nullable=False),
sa.Column('recording_path', sa.String(length=400), nullable=True),
sa.Column('transcript', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=False),
sa.Column('agent_seconds_charged', sa.Integer(), nullable=False),
sa.Column('trace', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=False),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_calls_tenant_id'), 'calls', ['tenant_id'], unique=False)
op.create_table('chat_sessions',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=False),
sa.Column('started_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('resume_token', sa.String(length=64), nullable=False),
sa.Column('client_ip', sa.String(length=60), nullable=False),
sa.Column('outcome', sa.String(length=30), nullable=False),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_chat_sessions_tenant_id'), 'chat_sessions', ['tenant_id'], unique=False)
op.create_table('email_log',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=True),
sa.Column('at', sa.DateTime(timezone=True), nullable=False),
sa.Column('to_addr', sa.String(length=300), nullable=False),
sa.Column('subject', sa.String(length=400), nullable=False),
sa.Column('kind', sa.String(length=30), nullable=False),
sa.Column('status', sa.String(length=20), nullable=False),
sa.Column('error', sa.Text(), nullable=False),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('messages_for_owner',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('client_name', sa.String(length=200), nullable=False),
sa.Column('client_phone', sa.String(length=40), nullable=False),
sa.Column('text', sa.Text(), nullable=False),
sa.Column('source', sa.String(length=10), nullable=False),
sa.Column('read', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_messages_for_owner_tenant_id'), 'messages_for_owner', ['tenant_id'], unique=False)
op.create_table('phone_numbers',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=True),
sa.Column('msisdn', sa.String(length=30), nullable=False),
sa.Column('gateway_port', sa.Integer(), nullable=True),
sa.Column('operator', sa.String(length=30), nullable=False),
sa.Column('sim_status', sa.String(length=20), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('msisdn'),
sa.UniqueConstraint('tenant_id')
)
op.create_table('provider_configs',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=False),
sa.Column('provider_type', sa.String(length=30), nullable=False),
sa.Column('config', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('tenant_id')
)
op.create_table('services',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=False),
sa.Column('name', sa.String(length=200), nullable=False),
sa.Column('duration_min', sa.Integer(), nullable=False),
sa.Column('price_min', sa.Float(), nullable=True),
sa.Column('price_max', sa.Float(), nullable=True),
sa.Column('home_visit', sa.Boolean(), nullable=False),
sa.Column('agent_note', sa.Text(), nullable=False),
sa.Column('buffer_min', sa.Integer(), nullable=False),
sa.Column('partner_service_id', sa.String(length=80), nullable=True),
sa.Column('active', sa.Boolean(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_services_tenant_id'), 'services', ['tenant_id'], unique=False)
op.create_table('sms_log',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=False),
sa.Column('at', sa.DateTime(timezone=True), nullable=False),
sa.Column('to_msisdn', sa.String(length=30), nullable=False),
sa.Column('body', sa.Text(), nullable=False),
sa.Column('kind', sa.String(length=30), nullable=False),
sa.Column('status', sa.String(length=20), nullable=False),
sa.Column('error', sa.Text(), nullable=False),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_sms_log_tenant_id'), 'sms_log', ['tenant_id'], unique=False)
op.create_table('tenant_prompt_overrides',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=False),
sa.Column('body', sa.Text(), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('tenant_id')
)
op.create_table('usage_counters',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=False),
sa.Column('month', sa.String(length=7), nullable=False),
sa.Column('agent_seconds', sa.Integer(), nullable=False),
sa.Column('calls', sa.Integer(), nullable=False),
sa.Column('sms_sent', sa.Integer(), nullable=False),
sa.Column('chat_sessions', sa.Integer(), nullable=False),
sa.Column('warned_80', sa.Boolean(), nullable=False),
sa.Column('warned_100', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('tenant_id', 'month')
)
op.create_index(op.f('ix_usage_counters_tenant_id'), 'usage_counters', ['tenant_id'], unique=False)
op.create_table('users',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=False),
sa.Column('email', sa.String(length=200), nullable=False),
sa.Column('password_hash', sa.String(length=200), nullable=False),
sa.Column('active', sa.Boolean(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('email'),
sa.UniqueConstraint('tenant_id')
)
op.create_table('workers',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=False),
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('sip_username', sa.String(length=80), nullable=False),
sa.Column('sip_password', sa.String(length=80), nullable=False),
sa.Column('active', sa.Boolean(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('sip_username')
)
op.create_index(op.f('ix_workers_tenant_id'), 'workers', ['tenant_id'], unique=False)
op.create_table('booking_requests',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=False),
sa.Column('source', sa.String(length=10), nullable=False),
sa.Column('client_name', sa.String(length=200), nullable=False),
sa.Column('client_phone', sa.String(length=40), nullable=False),
sa.Column('service_id', sa.Uuid(), nullable=True),
sa.Column('service_name_raw', sa.String(length=300), nullable=False),
sa.Column('slots', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=False),
sa.Column('time_preference_text', sa.String(length=300), nullable=False),
sa.Column('home_visit', sa.Boolean(), nullable=False),
sa.Column('address', sa.String(length=300), nullable=True),
sa.Column('summary', sa.Text(), nullable=False),
sa.Column('status', sa.String(length=30), nullable=False),
sa.Column('confirmed_slot', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=True),
sa.Column('partner_request_id', sa.String(length=120), nullable=True),
sa.Column('call_id', sa.Uuid(), nullable=True),
sa.Column('chat_session_id', sa.Uuid(), nullable=True),
sa.Column('expires_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('reminder_sent', sa.Boolean(), nullable=False),
sa.Column('resolved_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('resolution_note', sa.Text(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['call_id'], ['calls.id'], ),
sa.ForeignKeyConstraint(['chat_session_id'], ['chat_sessions.id'], ),
sa.ForeignKeyConstraint(['service_id'], ['services.id'], ),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_booking_requests_status'), 'booking_requests', ['status'], unique=False)
op.create_index(op.f('ix_booking_requests_tenant_id'), 'booking_requests', ['tenant_id'], unique=False)
op.create_table('calendar_mappings',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('tenant_id', sa.Uuid(), nullable=False),
sa.Column('service_id', sa.Uuid(), nullable=True),
sa.Column('google_calendar_id', sa.String(length=300), nullable=False),
sa.ForeignKeyConstraint(['service_id'], ['services.id'], ),
sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('tenant_id', 'service_id')
)
op.create_index(op.f('ix_calendar_mappings_tenant_id'), 'calendar_mappings', ['tenant_id'], unique=False)
op.create_table('chat_messages',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('session_id', sa.Uuid(), nullable=False),
sa.Column('at', sa.DateTime(timezone=True), nullable=False),
sa.Column('role', sa.String(length=20), nullable=False),
sa.Column('content', sa.Text(), nullable=False),
sa.ForeignKeyConstraint(['session_id'], ['chat_sessions.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_chat_messages_session_id'), 'chat_messages', ['session_id'], unique=False)
op.create_table('action_tokens',
sa.Column('token', sa.String(length=64), nullable=False),
sa.Column('request_id', sa.Uuid(), nullable=False),
sa.Column('action', sa.String(length=30), nullable=False),
sa.Column('slot_index', sa.Integer(), nullable=True),
sa.Column('used_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['request_id'], ['booking_requests.id'], ),
sa.PrimaryKeyConstraint('token')
)
op.create_index(op.f('ix_action_tokens_request_id'), 'action_tokens', ['request_id'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_action_tokens_request_id'), table_name='action_tokens')
op.drop_table('action_tokens')
op.drop_index(op.f('ix_chat_messages_session_id'), table_name='chat_messages')
op.drop_table('chat_messages')
op.drop_index(op.f('ix_calendar_mappings_tenant_id'), table_name='calendar_mappings')
op.drop_table('calendar_mappings')
op.drop_index(op.f('ix_booking_requests_tenant_id'), table_name='booking_requests')
op.drop_index(op.f('ix_booking_requests_status'), table_name='booking_requests')
op.drop_table('booking_requests')
op.drop_index(op.f('ix_workers_tenant_id'), table_name='workers')
op.drop_table('workers')
op.drop_table('users')
op.drop_index(op.f('ix_usage_counters_tenant_id'), table_name='usage_counters')
op.drop_table('usage_counters')
op.drop_table('tenant_prompt_overrides')
op.drop_index(op.f('ix_sms_log_tenant_id'), table_name='sms_log')
op.drop_table('sms_log')
op.drop_index(op.f('ix_services_tenant_id'), table_name='services')
op.drop_table('services')
op.drop_table('provider_configs')
op.drop_table('phone_numbers')
op.drop_index(op.f('ix_messages_for_owner_tenant_id'), table_name='messages_for_owner')
op.drop_table('messages_for_owner')
op.drop_table('email_log')
op.drop_index(op.f('ix_chat_sessions_tenant_id'), table_name='chat_sessions')
op.drop_table('chat_sessions')
op.drop_index(op.f('ix_calls_tenant_id'), table_name='calls')
op.drop_table('calls')
op.drop_table('calendar_connections')
op.drop_table('tenants')
op.drop_table('prompt_templates')
op.drop_table('admins')
# ### end Alembic commands ###