import { randomUUID } from "node:crypto"; import type pg from "pg"; // Ein Haushalt mit je mindestens einer Zeile in **jeder** Haushaltstabelle. // Kommt eine Tabelle dazu, gehört sie hierher — tests/rls.test.ts bricht sonst. export const SEEDED_TABLES = [ "households", "members", "passkeys", "sessions", "invitations", "stores", "store_aisles", "dishes", "dish_ingredients", "dish_steps", "dish_translations", "rounds", "wishes", "slots", "shopping_items", "pantry_staples", "receipts", "receipt_lines", "ingredient_aliases", "price_points", "ratings", "kudos", "helper_points", "achievements", "household_ai", "kid_devices", "dish_photos", "ingredients", "household_products", "household_pack_sizes", "household_mail", "mail_tokens", ]; /** Eine globale Zutat, wie sie eine Seed-Migration anlegt. */ export async function seedIngredient(client: pg.Client | pg.PoolClient): Promise { const id = randomUUID(); await client.query("begin"); await client.query("select set_config('app.maintenance', 'on', true)"); await client.query( "insert into ingredients (id, key, category, default_unit, names) values ($1, $2, 'meat', 'g', $3)", [id, `test_${id.replace(/-/g, "")}`, JSON.stringify({ de: "Hackfleisch", en: "Ground beef" })], ); await client.query("commit"); return id; } /** Legt den Haushalt `h` mit allem an; läuft in einer eigenen Transaktion als dieser Haushalt. */ export async function seedHousehold(client: pg.Client | pg.PoolClient, h: string, ingredient: string): Promise<{ dish: string; member: string }> { const q = (sql: string, params: unknown[] = []) => client.query(sql, params); const member = randomUUID(); const dish = randomUUID(); const store = randomUUID(); const round = randomUUID(); const slot = randomUUID(); const receipt = randomUUID(); const passkey = `pk-${h}`; await q("begin"); await q("select set_config('app.household_id', $1, true)", [h]); await q("insert into households (id, name) values ($1, $2)", [h, `Haushalt ${h.slice(0, 4)}`]); await q("insert into members (id, household_id, display_name, role) values ($1, $2, 'Kai', 'admin')", [member, h]); await q("update households set created_by = $1 where id = $2", [member, h]); await q("insert into passkeys (id, member_id, household_id, public_key) values ($1, $2, $3, '\\x0102')", [passkey, member, h]); await q( "insert into sessions (token_hash, member_id, household_id, passkey_id, expires_at) values (sha256(convert_to($1, 'utf8')), $2, $3, $4, now() + interval '1 day')", [randomUUID(), member, h, passkey], ); await q( "insert into invitations (household_id, token_hash, display_name, created_by, expires_at) values ($1, sha256(convert_to($2, 'utf8')), 'Maike', $3, now() + interval '1 day')", [h, randomUUID(), member], ); await q("insert into stores (id, household_id, name, chain, is_default) values ($1, $2, 'REWE', 'rewe', true)", [store, h]); await q("insert into store_aisles (household_id, store_id, position, name) values ($1, $2, 1, 'Obst')", [h, store]); await q("insert into dishes (id, household_id, title, base, created_by) values ($1, $2, 'Bolognese', 'pasta', $3)", [dish, h, member]); await q( "insert into dish_ingredients (household_id, dish_id, ingredient_id, qty_per_portion, unit) values ($1, $2, $3, 125, 'g')", [h, dish, ingredient], ); await q("insert into dish_steps (household_id, dish_id, position, text) values ($1, $2, 1, 'Anbraten')", [h, dish]); await q("insert into dish_photos (household_id, dish_id, path, position) values ($1, $2, $3, 1)", [h, dish, `${h}/${dish}-test.webp`]); await q("insert into dish_translations (household_id, dish_id, locale, title) values ($1, $2, 'en', 'Bolognese')", [h, dish]); await q( "insert into rounds (id, household_id, slots_count, default_portions, store_id, created_by) values ($1, $2, 4, 4, $3, $4)", [round, h, store, member], ); await q("insert into wishes (household_id, round_id, member_id, dish_id) values ($1, $2, $3, $4)", [h, round, member, dish]); await q( "insert into slots (id, household_id, round_id, position, dish_id, portions, cook_member_id, cooked_at) values ($1, $2, $3, 1, $4, 4, $5, now())", [slot, h, round, dish, member], ); await q( "insert into shopping_items (household_id, round_id, ingredient_id, label, qty, unit) values ($1, $2, $3, 'Hackfleisch', 500, 'g')", [h, round, ingredient], ); await q("insert into pantry_staples (household_id, ingredient_id) values ($1, $2)", [h, ingredient]); await q("insert into receipts (id, household_id, store_id, total, created_by) values ($1, $2, $3, 12.5, $4)", [receipt, h, store, member]); await q( "insert into receipt_lines (household_id, receipt_id, position, raw_text, ingredient_id) values ($1, $2, 1, 'RINDERHACK 500G', $3)", [h, receipt, ingredient], ); await q("insert into ingredient_aliases (household_id, raw_text_normalized, ingredient_id) values ($1, 'rinderhack 500g', $2)", [h, ingredient]); await q( "insert into price_points (household_id, ingredient_id, store_id, price_per_unit, unit, source, receipt_id) values ($1, $2, $3, 0.0099, 'g', 'receipt', $4)", [h, ingredient, store, receipt], ); await q("insert into ratings (household_id, slot_id, member_id, thumbs_up) values ($1, $2, $3, true)", [h, slot, member]); await q("insert into kudos (household_id, slot_id, from_member_id) values ($1, $2, $3)", [h, slot, member]); await q("insert into helper_points (household_id, member_id, points, granted_by) values ($1, $2, 1, $2)", [h, member]); await q("insert into achievements (household_id, key) values ($1, 'first_round')", [h]); await q("insert into kid_devices (household_id, label, token_hash, created_by) values ($1, 'Tablet', sha256(convert_to($2, 'utf8')), $3)", [h, `kid-${h}`, member]); const own = (await q("insert into ingredients (key, category, default_unit, names, household_id) values ($1, 'pasta_rice', 'g', '{\"de\":\"Gnocchi\"}', $2) returning id", [`own_${h.replace(/-/g, "")}`, h])).rows[0].id; await q("insert into household_products (household_id, ingredient_id, product) values ($1, $2, 'Barilla Nr. 5')", [h, own]); await q("insert into household_pack_sizes (household_id, ingredient_id, unit, pack_size) select $1, id, 'can', 500 from ingredients where key = 'chopped_tomatoes'", [h]); await q("insert into household_ai (household_id, endpoint, vision_model, updated_by) values ($1, 'http://llm.test/v1', 'vision', $2)", [h, member]); await q("insert into household_mail (household_id, host, port, username, from_address) values ($1, 'smtp.test', 465, 'resend', 'hallo@test.example')", [h]); await q("insert into mail_tokens (household_id, member_id, purpose, email, token_hash, expires_at) values ($1, $2, 'verify', $3, sha256(convert_to($3, 'utf8')), now() + interval '1 hour')", [h, member, `kai-${h}@test.example`]); await q("commit"); return { dish, member }; }