Files
xenia-pos-cloud/cloud_backend/migrations/versions/0003_order_sales_point.py
T
bonaminandClaude Opus 5.5 5bdd19d9fb feat(cloud): the cloud prices online orders itself; sites only touch their own data (platform-v2 3S, S.2)
- services/checkout.py: orders and quotes priced from the site's snapshot per sales point
  (prices map; older snapshots: digital_price less discount); refused: unavailable or
  not sold there, bad quantity/option/type, empty, order.price_changed when the browser's
  unit price or total differs. Nothing the browser says about money is stored.
- POST /api/orders/{site}/quote; delivery needs phone + address.
- Fixes: pending orders, menu sync and stats snapshot acted on the site_id given, not the
  authenticated site (a site could read another's orders or overwrite its menu/stats).
- Order refs are unique per site (a second site's first order failed); tracking by a
  random token GET /api/orders/track/{token}; old /status/{ref} only while unambiguous.
- Alembic 0003: online_orders sales_point, table_label, tracking_token; table rebuilt.
- menu-app: tracking link uses the token, shows the order number; tells the customer when
  the menu changed or the order failed.
- Tests: test_checkout.py (6), 0003 data migration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 11:38:37 +02:00

81 lines
4.0 KiB
Python

"""online orders: sales_point, table_label, tracking_token; public_ref unique per site (plan phase 3S, S.2)
- sales_point / table_label: the cloud prices orders per sales point; the QR menu's table
- public_ref ("ORD-0001") counts per site but was unique across all sites, so a second
site's first online order failed → unique per (site_id, public_ref)
- tracking_token: the customer's tracking page used the public ref alone (ambiguous across
sites and guessable) → a random token per order (existing orders get one)
The table is rebuilt from an explicit definition (SQLite can't drop the unnamed
UNIQUE(public_ref) in place; copy_from leaves it out, the indexes are kept).
Revision ID: 0003_order_sales_point
Revises: 0002_v2_licensing
Create Date: 2026-11-15 23:00:00
"""
import secrets
from alembic import op
import sqlalchemy as sa
revision = '0003_order_sales_point'
down_revision = '0002_v2_licensing'
branch_labels = None
depends_on = None
def _base(*extra):
"""online_orders as 0001 created it, without UNIQUE(public_ref), plus `extra`."""
return sa.Table(
'online_orders', sa.MetaData(),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('site_id', sa.Integer(), nullable=False),
sa.Column('public_ref', sa.String(), nullable=False),
sa.Column('order_type', sa.String(), nullable=False),
sa.Column('customer_name', sa.String(), nullable=False),
sa.Column('customer_phone', sa.String(), nullable=True),
sa.Column('customer_address', sa.Text(), nullable=True),
sa.Column('customer_notes', sa.Text(), nullable=True),
sa.Column('items_json', sa.Text(), nullable=False),
sa.Column('subtotal', sa.Float(), nullable=False),
sa.Column('delivery_fee', sa.Float(), nullable=False),
sa.Column('total', sa.Float(), nullable=False),
sa.Column('status', sa.String(), nullable=False),
sa.Column('rejection_reason', sa.String(), nullable=True),
sa.Column('synced_to_local', sa.Integer(), nullable=False),
sa.Column('local_order_id', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
*extra,
sa.ForeignKeyConstraint(['site_id'], ['sites.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.Index('ix_online_orders_id', 'id'),
sa.Index('ix_online_orders_site_id', 'site_id'),
sa.Index('ix_online_orders_status', 'status'),
)
def upgrade() -> None:
with op.batch_alter_table('online_orders', copy_from=_base(), recreate='always') as batch_op:
batch_op.add_column(sa.Column('table_label', sa.String(), nullable=True))
batch_op.add_column(sa.Column('sales_point', sa.String(), nullable=True))
batch_op.add_column(sa.Column('tracking_token', sa.String(), nullable=True))
batch_op.create_unique_constraint('uq_online_orders_site_ref', ['site_id', 'public_ref'])
batch_op.create_unique_constraint('uq_online_orders_tracking_token', ['tracking_token'])
conn = op.get_bind()
for (oid,) in conn.execute(sa.text("select id from online_orders where tracking_token is null")).fetchall():
conn.execute(sa.text("update online_orders set tracking_token = :t where id = :i"),
{"t": secrets.token_urlsafe(12), "i": oid})
def downgrade() -> None:
current = _base(sa.Column('table_label', sa.String(), nullable=True),
sa.Column('sales_point', sa.String(), nullable=True),
sa.Column('tracking_token', sa.String(), nullable=True))
with op.batch_alter_table('online_orders', copy_from=current, recreate='always') as batch_op:
batch_op.drop_column('tracking_token')
batch_op.drop_column('sales_point')
batch_op.drop_column('table_label')
batch_op.create_unique_constraint('uq_online_orders_public_ref', ['public_ref'])