/** * api/billing.js — Client for Hub /billing approval endpoints (P1). * * Porte d'approbation de facturation : lister les installations complétées en * attente, prévisualiser le prorata (LECTURE SEULE), approuver (= active le * service ; la facturation reste gatée côté hub par PRORATION_WRITE). */ import { HUB_URL } from 'src/config/hub' async function hubFetch (path, { method = 'GET', body } = {}) { const opts = { method, headers: { 'Content-Type': 'application/json' } } if (body) opts.body = JSON.stringify(body) const res = await fetch(`${HUB_URL}${path}`, opts) const text = await res.text() let data try { data = text ? JSON.parse(text) : {} } catch { throw new Error(`Invalid JSON from ${path}: ${text.slice(0, 200)}`) } if (!res.ok) { const err = new Error(data.error || `HTTP ${res.status}`); err.status = res.status; throw err } return data } // Activations en attente d'approbation (enrichies client + adresse). export function getApprovals () { return hubFetch('/billing/approvals').then(r => r.pending || []) } // Aperçu prorata consolidé pour un job (LECTURE SEULE — aucune facture créée). export function getApprovalPreview (job) { return hubFetch(`/billing/approvals/preview?job=${encodeURIComponent(job)}`) } // Approuve → active les abonnements. L'approbateur est attribué côté serveur // (en-tête x-authentik-email). La facturation reste gatée (PRORATION_WRITE). export function approveActivation (job) { return hubFetch('/billing/approvals/approve', { method: 'POST', body: { job } }) }