104 lines
2.8 KiB
TypeScript
104 lines
2.8 KiB
TypeScript
import { medusaFetch } from "./medusa";
|
||
|
||
export interface Subscription {
|
||
id: string;
|
||
status: "active" | "paused" | "canceled";
|
||
interval: "monthly" | "quarterly" | "biannual";
|
||
size: string | null;
|
||
quantity: number;
|
||
selection: "fixed" | "random";
|
||
/** Altbestand: eine Sorte für alle Gläser. */
|
||
variety: string | null;
|
||
/** Eine Variante je Glas, in der Reihenfolge der Auswahl. */
|
||
variant_ids: string[];
|
||
/** Die Namen dazu, gleiche Reihenfolge. */
|
||
varieties: (string | null)[];
|
||
last_variety: string | null;
|
||
next_order_at: string | null;
|
||
last_order_at: string | null;
|
||
canceled_at: string | null;
|
||
paused_at: string | null;
|
||
payment_method: string | null;
|
||
}
|
||
|
||
export const INTERVAL_LABELS: Record<string, string> = {
|
||
monthly: "monatlich",
|
||
quarterly: "alle 3 Monate",
|
||
biannual: "alle 6 Monate",
|
||
};
|
||
|
||
/**
|
||
* "2× Lindenhonig, Rapshonig" — gleiche Sorten zusammengefasst.
|
||
*
|
||
* Steht hier und nicht bei Warenkorb oder Konto, weil beide dieselbe
|
||
* Schreibweise brauchen.
|
||
*/
|
||
export function summariseVarieties(labels: (string | null | undefined)[]): string {
|
||
const counts = new Map<string, number>();
|
||
for (const label of labels) {
|
||
if (label) counts.set(label, (counts.get(label) ?? 0) + 1);
|
||
}
|
||
return [...counts]
|
||
.map(([label, count]) => (count > 1 ? `${count}× ${label}` : label))
|
||
.join(", ");
|
||
}
|
||
|
||
export const STATUS_LABELS: Record<string, string> = {
|
||
active: "aktiv",
|
||
paused: "pausiert",
|
||
canceled: "gekündigt",
|
||
};
|
||
|
||
function authHeaders(token: string) {
|
||
return { Authorization: `Bearer ${token}` };
|
||
}
|
||
|
||
export async function listMySubscriptions(
|
||
token: string
|
||
): Promise<{ subscriptions: Subscription[]; sizes: string[] }> {
|
||
return medusaFetch("/store/customers/me/subscriptions", { headers: authHeaders(token) });
|
||
}
|
||
|
||
export async function updateSubscription(
|
||
token: string,
|
||
id: string,
|
||
payload: {
|
||
action?: "pause" | "resume" | "cancel";
|
||
quantity?: number;
|
||
size?: string;
|
||
selection?: "fixed" | "random";
|
||
variant_id?: string | null;
|
||
/** Eine Variante je Glas. Hat Vorrang vor variant_id. */
|
||
variant_ids?: string[] | null;
|
||
cancel_reason?: string;
|
||
}
|
||
): Promise<{ subscription: Subscription }> {
|
||
return medusaFetch(`/store/customers/me/subscriptions/${id}`, {
|
||
method: "POST",
|
||
headers: authHeaders(token),
|
||
body: JSON.stringify(payload),
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Kündigung ohne Anmeldung — der gesetzlich vorgeschriebene Weg nach
|
||
* § 312k BGB. Braucht Abo-Nummer und E-Mail, kein Konto.
|
||
*/
|
||
export async function cancelWithoutLogin(payload: {
|
||
subscription_id: string;
|
||
email: string;
|
||
name?: string;
|
||
kind?: "ordentlich" | "ausserordentlich";
|
||
reason?: string;
|
||
}): Promise<{
|
||
subscription_id: string;
|
||
canceled_at: string;
|
||
already_canceled?: boolean;
|
||
message?: string;
|
||
}> {
|
||
return medusaFetch("/store/subscriptions/cancel", {
|
||
method: "POST",
|
||
body: JSON.stringify(payload),
|
||
});
|
||
}
|