aktueller stand
This commit is contained in:
+206
-4
@@ -1,4 +1,6 @@
|
||||
import { medusaFetch } from "./medusa";
|
||||
import { summariseVarieties } from "./subscriptions";
|
||||
import { varietyName } from "./varieties";
|
||||
|
||||
export interface MedusaCartItem {
|
||||
id: string;
|
||||
@@ -6,14 +8,55 @@ export interface MedusaCartItem {
|
||||
thumbnail: string | null;
|
||||
quantity: number;
|
||||
unit_price: number;
|
||||
/** Beim Abo steht hier die Sortenwahl. */
|
||||
metadata?: {
|
||||
subscription_selection?: "fixed" | "random";
|
||||
/** Altbestand: eine Sorte für alle Gläser. */
|
||||
subscription_variety?: string;
|
||||
subscription_variant_id?: string;
|
||||
/** Eine Angabe je Glas, in der Reihenfolge der Auswahl. */
|
||||
subscription_varieties?: string[];
|
||||
subscription_variant_ids?: string[];
|
||||
/** Beim verschenkten Abo gesetzt. */
|
||||
subscription_gift?: boolean;
|
||||
/** Bienenpatenschaft: Angaben für Urkunde, Etikett und Beschenkten. */
|
||||
sponsorship_name?: string;
|
||||
sponsorship_gift?: boolean;
|
||||
sponsorship_recipient_email?: string;
|
||||
sponsorship_sender_name?: string;
|
||||
subscription_deliveries?: number;
|
||||
gift_recipient_name?: string;
|
||||
gift_recipient_email?: string;
|
||||
gift_sender_name?: string;
|
||||
/** Geschenkgutschein: geht er direkt an den Beschenkten? */
|
||||
voucher_gift?: boolean;
|
||||
voucher_recipient_name?: string;
|
||||
voucher_recipient_email?: string;
|
||||
voucher_sender_name?: string;
|
||||
voucher_message?: string;
|
||||
} | null;
|
||||
/** `false` beim Geschenkgutschein: er kommt per E-Mail. */
|
||||
requires_shipping?: boolean;
|
||||
variant?: {
|
||||
id: string;
|
||||
metadata?: { subscription_interval?: string; subscription_size?: string } | null;
|
||||
product?: { metadata?: { subscription?: boolean } | null } | null;
|
||||
} | null;
|
||||
}
|
||||
|
||||
export interface MedusaCart {
|
||||
id: string;
|
||||
email: string | null;
|
||||
currency_code: string;
|
||||
/** Bestimmt, welche Zahlungsarten zur Verfügung stehen. */
|
||||
region_id: string | null;
|
||||
item_total: number;
|
||||
items: MedusaCartItem[];
|
||||
/** Nach Abzug eingelöster Gutscheine. */
|
||||
total?: number;
|
||||
credit_line_total?: number;
|
||||
credit_lines?: { id: string; amount: number; reference: string | null; reference_id: string | null }[];
|
||||
gift_cards?: { id: string; code: string }[];
|
||||
}
|
||||
|
||||
export interface MedusaShippingOption {
|
||||
@@ -30,15 +73,152 @@ export async function createCart(regionId: string): Promise<MedusaCart> {
|
||||
return data.cart;
|
||||
}
|
||||
|
||||
// Ohne diese Felder liefert Medusa weder die Posten-Metadaten noch die
|
||||
// Produktkennzeichnung — beides braucht die Kasse, um ein Abo zu erkennen.
|
||||
// Dazu die eingelösten Gutscheine und ob ein Posten verschickt wird.
|
||||
const CART_FIELDS =
|
||||
"+items.metadata,+items.variant.metadata,+items.variant.product.metadata,+items.requires_shipping," +
|
||||
"+total,+credit_line_total,+credit_lines.*,+gift_cards.id,+gift_cards.code";
|
||||
|
||||
export async function getCart(cartId: string): Promise<MedusaCart> {
|
||||
const data = await medusaFetch(`/store/carts/${cartId}`);
|
||||
const data = await medusaFetch(`/store/carts/${cartId}?fields=${encodeURIComponent(CART_FIELDS)}`);
|
||||
return data.cart;
|
||||
}
|
||||
|
||||
export async function addLineItem(cartId: string, variantId: string, quantity = 1): Promise<MedusaCart> {
|
||||
/** Enthält der Warenkorb ein Abo? Dann läuft die Kasse anders. */
|
||||
function istAboPosten(item: MedusaCartItem): boolean {
|
||||
return item.variant?.product?.metadata?.subscription === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ob der Warenkorb ein Abo enthält, das ein Mandat braucht.
|
||||
*
|
||||
* Ein verschenktes Abo braucht keines: es ist mit der Bestellung vollständig
|
||||
* bezahlt und wird nie abgebucht. Es läuft deshalb durch die normale Kasse,
|
||||
* nicht über die Weiterleitung zu Mollie.
|
||||
*/
|
||||
export function cartHasSubscription(cart: MedusaCart | null): boolean {
|
||||
return (cart?.items ?? []).some(
|
||||
(item) => istAboPosten(item) && item.metadata?.subscription_gift !== true
|
||||
);
|
||||
}
|
||||
|
||||
/** Ob im Warenkorb ein verschenktes Abo liegt. */
|
||||
export function cartHasGiftSubscription(cart: MedusaCart | null): boolean {
|
||||
return (cart?.items ?? []).some(
|
||||
(item) => istAboPosten(item) && item.metadata?.subscription_gift === true
|
||||
);
|
||||
}
|
||||
|
||||
const INTERVAL_LABELS: Record<string, string> = {
|
||||
monthly: "monatlich",
|
||||
quarterly: "alle 3 Monate",
|
||||
biannual: "alle 6 Monate",
|
||||
};
|
||||
|
||||
/**
|
||||
* Die Angaben, die ein Posten über sich selbst macht.
|
||||
*
|
||||
* Bei der Patenschaft sind das Name und Empfänger — sie stehen im Warenkorb
|
||||
* und an der Kasse, damit ein Tippfehler im Namen auffällt, bevor die
|
||||
* Urkunde gedruckt ist.
|
||||
*/
|
||||
export function describeSponsorshipItem(item: MedusaCartItem): string {
|
||||
const name = item.metadata?.sponsorship_name;
|
||||
if (!name) return "";
|
||||
const teile = [`Auf den Namen: ${name}`];
|
||||
if (item.metadata?.sponsorship_gift) {
|
||||
teile.push(`Geschenk an ${item.metadata.sponsorship_recipient_email ?? "—"}`);
|
||||
}
|
||||
return teile.join(" · ");
|
||||
}
|
||||
|
||||
/** Beim verschenkten Gutschein: an wen er per E-Mail geht. */
|
||||
export function describeVoucherItem(item: MedusaCartItem): string {
|
||||
if (!item.metadata?.voucher_gift || !item.metadata.voucher_recipient_email) return "";
|
||||
const an = item.metadata.voucher_recipient_name
|
||||
? `${item.metadata.voucher_recipient_name} (${item.metadata.voucher_recipient_email})`
|
||||
: item.metadata.voucher_recipient_email;
|
||||
return `Per E-Mail an ${an}`;
|
||||
}
|
||||
|
||||
/** Ob etwas im Warenkorb verschickt wird. Ein Gutschein allein nicht. */
|
||||
export function cartRequiresShipping(cart: MedusaCart | null): boolean {
|
||||
return (cart?.items ?? []).some((item) => item.requires_shipping !== false);
|
||||
}
|
||||
|
||||
/** Gutscheincode am Warenkorb einlösen. */
|
||||
export async function applyGiftCard(cartId: string, code: string): Promise<void> {
|
||||
await medusaFetch(`/store/carts/${cartId}/gift-cards`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ code }),
|
||||
});
|
||||
}
|
||||
|
||||
/** Eingelösten Gutschein wieder vom Warenkorb nehmen. */
|
||||
export async function removeGiftCard(cartId: string, code: string): Promise<void> {
|
||||
await medusaFetch(`/store/carts/${cartId}/gift-cards`, {
|
||||
method: "DELETE",
|
||||
body: JSON.stringify({ code }),
|
||||
});
|
||||
}
|
||||
|
||||
/** Das Guthaben der eingelösten Gutscheine — für den Abzug an der Kasse. */
|
||||
export async function getGiftCardBalances(cartId: string): Promise<{ code: string; balance: number }[]> {
|
||||
const data = await medusaFetch(`/store/carts/${cartId}/gift-card-balances`);
|
||||
return data.gift_cards ?? [];
|
||||
}
|
||||
|
||||
export function describeSubscriptionItem(item: MedusaCartItem): string {
|
||||
const interval = INTERVAL_LABELS[item.variant?.metadata?.subscription_interval ?? ""] ?? "";
|
||||
const size = item.variant?.metadata?.subscription_size ?? "";
|
||||
// Beim Anzeigen kürzen statt beim Speichern: so lesen sich auch Warenkörbe
|
||||
// kurz, die noch mit den vollen Katalogtiteln angelegt wurden.
|
||||
const perGlass = summariseVarieties(
|
||||
(item.metadata?.subscription_varieties ?? []).map(varietyName)
|
||||
);
|
||||
const variety =
|
||||
item.metadata?.subscription_selection === "fixed"
|
||||
? // Altbestand aus der Zeit vor der Wahl je Glas kennt nur die
|
||||
// Einzelangabe.
|
||||
(perGlass || (item.metadata.subscription_variety && varietyName(item.metadata.subscription_variety)) || "feste Sorte")
|
||||
: "Überraschungshonig";
|
||||
return [size, interval, variety].filter(Boolean).join(" · ");
|
||||
}
|
||||
|
||||
/** Startet die Abo-Kasse und liefert die Weiterleitungsadresse zu Mollie. */
|
||||
export async function startSubscriptionCheckout(payload: {
|
||||
cart_id: string;
|
||||
email: string;
|
||||
shipping_address: Record<string, unknown>;
|
||||
shipping_option_id?: string;
|
||||
method?: string;
|
||||
}): Promise<{ checkout_url: string }> {
|
||||
const data = await medusaFetch("/store/subscriptions/checkout", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function addLineItem(
|
||||
cartId: string,
|
||||
variantId: string,
|
||||
quantity = 1,
|
||||
/**
|
||||
* Zusatzangaben am Posten. Beim Abo steht hier die Sortenwahl, die
|
||||
* bewusst keine Produktvariante ist — mit 12 Sorten mal Größe mal
|
||||
* Intervall gäbe das 78 Varianten.
|
||||
*/
|
||||
metadata?: Record<string, unknown>
|
||||
): Promise<MedusaCart> {
|
||||
const data = await medusaFetch(`/store/carts/${cartId}/line-items`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ variant_id: variantId, quantity }),
|
||||
body: JSON.stringify({
|
||||
variant_id: variantId,
|
||||
quantity,
|
||||
...(metadata && Object.keys(metadata).length ? { metadata } : {}),
|
||||
}),
|
||||
});
|
||||
return data.cart;
|
||||
}
|
||||
@@ -80,14 +260,36 @@ export async function listShippingOptions(cartId: string): Promise<MedusaShippin
|
||||
return data.shipping_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Versandart setzen. Über die eigene Route statt Medusas `shipping-methods`:
|
||||
* nur sie rechnet die Versandkostenfreigrenze ohne Gutscheine im Warenkorb.
|
||||
*/
|
||||
export async function addShippingMethod(cartId: string, optionId: string): Promise<MedusaCart> {
|
||||
const data = await medusaFetch(`/store/carts/${cartId}/shipping-methods`, {
|
||||
const data = await medusaFetch(`/store/carts/${cartId}/select-shipping`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ option_id: optionId }),
|
||||
});
|
||||
return data.cart;
|
||||
}
|
||||
|
||||
export interface PaymentMethod {
|
||||
id: string;
|
||||
label: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Die Zahlungsarten, die der Shop zu Ende führen kann.
|
||||
*
|
||||
* Bewusst nicht Medusas `/store/payment-providers`: das listet auch
|
||||
* Anbieter, für die es an der Kasse noch keinen Ablauf gibt.
|
||||
*/
|
||||
export async function listPaymentMethods(regionId?: string): Promise<PaymentMethod[]> {
|
||||
const query = regionId ? `?region_id=${encodeURIComponent(regionId)}` : "";
|
||||
const data = await medusaFetch(`/store/payment-methods${query}`);
|
||||
return data.payment_methods ?? [];
|
||||
}
|
||||
|
||||
export async function createPaymentCollection(cartId: string) {
|
||||
const data = await medusaFetch("/store/payment-collections", {
|
||||
method: "POST",
|
||||
|
||||
Reference in New Issue
Block a user