Auf der Set-Detailseite lassen sich Preise notieren: Betrag, Zustand, Datum, wo gesehen und eine Notiz. Daraus entsteht eine Reihe statt eines einzelnen Feldes, denn die interessante Frage ist nicht "was kostet es", sondern "was hat es seit dem Kauf gemacht". Als Ausgangswert dient die UVP, sonst die aelteste Beobachtung; der aktuelle Wert ist die neueste. Fallen am selben Tag zwei Beobachtungen an, gewinnt der Neupreis - das ist die Zahl, die man zum Ersetzen zahlen wuerde. Die Statistik-Kachel in der Sammlung zeigt entsprechend "Aktueller Wert", sobald irgendwo ein Preis notiert ist, sonst unveraendert "Wert nach UVP". Bewusst nicht dabei: die Anbindung des BrickLink-Preisfuehrers. Sie war im Plan als optional vermerkt und braucht OAuth1 mit einer pro Nutzer registrierten Anwendung - viel Apparat fuer eine Zahl, die sich in zehn Sekunden eintippen laesst. Die Datenstruktur ist dafuer offen: das Feld "Wo" nimmt jede Quelle auf. 13 Tests fuer die Rechenwege, inklusive Division durch null, fehlender UVP und Rundung. Refs Phase 3 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
import { check, index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';
|
|
import { sql } from 'drizzle-orm';
|
|
import { set } from './catalog';
|
|
|
|
/**
|
|
* An observed price for a set at a point in time.
|
|
*
|
|
* Kept as a series rather than a single field, because the interesting
|
|
* question is not "what does it cost" but "what has it done since we bought
|
|
* it". Entries are maintained by hand - the UVP from the set itself is the
|
|
* starting point, everything after that is what you actually saw somewhere.
|
|
*/
|
|
export const pricePoint = sqliteTable(
|
|
'price_point',
|
|
{
|
|
id: text('id').primaryKey(),
|
|
setId: text('set_id')
|
|
.notNull()
|
|
.references(() => set.id, { onDelete: 'cascade' }),
|
|
/** EUR in cents, like every other amount in this app. */
|
|
priceCents: integer('price_cents').notNull(),
|
|
condition: text('condition', { enum: ['new', 'used'] })
|
|
.notNull()
|
|
.default('new'),
|
|
/** Where the price was seen, e.g. "Amazon", "Flohmarkt", "BrickLink". */
|
|
source: text('source'),
|
|
observedAt: integer('observed_at', { mode: 'timestamp' }).notNull(),
|
|
note: text('note'),
|
|
createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
|
|
},
|
|
(t) => [
|
|
index('price_point_set_idx').on(t.setId, t.observedAt),
|
|
check('price_point_positive', sql`${t.priceCents} >= 0`),
|
|
],
|
|
);
|