import { medusaConfig, medusaFetch } from "./medusa"; /** Firmenportal: Daten des angemeldeten Firmenkunden. */ export interface B2bDelivery { title: string; quantity: string | null; planned_at: string | null; shipped_at: string | null; tracking_number: string | null; status: "planned" | "shipped" | "delivered"; } export interface B2bReport { id: string; title: string; body: string | null; images: string[]; published_at: string; } export interface B2bSponsorship { id: string; title: string; colonies: number; location: string | null; start_date: string | null; end_date: string | null; price: number | null; status: "offer" | "active" | "renewal" | "ended"; deliveries: B2bDelivery[]; reports: B2bReport[]; } export interface B2bDocument { id: string; title: string; kind: "contract" | "certificate" | "invoice" | "offer" | "other"; file_name: string; sponsorship_id: string | null; created_at: string; } export interface B2bPortal { company: { name: string; street: string | null; postal_code: string | null; city: string | null; vat_id: string | null }; contacts: { name: string; email: string; role: string | null; has_login: boolean }[]; sponsorships: B2bSponsorship[]; documents: B2bDocument[]; inquiries: { kind: string; colonies: number | null; message: string | null; status: "open" | "done"; created_at: string }[]; } export const PATEN_STATUS: Record = { offer: "Angebot", active: "Aktiv", renewal: "Verlängerung steht an", ended: "Beendet", }; export const LIEFER_STATUS: Record = { planned: "Geplant", shipped: "Versendet", delivered: "Zugestellt", }; export const DOKUMENT_ARTEN: Record = { contract: "Vertrag", certificate: "Urkunde", invoice: "Rechnung", offer: "Angebot", other: "Dokument", }; export const ANFRAGE_ARTEN: Record = { new: "Neue Patenschaft", more: "Mehr Völker", other: "Sonstiges", }; /** Portal-Daten, oder `null`, wenn das Konto zu keiner Firma gehört. */ export async function getMyCompany(token: string): Promise { const { url, key } = medusaConfig(); const res = await fetch(`${url}/store/customers/me/b2b`, { headers: { Authorization: `Bearer ${token}`, "x-publishable-api-key": key }, }); if (res.status === 404) return null; if (!res.ok) throw new Error(`Firmenportal nicht ladbar (${res.status})`); return res.json(); } export async function getInvite(token: string): Promise<{ email: string; first_name: string | null; company: string } | null> { try { return await medusaFetch(`/store/b2b/invite?token=${encodeURIComponent(token)}`); } catch { return null; } } export async function acceptInvite(token: string, password: string): Promise<{ email: string }> { return medusaFetch("/store/b2b/invite", { method: "POST", body: JSON.stringify({ token, password }) }); } export async function createInquiry( token: string, payload: { kind: string; colonies?: number | null; message?: string } ): Promise { await medusaFetch("/store/customers/me/b2b/inquiries", { method: "POST", headers: { Authorization: `Bearer ${token}` }, body: JSON.stringify(payload), }); }