* feat(webshop-orders): schema, types and error codes for the orders surface webshop_orders (order/refund rows, financial-freeze trigger, member select/update RLS, no DELETE) + webshop_store_settings (per-store payment method -> account map), source_type 'webshop_order', multi-store index drop, customer_country, and a one-time woo cursor reset so the switch-over backfills and cross-marks existing feed rows. Tables classified in the full-archive export; pg-real coverage for RLS, freeze and CHECK. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(webshop-orders): core service (ingest, booking lines) upsertWebshopOrders: two-phase order/refund upsert with FX enrichment, legacy-feed cross-marking, frozen-row protection and field-wise jsonb comparisons (Postgres does not preserve object key order). Booking-line builder: per-rate VAT split with SIGNED buckets (discounts book as revenue reductions), refund mirroring, 3740 residual, per-store account prefill, and advisory export/EU + OSS warnings. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(webshop-orders): API routes for list, booking, invoicing and mapping Booking is draft -> atomic claim -> commit (conditional link-back closes the concurrent double-book race; a lost claim cancels the voucher-free draft). Legacy-feed guard honors transactions.is_ignored on both the book and create-invoice paths. Invoice conversion reuses buildInvoiceWriteData for an unnumbered draft with dominant-rate fallback and drift-safe unit prices. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(webshop-orders): Orders page, booking/invoice dialogs and gated nav /orders lists per-store orders with status tabs (server-side filters), exception chips and one action per row. Booking dialog prefills from the per-store payment-method mapping with an opt-in remember; invoice dialog converts to a draft kundfaktura. The Order nav item renders only for companies with an active WooCommerce connection or existing order rows (Shopify deliberately excluded until its sync writes webshop_orders). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(woocommerce): switch the order sync to webshop_orders, multi-store The sync maps rich wc/v3 payloads (billing, line/shipping/fee taxes, refund allocations with parent-prorated VAT fallback) and upserts order rows instead of transactions-inbox rows; already-imported feed rows stay bookable and get cross-marked. Multi-store: several active connections per company, per-store panel cards with the account-mapping editor. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs(webshop-orders): decision log entries and ratchet baseline Baseline moves DOWN only: naive-ore-round 638 -> 637 via roundOre adoption; hand-rolled invariants stay at 115 (ACCOUNT_NUMBER_RE imported, not inlined). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(webshop-orders): resolve PR #1525 review findings and CI failures Review batch (Superagent, CodeRabbit, Swedish compliance review): - Mutual-exclusion claims: booking guards invoice_id, invoice link-back guards journal_entry_id AND treats zero matched rows as the conflict it is (409 + rollback), closing both TOCTOU races. - Freeze v2 migration (20260812124858): the link columns themselves are protected: invoice links immutable, journal links clearable only while the entry is still a draft (the booking rollback path). - Scraped orgnr no longer auto-written to customers.org_number; rate fallback applies only on single-VAT-bucket orders; refunds get their own WEBSHOP_ORDER_REFUND_NOT_CONVERTIBLE code; VAT advisories outrank the invoice-mode hint in the booking dialog. - Ingest compares every synced field (billing corrections no longer drop as unchanged); sync guards absent refunds arrays; /sync aggregates per-store results; panel disables all cards while a request runs; orders page separates load failure from empty; account field explains itself. CI: regenerated skills/accounted-api; pg tests restructured for transaction-abort/rollback semantics + freeze-link coverage; unresolvable- expression ceiling 375 -> 378 with documented reason (partial-update payloads in ingest, shapes covered by unit tests). Declined: CodeRabbit docstring-coverage advisory (house style: comments only where the code cannot say it). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
163 lines
5.6 KiB
TypeScript
163 lines
5.6 KiB
TypeScript
/**
|
|
* The WooCommerce settings panel's server calls, each classified into exactly
|
|
* one outcome. Same doctrine as the Stripe panel's settings-actions (see the
|
|
* doc block there): never throw, one toast sentence per click, and the
|
|
* classification lives outside the component because component logic has no
|
|
* tests in this repo.
|
|
*/
|
|
|
|
import { fetchWithTimeout, isTimeoutError } from '@/lib/http/fetch-with-timeout'
|
|
import { getErrorMessage, type ErrorLocale } from '@/lib/errors/get-error-message'
|
|
import type { ActionFailure } from '@/lib/browser/action-failure'
|
|
|
|
/**
|
|
* Deadline for the quick calls (status, toggle, disconnect). Connect and
|
|
* manual-connect probe the merchant's WooCommerce host (often a slow shared
|
|
* PHP box, with retries), so they get a longer one.
|
|
*/
|
|
export const WOO_ACTION_TIMEOUT_MS = 15_000
|
|
export const WOO_CONNECT_TIMEOUT_MS = 120_000
|
|
|
|
/**
|
|
* Deadline for "Synka nu": the route's own ceiling (maxDuration 300 on the
|
|
* extension dispatcher) plus margin, same reasoning as the Stripe panel. A
|
|
* first sync backfills 90 days from a slow host and legitimately takes
|
|
* minutes; the server keeps working and advances the cursor even if we
|
|
* aborted, so aborting early would misreport a sync that landed.
|
|
*/
|
|
export const WOO_SYNC_TIMEOUT_MS = 310_000
|
|
|
|
export type WooRequestResult<T> =
|
|
/** 2xx. `data` is null when the body was not readable JSON. */
|
|
| { ok: true; data: T | null }
|
|
| ActionFailure
|
|
|
|
export interface WooRequestOptions {
|
|
url: string
|
|
method?: 'GET' | 'POST' | 'DELETE'
|
|
body?: unknown
|
|
locale?: ErrorLocale
|
|
timeoutMs?: number
|
|
}
|
|
|
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === 'object' && value !== null
|
|
}
|
|
|
|
/** The one sentence for a non-2xx; route copy wins over the generic map. */
|
|
export function serverErrorMessage(
|
|
body: unknown,
|
|
status: number,
|
|
locale: ErrorLocale,
|
|
): string {
|
|
if (isRecord(body)) {
|
|
if (locale === 'en' && typeof body.error_en === 'string' && body.error_en.trim()) {
|
|
return body.error_en.trim()
|
|
}
|
|
if (typeof body.error === 'string' && body.error.trim()) {
|
|
return body.error.trim()
|
|
}
|
|
}
|
|
return getErrorMessage(body, { statusCode: status, locale })
|
|
}
|
|
|
|
/** Call one of the panel's endpoints and report exactly why it failed. */
|
|
export async function wooRequest<T>({
|
|
url,
|
|
method = 'POST',
|
|
body,
|
|
locale = 'sv',
|
|
timeoutMs = WOO_ACTION_TIMEOUT_MS,
|
|
}: WooRequestOptions): Promise<WooRequestResult<T>> {
|
|
try {
|
|
const res = await fetchWithTimeout(
|
|
url,
|
|
body === undefined
|
|
? { method }
|
|
: {
|
|
method,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
},
|
|
{ timeoutMs, description: `${method} ${url}` },
|
|
)
|
|
|
|
const payload = await res.json().catch(() => null)
|
|
|
|
if (!res.ok) {
|
|
return {
|
|
ok: false,
|
|
reason: 'server',
|
|
status: res.status,
|
|
message: serverErrorMessage(payload, res.status, locale),
|
|
}
|
|
}
|
|
|
|
return { ok: true, data: payload as T | null }
|
|
} catch (err) {
|
|
if (isTimeoutError(err)) return { ok: false, reason: 'timeout' }
|
|
return { ok: false, reason: 'network', message: getErrorMessage(err, { locale }) }
|
|
}
|
|
}
|
|
|
|
/** Success body of POST /api/extensions/ext/woocommerce/sync. */
|
|
export interface WooSyncPayload {
|
|
success?: boolean
|
|
/** `WooCommerceSyncSummary` from lib/order-sync.ts, over the wire. */
|
|
transactions?: {
|
|
fetched?: number
|
|
refundsFetched?: number
|
|
inserted?: number
|
|
updated?: number
|
|
unchanged?: number
|
|
errors?: number
|
|
revoked?: boolean
|
|
deadlineReached?: boolean
|
|
} | null
|
|
}
|
|
|
|
type SyncCounts = {
|
|
fetched: number
|
|
imported: number
|
|
}
|
|
|
|
export type WooSyncSummary =
|
|
/** The store rejected the credentials; the connection was flipped to revoked. */
|
|
| { reason: 'revoked' }
|
|
/** The window genuinely held nothing. A real answer, not a silent success. */
|
|
| { reason: 'empty' }
|
|
/**
|
|
* The time budget ran out with orders still unfetched. Reported before the
|
|
* count-based outcomes so a truncated run never reads as a complete one;
|
|
* the cursor persisted, so pressing sync again continues where it stopped.
|
|
* Carries the error count too: a truncated run can also have failed rows,
|
|
* and dropping that number would repeat the silent-partial mistake.
|
|
*/
|
|
| { reason: 'partial'; values: SyncCounts & { errors: number } }
|
|
/** Rows landed, and some rows did not. Both halves get said. */
|
|
| { reason: 'errors'; values: SyncCounts & { errors: number } }
|
|
/** Rows landed. */
|
|
| { reason: 'feed'; values: SyncCounts }
|
|
/** 2xx whose body could not be read: the sync ran, the counts are unknown. */
|
|
| { reason: 'unknown' }
|
|
|
|
/** Turn the sync route's success body into the single sentence the user gets. */
|
|
export function syncSummary(payload: WooSyncPayload | null): WooSyncSummary {
|
|
const summary = payload?.transactions
|
|
if (!summary) return { reason: 'unknown' }
|
|
if (summary.revoked === true) return { reason: 'revoked' }
|
|
if (typeof summary.fetched !== 'number') return { reason: 'unknown' }
|
|
|
|
const fetched = summary.fetched
|
|
// "imported" in the user-facing sentence = new rows this run (inserts).
|
|
const imported = typeof summary.inserted === 'number' ? summary.inserted : 0
|
|
const errors = typeof summary.errors === 'number' ? summary.errors : 0
|
|
|
|
if (summary.deadlineReached === true) {
|
|
return { reason: 'partial', values: { fetched, imported, errors } }
|
|
}
|
|
if (fetched === 0) return { reason: 'empty' }
|
|
if (errors > 0) return { reason: 'errors', values: { fetched, imported, errors } }
|
|
return { reason: 'feed', values: { fetched, imported } }
|
|
}
|