diff --git a/backend/apps/backend/src/scripts/setup-neu-2026.ts b/backend/apps/backend/src/scripts/setup-neu-2026.ts new file mode 100644 index 0000000..e7c4493 --- /dev/null +++ b/backend/apps/backend/src/scripts/setup-neu-2026.ts @@ -0,0 +1,194 @@ +/** + * Die Neuheiten aus der Preisliste 9/2026. + * + * npx medusa exec ./src/scripts/setup-neu-2026.ts + * + * Zwei Dinge: + * 1. Balsamico-Essig 250 ml, 12,90 € — neues Produkt in der Kategorie + * "Feinkost", die dafür angelegt wird. + * 2. Vier neue Kerzen als eigene Produkte. + * + * Ursprünglich sollten sie Varianten des vorhandenen Produkts + * "Bienenwachskerzen" werden. Das geht nicht: Dessen Option "Variante" + * hat keinerlei Werte, und die vier bestehenden Varianten hängen an + * keinem — ein Überbleibsel der Shopify-Übernahme. Medusa lässt weder + * eine Variante ohne Wert zu, noch nimmt es nachträglich Werte für + * diese Option an. + * + * Eigene Produkte sind hier ohnehin das Bessere: Jede Form bekommt ein + * eigenes Foto und eine eigene Kachel im Shop. + * + * Neue Produkte entstehen als Entwurf: Ohne Foto wäre im Raster eine leere + * Kachel zu sehen. Ein erneuter Lauf veröffentlicht sie, sobald ein Bild + * hinterlegt ist. + * + * Wiederholbar: Vorhandenes wird nicht doppelt angelegt. + */ +import { + createProductCategoriesWorkflow, + createProductsWorkflow, + createProductVariantsWorkflow, + updateProductsWorkflow, +} from "@medusajs/core-flows"; + +const KATEGORIE = "Feinkost"; +const BALSAMICO = "balsamico-essig"; + +/** Die vier neuen Formen. Gewichte, wo die Liste sie nennt. */ +const KERZEN: { titel: string; preis: number; gramm?: number }[] = [ + { titel: "Bubble Kerze", preis: 9.9, gramm: 180 }, + { titel: "Regenbogen", preis: 7.9 }, + { titel: "Eierform 1", preis: 5.5 }, + { titel: "Eierform 2", preis: 5.5 }, +]; + +export default async function ({ container }: any) { + const query = container.resolve("query"); + const logger = container.resolve("logger"); + + // --- 1. Balsamico --------------------------------------------------------- + const { data: kategorien } = await query.graph({ + entity: "product_category", + fields: ["id", "name", "rank"], + }); + + let kategorie = (kategorien ?? []).find((c: any) => c.name === KATEGORIE); + if (!kategorie) { + const letzterRang = Math.max(0, ...(kategorien ?? []).map((c: any) => c.rank ?? 0)); + const { result } = await createProductCategoriesWorkflow(container).run({ + input: { + product_categories: [ + { name: KATEGORIE, handle: "feinkost", is_active: true, rank: letzterRang + 1 }, + ], + }, + }); + kategorie = (result as any)[0]; + logger.info(`Kategorie "${KATEGORIE}" angelegt.`); + } + + const { data: vorhanden } = await query.graph({ + entity: "product", + fields: ["id", "title", "status", "thumbnail"], + filters: { handle: BALSAMICO }, + }); + + if (vorhanden?.[0]) { + const p = vorhanden[0] as any; + if (p.thumbnail && p.status !== "published") { + await updateProductsWorkflow(container).run({ + input: { selector: { id: p.id }, update: { status: "published" } } as any, + }); + logger.info(`"${p.title}" hat ein Bild und ist jetzt veröffentlicht.`); + } else { + logger.info(`"${p.title}" existiert bereits (${p.status}).`); + } + } else { + const { data: profile } = await query.graph({ + entity: "shipping_profile", + fields: ["id"], + }); + const { data: kanaele } = await query.graph({ entity: "sales_channel", fields: ["id"] }); + + await createProductsWorkflow(container).run({ + input: { + products: [ + { + title: "Balsamico-Essig", + handle: BALSAMICO, + status: "draft" as any, + description: + "Balsamico-Essig, 250 ml.\n\n" + + "⚠️ Platzhalter: Beschreibung, Zutaten und Herkunft fehlen noch. " + + "Bei Lebensmitteln sind Zutatenverzeichnis, Nährwerte und " + + "Mindesthaltbarkeit vorgeschrieben, bevor verkauft werden darf.", + shipping_profile_id: profile?.[0]?.id, + sales_channels: kanaele?.[0] ? [{ id: kanaele[0].id }] : undefined, + metadata: { category: KATEGORIE, delivery_time: "2-3 Werktage" }, + category_ids: [kategorie.id], + options: [{ title: "Größe", values: ["250 ml"] }], + variants: [ + { + title: "250 ml", + options: { "Größe": "250 ml" }, + manage_inventory: false, + // Geschätzt: Flasche samt Inhalt und Polster. Vor dem ersten + // echten Versand prüfen, sonst stimmt das Porto nicht. + weight: 700, + prices: [{ amount: 12.9, currency_code: "eur" }], + }, + ], + }, + ], + }, + }); + logger.info('"Balsamico-Essig" angelegt: 12,90 €, Entwurf, wartet auf das Foto.'); + } + + // --- 2. Neue Kerzen als eigene Produkte ----------------------------------- + const { data: kerzenKategorie } = await query.graph({ + entity: "product_category", + fields: ["id", "name"], + }); + const bienenprodukte = (kerzenKategorie ?? []).find((c: any) => c.name === "Bienenprodukte"); + + const { data: profile2 } = await query.graph({ entity: "shipping_profile", fields: ["id"] }); + const { data: kanaele2 } = await query.graph({ entity: "sales_channel", fields: ["id"] }); + + for (const kerze of KERZEN) { + const handle = kerze.titel.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, ""); + + const { data: da } = await query.graph({ + entity: "product", + fields: ["id", "title", "status", "thumbnail"], + filters: { handle }, + }); + + if (da?.[0]) { + const vorhandenesProdukt = da[0] as any; + if (vorhandenesProdukt.thumbnail && vorhandenesProdukt.status !== "published") { + await updateProductsWorkflow(container).run({ + input: { selector: { id: vorhandenesProdukt.id }, update: { status: "published" } } as any, + }); + logger.info(`"${vorhandenesProdukt.title}" hat ein Bild und ist jetzt veröffentlicht.`); + } else { + logger.info(`"${vorhandenesProdukt.title}" existiert bereits (${vorhandenesProdukt.status}).`); + } + continue; + } + + await createProductsWorkflow(container).run({ + input: { + products: [ + { + title: kerze.titel, + handle, + status: "draft" as any, + description: + `${kerze.titel} aus reinem Bienenwachs.\n\n` + + "⚠️ Platzhalter: Beschreibung, Maße und Brenndauer fehlen noch.", + shipping_profile_id: profile2?.[0]?.id, + sales_channels: kanaele2?.[0] ? [{ id: kanaele2[0].id }] : undefined, + metadata: { category: "Bienenprodukte", delivery_time: "2-3 Werktage" }, + ...(bienenprodukte ? { category_ids: [bienenprodukte.id] } : {}), + options: [{ title: "Ausführung", values: ["Standard"] }], + variants: [ + { + title: "Standard", + options: { "Ausführung": "Standard" }, + manage_inventory: false, + ...(kerze.gramm ? { weight: kerze.gramm + 70 } : {}), + prices: [{ amount: kerze.preis, currency_code: "eur" }], + }, + ], + }, + ], + }, + }); + logger.info(`"${kerze.titel}" angelegt: ${kerze.preis.toFixed(2)} €, Entwurf.`); + } + + const ohneGewicht = KERZEN.filter((k) => !k.gramm).map((k) => k.titel); + if (ohneGewicht.length) { + logger.info(`Ohne Versandgewicht, bitte nachtragen: ${ohneGewicht.join(", ")}`); + } +}