@open-mercato/core 0.4.7-develop-0a657b411f → 0.4.7-develop-e249d3e7d0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/generated/entities/carrier_shipment/index.js +37 -0
- package/dist/generated/entities/carrier_shipment/index.js.map +7 -0
- package/dist/generated/entities/gateway_transaction/index.js +47 -0
- package/dist/generated/entities/gateway_transaction/index.js.map +7 -0
- package/dist/generated/entities/webhook_processed_event/index.js +17 -0
- package/dist/generated/entities/webhook_processed_event/index.js.map +7 -0
- package/dist/generated/entities.ids.generated.js +10 -1
- package/dist/generated/entities.ids.generated.js.map +2 -2
- package/dist/generated/entity-fields-registry.js +6 -0
- package/dist/generated/entity-fields-registry.js.map +2 -2
- package/dist/modules/data_sync/api/runs/[id]/cancel.js +14 -5
- package/dist/modules/data_sync/api/runs/[id]/cancel.js.map +2 -2
- package/dist/modules/data_sync/backend/data-sync/page.meta.js +2 -2
- package/dist/modules/data_sync/backend/data-sync/page.meta.js.map +1 -1
- package/dist/modules/data_sync/backend/data-sync/runs/[id]/page.js +37 -12
- package/dist/modules/data_sync/backend/data-sync/runs/[id]/page.js.map +2 -2
- package/dist/modules/directory/api/get/tenants/lookup.js +1 -0
- package/dist/modules/directory/api/get/tenants/lookup.js.map +2 -2
- package/dist/modules/integrations/api/[id]/route.js +38 -11
- package/dist/modules/integrations/api/[id]/route.js.map +2 -2
- package/dist/modules/integrations/api/logs/route.js +52 -26
- package/dist/modules/integrations/api/logs/route.js.map +2 -2
- package/dist/modules/integrations/api/route.js +37 -7
- package/dist/modules/integrations/api/route.js.map +2 -2
- package/dist/modules/integrations/api/umes-read.js +121 -0
- package/dist/modules/integrations/api/umes-read.js.map +7 -0
- package/dist/modules/integrations/backend/integrations/[id]/page.js +715 -183
- package/dist/modules/integrations/backend/integrations/[id]/page.js.map +2 -2
- package/dist/modules/integrations/backend/integrations/bundle/[id]/page.js +30 -9
- package/dist/modules/integrations/backend/integrations/bundle/[id]/page.js.map +2 -2
- package/dist/modules/integrations/backend/integrations/detail-page-widgets.js +46 -0
- package/dist/modules/integrations/backend/integrations/detail-page-widgets.js.map +7 -0
- package/dist/modules/integrations/backend/integrations/page.js +78 -62
- package/dist/modules/integrations/backend/integrations/page.js.map +2 -2
- package/dist/modules/integrations/backend/integrations/page.meta.js +2 -2
- package/dist/modules/integrations/backend/integrations/page.meta.js.map +1 -1
- package/dist/modules/integrations/setup.js +2 -2
- package/dist/modules/integrations/setup.js.map +2 -2
- package/dist/modules/payment_gateways/acl.js +12 -0
- package/dist/modules/payment_gateways/acl.js.map +7 -0
- package/dist/modules/payment_gateways/api/cancel/route.js +55 -0
- package/dist/modules/payment_gateways/api/cancel/route.js.map +7 -0
- package/dist/modules/payment_gateways/api/capture/route.js +55 -0
- package/dist/modules/payment_gateways/api/capture/route.js.map +7 -0
- package/dist/modules/payment_gateways/api/interceptors.js +24 -0
- package/dist/modules/payment_gateways/api/interceptors.js.map +7 -0
- package/dist/modules/payment_gateways/api/openapi.js +5 -0
- package/dist/modules/payment_gateways/api/openapi.js.map +7 -0
- package/dist/modules/payment_gateways/api/refund/route.js +56 -0
- package/dist/modules/payment_gateways/api/refund/route.js.map +7 -0
- package/dist/modules/payment_gateways/api/sessions/route.js +74 -0
- package/dist/modules/payment_gateways/api/sessions/route.js.map +7 -0
- package/dist/modules/payment_gateways/api/status/route.js +66 -0
- package/dist/modules/payment_gateways/api/status/route.js.map +7 -0
- package/dist/modules/payment_gateways/api/transactions/[id]/route.js +118 -0
- package/dist/modules/payment_gateways/api/transactions/[id]/route.js.map +7 -0
- package/dist/modules/payment_gateways/api/transactions/route.js +113 -0
- package/dist/modules/payment_gateways/api/transactions/route.js.map +7 -0
- package/dist/modules/payment_gateways/api/webhook/[provider]/route.js +136 -0
- package/dist/modules/payment_gateways/api/webhook/[provider]/route.js.map +7 -0
- package/dist/modules/payment_gateways/backend/payment-gateways/page.js +496 -0
- package/dist/modules/payment_gateways/backend/payment-gateways/page.js.map +7 -0
- package/dist/modules/payment_gateways/backend/payment-gateways/page.meta.js +23 -0
- package/dist/modules/payment_gateways/backend/payment-gateways/page.meta.js.map +7 -0
- package/dist/modules/payment_gateways/data/enrichers.js +5 -0
- package/dist/modules/payment_gateways/data/enrichers.js.map +7 -0
- package/dist/modules/payment_gateways/data/entities.js +131 -0
- package/dist/modules/payment_gateways/data/entities.js.map +7 -0
- package/dist/modules/payment_gateways/data/validators.js +57 -0
- package/dist/modules/payment_gateways/data/validators.js.map +7 -0
- package/dist/modules/payment_gateways/di.js +16 -0
- package/dist/modules/payment_gateways/di.js.map +7 -0
- package/dist/modules/payment_gateways/events.js +21 -0
- package/dist/modules/payment_gateways/events.js.map +7 -0
- package/dist/modules/payment_gateways/i18n/en.js +6 -0
- package/dist/modules/payment_gateways/i18n/en.js.map +7 -0
- package/dist/modules/payment_gateways/i18n/pl.js +6 -0
- package/dist/modules/payment_gateways/i18n/pl.js.map +7 -0
- package/dist/modules/payment_gateways/index.js +9 -0
- package/dist/modules/payment_gateways/index.js.map +7 -0
- package/dist/modules/payment_gateways/lib/gateway-service.js +378 -0
- package/dist/modules/payment_gateways/lib/gateway-service.js.map +7 -0
- package/dist/modules/payment_gateways/lib/queue.js +17 -0
- package/dist/modules/payment_gateways/lib/queue.js.map +7 -0
- package/dist/modules/payment_gateways/lib/status-machine.js +29 -0
- package/dist/modules/payment_gateways/lib/status-machine.js.map +7 -0
- package/dist/modules/payment_gateways/lib/webhook-processor.js +88 -0
- package/dist/modules/payment_gateways/lib/webhook-processor.js.map +7 -0
- package/dist/modules/payment_gateways/lib/webhook-utils.js +42 -0
- package/dist/modules/payment_gateways/lib/webhook-utils.js.map +7 -0
- package/dist/modules/payment_gateways/migrations/Migration20260305122155.js +19 -0
- package/dist/modules/payment_gateways/migrations/Migration20260305122155.js.map +7 -0
- package/dist/modules/payment_gateways/setup.js +13 -0
- package/dist/modules/payment_gateways/setup.js.map +7 -0
- package/dist/modules/payment_gateways/widgets/injection-table.js +7 -0
- package/dist/modules/payment_gateways/widgets/injection-table.js.map +7 -0
- package/dist/modules/payment_gateways/workers/status-poller.js +44 -0
- package/dist/modules/payment_gateways/workers/status-poller.js.map +7 -0
- package/dist/modules/payment_gateways/workers/webhook-processor.js +20 -0
- package/dist/modules/payment_gateways/workers/webhook-processor.js.map +7 -0
- package/dist/modules/sales/data/enrichers.js +72 -0
- package/dist/modules/sales/data/enrichers.js.map +7 -0
- package/dist/modules/sales/lib/makeSalesLineRoute.js +3 -0
- package/dist/modules/sales/lib/makeSalesLineRoute.js.map +2 -2
- package/dist/modules/sales/widgets/injection/payment-gateway-config-field/widget.js +29 -0
- package/dist/modules/sales/widgets/injection/payment-gateway-config-field/widget.js.map +7 -0
- package/dist/modules/sales/widgets/injection/payment-gateway-status-column/widget.js +23 -0
- package/dist/modules/sales/widgets/injection/payment-gateway-status-column/widget.js.map +7 -0
- package/dist/modules/sales/widgets/injection-table.js +13 -1
- package/dist/modules/sales/widgets/injection-table.js.map +2 -2
- package/dist/modules/shipping_carriers/acl.js +10 -0
- package/dist/modules/shipping_carriers/acl.js.map +7 -0
- package/dist/modules/shipping_carriers/api/cancel/route.js +55 -0
- package/dist/modules/shipping_carriers/api/cancel/route.js.map +7 -0
- package/dist/modules/shipping_carriers/api/interceptors.js +21 -0
- package/dist/modules/shipping_carriers/api/interceptors.js.map +7 -0
- package/dist/modules/shipping_carriers/api/openapi.js +5 -0
- package/dist/modules/shipping_carriers/api/openapi.js.map +7 -0
- package/dist/modules/shipping_carriers/api/rates/route.js +55 -0
- package/dist/modules/shipping_carriers/api/rates/route.js.map +7 -0
- package/dist/modules/shipping_carriers/api/shipments/route.js +61 -0
- package/dist/modules/shipping_carriers/api/shipments/route.js.map +7 -0
- package/dist/modules/shipping_carriers/api/tracking/route.js +58 -0
- package/dist/modules/shipping_carriers/api/tracking/route.js.map +7 -0
- package/dist/modules/shipping_carriers/api/webhook/[provider]/route.js +119 -0
- package/dist/modules/shipping_carriers/api/webhook/[provider]/route.js.map +7 -0
- package/dist/modules/shipping_carriers/data/enrichers.js +82 -0
- package/dist/modules/shipping_carriers/data/enrichers.js.map +7 -0
- package/dist/modules/shipping_carriers/data/entities.js +80 -0
- package/dist/modules/shipping_carriers/data/entities.js.map +7 -0
- package/dist/modules/shipping_carriers/data/validators.js +49 -0
- package/dist/modules/shipping_carriers/data/validators.js.map +7 -0
- package/dist/modules/shipping_carriers/di.js +15 -0
- package/dist/modules/shipping_carriers/di.js.map +7 -0
- package/dist/modules/shipping_carriers/events.js +19 -0
- package/dist/modules/shipping_carriers/events.js.map +7 -0
- package/dist/modules/shipping_carriers/i18n/en.js +11 -0
- package/dist/modules/shipping_carriers/i18n/en.js.map +7 -0
- package/dist/modules/shipping_carriers/i18n/pl.js +11 -0
- package/dist/modules/shipping_carriers/i18n/pl.js.map +7 -0
- package/dist/modules/shipping_carriers/index.js +9 -0
- package/dist/modules/shipping_carriers/index.js.map +7 -0
- package/dist/modules/shipping_carriers/lib/adapter-registry.js +29 -0
- package/dist/modules/shipping_carriers/lib/adapter-registry.js.map +7 -0
- package/dist/modules/shipping_carriers/lib/adapter.js +1 -0
- package/dist/modules/shipping_carriers/lib/adapter.js.map +7 -0
- package/dist/modules/shipping_carriers/lib/queue.js +17 -0
- package/dist/modules/shipping_carriers/lib/queue.js.map +7 -0
- package/dist/modules/shipping_carriers/lib/shipping-service.js +155 -0
- package/dist/modules/shipping_carriers/lib/shipping-service.js.map +7 -0
- package/dist/modules/shipping_carriers/lib/status-sync.js +37 -0
- package/dist/modules/shipping_carriers/lib/status-sync.js.map +7 -0
- package/dist/modules/shipping_carriers/migrations/Migration20260305170000.js +16 -0
- package/dist/modules/shipping_carriers/migrations/Migration20260305170000.js.map +7 -0
- package/dist/modules/shipping_carriers/setup.js +13 -0
- package/dist/modules/shipping_carriers/setup.js.map +7 -0
- package/dist/modules/shipping_carriers/widgets/injection/create-shipment-button/widget.js +25 -0
- package/dist/modules/shipping_carriers/widgets/injection/create-shipment-button/widget.js.map +7 -0
- package/dist/modules/shipping_carriers/widgets/injection/tracking-column/widget.js +23 -0
- package/dist/modules/shipping_carriers/widgets/injection/tracking-column/widget.js.map +7 -0
- package/dist/modules/shipping_carriers/widgets/injection/tracking-status-badge/widget.js +40 -0
- package/dist/modules/shipping_carriers/widgets/injection/tracking-status-badge/widget.js.map +7 -0
- package/dist/modules/shipping_carriers/widgets/injection-table.js +24 -0
- package/dist/modules/shipping_carriers/widgets/injection-table.js.map +7 -0
- package/dist/modules/shipping_carriers/workers/status-poller.js +21 -0
- package/dist/modules/shipping_carriers/workers/status-poller.js.map +7 -0
- package/dist/modules/shipping_carriers/workers/webhook-processor.js +54 -0
- package/dist/modules/shipping_carriers/workers/webhook-processor.js.map +7 -0
- package/dist/modules/translations/api/get/locales.js +1 -0
- package/dist/modules/translations/api/get/locales.js.map +2 -2
- package/dist/modules/translations/api/put/locales.js +1 -0
- package/dist/modules/translations/api/put/locales.js.map +2 -2
- package/generated/entities/carrier_shipment/index.ts +17 -0
- package/generated/entities/gateway_transaction/index.ts +22 -0
- package/generated/entities/webhook_processed_event/index.ts +7 -0
- package/generated/entities.ids.generated.ts +10 -1
- package/generated/entity-fields-registry.ts +6 -0
- package/jest.config.cjs +1 -0
- package/package.json +5 -2
- package/src/modules/auth/i18n/de.json +1 -0
- package/src/modules/auth/i18n/en.json +1 -0
- package/src/modules/auth/i18n/es.json +1 -0
- package/src/modules/auth/i18n/pl.json +1 -0
- package/src/modules/data_sync/api/runs/[id]/cancel.ts +18 -5
- package/src/modules/data_sync/backend/data-sync/page.meta.ts +2 -2
- package/src/modules/data_sync/backend/data-sync/runs/[id]/page.tsx +50 -12
- package/src/modules/directory/api/get/tenants/lookup.ts +1 -0
- package/src/modules/integrations/AGENTS.md +31 -0
- package/src/modules/integrations/api/[id]/route.ts +38 -11
- package/src/modules/integrations/api/logs/route.ts +53 -27
- package/src/modules/integrations/api/route.ts +31 -1
- package/src/modules/integrations/api/umes-read.ts +177 -0
- package/src/modules/integrations/backend/integrations/[id]/page.tsx +902 -202
- package/src/modules/integrations/backend/integrations/bundle/[id]/page.tsx +43 -9
- package/src/modules/integrations/backend/integrations/detail-page-widgets.ts +74 -0
- package/src/modules/integrations/backend/integrations/page.meta.ts +2 -2
- package/src/modules/integrations/backend/integrations/page.tsx +65 -54
- package/src/modules/integrations/i18n/de.json +15 -0
- package/src/modules/integrations/i18n/en.json +15 -0
- package/src/modules/integrations/i18n/es.json +15 -0
- package/src/modules/integrations/i18n/pl.json +15 -0
- package/src/modules/integrations/setup.ts +2 -2
- package/src/modules/payment_gateways/acl.ts +8 -0
- package/src/modules/payment_gateways/api/cancel/route.ts +56 -0
- package/src/modules/payment_gateways/api/capture/route.ts +56 -0
- package/src/modules/payment_gateways/api/interceptors.ts +22 -0
- package/src/modules/payment_gateways/api/openapi.ts +1 -0
- package/src/modules/payment_gateways/api/refund/route.ts +57 -0
- package/src/modules/payment_gateways/api/sessions/route.ts +76 -0
- package/src/modules/payment_gateways/api/status/route.ts +69 -0
- package/src/modules/payment_gateways/api/transactions/[id]/route.ts +123 -0
- package/src/modules/payment_gateways/api/transactions/route.ts +120 -0
- package/src/modules/payment_gateways/api/webhook/[provider]/route.ts +161 -0
- package/src/modules/payment_gateways/backend/payment-gateways/page.meta.ts +19 -0
- package/src/modules/payment_gateways/backend/payment-gateways/page.tsx +660 -0
- package/src/modules/payment_gateways/data/enrichers.ts +8 -0
- package/src/modules/payment_gateways/data/entities.ts +106 -0
- package/src/modules/payment_gateways/data/validators.ts +67 -0
- package/src/modules/payment_gateways/di.ts +26 -0
- package/src/modules/payment_gateways/events.ts +17 -0
- package/src/modules/payment_gateways/i18n/de.json +77 -0
- package/src/modules/payment_gateways/i18n/en.json +77 -0
- package/src/modules/payment_gateways/i18n/en.ts +4 -0
- package/src/modules/payment_gateways/i18n/es.json +77 -0
- package/src/modules/payment_gateways/i18n/pl.json +77 -0
- package/src/modules/payment_gateways/i18n/pl.ts +4 -0
- package/src/modules/payment_gateways/index.ts +5 -0
- package/src/modules/payment_gateways/lib/gateway-service.ts +486 -0
- package/src/modules/payment_gateways/lib/queue.ts +19 -0
- package/src/modules/payment_gateways/lib/status-machine.ts +28 -0
- package/src/modules/payment_gateways/lib/webhook-processor.ts +133 -0
- package/src/modules/payment_gateways/lib/webhook-utils.ts +52 -0
- package/src/modules/payment_gateways/migrations/.snapshot-open-mercato.json +373 -0
- package/src/modules/payment_gateways/migrations/Migration20260305122155.ts +20 -0
- package/src/modules/payment_gateways/setup.ts +11 -0
- package/src/modules/payment_gateways/widgets/injection-table.ts +9 -0
- package/src/modules/payment_gateways/workers/status-poller.ts +58 -0
- package/src/modules/payment_gateways/workers/webhook-processor.ts +30 -0
- package/src/modules/sales/data/enrichers.ts +120 -0
- package/src/modules/sales/lib/makeSalesLineRoute.ts +3 -0
- package/src/modules/sales/widgets/injection/payment-gateway-config-field/widget.ts +28 -0
- package/src/modules/sales/widgets/injection/payment-gateway-status-column/widget.ts +22 -0
- package/src/modules/sales/widgets/injection-table.ts +12 -0
- package/src/modules/shipping_carriers/acl.ts +6 -0
- package/src/modules/shipping_carriers/api/cancel/route.ts +53 -0
- package/src/modules/shipping_carriers/api/interceptors.ts +19 -0
- package/src/modules/shipping_carriers/api/openapi.ts +1 -0
- package/src/modules/shipping_carriers/api/rates/route.ts +53 -0
- package/src/modules/shipping_carriers/api/shipments/route.ts +59 -0
- package/src/modules/shipping_carriers/api/tracking/route.ts +56 -0
- package/src/modules/shipping_carriers/api/webhook/[provider]/route.ts +134 -0
- package/src/modules/shipping_carriers/data/enrichers.ts +89 -0
- package/src/modules/shipping_carriers/data/entities.ts +60 -0
- package/src/modules/shipping_carriers/data/validators.ts +48 -0
- package/src/modules/shipping_carriers/di.ts +20 -0
- package/src/modules/shipping_carriers/events.ts +16 -0
- package/src/modules/shipping_carriers/i18n/de.json +7 -0
- package/src/modules/shipping_carriers/i18n/en.json +7 -0
- package/src/modules/shipping_carriers/i18n/en.ts +7 -0
- package/src/modules/shipping_carriers/i18n/es.json +7 -0
- package/src/modules/shipping_carriers/i18n/pl.json +7 -0
- package/src/modules/shipping_carriers/i18n/pl.ts +7 -0
- package/src/modules/shipping_carriers/index.ts +5 -0
- package/src/modules/shipping_carriers/lib/adapter-registry.ts +33 -0
- package/src/modules/shipping_carriers/lib/adapter.ts +93 -0
- package/src/modules/shipping_carriers/lib/queue.ts +19 -0
- package/src/modules/shipping_carriers/lib/shipping-service.ts +204 -0
- package/src/modules/shipping_carriers/lib/status-sync.ts +38 -0
- package/src/modules/shipping_carriers/migrations/Migration20260305170000.ts +14 -0
- package/src/modules/shipping_carriers/setup.ts +11 -0
- package/src/modules/shipping_carriers/widgets/injection/create-shipment-button/widget.ts +24 -0
- package/src/modules/shipping_carriers/widgets/injection/tracking-column/widget.ts +22 -0
- package/src/modules/shipping_carriers/widgets/injection/tracking-status-badge/widget.tsx +44 -0
- package/src/modules/shipping_carriers/widgets/injection-table.ts +22 -0
- package/src/modules/shipping_carriers/workers/status-poller.ts +33 -0
- package/src/modules/shipping_carriers/workers/webhook-processor.ts +79 -0
- package/src/modules/translations/api/get/locales.ts +1 -0
- package/src/modules/translations/api/put/locales.ts +1 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { UniqueConstraintViolationException } from '@mikro-orm/core'
|
|
2
|
+
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
3
|
+
import { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'
|
|
4
|
+
import { WebhookProcessedEvent } from '../data/entities'
|
|
5
|
+
|
|
6
|
+
export async function claimWebhookProcessing(
|
|
7
|
+
em: EntityManager,
|
|
8
|
+
idempotencyKey: string,
|
|
9
|
+
providerKey: string,
|
|
10
|
+
scope: { organizationId: string; tenantId: string },
|
|
11
|
+
eventType: string,
|
|
12
|
+
): Promise<boolean> {
|
|
13
|
+
const record = em.create(WebhookProcessedEvent, {
|
|
14
|
+
idempotencyKey,
|
|
15
|
+
providerKey,
|
|
16
|
+
eventType,
|
|
17
|
+
organizationId: scope.organizationId,
|
|
18
|
+
tenantId: scope.tenantId,
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
await em.persistAndFlush(record)
|
|
23
|
+
return true
|
|
24
|
+
} catch (error: unknown) {
|
|
25
|
+
if (error instanceof UniqueConstraintViolationException) {
|
|
26
|
+
return false
|
|
27
|
+
}
|
|
28
|
+
throw error
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function releaseWebhookClaim(
|
|
33
|
+
em: EntityManager,
|
|
34
|
+
idempotencyKey: string,
|
|
35
|
+
providerKey: string,
|
|
36
|
+
scope: { organizationId: string; tenantId: string },
|
|
37
|
+
): Promise<void> {
|
|
38
|
+
const existing = await findOneWithDecryption(
|
|
39
|
+
em,
|
|
40
|
+
WebhookProcessedEvent,
|
|
41
|
+
{
|
|
42
|
+
idempotencyKey,
|
|
43
|
+
providerKey,
|
|
44
|
+
organizationId: scope.organizationId,
|
|
45
|
+
tenantId: scope.tenantId,
|
|
46
|
+
},
|
|
47
|
+
undefined,
|
|
48
|
+
scope,
|
|
49
|
+
)
|
|
50
|
+
if (!existing) return
|
|
51
|
+
await em.removeAndFlush(existing)
|
|
52
|
+
}
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
{
|
|
2
|
+
"namespaces": [
|
|
3
|
+
"public"
|
|
4
|
+
],
|
|
5
|
+
"name": "public",
|
|
6
|
+
"tables": [
|
|
7
|
+
{
|
|
8
|
+
"columns": {
|
|
9
|
+
"id": {
|
|
10
|
+
"name": "id",
|
|
11
|
+
"type": "uuid",
|
|
12
|
+
"unsigned": false,
|
|
13
|
+
"autoincrement": false,
|
|
14
|
+
"primary": false,
|
|
15
|
+
"nullable": false,
|
|
16
|
+
"default": "gen_random_uuid()",
|
|
17
|
+
"mappedType": "uuid"
|
|
18
|
+
},
|
|
19
|
+
"payment_id": {
|
|
20
|
+
"name": "payment_id",
|
|
21
|
+
"type": "uuid",
|
|
22
|
+
"unsigned": false,
|
|
23
|
+
"autoincrement": false,
|
|
24
|
+
"primary": false,
|
|
25
|
+
"nullable": false,
|
|
26
|
+
"mappedType": "uuid"
|
|
27
|
+
},
|
|
28
|
+
"provider_key": {
|
|
29
|
+
"name": "provider_key",
|
|
30
|
+
"type": "text",
|
|
31
|
+
"unsigned": false,
|
|
32
|
+
"autoincrement": false,
|
|
33
|
+
"primary": false,
|
|
34
|
+
"nullable": false,
|
|
35
|
+
"mappedType": "text"
|
|
36
|
+
},
|
|
37
|
+
"provider_session_id": {
|
|
38
|
+
"name": "provider_session_id",
|
|
39
|
+
"type": "text",
|
|
40
|
+
"unsigned": false,
|
|
41
|
+
"autoincrement": false,
|
|
42
|
+
"primary": false,
|
|
43
|
+
"nullable": true,
|
|
44
|
+
"mappedType": "text"
|
|
45
|
+
},
|
|
46
|
+
"gateway_payment_id": {
|
|
47
|
+
"name": "gateway_payment_id",
|
|
48
|
+
"type": "text",
|
|
49
|
+
"unsigned": false,
|
|
50
|
+
"autoincrement": false,
|
|
51
|
+
"primary": false,
|
|
52
|
+
"nullable": true,
|
|
53
|
+
"mappedType": "text"
|
|
54
|
+
},
|
|
55
|
+
"gateway_refund_id": {
|
|
56
|
+
"name": "gateway_refund_id",
|
|
57
|
+
"type": "text",
|
|
58
|
+
"unsigned": false,
|
|
59
|
+
"autoincrement": false,
|
|
60
|
+
"primary": false,
|
|
61
|
+
"nullable": true,
|
|
62
|
+
"mappedType": "text"
|
|
63
|
+
},
|
|
64
|
+
"unified_status": {
|
|
65
|
+
"name": "unified_status",
|
|
66
|
+
"type": "text",
|
|
67
|
+
"unsigned": false,
|
|
68
|
+
"autoincrement": false,
|
|
69
|
+
"primary": false,
|
|
70
|
+
"nullable": false,
|
|
71
|
+
"default": "'pending'",
|
|
72
|
+
"mappedType": "text"
|
|
73
|
+
},
|
|
74
|
+
"gateway_status": {
|
|
75
|
+
"name": "gateway_status",
|
|
76
|
+
"type": "text",
|
|
77
|
+
"unsigned": false,
|
|
78
|
+
"autoincrement": false,
|
|
79
|
+
"primary": false,
|
|
80
|
+
"nullable": true,
|
|
81
|
+
"mappedType": "text"
|
|
82
|
+
},
|
|
83
|
+
"redirect_url": {
|
|
84
|
+
"name": "redirect_url",
|
|
85
|
+
"type": "text",
|
|
86
|
+
"unsigned": false,
|
|
87
|
+
"autoincrement": false,
|
|
88
|
+
"primary": false,
|
|
89
|
+
"nullable": true,
|
|
90
|
+
"mappedType": "text"
|
|
91
|
+
},
|
|
92
|
+
"client_secret": {
|
|
93
|
+
"name": "client_secret",
|
|
94
|
+
"type": "text",
|
|
95
|
+
"unsigned": false,
|
|
96
|
+
"autoincrement": false,
|
|
97
|
+
"primary": false,
|
|
98
|
+
"nullable": true,
|
|
99
|
+
"mappedType": "text"
|
|
100
|
+
},
|
|
101
|
+
"amount": {
|
|
102
|
+
"name": "amount",
|
|
103
|
+
"type": "numeric(18,4)",
|
|
104
|
+
"unsigned": false,
|
|
105
|
+
"autoincrement": false,
|
|
106
|
+
"primary": false,
|
|
107
|
+
"nullable": false,
|
|
108
|
+
"precision": 18,
|
|
109
|
+
"scale": 4,
|
|
110
|
+
"mappedType": "decimal"
|
|
111
|
+
},
|
|
112
|
+
"currency_code": {
|
|
113
|
+
"name": "currency_code",
|
|
114
|
+
"type": "text",
|
|
115
|
+
"unsigned": false,
|
|
116
|
+
"autoincrement": false,
|
|
117
|
+
"primary": false,
|
|
118
|
+
"nullable": false,
|
|
119
|
+
"mappedType": "text"
|
|
120
|
+
},
|
|
121
|
+
"gateway_metadata": {
|
|
122
|
+
"name": "gateway_metadata",
|
|
123
|
+
"type": "jsonb",
|
|
124
|
+
"unsigned": false,
|
|
125
|
+
"autoincrement": false,
|
|
126
|
+
"primary": false,
|
|
127
|
+
"nullable": true,
|
|
128
|
+
"mappedType": "json"
|
|
129
|
+
},
|
|
130
|
+
"webhook_log": {
|
|
131
|
+
"name": "webhook_log",
|
|
132
|
+
"type": "jsonb",
|
|
133
|
+
"unsigned": false,
|
|
134
|
+
"autoincrement": false,
|
|
135
|
+
"primary": false,
|
|
136
|
+
"nullable": true,
|
|
137
|
+
"mappedType": "json"
|
|
138
|
+
},
|
|
139
|
+
"last_webhook_at": {
|
|
140
|
+
"name": "last_webhook_at",
|
|
141
|
+
"type": "timestamptz",
|
|
142
|
+
"unsigned": false,
|
|
143
|
+
"autoincrement": false,
|
|
144
|
+
"primary": false,
|
|
145
|
+
"nullable": true,
|
|
146
|
+
"length": 6,
|
|
147
|
+
"mappedType": "datetime"
|
|
148
|
+
},
|
|
149
|
+
"last_polled_at": {
|
|
150
|
+
"name": "last_polled_at",
|
|
151
|
+
"type": "timestamptz",
|
|
152
|
+
"unsigned": false,
|
|
153
|
+
"autoincrement": false,
|
|
154
|
+
"primary": false,
|
|
155
|
+
"nullable": true,
|
|
156
|
+
"length": 6,
|
|
157
|
+
"mappedType": "datetime"
|
|
158
|
+
},
|
|
159
|
+
"expires_at": {
|
|
160
|
+
"name": "expires_at",
|
|
161
|
+
"type": "timestamptz",
|
|
162
|
+
"unsigned": false,
|
|
163
|
+
"autoincrement": false,
|
|
164
|
+
"primary": false,
|
|
165
|
+
"nullable": true,
|
|
166
|
+
"length": 6,
|
|
167
|
+
"mappedType": "datetime"
|
|
168
|
+
},
|
|
169
|
+
"organization_id": {
|
|
170
|
+
"name": "organization_id",
|
|
171
|
+
"type": "uuid",
|
|
172
|
+
"unsigned": false,
|
|
173
|
+
"autoincrement": false,
|
|
174
|
+
"primary": false,
|
|
175
|
+
"nullable": false,
|
|
176
|
+
"mappedType": "uuid"
|
|
177
|
+
},
|
|
178
|
+
"tenant_id": {
|
|
179
|
+
"name": "tenant_id",
|
|
180
|
+
"type": "uuid",
|
|
181
|
+
"unsigned": false,
|
|
182
|
+
"autoincrement": false,
|
|
183
|
+
"primary": false,
|
|
184
|
+
"nullable": false,
|
|
185
|
+
"mappedType": "uuid"
|
|
186
|
+
},
|
|
187
|
+
"created_at": {
|
|
188
|
+
"name": "created_at",
|
|
189
|
+
"type": "timestamptz",
|
|
190
|
+
"unsigned": false,
|
|
191
|
+
"autoincrement": false,
|
|
192
|
+
"primary": false,
|
|
193
|
+
"nullable": false,
|
|
194
|
+
"length": 6,
|
|
195
|
+
"mappedType": "datetime"
|
|
196
|
+
},
|
|
197
|
+
"updated_at": {
|
|
198
|
+
"name": "updated_at",
|
|
199
|
+
"type": "timestamptz",
|
|
200
|
+
"unsigned": false,
|
|
201
|
+
"autoincrement": false,
|
|
202
|
+
"primary": false,
|
|
203
|
+
"nullable": false,
|
|
204
|
+
"length": 6,
|
|
205
|
+
"mappedType": "datetime"
|
|
206
|
+
},
|
|
207
|
+
"deleted_at": {
|
|
208
|
+
"name": "deleted_at",
|
|
209
|
+
"type": "timestamptz",
|
|
210
|
+
"unsigned": false,
|
|
211
|
+
"autoincrement": false,
|
|
212
|
+
"primary": false,
|
|
213
|
+
"nullable": true,
|
|
214
|
+
"length": 6,
|
|
215
|
+
"mappedType": "datetime"
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
"name": "gateway_transactions",
|
|
219
|
+
"schema": "public",
|
|
220
|
+
"indexes": [
|
|
221
|
+
{
|
|
222
|
+
"keyName": "gateway_transactions_organization_id_tenant_id_uni_5a9b9_index",
|
|
223
|
+
"columnNames": [
|
|
224
|
+
"organization_id",
|
|
225
|
+
"tenant_id",
|
|
226
|
+
"unified_status"
|
|
227
|
+
],
|
|
228
|
+
"composite": true,
|
|
229
|
+
"constraint": false,
|
|
230
|
+
"primary": false,
|
|
231
|
+
"unique": false
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
"keyName": "gateway_transactions_provider_key_provider_session_d8577_index",
|
|
235
|
+
"columnNames": [
|
|
236
|
+
"provider_key",
|
|
237
|
+
"provider_session_id",
|
|
238
|
+
"organization_id"
|
|
239
|
+
],
|
|
240
|
+
"composite": true,
|
|
241
|
+
"constraint": false,
|
|
242
|
+
"primary": false,
|
|
243
|
+
"unique": false
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
"keyName": "gateway_transactions_payment_id_organization_id_tenant_id_index",
|
|
247
|
+
"columnNames": [
|
|
248
|
+
"payment_id",
|
|
249
|
+
"organization_id",
|
|
250
|
+
"tenant_id"
|
|
251
|
+
],
|
|
252
|
+
"composite": true,
|
|
253
|
+
"constraint": false,
|
|
254
|
+
"primary": false,
|
|
255
|
+
"unique": false
|
|
256
|
+
},
|
|
257
|
+
{
|
|
258
|
+
"keyName": "gateway_transactions_pkey",
|
|
259
|
+
"columnNames": [
|
|
260
|
+
"id"
|
|
261
|
+
],
|
|
262
|
+
"composite": false,
|
|
263
|
+
"constraint": true,
|
|
264
|
+
"primary": true,
|
|
265
|
+
"unique": true
|
|
266
|
+
}
|
|
267
|
+
],
|
|
268
|
+
"checks": [],
|
|
269
|
+
"foreignKeys": {},
|
|
270
|
+
"nativeEnums": {}
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
"columns": {
|
|
274
|
+
"id": {
|
|
275
|
+
"name": "id",
|
|
276
|
+
"type": "uuid",
|
|
277
|
+
"unsigned": false,
|
|
278
|
+
"autoincrement": false,
|
|
279
|
+
"primary": false,
|
|
280
|
+
"nullable": false,
|
|
281
|
+
"default": "gen_random_uuid()",
|
|
282
|
+
"mappedType": "uuid"
|
|
283
|
+
},
|
|
284
|
+
"provider_key": {
|
|
285
|
+
"name": "provider_key",
|
|
286
|
+
"type": "text",
|
|
287
|
+
"unsigned": false,
|
|
288
|
+
"autoincrement": false,
|
|
289
|
+
"primary": false,
|
|
290
|
+
"nullable": false,
|
|
291
|
+
"mappedType": "text"
|
|
292
|
+
},
|
|
293
|
+
"idempotency_key": {
|
|
294
|
+
"name": "idempotency_key",
|
|
295
|
+
"type": "text",
|
|
296
|
+
"unsigned": false,
|
|
297
|
+
"autoincrement": false,
|
|
298
|
+
"primary": false,
|
|
299
|
+
"nullable": false,
|
|
300
|
+
"mappedType": "text"
|
|
301
|
+
},
|
|
302
|
+
"event_type": {
|
|
303
|
+
"name": "event_type",
|
|
304
|
+
"type": "text",
|
|
305
|
+
"unsigned": false,
|
|
306
|
+
"autoincrement": false,
|
|
307
|
+
"primary": false,
|
|
308
|
+
"nullable": false,
|
|
309
|
+
"mappedType": "text"
|
|
310
|
+
},
|
|
311
|
+
"organization_id": {
|
|
312
|
+
"name": "organization_id",
|
|
313
|
+
"type": "uuid",
|
|
314
|
+
"unsigned": false,
|
|
315
|
+
"autoincrement": false,
|
|
316
|
+
"primary": false,
|
|
317
|
+
"nullable": false,
|
|
318
|
+
"mappedType": "uuid"
|
|
319
|
+
},
|
|
320
|
+
"tenant_id": {
|
|
321
|
+
"name": "tenant_id",
|
|
322
|
+
"type": "uuid",
|
|
323
|
+
"unsigned": false,
|
|
324
|
+
"autoincrement": false,
|
|
325
|
+
"primary": false,
|
|
326
|
+
"nullable": false,
|
|
327
|
+
"mappedType": "uuid"
|
|
328
|
+
},
|
|
329
|
+
"processed_at": {
|
|
330
|
+
"name": "processed_at",
|
|
331
|
+
"type": "timestamptz",
|
|
332
|
+
"unsigned": false,
|
|
333
|
+
"autoincrement": false,
|
|
334
|
+
"primary": false,
|
|
335
|
+
"nullable": false,
|
|
336
|
+
"length": 6,
|
|
337
|
+
"mappedType": "datetime"
|
|
338
|
+
}
|
|
339
|
+
},
|
|
340
|
+
"name": "gateway_webhook_events",
|
|
341
|
+
"schema": "public",
|
|
342
|
+
"indexes": [
|
|
343
|
+
{
|
|
344
|
+
"keyName": "gateway_webhook_events_idempotency_unique",
|
|
345
|
+
"columnNames": [
|
|
346
|
+
"idempotency_key",
|
|
347
|
+
"provider_key",
|
|
348
|
+
"organization_id",
|
|
349
|
+
"tenant_id"
|
|
350
|
+
],
|
|
351
|
+
"composite": true,
|
|
352
|
+
"constraint": false,
|
|
353
|
+
"primary": false,
|
|
354
|
+
"unique": true
|
|
355
|
+
},
|
|
356
|
+
{
|
|
357
|
+
"keyName": "gateway_webhook_events_pkey",
|
|
358
|
+
"columnNames": [
|
|
359
|
+
"id"
|
|
360
|
+
],
|
|
361
|
+
"composite": false,
|
|
362
|
+
"constraint": true,
|
|
363
|
+
"primary": true,
|
|
364
|
+
"unique": true
|
|
365
|
+
}
|
|
366
|
+
],
|
|
367
|
+
"checks": [],
|
|
368
|
+
"foreignKeys": {},
|
|
369
|
+
"nativeEnums": {}
|
|
370
|
+
}
|
|
371
|
+
],
|
|
372
|
+
"nativeEnums": {}
|
|
373
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Migration } from '@mikro-orm/migrations';
|
|
2
|
+
|
|
3
|
+
export class Migration20260305122155 extends Migration {
|
|
4
|
+
|
|
5
|
+
override async up(): Promise<void> {
|
|
6
|
+
this.addSql(`create table "gateway_transactions" ("id" uuid not null default gen_random_uuid(), "payment_id" uuid not null, "provider_key" text not null, "provider_session_id" text null, "gateway_payment_id" text null, "gateway_refund_id" text null, "unified_status" text not null default 'pending', "gateway_status" text null, "redirect_url" text null, "client_secret" text null, "amount" numeric(18,4) not null, "currency_code" text not null, "gateway_metadata" jsonb null, "webhook_log" jsonb null, "last_webhook_at" timestamptz null, "last_polled_at" timestamptz null, "expires_at" timestamptz null, "organization_id" uuid not null, "tenant_id" uuid not null, "created_at" timestamptz not null, "updated_at" timestamptz not null, "deleted_at" timestamptz null, constraint "gateway_transactions_pkey" primary key ("id"));`);
|
|
7
|
+
this.addSql(`create index "gateway_transactions_organization_id_tenant_id_uni_5a9b9_index" on "gateway_transactions" ("organization_id", "tenant_id", "unified_status");`);
|
|
8
|
+
this.addSql(`create index "gateway_transactions_provider_key_provider_session_d8577_index" on "gateway_transactions" ("provider_key", "provider_session_id", "organization_id");`);
|
|
9
|
+
this.addSql(`create index "gateway_transactions_payment_id_organization_id_tenant_id_index" on "gateway_transactions" ("payment_id", "organization_id", "tenant_id");`);
|
|
10
|
+
|
|
11
|
+
this.addSql(`create table "gateway_webhook_events" ("id" uuid not null default gen_random_uuid(), "provider_key" text not null, "idempotency_key" text not null, "event_type" text not null, "organization_id" uuid not null, "tenant_id" uuid not null, "processed_at" timestamptz not null, constraint "gateway_webhook_events_pkey" primary key ("id"));`);
|
|
12
|
+
this.addSql(`create unique index "gateway_webhook_events_idempotency_unique" on "gateway_webhook_events" ("idempotency_key", "provider_key", "organization_id", "tenant_id");`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
override async down(): Promise<void> {
|
|
16
|
+
this.addSql(`drop table if exists "gateway_webhook_events";`);
|
|
17
|
+
this.addSql(`drop table if exists "gateway_transactions";`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ModuleSetupConfig } from '@open-mercato/shared/modules/setup'
|
|
2
|
+
|
|
3
|
+
export const setup: ModuleSetupConfig = {
|
|
4
|
+
defaultRoleFeatures: {
|
|
5
|
+
superadmin: ['payment_gateways.*'],
|
|
6
|
+
admin: ['payment_gateways.*'],
|
|
7
|
+
employee: ['payment_gateways.view'],
|
|
8
|
+
},
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default setup
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ModuleInjectionTable } from '@open-mercato/shared/modules/widgets/injection'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Keep hub module free of cross-module UI bindings.
|
|
5
|
+
* Consumer modules should map their own spots to payment_gateway widgets.
|
|
6
|
+
*/
|
|
7
|
+
export const injectionTable: ModuleInjectionTable = {}
|
|
8
|
+
|
|
9
|
+
export default injectionTable
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { JobContext, QueuedJob, WorkerMeta } from '@open-mercato/queue'
|
|
2
|
+
import type { IntegrationLogService } from '../../integrations/lib/log-service'
|
|
3
|
+
import type { PaymentGatewayService } from '../lib/gateway-service'
|
|
4
|
+
|
|
5
|
+
type PollerJobPayload = {
|
|
6
|
+
scope?: {
|
|
7
|
+
organizationId?: string
|
|
8
|
+
tenantId?: string
|
|
9
|
+
providerKey?: string
|
|
10
|
+
}
|
|
11
|
+
limit?: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type HandlerContext = JobContext & {
|
|
15
|
+
resolve: <T = unknown>(name: string) => T
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const metadata: WorkerMeta = {
|
|
19
|
+
queue: 'payment-gateways-status-poller',
|
|
20
|
+
id: 'payment-gateways:status-poller',
|
|
21
|
+
concurrency: 2,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default async function handle(job: QueuedJob<PollerJobPayload>, ctx: HandlerContext): Promise<void> {
|
|
25
|
+
const service = ctx.resolve<PaymentGatewayService>('paymentGatewayService')
|
|
26
|
+
const integrationLogService = ctx.resolve<IntegrationLogService>('integrationLogService')
|
|
27
|
+
const transactions = await service.listTransactionsForStatusPolling({
|
|
28
|
+
organizationId: job.payload.scope?.organizationId,
|
|
29
|
+
tenantId: job.payload.scope?.tenantId,
|
|
30
|
+
providerKey: job.payload.scope?.providerKey,
|
|
31
|
+
limit: job.payload.limit ?? 100,
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
for (const transaction of transactions) {
|
|
35
|
+
try {
|
|
36
|
+
await service.getPaymentStatus(transaction.id, {
|
|
37
|
+
organizationId: transaction.organizationId,
|
|
38
|
+
tenantId: transaction.tenantId,
|
|
39
|
+
})
|
|
40
|
+
} catch (error: unknown) {
|
|
41
|
+
const message = error instanceof Error ? error.message : 'Unknown polling error'
|
|
42
|
+
await integrationLogService.write({
|
|
43
|
+
integrationId: `gateway_${transaction.providerKey}`,
|
|
44
|
+
scopeEntityType: 'payment_transaction',
|
|
45
|
+
scopeEntityId: transaction.id,
|
|
46
|
+
level: 'error',
|
|
47
|
+
message: 'Payment status polling failed',
|
|
48
|
+
payload: {
|
|
49
|
+
transactionId: transaction.id,
|
|
50
|
+
message,
|
|
51
|
+
},
|
|
52
|
+
}, {
|
|
53
|
+
organizationId: transaction.organizationId,
|
|
54
|
+
tenantId: transaction.tenantId,
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
2
|
+
import type { WebhookEvent } from '@open-mercato/shared/modules/payment_gateways/types'
|
|
3
|
+
import type { JobContext, QueuedJob, WorkerMeta } from '@open-mercato/queue'
|
|
4
|
+
import type { IntegrationLogService } from '../../integrations/lib/log-service'
|
|
5
|
+
import type { PaymentGatewayService } from '../lib/gateway-service'
|
|
6
|
+
import { processPaymentGatewayWebhookJob, type PaymentGatewayWebhookJobPayload } from '../lib/webhook-processor'
|
|
7
|
+
|
|
8
|
+
type WebhookJobPayload = PaymentGatewayWebhookJobPayload & {
|
|
9
|
+
event: WebhookEvent
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type HandlerContext = JobContext & {
|
|
13
|
+
resolve: <T = unknown>(name: string) => T
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const metadata: WorkerMeta = {
|
|
17
|
+
queue: 'payment-gateways-webhook',
|
|
18
|
+
id: 'payment-gateways:webhook-processor',
|
|
19
|
+
concurrency: 5,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default async function handle(job: QueuedJob<WebhookJobPayload>, ctx: HandlerContext): Promise<void> {
|
|
23
|
+
const em = ctx.resolve<EntityManager>('em')
|
|
24
|
+
const paymentGatewayService = ctx.resolve<PaymentGatewayService>('paymentGatewayService')
|
|
25
|
+
const integrationLogService = ctx.resolve<IntegrationLogService>('integrationLogService')
|
|
26
|
+
await processPaymentGatewayWebhookJob(
|
|
27
|
+
{ em, paymentGatewayService, integrationLogService },
|
|
28
|
+
job.payload,
|
|
29
|
+
)
|
|
30
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Catalog Image Enricher
|
|
3
|
+
*
|
|
4
|
+
* Overrides the snapshot's `thumbnailUrl` with a freshly built URL from the
|
|
5
|
+
* product/variant's current `defaultMediaId`. This ensures quote/order lines
|
|
6
|
+
* always reflect the latest product image, even when the underlying attachment
|
|
7
|
+
* changes. The snapshot serves as fallback for deleted products.
|
|
8
|
+
*
|
|
9
|
+
* Uses raw Knex queries because cross-module ORM entity class references
|
|
10
|
+
* do not resolve correctly at runtime (the imported class does not match the
|
|
11
|
+
* entity registered in MikroORM's metadata by the app bootstrap).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { ResponseEnricher, EnricherContext } from '@open-mercato/shared/lib/crud/response-enricher'
|
|
15
|
+
import { buildAttachmentImageUrl } from '../../attachments/lib/imageUrls'
|
|
16
|
+
|
|
17
|
+
type LineRecord = Record<string, unknown> & { id: string }
|
|
18
|
+
|
|
19
|
+
type SnapshotNode = { thumbnailUrl?: string | null; [key: string]: unknown }
|
|
20
|
+
type CatalogSnapshot = { product?: SnapshotNode; variant?: SnapshotNode; [key: string]: unknown }
|
|
21
|
+
|
|
22
|
+
function getKnex(em: unknown): unknown {
|
|
23
|
+
return (em as any).getConnection?.()?.getKnex?.()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function fetchMediaIds(
|
|
27
|
+
knex: unknown,
|
|
28
|
+
table: string,
|
|
29
|
+
ids: Set<string>,
|
|
30
|
+
organizationId: string,
|
|
31
|
+
): Promise<Map<string, string | null>> {
|
|
32
|
+
const map = new Map<string, string | null>()
|
|
33
|
+
if (ids.size === 0) return map
|
|
34
|
+
|
|
35
|
+
const rows: Array<{ id: string; default_media_id: string | null }> = await (knex as any)(table)
|
|
36
|
+
.select('id', 'default_media_id')
|
|
37
|
+
.whereIn('id', [...ids])
|
|
38
|
+
.where('organization_id', organizationId)
|
|
39
|
+
.whereNull('deleted_at')
|
|
40
|
+
|
|
41
|
+
for (const row of rows) {
|
|
42
|
+
map.set(row.id, row.default_media_id ? buildAttachmentImageUrl(row.default_media_id) : null)
|
|
43
|
+
}
|
|
44
|
+
return map
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function enrichRecords(
|
|
48
|
+
records: LineRecord[],
|
|
49
|
+
productMedia: Map<string, string | null>,
|
|
50
|
+
variantMedia: Map<string, string | null>,
|
|
51
|
+
): LineRecord[] {
|
|
52
|
+
return records.map((record) => {
|
|
53
|
+
const productId = record['product_id'] as string | undefined
|
|
54
|
+
const variantId = record['product_variant_id'] as string | undefined
|
|
55
|
+
|
|
56
|
+
const productUrl = productId ? productMedia.get(productId) : undefined
|
|
57
|
+
const variantUrl = variantId ? variantMedia.get(variantId) : undefined
|
|
58
|
+
if (productUrl === undefined && variantUrl === undefined) return record
|
|
59
|
+
|
|
60
|
+
const snapshot = (record['catalog_snapshot'] as CatalogSnapshot | null | undefined) ?? {}
|
|
61
|
+
const updatedSnapshot = { ...snapshot }
|
|
62
|
+
|
|
63
|
+
if (productUrl !== undefined) {
|
|
64
|
+
updatedSnapshot.product = { ...snapshot.product, thumbnailUrl: productUrl ?? snapshot.product?.thumbnailUrl }
|
|
65
|
+
}
|
|
66
|
+
if (variantUrl !== undefined) {
|
|
67
|
+
updatedSnapshot.variant = { ...snapshot.variant, thumbnailUrl: variantUrl ?? snapshot.variant?.thumbnailUrl }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const changed =
|
|
71
|
+
updatedSnapshot.product?.thumbnailUrl !== snapshot.product?.thumbnailUrl ||
|
|
72
|
+
updatedSnapshot.variant?.thumbnailUrl !== snapshot.variant?.thumbnailUrl
|
|
73
|
+
if (!changed) return record
|
|
74
|
+
|
|
75
|
+
return { ...record, catalog_snapshot: updatedSnapshot }
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function createCatalogImageEnricher(targetEntity: string): ResponseEnricher<LineRecord> {
|
|
80
|
+
return {
|
|
81
|
+
id: `sales.catalog-image:${targetEntity}`,
|
|
82
|
+
targetEntity,
|
|
83
|
+
features: [],
|
|
84
|
+
priority: 5,
|
|
85
|
+
timeout: 1000,
|
|
86
|
+
critical: false,
|
|
87
|
+
fallback: {},
|
|
88
|
+
|
|
89
|
+
async enrichOne(record, context: EnricherContext) {
|
|
90
|
+
return (await this.enrichMany!([record], context))[0]
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
async enrichMany(records, context: EnricherContext) {
|
|
94
|
+
if (records.length === 0) return records
|
|
95
|
+
|
|
96
|
+
const knex = getKnex(context.em)
|
|
97
|
+
if (!knex) return records
|
|
98
|
+
|
|
99
|
+
const productIds = new Set<string>()
|
|
100
|
+
const variantIds = new Set<string>()
|
|
101
|
+
for (const record of records) {
|
|
102
|
+
if (typeof record['product_id'] === 'string') productIds.add(record['product_id'])
|
|
103
|
+
if (typeof record['product_variant_id'] === 'string') variantIds.add(record['product_variant_id'])
|
|
104
|
+
}
|
|
105
|
+
if (productIds.size === 0 && variantIds.size === 0) return records
|
|
106
|
+
|
|
107
|
+
const [productMedia, variantMedia] = await Promise.all([
|
|
108
|
+
fetchMediaIds(knex, 'catalog_products', productIds, context.organizationId),
|
|
109
|
+
fetchMediaIds(knex, 'catalog_product_variants', variantIds, context.organizationId),
|
|
110
|
+
])
|
|
111
|
+
|
|
112
|
+
return enrichRecords(records, productMedia, variantMedia)
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export const enrichers: ResponseEnricher[] = [
|
|
118
|
+
createCatalogImageEnricher('sales:sales_quote_line'),
|
|
119
|
+
createCatalogImageEnricher('sales:sales_order_line'),
|
|
120
|
+
]
|