diff --git a/backend/apps/backend/src/scripts/ergaenzungen-2026.ts b/backend/apps/backend/src/scripts/ergaenzungen-2026.ts new file mode 100644 index 0000000..097df04 --- /dev/null +++ b/backend/apps/backend/src/scripts/ergaenzungen-2026.ts @@ -0,0 +1,91 @@ +import { + createProductVariantsWorkflow, + updateProductOptionsWorkflow, + updateProductVariantsWorkflow, +} from "@medusajs/core-flows"; + +/** + * Lücken gegenüber der Preisliste 9/2026 schließen. + * + * - Sonnenblumenhonig steht dort auch mit 50 g (2,80 €), im Shop gab es nur + * 250 g. + * - Versandgewichte, die fehlten: Buchweizenhonig und Honig-Abo. Es sind + * dieselben Gläser wie bei allen anderen Honigen, also dieselben + * Bruttogewichte (50 g → 100 g, 250 g → 400 g, 500 g → 700 g). + * + * Mehrfach ausführbar: legt nichts doppelt an, setzt dieselben Werte. + * + * npx medusa exec ./src/scripts/ergaenzungen-2026.ts + */ + +const BRUTTO: Record = { "50 Gramm": 100, "250 Gramm": 400, "500 Gramm": 700 }; + +export default async function ({ container }: any) { + const logger = container.resolve("logger"); + const query = container.resolve("query"); + + const { data: produkte } = await query.graph({ + entity: "product", + fields: [ + "id", + "handle", + "options.id", + "options.title", + "options.values.value", + "variants.id", + "variants.title", + "variants.weight", + "variants.metadata", + ], + filters: { + handle: ["sonnenblumenhonig-aus-der-harzregion", "buchweizenhonig-aus-der-oder-spree-region", "honig-abo"], + }, + }); + const nach = new Map(produkte.map((p: any) => [p.handle, p])); + + // --- Sonnenblumenhonig 50 g --------------------------------------------- + const sonne: any = nach.get("sonnenblumenhonig-aus-der-harzregion"); + if (!sonne) { + logger.warn("[ergaenzung] Sonnenblumenhonig nicht gefunden."); + } else if (sonne.variants.some((v: any) => v.title === "50 Gramm")) { + logger.info("[ergaenzung] Sonnenblumenhonig 50 g gibt es schon."); + } else { + const option = sonne.options[0]; + const werte = option.values.map((v: any) => v.value); + if (!werte.includes("50 Gramm")) { + await updateProductOptionsWorkflow(container).run({ + input: { selector: { id: option.id }, update: { values: ["50 Gramm", ...werte] } }, + }); + } + await createProductVariantsWorkflow(container).run({ + input: { + product_variants: [ + { + product_id: sonne.id, + title: "50 Gramm", + options: { [option.title]: "50 Gramm" }, + prices: [{ amount: 2.8, currency_code: "eur" }], + weight: BRUTTO["50 Gramm"], + manage_inventory: false, + metadata: { net_weight_grams: 50 }, + }, + ], + }, + }); + logger.info("[ergaenzung] Sonnenblumenhonig 50 g angelegt (2,80 €)."); + } + + // --- Fehlende Versandgewichte ------------------------------------------- + for (const handle of ["buchweizenhonig-aus-der-oder-spree-region", "honig-abo"]) { + const produkt: any = nach.get(handle); + for (const variante of produkt?.variants ?? []) { + // Beim Abo heißt die Variante "250 Gramm / Monatlich". + const groesse = Object.keys(BRUTTO).find((g) => variante.title.startsWith(g)); + if (!groesse || variante.weight) continue; + await updateProductVariantsWorkflow(container).run({ + input: { selector: { id: variante.id }, update: { weight: BRUTTO[groesse] } }, + }); + logger.info(`[ergaenzung] ${handle} / ${variante.title}: ${BRUTTO[groesse]} g`); + } + } +}