feat(shopify): port the order sync from the transactions feed to webshop_orders (#1676)
* feat(shopify): port the order sync from the transactions feed to webshop_orders Shopify orders now land as rich rows on the Orders page (platform 'shopify'), the same surface WooCommerce uses, instead of opaque bank-feed rows on the 1584 cash account: - order-sync.ts writes through the shared upsertWebshopOrders service; the 1584/ensureManualCashAccount wiring is gone (prod has zero Shopify feed rows). Cursor/overlap/dedup, revoked classification and the frozen external_id formats are unchanged. - vat_breakdown is reconstructed from the order-level taxLines (net = tax/rate, remainder as a 0%-bucket, refuse on unusable data); refund VAT is prorated from the parent order's mix. The line-item snapshot is stored only when it reconstructs the charged total to the ore, else the invoice conversion falls back to one aggregate line. - GraphQL query gains createdAt, taxesIncluded, taxLines, lineItems and shippingLines (all non-PII; page size 100 -> 25 for query cost). - Nav gate counts active shopify_connections; the Orders empty-state CTA goes to the platform-neutral /import hub; panel/manifest copy now points at the Orders page (sv + en). - Paid-only qualification and the 90-day backfill stay; the bookkeeping-lock row filter is dropped (lock is enforced at booking, parity with WooCommerce). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(shopify): carry the prorated parent tax on refunds when per-rate bucketing is refused A refund whose parent vat_breakdown was refused (unreported rates) stored total_tax 0 and prefilled a 0%-refund with no moms reversal. The parent's total tax is now prorated into the refund row, so the booking dialog's ratio-inference fallback presents an editable bucket with the reversal instead (CodeRabbit + Swedish review + skeptic finding). Adds the mixed-rate line and truncated shipping-page tests CodeRabbit asked for. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
cfdddb2d7e
commit
bc357531cc
@@ -61,9 +61,11 @@ beforeEach(() => {
|
||||
syncShopifyOrders.mockResolvedValue({
|
||||
fetched: 2,
|
||||
refundsFetched: 0,
|
||||
imported: 2,
|
||||
duplicates: 0,
|
||||
skippedLocked: 0,
|
||||
inserted: 2,
|
||||
updated: 0,
|
||||
unchanged: 0,
|
||||
frozenFlagged: 0,
|
||||
crossMarked: 0,
|
||||
errors: 0,
|
||||
})
|
||||
process.env.NEXT_PUBLIC_SUPABASE_URL = 'https://project.supabase.co'
|
||||
@@ -109,7 +111,7 @@ describe('GET /api/extensions/shopify/orders/cron', () => {
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.json()
|
||||
expect(body.processed).toBe(2)
|
||||
expect(body.imported).toBe(4)
|
||||
expect(body.inserted).toBe(4)
|
||||
expect(syncShopifyOrders).toHaveBeenCalledTimes(2)
|
||||
// Runs on the service client with a shared deadline.
|
||||
expect(syncShopifyOrders.mock.calls[0][0]).toBeTruthy()
|
||||
@@ -134,9 +136,11 @@ describe('GET /api/extensions/shopify/orders/cron', () => {
|
||||
.mockResolvedValueOnce({
|
||||
fetched: 1,
|
||||
refundsFetched: 0,
|
||||
imported: 1,
|
||||
duplicates: 0,
|
||||
skippedLocked: 0,
|
||||
inserted: 1,
|
||||
updated: 0,
|
||||
unchanged: 0,
|
||||
frozenFlagged: 0,
|
||||
crossMarked: 0,
|
||||
errors: 0,
|
||||
})
|
||||
const res = await callRoute()
|
||||
@@ -150,9 +154,11 @@ describe('GET /api/extensions/shopify/orders/cron', () => {
|
||||
syncShopifyOrders.mockResolvedValue({
|
||||
fetched: 0,
|
||||
refundsFetched: 0,
|
||||
imported: 0,
|
||||
duplicates: 0,
|
||||
skippedLocked: 0,
|
||||
inserted: 0,
|
||||
updated: 0,
|
||||
unchanged: 0,
|
||||
frozenFlagged: 0,
|
||||
crossMarked: 0,
|
||||
errors: 0,
|
||||
revoked: true,
|
||||
})
|
||||
|
||||
@@ -15,13 +15,13 @@ export const maxDuration = 300
|
||||
/**
|
||||
* GET /api/extensions/shopify/orders/cron
|
||||
* Nightly order sync for connections that opted in (transaction_sync_enabled):
|
||||
* imports each connected store's paid orders and refunds into the
|
||||
* transactions inbox as a bank-style feed on the 1584 cash account.
|
||||
* upserts each connected store's paid orders and refunds into webshop_orders
|
||||
* (the Orders page), replacing the earlier transactions-inbox feed.
|
||||
*
|
||||
* Read-only against the stores, and it never posts to the journal: rows land
|
||||
* unbooked; booking stays a human decision. Idempotent via the
|
||||
* (company_id, external_id) unique index, so overlapping windows and re-runs
|
||||
* are no-ops. Emits no events, so no ensureInitialized() is needed.
|
||||
* unbooked; booking stays a human decision on the Orders page. Idempotent via
|
||||
* the (company_id, external_id) unique index; overlap re-polls become status
|
||||
* updates. Emits no events, so no ensureInitialized() is needed.
|
||||
*/
|
||||
export const GET = withCronContext('cron.shopify_order_sync', async (_request, ctx) => {
|
||||
// Physical routes under app/api/extensions/<id>/ compile into EVERY build,
|
||||
@@ -84,8 +84,8 @@ export const GET = withCronContext('cron.shopify_order_sync', async (_request, c
|
||||
|
||||
const results: Array<{
|
||||
connectionId: string
|
||||
imported: number
|
||||
duplicates: number
|
||||
inserted: number
|
||||
updated: number
|
||||
status: 'synced' | 'revoked' | 'error'
|
||||
}> = []
|
||||
|
||||
@@ -109,8 +109,8 @@ export const GET = withCronContext('cron.shopify_order_sync', async (_request, c
|
||||
}
|
||||
results.push({
|
||||
connectionId: connection.id,
|
||||
imported: summary.imported,
|
||||
duplicates: summary.duplicates,
|
||||
inserted: summary.inserted,
|
||||
updated: summary.updated,
|
||||
status: summary.revoked ? 'revoked' : 'synced',
|
||||
})
|
||||
} catch (error) {
|
||||
@@ -120,19 +120,19 @@ export const GET = withCronContext('cron.shopify_order_sync', async (_request, c
|
||||
})
|
||||
results.push({
|
||||
connectionId: connection.id,
|
||||
imported: 0,
|
||||
duplicates: 0,
|
||||
inserted: 0,
|
||||
updated: 0,
|
||||
status: 'error',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const totalImported = results.reduce((acc, r) => acc + r.imported, 0)
|
||||
const totalInserted = results.reduce((acc, r) => acc + r.inserted, 0)
|
||||
ctx.log.info('shopify order sync summary', {
|
||||
processed: results.length,
|
||||
totalImported,
|
||||
totalInserted,
|
||||
failed: results.filter((r) => r.status === 'error').length,
|
||||
})
|
||||
|
||||
return NextResponse.json({ processed: results.length, imported: totalImported, results })
|
||||
return NextResponse.json({ processed: results.length, inserted: totalInserted, results })
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user