Files
where_woof/frontend/internal/db/queries.sql
Sam Rolfe aabaa750f4 Phase 5 billing: account-based annual sub + tag SKUs + SMS pool + tag lifecycle
- Schema: plans, orders subscription cols + plan_id + period_started_at,
  billing_events log, users.stripe_id + sms_credits, products.price_aud
  (db/schema.sql + Laravel migration, idempotent)
- Laravel/Cashier: Billable User, checkout (annual sub + one-off SKUs with
  Managed Payments tax_code), webhook controller (signature-verified,
  idempotent, BILLING_ENABLED kill-switch), account tag transitions,
  nightly reconcile, Stripe portal link, PlanResource + billing dashboard
- Go frontend: account-level gating (paid sub required), SMS pool
  (included 50/yr + credits, drawn after included), plan caps replace
  constants, 60s plan cache (credits fresh), 25-tag cap (plan max_tags)
- BillingSeeder: personal plan + 3 SKUs + dev paid orders
- Verified test-mode e2e: subscribe/paid/active/alerts, pool exhaust +
  credits resume, lapsed/suspended, cancelled/closed, recover/active,
  webhook idempotency, 25-cap, one-off SKUs, replacement, kill-switch,
  invalid signature 400
2026-08-28 13:19:32 +10:00

149 lines
4.4 KiB
SQL

-- name: CreateUser :one
INSERT INTO users (email, password_hash, name, phone)
VALUES ($1, $2, $3, $4)
RETURNING *;
-- name: GetUserByEmail :one
SELECT * FROM users WHERE email = $1;
-- name: GetUserByID :one
SELECT * FROM users WHERE id = $1;
-- name: ListTagsByOwner :many
SELECT * FROM tags WHERE owner_id = $1 ORDER BY created_at DESC;
-- name: GetTagByCode :one
SELECT * FROM tags WHERE tag_code = $1;
-- name: GetTagByID :one
SELECT * FROM tags WHERE id = $1;
-- name: CountTagsByOwner :one
SELECT count(*) FROM tags WHERE owner_id = $1;
-- name: BindTag :one
UPDATE tags
SET owner_id = $1, updated_at = now()
WHERE tag_code = $2 AND owner_id IS NULL AND status = 'unset'
RETURNING *;
-- name: UpdateTagDetails :one
UPDATE tags
SET item_type=$2, description=$3, photo_url=$4, phone=$5, address=$6, notes=$7, sms_enabled=$8, status='active', updated_at=now()
WHERE id=$1
RETURNING *;
-- name: ClearTagOwner :one
UPDATE tags
SET owner_id=NULL, status='unset', item_type=NULL, description=NULL, photo_url=NULL, phone=NULL, address=NULL, notes=NULL, updated_at=now()
WHERE id=$1
RETURNING *;
-- name: SetTagStatus :exec
UPDATE tags SET status=$2, updated_at=now() WHERE id=$1;
-- name: InsertTag :one
INSERT INTO tags (tag_code) VALUES ($1)
RETURNING *;
-- name: UpsertAdmin :one
INSERT INTO users (email, password_hash, name, is_admin)
VALUES ($1, $2, $3, true)
ON CONFLICT (email) DO UPDATE SET password_hash = EXCLUDED.password_hash, name = EXCLUDED.name, is_admin = true
RETURNING *;
-- name: InsertScan :one
INSERT INTO scans (tag_id, lat, lng, location_shared, scanner_phone, fingerprint, ip)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING *;
-- name: GetLastAlertByTag :one
SELECT * FROM scans
WHERE tag_id = $1 AND alert_sent = TRUE
ORDER BY scanned_at DESC
LIMIT 1;
-- name: GetLatestScanByTag :one
SELECT * FROM scans
WHERE tag_id = $1
ORDER BY scanned_at DESC
LIMIT 1;
-- name: SetScanAlertSent :exec
UPDATE scans SET alert_sent = $2 WHERE id = $1;
-- name: SetScanPhone :exec
UPDATE scans SET scanner_phone = $2, fingerprint = $3 WHERE id = $1;
-- name: GetRecentScanByFingerprint :one
SELECT * FROM scans
WHERE fingerprint = $1 AND id <> $2 AND scanned_at > now() - interval '24 hours'
ORDER BY scanned_at DESC
LIMIT 1;
-- name: ListRecentScansWithTag :many
SELECT s.id, s.tag_id, s.scanned_at, s.lat, s.lng, s.location_shared, s.alert_sent,
t.tag_code, t.description, t.item_type
FROM scans s
JOIN tags t ON t.id = s.tag_id
ORDER BY s.scanned_at DESC
LIMIT $1;
-- name: GetOrderByID :one
SELECT * FROM orders WHERE id = $1;
-- name: CountAlertsByTagSince :one
SELECT count(*) FROM scans
WHERE tag_id = $1 AND alert_sent = TRUE AND scanned_at > $2;
-- name: CountAlertsByIPSince :one
SELECT count(*) FROM scans
WHERE ip = $1 AND alert_sent = TRUE AND scanned_at > $2;
-- name: AddSmsUsed :exec
UPDATE tags SET sms_used = sms_used + 1, updated_at = now() WHERE id = $1;
-- name: InsertShortCode :one
INSERT INTO url_shortened (code, scan_id) VALUES ($1, $2) RETURNING *;
-- name: GetShortCode :one
SELECT * FROM url_shortened WHERE code = $1;
-- name: GetScanByID :one
SELECT * FROM scans WHERE id = $1;
-- name: GetActiveOrderByAccount :one
-- The account's current paid subscription order (if any), with its plan.
SELECT o.*, p.plan_type, p.price_aud, p.sms_included, p.max_tags,
p.alerts_per_day, p.alerts_per_hour
FROM orders o
LEFT JOIN plans p ON p.id = o.plan_id
WHERE o.account_id = $1 AND o.status = 'paid'
ORDER BY o.id DESC
LIMIT 1;
-- name: GetActiveOrderByOwner :one
-- Account-level gating for a tag: resolve the tag's owner account, then the
-- account's active paid subscription order (with plan). Returns zero rows
-- when the tag is unowned or the account has no paid order.
SELECT o.*, p.plan_type, p.price_aud, p.sms_included, p.max_tags,
p.alerts_per_day, p.alerts_per_hour
FROM tags t
JOIN orders o ON o.account_id = t.owner_id AND o.status = 'paid'
LEFT JOIN plans p ON p.id = o.plan_id
WHERE t.id = $1
ORDER BY o.id DESC
LIMIT 1;
-- name: CountAlertsByAccountSince :one
-- SMS pool draw: alerts sent for any tag owned by the account in the period.
SELECT count(*) FROM scans s
JOIN tags t ON t.id = s.tag_id
WHERE t.owner_id = $1 AND s.alert_sent = TRUE AND s.scanned_at > $2;
-- name: CountOwnedTags :one
-- Live owned-tag count for the per-account cap: everything bound to the
-- account except retired (closed) tags, including bound-but-unset codes.
SELECT count(*) FROM tags
WHERE owner_id = $1 AND status <> 'closed';