Superadmin view in /admin showing operating cost with per-restaurant breakdown.
Models (migration 0012):
- TokenUsage(restaurantId?, model, feature, inputTokens, outputTokens, createdAt)
- ModelPrice(model, inputPricePerMTok, outputPricePerMTok, updatedAt) — seeded
Haiku 4.5 1/5, Sonnet 4.6 3/15, Opus 4.8 5/25 (USD per million)
- PriceUpdateLog(model, old/new input+output, source, createdAt)
- ExchangeRate(date, rate) — daily USD→MXN cache
- AppConfig(key,value) — seeded costPerGb=0
B1 Instrumentation: recordUsage() (fire-and-forget) logs every Anthropic call with
its model/feature/restaurant and the response usage tokens — wired into the
consultor (ai-consult), translation, and menu import. restaurantId null = Collina
operational.
B2 Price update: 'Update prices from Anthropic' button → fetches the public pricing
page, Haiku extracts prices FROM THE PAGE TEXT ONLY (omits any not found), updates
ModelPrice and writes a PriceUpdateLog (old→new). If the fetch is blocked, the UI
offers a manual paste box processed the same way. These calls log as
feature 'precios-update', restaurantId null.
B3 Exchange: getUsdMxn() caches once/day via Frankfurter (no key); on failure falls
back to the last stored rate, then a constant — never /bin/zsh; shows the rate's date.
B4 Storage: measures GB per restaurant from /data/uploads/{slug}; configurable
costPerGb (editable) projects a storage cost.
B5 Dashboard (CostsDashboard): month total in USD + MXN, per-restaurant token cost
(USD/MXN) + GB + projected storage cost, average per active restaurant, a separate
'Operativo Collina' line for restaurantId-null calls, and context (exchange rate +
date, last price update). i18n es/en at parity.
Verified (build passes; migration 0012 applies; seeds present): a usage row records
model/tokens/restaurant; dashboard shows USD + MXN with the day's rate (Frankfurter
live 17.21, cached); token cost math correct (/bin/zsh.04 for 20k/4k Haiku); storage GB +
projected cost with cost/GB=0.10; operational calls isolated; price endpoint guarded
(401) and graceful without key (502); PriceUpdateLog records old→new.
170 lines
6.2 KiB
Plaintext
170 lines
6.2 KiB
Plaintext
generator client {
|
|
provider = "prisma-client-js"
|
|
// native = host platform (builder/runner are both node:20-alpine = musl).
|
|
// The explicit musl target guards the runtime engine in the final image.
|
|
binaryTargets = ["native", "linux-musl-openssl-3.0.x"]
|
|
}
|
|
|
|
datasource db {
|
|
provider = "postgresql"
|
|
url = env("DATABASE_URL")
|
|
}
|
|
|
|
model Restaurant {
|
|
id String @id @default(cuid())
|
|
slug String @unique
|
|
name String
|
|
editorSecret String
|
|
username String
|
|
hashedPassword String
|
|
palette Json @default("{}")
|
|
qrConfig Json @default("{}") // artistic QR styling (qr-code-styling config)
|
|
ordersEnabled Boolean @default(false)
|
|
splitEnabled Boolean @default(false) // enables collaborative split-by-table accounts
|
|
orderRetentionHours Int @default(24) // completed orders older than this are pruned on read
|
|
timerGreenMax Int @default(10) // minutes: under this an item timer is green
|
|
timerYellowMax Int @default(20) // minutes: under this yellow, over this red
|
|
showMacros Boolean @default(false) // when false, the public menu hides all macro layers
|
|
pairings Json @default("{}") // precomputed cart suggestions { productId: [productId] }
|
|
bannerImage String? // wide brand banner (uploaded)
|
|
profileImage String? // circular logo/avatar (uploaded)
|
|
socialLinks Json @default("[]") // [{ platform, url }]
|
|
|
|
defaultLocale String @default("es")
|
|
active Boolean @default(true)
|
|
publishedVersionId String? @unique
|
|
createdAt DateTime @default(now())
|
|
|
|
publishedVersion MenuVersion? @relation("PublishedVersion", fields: [publishedVersionId], references: [id])
|
|
versions MenuVersion[] @relation("RestaurantVersions")
|
|
sections Section[]
|
|
products Product[]
|
|
orders Order[]
|
|
}
|
|
|
|
// Versioning system: a published snapshot of the whole menu.
|
|
model MenuVersion {
|
|
id String @id @default(cuid())
|
|
restaurantId String
|
|
versionName String
|
|
snapshot Json
|
|
publishedAt DateTime @default(now())
|
|
createdBy String?
|
|
|
|
restaurant Restaurant @relation("RestaurantVersions", fields: [restaurantId], references: [id], onDelete: Cascade)
|
|
publishedFor Restaurant? @relation("PublishedVersion")
|
|
|
|
@@index([restaurantId])
|
|
}
|
|
|
|
// Working DRAFT (not what the public sees).
|
|
model Section {
|
|
id String @id @default(cuid())
|
|
restaurantId String
|
|
name Json // { es, en }
|
|
order Int @default(0)
|
|
showMacros Boolean @default(true) // per-section override; only applies when Restaurant.showMacros is on
|
|
createdAt DateTime @default(now())
|
|
|
|
restaurant Restaurant @relation(fields: [restaurantId], references: [id], onDelete: Cascade)
|
|
products Product[]
|
|
|
|
@@index([restaurantId])
|
|
}
|
|
|
|
// Working DRAFT product.
|
|
model Product {
|
|
id String @id @default(cuid())
|
|
sectionId String
|
|
restaurantId String
|
|
name Json // { es, en }
|
|
description Json @default("{}") // { es, en }
|
|
price Decimal @default(0) @db.Decimal(10, 2)
|
|
order Int @default(0)
|
|
isActive Boolean @default(true)
|
|
allergens String[] @default([])
|
|
dietTags String[] @default([])
|
|
macros Json @default("{}") // { protein, carbs, sugar, fat, fiber, kcal }
|
|
media Json @default("[]") // [{ type, path, order }]
|
|
translationStatus Json @default("{}") // { en: auto|reviewed }
|
|
aiEstimated Json @default("{}") // { macros: bool, allergens: bool } — flags AI-inferred data to verify
|
|
createdAt DateTime @default(now())
|
|
|
|
section Section @relation(fields: [sectionId], references: [id], onDelete: Cascade)
|
|
restaurant Restaurant @relation(fields: [restaurantId], references: [id], onDelete: Cascade)
|
|
|
|
@@index([restaurantId])
|
|
@@index([sectionId])
|
|
}
|
|
|
|
model Order {
|
|
id String @id @default(cuid())
|
|
restaurantId String
|
|
tableNumber Int
|
|
items Json // [{ productId, name, qty, note?, price?, addedAt? }]
|
|
note String? // general note for the whole order
|
|
status String @default("active") // active | completed
|
|
// Split: each Order is one subaccount of a table. A table is "in split" when
|
|
// any active order for that tableNumber has split=true. Reuses table grouping.
|
|
split Boolean @default(false)
|
|
subaccountName String? // diner name for the subaccount (null = unnamed/"table")
|
|
paid Boolean @default(false) // restaurant marks a subaccount as paid
|
|
createdAt DateTime @default(now())
|
|
completedAt DateTime?
|
|
|
|
restaurant Restaurant @relation(fields: [restaurantId], references: [id], onDelete: Cascade)
|
|
|
|
@@index([restaurantId, status])
|
|
}
|
|
|
|
// ── Cost dashboard (superadmin) ──────────────────────────────────────────────
|
|
|
|
// One row per Anthropic API call. restaurantId null = Collina operational calls.
|
|
model TokenUsage {
|
|
id String @id @default(cuid())
|
|
restaurantId String? // null = operational (e.g. price-update button)
|
|
model String
|
|
feature String // consultor | traduccion | import | precios-update
|
|
inputTokens Int @default(0)
|
|
outputTokens Int @default(0)
|
|
createdAt DateTime @default(now())
|
|
|
|
@@index([createdAt])
|
|
@@index([restaurantId, createdAt])
|
|
}
|
|
|
|
// Editable per-model pricing (USD per million tokens).
|
|
model ModelPrice {
|
|
model String @id
|
|
inputPricePerMTok Float
|
|
outputPricePerMTok Float
|
|
updatedAt DateTime @default(now())
|
|
}
|
|
|
|
// Audit trail of price changes (old → new).
|
|
model PriceUpdateLog {
|
|
id String @id @default(cuid())
|
|
model String
|
|
oldInput Float?
|
|
newInput Float?
|
|
oldOutput Float?
|
|
newOutput Float?
|
|
source String @default("anthropic") // anthropic | manual
|
|
createdAt DateTime @default(now())
|
|
|
|
@@index([createdAt])
|
|
}
|
|
|
|
// Daily USD→MXN exchange rate cache.
|
|
model ExchangeRate {
|
|
date String @id // YYYY-MM-DD
|
|
rate Float
|
|
createdAt DateTime @default(now())
|
|
}
|
|
|
|
// Generic key/value app config (e.g. costPerGb).
|
|
model AppConfig {
|
|
key String @id
|
|
value String
|
|
}
|