@unifiedcommerce/core 0.0.1
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/adapters/console-email.d.ts +23 -0
- package/dist/adapters/console-email.d.ts.map +1 -0
- package/dist/adapters/console-email.js +38 -0
- package/dist/auth/access.d.ts +101 -0
- package/dist/auth/access.d.ts.map +1 -0
- package/dist/auth/access.js +128 -0
- package/dist/auth/auth-schema.d.ts +1383 -0
- package/dist/auth/auth-schema.d.ts.map +1 -0
- package/dist/auth/auth-schema.js +117 -0
- package/dist/auth/middleware.d.ts +5 -0
- package/dist/auth/middleware.d.ts.map +1 -0
- package/dist/auth/middleware.js +91 -0
- package/dist/auth/org.d.ts +22 -0
- package/dist/auth/org.d.ts.map +1 -0
- package/dist/auth/org.js +36 -0
- package/dist/auth/permissions.d.ts +4 -0
- package/dist/auth/permissions.d.ts.map +1 -0
- package/dist/auth/permissions.js +24 -0
- package/dist/auth/pos.d.ts +3 -0
- package/dist/auth/pos.d.ts.map +1 -0
- package/dist/auth/pos.js +62 -0
- package/dist/auth/setup.d.ts +31 -0
- package/dist/auth/setup.d.ts.map +1 -0
- package/dist/auth/setup.js +106 -0
- package/dist/auth/system-actor.d.ts +7 -0
- package/dist/auth/system-actor.d.ts.map +1 -0
- package/dist/auth/system-actor.js +17 -0
- package/dist/auth/types.d.ts +11 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +1 -0
- package/dist/config/defaults.d.ts +3 -0
- package/dist/config/defaults.d.ts.map +1 -0
- package/dist/config/defaults.js +80 -0
- package/dist/config/define-config.d.ts +9 -0
- package/dist/config/define-config.d.ts.map +1 -0
- package/dist/config/define-config.js +44 -0
- package/dist/config/types.d.ts +299 -0
- package/dist/config/types.d.ts.map +1 -0
- package/dist/config/types.js +1 -0
- package/dist/generated/plugin-manifest.d.ts +48 -0
- package/dist/generated/plugin-manifest.d.ts.map +1 -0
- package/dist/generated/plugin-manifest.js +20 -0
- package/dist/hooks/checkout-completion.d.ts +58 -0
- package/dist/hooks/checkout-completion.d.ts.map +1 -0
- package/dist/hooks/checkout-completion.js +137 -0
- package/dist/hooks/checkout.d.ts +99 -0
- package/dist/hooks/checkout.d.ts.map +1 -0
- package/dist/hooks/checkout.js +317 -0
- package/dist/hooks/order-emails.d.ts +16 -0
- package/dist/hooks/order-emails.d.ts.map +1 -0
- package/dist/hooks/order-emails.js +44 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13381 -0
- package/dist/index.js.map +156 -0
- package/dist/interfaces/graphql/index.d.ts +4 -0
- package/dist/interfaces/graphql/index.d.ts.map +1 -0
- package/dist/interfaces/graphql/index.js +415 -0
- package/dist/interfaces/mcp/agent-prompt.d.ts +16 -0
- package/dist/interfaces/mcp/agent-prompt.d.ts.map +1 -0
- package/dist/interfaces/mcp/agent-prompt.js +172 -0
- package/dist/interfaces/mcp/context-enrichment.d.ts +39 -0
- package/dist/interfaces/mcp/context-enrichment.d.ts.map +1 -0
- package/dist/interfaces/mcp/context-enrichment.js +119 -0
- package/dist/interfaces/mcp/server.d.ts +7 -0
- package/dist/interfaces/mcp/server.d.ts.map +1 -0
- package/dist/interfaces/mcp/server.js +515 -0
- package/dist/interfaces/mcp/transport.d.ts +5 -0
- package/dist/interfaces/mcp/transport.d.ts.map +1 -0
- package/dist/interfaces/mcp/transport.js +52 -0
- package/dist/interfaces/rest/customer-portal.d.ts +5 -0
- package/dist/interfaces/rest/customer-portal.d.ts.map +1 -0
- package/dist/interfaces/rest/customer-portal.js +206 -0
- package/dist/interfaces/rest/index.d.ts +5 -0
- package/dist/interfaces/rest/index.d.ts.map +1 -0
- package/dist/interfaces/rest/index.js +68 -0
- package/dist/interfaces/rest/router.d.ts +164 -0
- package/dist/interfaces/rest/router.d.ts.map +1 -0
- package/dist/interfaces/rest/router.js +259 -0
- package/dist/interfaces/rest/routes/admin-jobs.d.ts +5 -0
- package/dist/interfaces/rest/routes/admin-jobs.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/admin-jobs.js +48 -0
- package/dist/interfaces/rest/routes/audit.d.ts +5 -0
- package/dist/interfaces/rest/routes/audit.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/audit.js +43 -0
- package/dist/interfaces/rest/routes/carts.d.ts +5 -0
- package/dist/interfaces/rest/routes/carts.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/carts.js +55 -0
- package/dist/interfaces/rest/routes/catalog.d.ts +5 -0
- package/dist/interfaces/rest/routes/catalog.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/catalog.js +256 -0
- package/dist/interfaces/rest/routes/checkout.d.ts +5 -0
- package/dist/interfaces/rest/routes/checkout.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/checkout.js +216 -0
- package/dist/interfaces/rest/routes/inventory.d.ts +5 -0
- package/dist/interfaces/rest/routes/inventory.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/inventory.js +59 -0
- package/dist/interfaces/rest/routes/media.d.ts +5 -0
- package/dist/interfaces/rest/routes/media.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/media.js +65 -0
- package/dist/interfaces/rest/routes/orders.d.ts +5 -0
- package/dist/interfaces/rest/routes/orders.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/orders.js +64 -0
- package/dist/interfaces/rest/routes/payments.d.ts +5 -0
- package/dist/interfaces/rest/routes/payments.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/payments.js +45 -0
- package/dist/interfaces/rest/routes/pricing.d.ts +5 -0
- package/dist/interfaces/rest/routes/pricing.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/pricing.js +48 -0
- package/dist/interfaces/rest/routes/promotions.d.ts +5 -0
- package/dist/interfaces/rest/routes/promotions.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/promotions.js +75 -0
- package/dist/interfaces/rest/routes/search.d.ts +5 -0
- package/dist/interfaces/rest/routes/search.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/search.js +60 -0
- package/dist/interfaces/rest/routes/webhooks.d.ts +5 -0
- package/dist/interfaces/rest/routes/webhooks.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/webhooks.js +39 -0
- package/dist/interfaces/rest/schemas/admin-jobs.d.ts +327 -0
- package/dist/interfaces/rest/schemas/admin-jobs.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/admin-jobs.js +37 -0
- package/dist/interfaces/rest/schemas/audit.d.ts +59 -0
- package/dist/interfaces/rest/schemas/audit.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/audit.js +43 -0
- package/dist/interfaces/rest/schemas/carts.d.ts +1446 -0
- package/dist/interfaces/rest/schemas/carts.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/carts.js +109 -0
- package/dist/interfaces/rest/schemas/catalog.d.ts +5416 -0
- package/dist/interfaces/rest/schemas/catalog.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/catalog.js +397 -0
- package/dist/interfaces/rest/schemas/checkout.d.ts +160 -0
- package/dist/interfaces/rest/schemas/checkout.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/checkout.js +60 -0
- package/dist/interfaces/rest/schemas/customer-portal.d.ts +2197 -0
- package/dist/interfaces/rest/schemas/customer-portal.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/customer-portal.js +177 -0
- package/dist/interfaces/rest/schemas/inventory.d.ts +469 -0
- package/dist/interfaces/rest/schemas/inventory.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/inventory.js +128 -0
- package/dist/interfaces/rest/schemas/media.d.ts +303 -0
- package/dist/interfaces/rest/schemas/media.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/media.js +69 -0
- package/dist/interfaces/rest/schemas/orders.d.ts +1782 -0
- package/dist/interfaces/rest/schemas/orders.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/orders.js +93 -0
- package/dist/interfaces/rest/schemas/pricing.d.ts +256 -0
- package/dist/interfaces/rest/schemas/pricing.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/pricing.js +72 -0
- package/dist/interfaces/rest/schemas/promotions.d.ts +363 -0
- package/dist/interfaces/rest/schemas/promotions.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/promotions.js +98 -0
- package/dist/interfaces/rest/schemas/responses.d.ts +4062 -0
- package/dist/interfaces/rest/schemas/responses.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/responses.js +63 -0
- package/dist/interfaces/rest/schemas/search.d.ts +245 -0
- package/dist/interfaces/rest/schemas/search.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/search.js +55 -0
- package/dist/interfaces/rest/schemas/shared.d.ts +95 -0
- package/dist/interfaces/rest/schemas/shared.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/shared.js +51 -0
- package/dist/interfaces/rest/schemas/webhooks.d.ts +221 -0
- package/dist/interfaces/rest/schemas/webhooks.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/webhooks.js +62 -0
- package/dist/interfaces/rest/utils.d.ts +45 -0
- package/dist/interfaces/rest/utils.d.ts.map +1 -0
- package/dist/interfaces/rest/utils.js +71 -0
- package/dist/interfaces/rest/webhook-router.d.ts +41 -0
- package/dist/interfaces/rest/webhook-router.d.ts.map +1 -0
- package/dist/interfaces/rest/webhook-router.js +36 -0
- package/dist/kernel/compensation/executor.d.ts +21 -0
- package/dist/kernel/compensation/executor.d.ts.map +1 -0
- package/dist/kernel/compensation/executor.js +36 -0
- package/dist/kernel/compensation/types.d.ts +25 -0
- package/dist/kernel/compensation/types.d.ts.map +1 -0
- package/dist/kernel/compensation/types.js +1 -0
- package/dist/kernel/database/adapter.d.ts +10 -0
- package/dist/kernel/database/adapter.d.ts.map +1 -0
- package/dist/kernel/database/adapter.js +3 -0
- package/dist/kernel/database/drizzle-db.d.ts +49 -0
- package/dist/kernel/database/drizzle-db.d.ts.map +1 -0
- package/dist/kernel/database/drizzle-db.js +20 -0
- package/dist/kernel/database/migrate.d.ts +38 -0
- package/dist/kernel/database/migrate.d.ts.map +1 -0
- package/dist/kernel/database/migrate.js +61 -0
- package/dist/kernel/database/plugin-types.d.ts +32 -0
- package/dist/kernel/database/plugin-types.d.ts.map +1 -0
- package/dist/kernel/database/plugin-types.js +10 -0
- package/dist/kernel/database/schema.d.ts +24 -0
- package/dist/kernel/database/schema.d.ts.map +1 -0
- package/dist/kernel/database/schema.js +36 -0
- package/dist/kernel/database/scoped-db.d.ts +20 -0
- package/dist/kernel/database/scoped-db.d.ts.map +1 -0
- package/dist/kernel/database/scoped-db.js +62 -0
- package/dist/kernel/database/tx-context.d.ts +15 -0
- package/dist/kernel/database/tx-context.d.ts.map +1 -0
- package/dist/kernel/database/tx-context.js +19 -0
- package/dist/kernel/error-mapper.d.ts +3 -0
- package/dist/kernel/error-mapper.d.ts.map +1 -0
- package/dist/kernel/error-mapper.js +12 -0
- package/dist/kernel/errors.d.ts +38 -0
- package/dist/kernel/errors.d.ts.map +1 -0
- package/dist/kernel/errors.js +69 -0
- package/dist/kernel/factory/in-memory-repository-factory.d.ts +20 -0
- package/dist/kernel/factory/in-memory-repository-factory.d.ts.map +1 -0
- package/dist/kernel/factory/in-memory-repository-factory.js +83 -0
- package/dist/kernel/factory/repository-factory.d.ts +71 -0
- package/dist/kernel/factory/repository-factory.d.ts.map +1 -0
- package/dist/kernel/factory/repository-factory.js +136 -0
- package/dist/kernel/hooks/create-context.d.ts +25 -0
- package/dist/kernel/hooks/create-context.d.ts.map +1 -0
- package/dist/kernel/hooks/create-context.js +22 -0
- package/dist/kernel/hooks/executor.d.ts +12 -0
- package/dist/kernel/hooks/executor.d.ts.map +1 -0
- package/dist/kernel/hooks/executor.js +50 -0
- package/dist/kernel/hooks/registry.d.ts +28 -0
- package/dist/kernel/hooks/registry.d.ts.map +1 -0
- package/dist/kernel/hooks/registry.js +58 -0
- package/dist/kernel/hooks/types.d.ts +37 -0
- package/dist/kernel/hooks/types.d.ts.map +1 -0
- package/dist/kernel/hooks/types.js +1 -0
- package/dist/kernel/http-error.d.ts +30 -0
- package/dist/kernel/http-error.d.ts.map +1 -0
- package/dist/kernel/http-error.js +35 -0
- package/dist/kernel/jobs/adapter.d.ts +25 -0
- package/dist/kernel/jobs/adapter.d.ts.map +1 -0
- package/dist/kernel/jobs/adapter.js +9 -0
- package/dist/kernel/jobs/drizzle-adapter.d.ts +15 -0
- package/dist/kernel/jobs/drizzle-adapter.d.ts.map +1 -0
- package/dist/kernel/jobs/drizzle-adapter.js +42 -0
- package/dist/kernel/jobs/runner.d.ts +24 -0
- package/dist/kernel/jobs/runner.d.ts.map +1 -0
- package/dist/kernel/jobs/runner.js +114 -0
- package/dist/kernel/jobs/schema.d.ts +280 -0
- package/dist/kernel/jobs/schema.d.ts.map +1 -0
- package/dist/kernel/jobs/schema.js +37 -0
- package/dist/kernel/jobs/types.d.ts +30 -0
- package/dist/kernel/jobs/types.d.ts.map +1 -0
- package/dist/kernel/jobs/types.js +1 -0
- package/dist/kernel/local-api.d.ts +103 -0
- package/dist/kernel/local-api.d.ts.map +1 -0
- package/dist/kernel/local-api.js +87 -0
- package/dist/kernel/plugin/capability-registry.d.ts +9 -0
- package/dist/kernel/plugin/capability-registry.d.ts.map +1 -0
- package/dist/kernel/plugin/capability-registry.js +28 -0
- package/dist/kernel/plugin/dependency-graph.d.ts +6 -0
- package/dist/kernel/plugin/dependency-graph.d.ts.map +1 -0
- package/dist/kernel/plugin/dependency-graph.js +55 -0
- package/dist/kernel/plugin/manifest.d.ts +90 -0
- package/dist/kernel/plugin/manifest.d.ts.map +1 -0
- package/dist/kernel/plugin/manifest.js +140 -0
- package/dist/kernel/query/executor.d.ts +21 -0
- package/dist/kernel/query/executor.d.ts.map +1 -0
- package/dist/kernel/query/executor.js +128 -0
- package/dist/kernel/query/registry.d.ts +33 -0
- package/dist/kernel/query/registry.d.ts.map +1 -0
- package/dist/kernel/query/registry.js +20 -0
- package/dist/kernel/result.d.ts +36 -0
- package/dist/kernel/result.d.ts.map +1 -0
- package/dist/kernel/result.js +16 -0
- package/dist/kernel/schema/extra-columns.d.ts +23 -0
- package/dist/kernel/schema/extra-columns.d.ts.map +1 -0
- package/dist/kernel/schema/extra-columns.js +10 -0
- package/dist/kernel/service-registry.d.ts +109 -0
- package/dist/kernel/service-registry.d.ts.map +1 -0
- package/dist/kernel/service-registry.js +26 -0
- package/dist/kernel/service-timing.d.ts +25 -0
- package/dist/kernel/service-timing.d.ts.map +1 -0
- package/dist/kernel/service-timing.js +62 -0
- package/dist/kernel/state-machine/machine.d.ts +24 -0
- package/dist/kernel/state-machine/machine.d.ts.map +1 -0
- package/dist/kernel/state-machine/machine.js +70 -0
- package/dist/modules/analytics/cubes.d.ts +19 -0
- package/dist/modules/analytics/cubes.d.ts.map +1 -0
- package/dist/modules/analytics/cubes.js +187 -0
- package/dist/modules/analytics/drizzle-adapter.d.ts +13 -0
- package/dist/modules/analytics/drizzle-adapter.d.ts.map +1 -0
- package/dist/modules/analytics/drizzle-adapter.js +358 -0
- package/dist/modules/analytics/hooks.d.ts +13 -0
- package/dist/modules/analytics/hooks.d.ts.map +1 -0
- package/dist/modules/analytics/hooks.js +12 -0
- package/dist/modules/analytics/models.d.ts +14 -0
- package/dist/modules/analytics/models.d.ts.map +1 -0
- package/dist/modules/analytics/models.js +118 -0
- package/dist/modules/analytics/repository/index.d.ts +5 -0
- package/dist/modules/analytics/repository/index.d.ts.map +1 -0
- package/dist/modules/analytics/repository/index.js +1 -0
- package/dist/modules/analytics/service.d.ts +45 -0
- package/dist/modules/analytics/service.d.ts.map +1 -0
- package/dist/modules/analytics/service.js +196 -0
- package/dist/modules/analytics/types.d.ts +119 -0
- package/dist/modules/analytics/types.d.ts.map +1 -0
- package/dist/modules/analytics/types.js +25 -0
- package/dist/modules/audit/hooks.d.ts +7 -0
- package/dist/modules/audit/hooks.d.ts.map +1 -0
- package/dist/modules/audit/hooks.js +67 -0
- package/dist/modules/audit/schema.d.ts +178 -0
- package/dist/modules/audit/schema.d.ts.map +1 -0
- package/dist/modules/audit/schema.js +21 -0
- package/dist/modules/audit/service.d.ts +38 -0
- package/dist/modules/audit/service.d.ts.map +1 -0
- package/dist/modules/audit/service.js +109 -0
- package/dist/modules/cart/access.d.ts +11 -0
- package/dist/modules/cart/access.d.ts.map +1 -0
- package/dist/modules/cart/access.js +18 -0
- package/dist/modules/cart/matcher.d.ts +20 -0
- package/dist/modules/cart/matcher.d.ts.map +1 -0
- package/dist/modules/cart/matcher.js +2 -0
- package/dist/modules/cart/repository/in-memory.d.ts +30 -0
- package/dist/modules/cart/repository/in-memory.d.ts.map +1 -0
- package/dist/modules/cart/repository/in-memory.js +159 -0
- package/dist/modules/cart/repository/index.d.ts +45 -0
- package/dist/modules/cart/repository/index.d.ts.map +1 -0
- package/dist/modules/cart/repository/index.js +158 -0
- package/dist/modules/cart/schema.d.ts +359 -0
- package/dist/modules/cart/schema.d.ts.map +1 -0
- package/dist/modules/cart/schema.js +40 -0
- package/dist/modules/cart/schemas.d.ts +29 -0
- package/dist/modules/cart/schemas.d.ts.map +1 -0
- package/dist/modules/cart/schemas.js +14 -0
- package/dist/modules/cart/service.d.ts +63 -0
- package/dist/modules/cart/service.d.ts.map +1 -0
- package/dist/modules/cart/service.js +339 -0
- package/dist/modules/catalog/repository/in-memory.d.ts +82 -0
- package/dist/modules/catalog/repository/in-memory.d.ts.map +1 -0
- package/dist/modules/catalog/repository/in-memory.js +444 -0
- package/dist/modules/catalog/repository/index.d.ts +106 -0
- package/dist/modules/catalog/repository/index.d.ts.map +1 -0
- package/dist/modules/catalog/repository/index.js +455 -0
- package/dist/modules/catalog/schema.d.ts +1193 -0
- package/dist/modules/catalog/schema.d.ts.map +1 -0
- package/dist/modules/catalog/schema.js +149 -0
- package/dist/modules/catalog/schemas.d.ts +81 -0
- package/dist/modules/catalog/schemas.d.ts.map +1 -0
- package/dist/modules/catalog/schemas.js +62 -0
- package/dist/modules/catalog/service.d.ts +160 -0
- package/dist/modules/catalog/service.d.ts.map +1 -0
- package/dist/modules/catalog/service.js +759 -0
- package/dist/modules/customers/repository/in-memory.d.ts +37 -0
- package/dist/modules/customers/repository/in-memory.d.ts.map +1 -0
- package/dist/modules/customers/repository/in-memory.js +278 -0
- package/dist/modules/customers/repository/index.d.ts +47 -0
- package/dist/modules/customers/repository/index.d.ts.map +1 -0
- package/dist/modules/customers/repository/index.js +206 -0
- package/dist/modules/customers/schema.d.ts +560 -0
- package/dist/modules/customers/schema.d.ts.map +1 -0
- package/dist/modules/customers/schema.js +60 -0
- package/dist/modules/customers/service.d.ts +25 -0
- package/dist/modules/customers/service.d.ts.map +1 -0
- package/dist/modules/customers/service.js +91 -0
- package/dist/modules/fulfillment/repository/in-memory.d.ts +53 -0
- package/dist/modules/fulfillment/repository/in-memory.d.ts.map +1 -0
- package/dist/modules/fulfillment/repository/in-memory.js +327 -0
- package/dist/modules/fulfillment/repository/index.d.ts +63 -0
- package/dist/modules/fulfillment/repository/index.d.ts.map +1 -0
- package/dist/modules/fulfillment/repository/index.js +268 -0
- package/dist/modules/fulfillment/schema.d.ts +655 -0
- package/dist/modules/fulfillment/schema.d.ts.map +1 -0
- package/dist/modules/fulfillment/schema.js +83 -0
- package/dist/modules/fulfillment/service.d.ts +58 -0
- package/dist/modules/fulfillment/service.d.ts.map +1 -0
- package/dist/modules/fulfillment/service.js +338 -0
- package/dist/modules/fulfillment/types.d.ts +44 -0
- package/dist/modules/fulfillment/types.d.ts.map +1 -0
- package/dist/modules/fulfillment/types.js +1 -0
- package/dist/modules/inventory/repository/in-memory.d.ts +51 -0
- package/dist/modules/inventory/repository/in-memory.d.ts.map +1 -0
- package/dist/modules/inventory/repository/in-memory.js +281 -0
- package/dist/modules/inventory/repository/index.d.ts +81 -0
- package/dist/modules/inventory/repository/index.d.ts.map +1 -0
- package/dist/modules/inventory/repository/index.js +310 -0
- package/dist/modules/inventory/schema.d.ts +570 -0
- package/dist/modules/inventory/schema.d.ts.map +1 -0
- package/dist/modules/inventory/schema.js +69 -0
- package/dist/modules/inventory/schemas.d.ts +31 -0
- package/dist/modules/inventory/schemas.d.ts.map +1 -0
- package/dist/modules/inventory/schemas.js +28 -0
- package/dist/modules/inventory/service.d.ts +65 -0
- package/dist/modules/inventory/service.d.ts.map +1 -0
- package/dist/modules/inventory/service.js +271 -0
- package/dist/modules/media/adapter.d.ts +16 -0
- package/dist/modules/media/adapter.d.ts.map +1 -0
- package/dist/modules/media/adapter.js +1 -0
- package/dist/modules/media/repository/in-memory.d.ts +26 -0
- package/dist/modules/media/repository/in-memory.d.ts.map +1 -0
- package/dist/modules/media/repository/in-memory.js +145 -0
- package/dist/modules/media/repository/index.d.ts +35 -0
- package/dist/modules/media/repository/index.d.ts.map +1 -0
- package/dist/modules/media/repository/index.js +176 -0
- package/dist/modules/media/schema.d.ts +289 -0
- package/dist/modules/media/schema.d.ts.map +1 -0
- package/dist/modules/media/schema.js +35 -0
- package/dist/modules/media/service.d.ts +42 -0
- package/dist/modules/media/service.d.ts.map +1 -0
- package/dist/modules/media/service.js +89 -0
- package/dist/modules/orders/repository/in-memory.d.ts +40 -0
- package/dist/modules/orders/repository/in-memory.d.ts.map +1 -0
- package/dist/modules/orders/repository/in-memory.js +245 -0
- package/dist/modules/orders/repository/index.d.ts +48 -0
- package/dist/modules/orders/repository/index.d.ts.map +1 -0
- package/dist/modules/orders/repository/index.js +199 -0
- package/dist/modules/orders/schema.d.ts +672 -0
- package/dist/modules/orders/schema.d.ts.map +1 -0
- package/dist/modules/orders/schema.js +63 -0
- package/dist/modules/orders/service.d.ts +85 -0
- package/dist/modules/orders/service.d.ts.map +1 -0
- package/dist/modules/orders/service.js +313 -0
- package/dist/modules/orders/stale-order-cleanup.d.ts +27 -0
- package/dist/modules/orders/stale-order-cleanup.d.ts.map +1 -0
- package/dist/modules/orders/stale-order-cleanup.js +55 -0
- package/dist/modules/organization/service.d.ts +53 -0
- package/dist/modules/organization/service.d.ts.map +1 -0
- package/dist/modules/organization/service.js +151 -0
- package/dist/modules/payments/adapter.d.ts +42 -0
- package/dist/modules/payments/adapter.d.ts.map +1 -0
- package/dist/modules/payments/adapter.js +1 -0
- package/dist/modules/payments/repository/index.d.ts +5 -0
- package/dist/modules/payments/repository/index.d.ts.map +1 -0
- package/dist/modules/payments/repository/index.js +1 -0
- package/dist/modules/payments/service.d.ts +23 -0
- package/dist/modules/payments/service.d.ts.map +1 -0
- package/dist/modules/payments/service.js +72 -0
- package/dist/modules/pricing/repository/in-memory.d.ts +25 -0
- package/dist/modules/pricing/repository/in-memory.d.ts.map +1 -0
- package/dist/modules/pricing/repository/in-memory.js +288 -0
- package/dist/modules/pricing/repository/index.d.ts +34 -0
- package/dist/modules/pricing/repository/index.d.ts.map +1 -0
- package/dist/modules/pricing/repository/index.js +176 -0
- package/dist/modules/pricing/schema.d.ts +565 -0
- package/dist/modules/pricing/schema.d.ts.map +1 -0
- package/dist/modules/pricing/schema.js +57 -0
- package/dist/modules/pricing/schemas.d.ts +37 -0
- package/dist/modules/pricing/schemas.d.ts.map +1 -0
- package/dist/modules/pricing/schemas.js +30 -0
- package/dist/modules/pricing/service.d.ts +62 -0
- package/dist/modules/pricing/service.d.ts.map +1 -0
- package/dist/modules/pricing/service.js +308 -0
- package/dist/modules/promotions/repository/in-memory.d.ts +32 -0
- package/dist/modules/promotions/repository/in-memory.d.ts.map +1 -0
- package/dist/modules/promotions/repository/in-memory.js +227 -0
- package/dist/modules/promotions/repository/index.d.ts +41 -0
- package/dist/modules/promotions/repository/index.d.ts.map +1 -0
- package/dist/modules/promotions/repository/index.js +204 -0
- package/dist/modules/promotions/schema.d.ts +427 -0
- package/dist/modules/promotions/schema.d.ts.map +1 -0
- package/dist/modules/promotions/schema.js +52 -0
- package/dist/modules/promotions/schemas.d.ts +33 -0
- package/dist/modules/promotions/schemas.d.ts.map +1 -0
- package/dist/modules/promotions/schemas.js +32 -0
- package/dist/modules/promotions/service.d.ts +80 -0
- package/dist/modules/promotions/service.d.ts.map +1 -0
- package/dist/modules/promotions/service.js +347 -0
- package/dist/modules/search/adapter.d.ts +51 -0
- package/dist/modules/search/adapter.d.ts.map +1 -0
- package/dist/modules/search/adapter.js +1 -0
- package/dist/modules/search/hooks.d.ts +8 -0
- package/dist/modules/search/hooks.d.ts.map +1 -0
- package/dist/modules/search/hooks.js +6 -0
- package/dist/modules/search/repository/index.d.ts +5 -0
- package/dist/modules/search/repository/index.d.ts.map +1 -0
- package/dist/modules/search/repository/index.js +1 -0
- package/dist/modules/search/service.d.ts +24 -0
- package/dist/modules/search/service.d.ts.map +1 -0
- package/dist/modules/search/service.js +217 -0
- package/dist/modules/shipping/calculator.d.ts +42 -0
- package/dist/modules/shipping/calculator.d.ts.map +1 -0
- package/dist/modules/shipping/calculator.js +91 -0
- package/dist/modules/shipping/repository/index.d.ts +5 -0
- package/dist/modules/shipping/repository/index.d.ts.map +1 -0
- package/dist/modules/shipping/repository/index.js +1 -0
- package/dist/modules/shipping/service.d.ts +28 -0
- package/dist/modules/shipping/service.d.ts.map +1 -0
- package/dist/modules/shipping/service.js +20 -0
- package/dist/modules/tax/adapter.d.ts +58 -0
- package/dist/modules/tax/adapter.d.ts.map +1 -0
- package/dist/modules/tax/adapter.js +1 -0
- package/dist/modules/tax/repository/index.d.ts +5 -0
- package/dist/modules/tax/repository/index.d.ts.map +1 -0
- package/dist/modules/tax/repository/index.js +1 -0
- package/dist/modules/tax/service.d.ts +19 -0
- package/dist/modules/tax/service.d.ts.map +1 -0
- package/dist/modules/tax/service.js +34 -0
- package/dist/modules/webhooks/hook.d.ts +13 -0
- package/dist/modules/webhooks/hook.d.ts.map +1 -0
- package/dist/modules/webhooks/hook.js +29 -0
- package/dist/modules/webhooks/repository/in-memory.d.ts +31 -0
- package/dist/modules/webhooks/repository/in-memory.d.ts.map +1 -0
- package/dist/modules/webhooks/repository/in-memory.js +187 -0
- package/dist/modules/webhooks/repository/index.d.ts +40 -0
- package/dist/modules/webhooks/repository/index.d.ts.map +1 -0
- package/dist/modules/webhooks/repository/index.js +175 -0
- package/dist/modules/webhooks/schema.d.ts +404 -0
- package/dist/modules/webhooks/schema.d.ts.map +1 -0
- package/dist/modules/webhooks/schema.js +40 -0
- package/dist/modules/webhooks/service.d.ts +23 -0
- package/dist/modules/webhooks/service.d.ts.map +1 -0
- package/dist/modules/webhooks/service.js +92 -0
- package/dist/modules/webhooks/signing.d.ts +2 -0
- package/dist/modules/webhooks/signing.d.ts.map +1 -0
- package/dist/modules/webhooks/signing.js +5 -0
- package/dist/modules/webhooks/ssrf-guard.d.ts +19 -0
- package/dist/modules/webhooks/ssrf-guard.d.ts.map +1 -0
- package/dist/modules/webhooks/ssrf-guard.js +79 -0
- package/dist/modules/webhooks/tasks.d.ts +16 -0
- package/dist/modules/webhooks/tasks.d.ts.map +1 -0
- package/dist/modules/webhooks/tasks.js +35 -0
- package/dist/modules/webhooks/worker.d.ts +21 -0
- package/dist/modules/webhooks/worker.d.ts.map +1 -0
- package/dist/modules/webhooks/worker.js +113 -0
- package/dist/runtime/commerce.d.ts +110 -0
- package/dist/runtime/commerce.d.ts.map +1 -0
- package/dist/runtime/commerce.js +37 -0
- package/dist/runtime/kernel.d.ts +72 -0
- package/dist/runtime/kernel.d.ts.map +1 -0
- package/dist/runtime/kernel.js +298 -0
- package/dist/runtime/logger.d.ts +11 -0
- package/dist/runtime/logger.d.ts.map +1 -0
- package/dist/runtime/logger.js +32 -0
- package/dist/runtime/server.d.ts +28 -0
- package/dist/runtime/server.d.ts.map +1 -0
- package/dist/runtime/server.js +277 -0
- package/dist/runtime/shutdown.d.ts +15 -0
- package/dist/runtime/shutdown.d.ts.map +1 -0
- package/dist/runtime/shutdown.js +34 -0
- package/dist/runtime/state-manager.d.ts +14 -0
- package/dist/runtime/state-manager.d.ts.map +1 -0
- package/dist/runtime/state-manager.js +84 -0
- package/dist/runtime/state-repository.d.ts +13 -0
- package/dist/runtime/state-repository.d.ts.map +1 -0
- package/dist/runtime/state-repository.js +160 -0
- package/dist/runtime/state.d.ts +382 -0
- package/dist/runtime/state.d.ts.map +1 -0
- package/dist/runtime/state.js +37 -0
- package/dist/runtime/store.d.ts +382 -0
- package/dist/runtime/store.d.ts.map +1 -0
- package/dist/runtime/store.js +37 -0
- package/dist/test-utils/create-pglite-adapter.d.ts +32 -0
- package/dist/test-utils/create-pglite-adapter.d.ts.map +1 -0
- package/dist/test-utils/create-pglite-adapter.js +106 -0
- package/dist/test-utils/create-plugin-test-app.d.ts +50 -0
- package/dist/test-utils/create-plugin-test-app.d.ts.map +1 -0
- package/dist/test-utils/create-plugin-test-app.js +74 -0
- package/dist/test-utils/create-repository-test-harness.d.ts +8 -0
- package/dist/test-utils/create-repository-test-harness.d.ts.map +1 -0
- package/dist/test-utils/create-repository-test-harness.js +7 -0
- package/dist/test-utils/create-test-config.d.ts +18 -0
- package/dist/test-utils/create-test-config.d.ts.map +1 -0
- package/dist/test-utils/create-test-config.js +174 -0
- package/dist/test-utils/create-test-kernel.d.ts +3 -0
- package/dist/test-utils/create-test-kernel.d.ts.map +1 -0
- package/dist/test-utils/create-test-kernel.js +5 -0
- package/dist/test-utils/create-test-plugin-context.d.ts +42 -0
- package/dist/test-utils/create-test-plugin-context.d.ts.map +1 -0
- package/dist/test-utils/create-test-plugin-context.js +46 -0
- package/dist/test-utils/rest-api-test-utils.d.ts +64 -0
- package/dist/test-utils/rest-api-test-utils.d.ts.map +1 -0
- package/dist/test-utils/rest-api-test-utils.js +207 -0
- package/dist/test-utils/test-actors.d.ts +15 -0
- package/dist/test-utils/test-actors.d.ts.map +1 -0
- package/dist/test-utils/test-actors.js +57 -0
- package/dist/test-utils/typed-hooks.d.ts +43 -0
- package/dist/test-utils/typed-hooks.d.ts.map +1 -0
- package/dist/test-utils/typed-hooks.js +35 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/commerce-types.d.ts +34 -0
- package/dist/types/commerce-types.d.ts.map +1 -0
- package/dist/types/commerce-types.js +1 -0
- package/dist/utils/id.d.ts +2 -0
- package/dist/utils/id.d.ts.map +1 -0
- package/dist/utils/id.js +3 -0
- package/dist/utils/logger.d.ts +3 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +16 -0
- package/dist/utils/pagination.d.ts +11 -0
- package/dist/utils/pagination.d.ts.map +1 -0
- package/dist/utils/pagination.js +15 -0
- package/package.json +63 -0
- package/src/adapters/console-email.ts +43 -0
- package/src/auth/access.ts +187 -0
- package/src/auth/auth-schema.ts +131 -0
- package/src/auth/middleware.ts +115 -0
- package/src/auth/org.ts +41 -0
- package/src/auth/permissions.ts +28 -0
- package/src/auth/setup.ts +160 -0
- package/src/auth/system-actor.ts +19 -0
- package/src/auth/types.ts +10 -0
- package/src/config/defaults.ts +82 -0
- package/src/config/define-config.ts +53 -0
- package/src/config/types.ts +299 -0
- package/src/generated/plugin-capabilities.d.ts +20 -0
- package/src/generated/plugin-manifest.ts +23 -0
- package/src/generated/plugin-repositories.d.ts +20 -0
- package/src/hooks/checkout-completion.ts +262 -0
- package/src/hooks/checkout.ts +677 -0
- package/src/hooks/order-emails.ts +62 -0
- package/src/index.ts +214 -0
- package/src/interfaces/mcp/agent-prompt.ts +174 -0
- package/src/interfaces/mcp/context-enrichment.ts +177 -0
- package/src/interfaces/mcp/server.ts +617 -0
- package/src/interfaces/mcp/transport.ts +68 -0
- package/src/interfaces/rest/customer-portal.ts +299 -0
- package/src/interfaces/rest/index.ts +74 -0
- package/src/interfaces/rest/router.ts +334 -0
- package/src/interfaces/rest/routes/admin-jobs.ts +58 -0
- package/src/interfaces/rest/routes/audit.ts +50 -0
- package/src/interfaces/rest/routes/carts.ts +89 -0
- package/src/interfaces/rest/routes/catalog.ts +493 -0
- package/src/interfaces/rest/routes/checkout.ts +283 -0
- package/src/interfaces/rest/routes/inventory.ts +70 -0
- package/src/interfaces/rest/routes/media.ts +86 -0
- package/src/interfaces/rest/routes/orders.ts +78 -0
- package/src/interfaces/rest/routes/payments.ts +60 -0
- package/src/interfaces/rest/routes/pricing.ts +57 -0
- package/src/interfaces/rest/routes/promotions.ts +92 -0
- package/src/interfaces/rest/routes/search.ts +71 -0
- package/src/interfaces/rest/routes/webhooks.ts +46 -0
- package/src/interfaces/rest/schemas/admin-jobs.ts +40 -0
- package/src/interfaces/rest/schemas/audit.ts +46 -0
- package/src/interfaces/rest/schemas/carts.ts +125 -0
- package/src/interfaces/rest/schemas/catalog.ts +450 -0
- package/src/interfaces/rest/schemas/checkout.ts +66 -0
- package/src/interfaces/rest/schemas/customer-portal.ts +195 -0
- package/src/interfaces/rest/schemas/inventory.ts +138 -0
- package/src/interfaces/rest/schemas/media.ts +75 -0
- package/src/interfaces/rest/schemas/orders.ts +104 -0
- package/src/interfaces/rest/schemas/pricing.ts +80 -0
- package/src/interfaces/rest/schemas/promotions.ts +110 -0
- package/src/interfaces/rest/schemas/responses.ts +85 -0
- package/src/interfaces/rest/schemas/search.ts +58 -0
- package/src/interfaces/rest/schemas/shared.ts +62 -0
- package/src/interfaces/rest/schemas/webhooks.ts +68 -0
- package/src/interfaces/rest/utils.ts +104 -0
- package/src/interfaces/rest/webhook-router.ts +50 -0
- package/src/kernel/compensation/executor.ts +61 -0
- package/src/kernel/compensation/types.ts +26 -0
- package/src/kernel/database/adapter.ts +13 -0
- package/src/kernel/database/drizzle-db.ts +56 -0
- package/src/kernel/database/migrate.ts +76 -0
- package/src/kernel/database/plugin-types.ts +34 -0
- package/src/kernel/database/schema.ts +49 -0
- package/src/kernel/database/scoped-db.ts +68 -0
- package/src/kernel/database/tx-context.ts +46 -0
- package/src/kernel/error-mapper.ts +15 -0
- package/src/kernel/errors.ts +89 -0
- package/src/kernel/factory/repository-factory.ts +242 -0
- package/src/kernel/hooks/create-context.ts +43 -0
- package/src/kernel/hooks/executor.ts +88 -0
- package/src/kernel/hooks/registry.ts +74 -0
- package/src/kernel/hooks/types.ts +52 -0
- package/src/kernel/http-error.ts +44 -0
- package/src/kernel/jobs/adapter.ts +36 -0
- package/src/kernel/jobs/drizzle-adapter.ts +58 -0
- package/src/kernel/jobs/runner.ts +153 -0
- package/src/kernel/jobs/schema.ts +46 -0
- package/src/kernel/jobs/types.ts +30 -0
- package/src/kernel/local-api.ts +185 -0
- package/src/kernel/plugin/manifest.ts +253 -0
- package/src/kernel/query/executor.ts +184 -0
- package/src/kernel/query/registry.ts +46 -0
- package/src/kernel/result.ts +33 -0
- package/src/kernel/schema/extra-columns.ts +37 -0
- package/src/kernel/service-registry.ts +76 -0
- package/src/kernel/service-timing.ts +89 -0
- package/src/kernel/state-machine/machine.ts +101 -0
- package/src/modules/analytics/drizzle-adapter.ts +426 -0
- package/src/modules/analytics/hooks.ts +11 -0
- package/src/modules/analytics/models.ts +125 -0
- package/src/modules/analytics/repository/index.ts +6 -0
- package/src/modules/analytics/service.ts +245 -0
- package/src/modules/analytics/types.ts +180 -0
- package/src/modules/audit/hooks.ts +78 -0
- package/src/modules/audit/schema.ts +33 -0
- package/src/modules/audit/service.ts +151 -0
- package/src/modules/cart/access.ts +27 -0
- package/src/modules/cart/matcher.ts +26 -0
- package/src/modules/cart/repository/index.ts +234 -0
- package/src/modules/cart/schema.ts +42 -0
- package/src/modules/cart/schemas.ts +38 -0
- package/src/modules/cart/service.ts +541 -0
- package/src/modules/catalog/repository/index.ts +772 -0
- package/src/modules/catalog/schema.ts +203 -0
- package/src/modules/catalog/schemas.ts +104 -0
- package/src/modules/catalog/service.ts +1544 -0
- package/src/modules/customers/repository/index.ts +327 -0
- package/src/modules/customers/schema.ts +64 -0
- package/src/modules/customers/service.ts +171 -0
- package/src/modules/fulfillment/repository/index.ts +426 -0
- package/src/modules/fulfillment/schema.ts +101 -0
- package/src/modules/fulfillment/service.ts +555 -0
- package/src/modules/fulfillment/types.ts +59 -0
- package/src/modules/inventory/repository/index.ts +509 -0
- package/src/modules/inventory/schema.ts +94 -0
- package/src/modules/inventory/schemas.ts +38 -0
- package/src/modules/inventory/service.ts +490 -0
- package/src/modules/media/adapter.ts +17 -0
- package/src/modules/media/repository/index.ts +274 -0
- package/src/modules/media/schema.ts +41 -0
- package/src/modules/media/service.ts +151 -0
- package/src/modules/orders/repository/index.ts +287 -0
- package/src/modules/orders/schema.ts +66 -0
- package/src/modules/orders/service.ts +619 -0
- package/src/modules/orders/stale-order-cleanup.ts +76 -0
- package/src/modules/organization/service.ts +191 -0
- package/src/modules/payments/adapter.ts +47 -0
- package/src/modules/payments/repository/index.ts +6 -0
- package/src/modules/payments/service.ts +107 -0
- package/src/modules/pricing/repository/index.ts +291 -0
- package/src/modules/pricing/schema.ts +71 -0
- package/src/modules/pricing/schemas.ts +38 -0
- package/src/modules/pricing/service.ts +494 -0
- package/src/modules/promotions/repository/index.ts +325 -0
- package/src/modules/promotions/schema.ts +62 -0
- package/src/modules/promotions/schemas.ts +38 -0
- package/src/modules/promotions/service.ts +598 -0
- package/src/modules/search/adapter.ts +57 -0
- package/src/modules/search/hooks.ts +12 -0
- package/src/modules/search/repository/index.ts +6 -0
- package/src/modules/search/service.ts +315 -0
- package/src/modules/shipping/calculator.ts +188 -0
- package/src/modules/shipping/repository/index.ts +6 -0
- package/src/modules/shipping/service.ts +51 -0
- package/src/modules/tax/adapter.ts +60 -0
- package/src/modules/tax/repository/index.ts +6 -0
- package/src/modules/tax/service.ts +53 -0
- package/src/modules/webhooks/hook.ts +34 -0
- package/src/modules/webhooks/repository/index.ts +278 -0
- package/src/modules/webhooks/schema.ts +56 -0
- package/src/modules/webhooks/service.ts +117 -0
- package/src/modules/webhooks/signing.ts +6 -0
- package/src/modules/webhooks/ssrf-guard.ts +71 -0
- package/src/modules/webhooks/tasks.ts +52 -0
- package/src/modules/webhooks/worker.ts +134 -0
- package/src/runtime/commerce.ts +145 -0
- package/src/runtime/kernel.ts +419 -0
- package/src/runtime/logger.ts +36 -0
- package/src/runtime/server.ts +345 -0
- package/src/runtime/shutdown.ts +43 -0
- package/src/test-utils/create-pglite-adapter.ts +129 -0
- package/src/test-utils/create-plugin-test-app.ts +128 -0
- package/src/test-utils/create-repository-test-harness.ts +16 -0
- package/src/test-utils/create-test-config.ts +190 -0
- package/src/test-utils/create-test-kernel.ts +7 -0
- package/src/test-utils/create-test-plugin-context.ts +75 -0
- package/src/test-utils/rest-api-test-utils.ts +265 -0
- package/src/test-utils/test-actors.ts +62 -0
- package/src/test-utils/typed-hooks.ts +54 -0
- package/src/types/commerce-types.ts +34 -0
- package/src/utils/id.ts +3 -0
- package/src/utils/logger.ts +18 -0
- package/src/utils/pagination.ts +22 -0
|
@@ -0,0 +1,1544 @@
|
|
|
1
|
+
import { resolveOrgId } from "../../auth/org";
|
|
2
|
+
import { assertPermission } from "../../auth/permissions";
|
|
3
|
+
import type { Actor } from "../../auth/types";
|
|
4
|
+
import type { CommerceConfig } from "../../config/types";
|
|
5
|
+
import {
|
|
6
|
+
CommerceConflictError,
|
|
7
|
+
CommerceNotFoundError,
|
|
8
|
+
CommerceValidationError,
|
|
9
|
+
toCommerceError,
|
|
10
|
+
} from "../../kernel/errors";
|
|
11
|
+
import { runAfterHooks, runBeforeHooks } from "../../kernel/hooks/executor";
|
|
12
|
+
import { createHookContext } from "../../kernel/hooks/create-context";
|
|
13
|
+
import type {
|
|
14
|
+
AfterHook,
|
|
15
|
+
BeforeHook,
|
|
16
|
+
HookContext,
|
|
17
|
+
} from "../../kernel/hooks/types";
|
|
18
|
+
import type { HookRegistry } from "../../kernel/hooks/registry";
|
|
19
|
+
import { Err, Ok, type Result } from "../../kernel/result";
|
|
20
|
+
import { createLogger } from "../../utils/logger";
|
|
21
|
+
import { paginate, type Pagination } from "../../utils/pagination";
|
|
22
|
+
import type { TxContext } from "../../kernel/database/tx-context";
|
|
23
|
+
import {
|
|
24
|
+
CatalogRepository,
|
|
25
|
+
type SellableEntity,
|
|
26
|
+
type SellableAttribute,
|
|
27
|
+
type SellableCustomField,
|
|
28
|
+
type SellableCustomFieldInsert,
|
|
29
|
+
type Category,
|
|
30
|
+
type EntityCategory,
|
|
31
|
+
type EntityBrand,
|
|
32
|
+
type Brand,
|
|
33
|
+
type OptionType,
|
|
34
|
+
type OptionValue,
|
|
35
|
+
type Variant,
|
|
36
|
+
} from "./repository";
|
|
37
|
+
|
|
38
|
+
// ─── Re-exported schema-derived types ────────────────────────────────────────
|
|
39
|
+
export type {
|
|
40
|
+
CreateEntityInput,
|
|
41
|
+
UpdateEntityInput,
|
|
42
|
+
CreateCategoryInput,
|
|
43
|
+
UpdateCategoryInput,
|
|
44
|
+
CreateBrandInput,
|
|
45
|
+
UpdateBrandInput,
|
|
46
|
+
CreateOptionTypeInput,
|
|
47
|
+
CreateOptionValueInput,
|
|
48
|
+
CreateVariantInput,
|
|
49
|
+
} from "./schemas";
|
|
50
|
+
|
|
51
|
+
import type {
|
|
52
|
+
CreateEntityInput,
|
|
53
|
+
UpdateEntityInput,
|
|
54
|
+
CreateCategoryInput,
|
|
55
|
+
UpdateCategoryInput,
|
|
56
|
+
CreateBrandInput,
|
|
57
|
+
UpdateBrandInput,
|
|
58
|
+
CreateOptionTypeInput,
|
|
59
|
+
CreateOptionValueInput,
|
|
60
|
+
CreateVariantInput,
|
|
61
|
+
} from "./schemas";
|
|
62
|
+
|
|
63
|
+
// ─── Hand-written types (not derivable from a single z.infer) ───────────────
|
|
64
|
+
|
|
65
|
+
export interface SetAttributesInput {
|
|
66
|
+
title: string;
|
|
67
|
+
subtitle?: string;
|
|
68
|
+
description?: string;
|
|
69
|
+
richDescription?: unknown;
|
|
70
|
+
seoTitle?: string;
|
|
71
|
+
seoDescription?: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface ListParams {
|
|
75
|
+
filter?: {
|
|
76
|
+
type?: string;
|
|
77
|
+
status?: string;
|
|
78
|
+
category?: string;
|
|
79
|
+
brand?: string;
|
|
80
|
+
customField?: {
|
|
81
|
+
fieldName: string;
|
|
82
|
+
value: unknown;
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
sort?: {
|
|
86
|
+
field: "createdAt" | "updatedAt" | "slug";
|
|
87
|
+
direction: "asc" | "desc";
|
|
88
|
+
};
|
|
89
|
+
pagination?: {
|
|
90
|
+
page: number;
|
|
91
|
+
limit: number;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface GetOptions {
|
|
96
|
+
includeAttributes?: boolean | { locales: string[] };
|
|
97
|
+
includeVariants?: boolean;
|
|
98
|
+
includeOptionTypes?: boolean;
|
|
99
|
+
includePricing?: boolean;
|
|
100
|
+
includeInventory?: boolean;
|
|
101
|
+
includeMedia?: boolean;
|
|
102
|
+
includeCategories?: boolean;
|
|
103
|
+
includeBrands?: boolean;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface VariantMatrixRule {
|
|
107
|
+
include?: string[][];
|
|
108
|
+
exclude?: string[][];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export type VariantGenerationStrategy =
|
|
112
|
+
| { mode: "all" }
|
|
113
|
+
| { mode: "manual"; combinations: string[][] }
|
|
114
|
+
| { mode: "matrix"; matrix: VariantMatrixRule };
|
|
115
|
+
|
|
116
|
+
export interface CatalogEntityHydrated extends SellableEntity {
|
|
117
|
+
attributes?: SellableAttribute[];
|
|
118
|
+
variants?: Array<Variant & { optionValueIds: string[] }>;
|
|
119
|
+
optionTypes?: Array<OptionType & { values: OptionValue[] }>;
|
|
120
|
+
categories?: EntityCategory[];
|
|
121
|
+
brands?: EntityBrand[];
|
|
122
|
+
media?: Array<{ mediaAssetId: string; role: string; variantId?: string }>;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface CatalogService {
|
|
126
|
+
create(
|
|
127
|
+
input: CreateEntityInput,
|
|
128
|
+
actor: Actor | null,
|
|
129
|
+
ctx?: TxContext,
|
|
130
|
+
): Promise<Result<CatalogEntityHydrated>>;
|
|
131
|
+
update(
|
|
132
|
+
id: string,
|
|
133
|
+
input: UpdateEntityInput,
|
|
134
|
+
actor: Actor | null,
|
|
135
|
+
ctx?: TxContext,
|
|
136
|
+
): Promise<Result<CatalogEntityHydrated>>;
|
|
137
|
+
delete(
|
|
138
|
+
id: string,
|
|
139
|
+
actor: Actor | null,
|
|
140
|
+
ctx?: TxContext,
|
|
141
|
+
): Promise<Result<void>>;
|
|
142
|
+
getById(
|
|
143
|
+
id: string,
|
|
144
|
+
options?: GetOptions,
|
|
145
|
+
actor?: Actor | null,
|
|
146
|
+
ctx?: TxContext,
|
|
147
|
+
): Promise<Result<CatalogEntityHydrated>>;
|
|
148
|
+
getBySlug(
|
|
149
|
+
slug: string,
|
|
150
|
+
options?: GetOptions,
|
|
151
|
+
actor?: Actor | null,
|
|
152
|
+
ctx?: TxContext,
|
|
153
|
+
): Promise<Result<CatalogEntityHydrated>>;
|
|
154
|
+
list(params: ListParams, actor?: Actor | null, ctx?: TxContext): Promise<Result<CatalogListResult>>;
|
|
155
|
+
publish(
|
|
156
|
+
id: string,
|
|
157
|
+
actor: Actor | null,
|
|
158
|
+
ctx?: TxContext,
|
|
159
|
+
): Promise<Result<CatalogEntityHydrated>>;
|
|
160
|
+
archive(
|
|
161
|
+
id: string,
|
|
162
|
+
actor: Actor | null,
|
|
163
|
+
ctx?: TxContext,
|
|
164
|
+
): Promise<Result<CatalogEntityHydrated>>;
|
|
165
|
+
discontinue(
|
|
166
|
+
id: string,
|
|
167
|
+
actor: Actor | null,
|
|
168
|
+
ctx?: TxContext,
|
|
169
|
+
): Promise<Result<CatalogEntityHydrated>>;
|
|
170
|
+
setAttributes(
|
|
171
|
+
entityId: string,
|
|
172
|
+
locale: string,
|
|
173
|
+
attrs: SetAttributesInput,
|
|
174
|
+
ctx?: TxContext,
|
|
175
|
+
): Promise<Result<void>>;
|
|
176
|
+
getAttributes(
|
|
177
|
+
entityId: string,
|
|
178
|
+
locale: string,
|
|
179
|
+
ctx?: TxContext,
|
|
180
|
+
): Promise<Result<SellableAttribute>>;
|
|
181
|
+
listCategories(ctx?: TxContext): Promise<Result<CategorySummary[]>>;
|
|
182
|
+
createCategory(
|
|
183
|
+
input: CreateCategoryInput,
|
|
184
|
+
actor: Actor | null,
|
|
185
|
+
ctx?: TxContext,
|
|
186
|
+
): Promise<Result<CategorySummary>>;
|
|
187
|
+
updateCategory(
|
|
188
|
+
id: string,
|
|
189
|
+
input: UpdateCategoryInput,
|
|
190
|
+
actor: Actor | null,
|
|
191
|
+
ctx?: TxContext,
|
|
192
|
+
): Promise<Result<CategorySummary>>;
|
|
193
|
+
deleteCategory(
|
|
194
|
+
id: string,
|
|
195
|
+
actor: Actor | null,
|
|
196
|
+
ctx?: TxContext,
|
|
197
|
+
): Promise<Result<void>>;
|
|
198
|
+
addToCategory(
|
|
199
|
+
entityId: string,
|
|
200
|
+
categoryId: string,
|
|
201
|
+
ctx?: TxContext,
|
|
202
|
+
): Promise<Result<void>>;
|
|
203
|
+
removeFromCategory(
|
|
204
|
+
entityId: string,
|
|
205
|
+
categoryId: string,
|
|
206
|
+
ctx?: TxContext,
|
|
207
|
+
): Promise<Result<void>>;
|
|
208
|
+
listBrands(ctx?: TxContext): Promise<Result<Brand[]>>;
|
|
209
|
+
createBrand(
|
|
210
|
+
input: CreateBrandInput,
|
|
211
|
+
actor: Actor | null,
|
|
212
|
+
ctx?: TxContext,
|
|
213
|
+
): Promise<Result<Brand>>;
|
|
214
|
+
updateBrand(
|
|
215
|
+
id: string,
|
|
216
|
+
input: UpdateBrandInput,
|
|
217
|
+
actor: Actor | null,
|
|
218
|
+
ctx?: TxContext,
|
|
219
|
+
): Promise<Result<Brand>>;
|
|
220
|
+
deleteBrand(
|
|
221
|
+
id: string,
|
|
222
|
+
actor: Actor | null,
|
|
223
|
+
ctx?: TxContext,
|
|
224
|
+
): Promise<Result<void>>;
|
|
225
|
+
addToBrand(
|
|
226
|
+
entityId: string,
|
|
227
|
+
brandId: string,
|
|
228
|
+
ctx?: TxContext,
|
|
229
|
+
): Promise<Result<void>>;
|
|
230
|
+
removeFromBrand(
|
|
231
|
+
entityId: string,
|
|
232
|
+
brandId: string,
|
|
233
|
+
ctx?: TxContext,
|
|
234
|
+
): Promise<Result<void>>;
|
|
235
|
+
createOptionType(
|
|
236
|
+
input: CreateOptionTypeInput,
|
|
237
|
+
actor: Actor | null,
|
|
238
|
+
ctx?: TxContext,
|
|
239
|
+
): Promise<Result<OptionType>>;
|
|
240
|
+
createOptionValue(
|
|
241
|
+
input: CreateOptionValueInput,
|
|
242
|
+
actor: Actor | null,
|
|
243
|
+
ctx?: TxContext,
|
|
244
|
+
): Promise<Result<OptionValue>>;
|
|
245
|
+
createVariant(
|
|
246
|
+
input: CreateVariantInput,
|
|
247
|
+
actor: Actor | null,
|
|
248
|
+
ctx?: TxContext,
|
|
249
|
+
): Promise<Result<Variant>>;
|
|
250
|
+
generateVariants(
|
|
251
|
+
entityId: string,
|
|
252
|
+
strategy: VariantGenerationStrategy,
|
|
253
|
+
actor: Actor | null,
|
|
254
|
+
ctx?: TxContext,
|
|
255
|
+
): Promise<Result<Variant[]>>;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export interface CatalogServiceDeps {
|
|
259
|
+
repository: CatalogRepository;
|
|
260
|
+
hooks: HookRegistry;
|
|
261
|
+
config: CommerceConfig;
|
|
262
|
+
services: Record<string, unknown>;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
type CatalogListResult = {
|
|
266
|
+
items: CatalogEntityHydrated[];
|
|
267
|
+
pagination: Pagination;
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
type CategorySummary = {
|
|
271
|
+
id: string;
|
|
272
|
+
parentId?: string | null;
|
|
273
|
+
slug: string;
|
|
274
|
+
sortOrder: number;
|
|
275
|
+
metadata: Record<string, unknown>;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
type CatalogCreateBeforeHook = BeforeHook<CreateEntityInput>;
|
|
279
|
+
type CatalogCreateAfterHook = AfterHook<SellableEntity>;
|
|
280
|
+
type CatalogUpdateBeforeHook = BeforeHook<UpdateEntityInput>;
|
|
281
|
+
type CatalogUpdateAfterHook = AfterHook<SellableEntity>;
|
|
282
|
+
type CatalogReadHookInput = {
|
|
283
|
+
id?: string;
|
|
284
|
+
slug?: string;
|
|
285
|
+
options?: GetOptions;
|
|
286
|
+
};
|
|
287
|
+
type CatalogReadBeforeHook = BeforeHook<CatalogReadHookInput>;
|
|
288
|
+
type CatalogReadAfterHook = AfterHook<CatalogEntityHydrated>;
|
|
289
|
+
type CatalogListBeforeHook = BeforeHook<ListParams>;
|
|
290
|
+
type CatalogListAfterHook = AfterHook<CatalogListResult>;
|
|
291
|
+
|
|
292
|
+
function cartesian<T>(arrays: T[][]): T[][] {
|
|
293
|
+
return arrays.reduce<T[][]>(
|
|
294
|
+
(acc, values) =>
|
|
295
|
+
acc.flatMap((entry) => values.map((value) => [...entry, value])),
|
|
296
|
+
[[]],
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Helper to extract the value from a SellableCustomField based on its type.
|
|
302
|
+
*/
|
|
303
|
+
function getCustomFieldValue(field: SellableCustomField): unknown {
|
|
304
|
+
switch (field.fieldType) {
|
|
305
|
+
case "text":
|
|
306
|
+
case "relation":
|
|
307
|
+
return field.textValue;
|
|
308
|
+
case "number":
|
|
309
|
+
return field.numberValue;
|
|
310
|
+
case "boolean":
|
|
311
|
+
return field.booleanValue;
|
|
312
|
+
case "date":
|
|
313
|
+
return field.dateValue;
|
|
314
|
+
case "json":
|
|
315
|
+
return field.jsonValue;
|
|
316
|
+
default:
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export class CatalogServiceImpl implements CatalogService {
|
|
322
|
+
private readonly repo: CatalogRepository;
|
|
323
|
+
|
|
324
|
+
constructor(private deps: CatalogServiceDeps) {
|
|
325
|
+
this.repo = deps.repository;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private async validateAndCreateCustomFields(
|
|
329
|
+
entityId: string,
|
|
330
|
+
entityType: string,
|
|
331
|
+
customFields: Record<string, unknown> | undefined,
|
|
332
|
+
ctx?: TxContext,
|
|
333
|
+
): Promise<Result<void>> {
|
|
334
|
+
if (!customFields) return Ok(undefined);
|
|
335
|
+
const entityConfig = this.deps.config.entities?.[entityType];
|
|
336
|
+
if (!entityConfig) return Ok(undefined);
|
|
337
|
+
|
|
338
|
+
const definitionMap = new Map(entityConfig.fields.map((f) => [f.name, f]));
|
|
339
|
+
|
|
340
|
+
for (const [name, value] of Object.entries(customFields)) {
|
|
341
|
+
const def = definitionMap.get(name);
|
|
342
|
+
if (!def) {
|
|
343
|
+
return Err(
|
|
344
|
+
new CommerceValidationError(`Unknown custom field: ${name}`),
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const type = def.type;
|
|
349
|
+
let valid = false;
|
|
350
|
+
switch (type) {
|
|
351
|
+
case "text":
|
|
352
|
+
case "relation":
|
|
353
|
+
case "select":
|
|
354
|
+
valid = typeof value === "string";
|
|
355
|
+
break;
|
|
356
|
+
case "number":
|
|
357
|
+
valid = typeof value === "number";
|
|
358
|
+
break;
|
|
359
|
+
case "boolean":
|
|
360
|
+
valid = typeof value === "boolean";
|
|
361
|
+
break;
|
|
362
|
+
case "date":
|
|
363
|
+
valid = typeof value === "string" || value instanceof Date;
|
|
364
|
+
break;
|
|
365
|
+
case "json":
|
|
366
|
+
valid = typeof value === "object";
|
|
367
|
+
break;
|
|
368
|
+
default:
|
|
369
|
+
valid = false;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (!valid) {
|
|
373
|
+
return Err(
|
|
374
|
+
new CommerceValidationError(
|
|
375
|
+
`Custom field ${name} expected type ${type}.`,
|
|
376
|
+
),
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const fieldType = (
|
|
381
|
+
type === "select" ? "text" : type
|
|
382
|
+
) as SellableCustomField["fieldType"];
|
|
383
|
+
const insertData: SellableCustomFieldInsert = {
|
|
384
|
+
entityId,
|
|
385
|
+
fieldName: name,
|
|
386
|
+
fieldType,
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
switch (fieldType) {
|
|
390
|
+
case "text":
|
|
391
|
+
case "relation":
|
|
392
|
+
insertData.textValue = value as string;
|
|
393
|
+
break;
|
|
394
|
+
case "number":
|
|
395
|
+
insertData.numberValue = value as number;
|
|
396
|
+
break;
|
|
397
|
+
case "boolean":
|
|
398
|
+
insertData.booleanValue = value as boolean;
|
|
399
|
+
break;
|
|
400
|
+
case "date":
|
|
401
|
+
insertData.dateValue =
|
|
402
|
+
value instanceof Date ? value : new Date(value as string);
|
|
403
|
+
break;
|
|
404
|
+
case "json":
|
|
405
|
+
insertData.jsonValue = value;
|
|
406
|
+
break;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
await this.repo.createCustomField(insertData, ctx);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
return Ok(undefined);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
private async hydrateEntity(
|
|
416
|
+
entity: SellableEntity,
|
|
417
|
+
options?: GetOptions,
|
|
418
|
+
ctx?: TxContext,
|
|
419
|
+
): Promise<CatalogEntityHydrated> {
|
|
420
|
+
const hydrated: CatalogEntityHydrated = { ...entity };
|
|
421
|
+
|
|
422
|
+
if (options?.includeAttributes) {
|
|
423
|
+
const attrs = await this.repo.findAttributesByEntityId(entity.id, ctx);
|
|
424
|
+
if (typeof options.includeAttributes === "object") {
|
|
425
|
+
hydrated.attributes = attrs.filter(
|
|
426
|
+
(a) =>
|
|
427
|
+
options.includeAttributes &&
|
|
428
|
+
typeof options.includeAttributes === "object" &&
|
|
429
|
+
options.includeAttributes.locales.includes(a.locale),
|
|
430
|
+
);
|
|
431
|
+
} else {
|
|
432
|
+
hydrated.attributes = attrs;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
if (options?.includeVariants) {
|
|
437
|
+
const entityVariants = await this.repo.findVariantsByEntityId(
|
|
438
|
+
entity.id,
|
|
439
|
+
ctx,
|
|
440
|
+
);
|
|
441
|
+
const variantsWithOptions = await Promise.all(
|
|
442
|
+
entityVariants.map(async (variant) => {
|
|
443
|
+
const optionValues = await this.repo.findVariantOptionValues(
|
|
444
|
+
variant.id,
|
|
445
|
+
ctx,
|
|
446
|
+
);
|
|
447
|
+
return {
|
|
448
|
+
...variant,
|
|
449
|
+
optionValueIds: optionValues.map((vov) => vov.optionValueId),
|
|
450
|
+
};
|
|
451
|
+
}),
|
|
452
|
+
);
|
|
453
|
+
hydrated.variants = variantsWithOptions;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (options?.includeOptionTypes) {
|
|
457
|
+
const entityOptionTypes = await this.repo.findOptionTypesByEntityId(
|
|
458
|
+
entity.id,
|
|
459
|
+
ctx,
|
|
460
|
+
);
|
|
461
|
+
hydrated.optionTypes = await Promise.all(
|
|
462
|
+
entityOptionTypes.map(async (ot) => {
|
|
463
|
+
const values = await this.repo.findOptionValuesByTypeId(ot.id, ctx);
|
|
464
|
+
return { ...ot, values };
|
|
465
|
+
}),
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (options?.includeCategories) {
|
|
470
|
+
hydrated.categories = await this.repo.findEntityCategories(
|
|
471
|
+
entity.id,
|
|
472
|
+
ctx,
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if (options?.includeBrands) {
|
|
477
|
+
hydrated.brands = await this.repo.findEntityBrands(entity.id, ctx);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if (options?.includeMedia) {
|
|
481
|
+
// TODO: integrate with MediaRepository when media service is refactored
|
|
482
|
+
hydrated.media = [];
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return hydrated;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
async create(
|
|
489
|
+
input: CreateEntityInput,
|
|
490
|
+
actor: Actor | null,
|
|
491
|
+
ctx?: TxContext,
|
|
492
|
+
): Promise<Result<CatalogEntityHydrated>> {
|
|
493
|
+
try {
|
|
494
|
+
assertPermission(actor, "catalog:create");
|
|
495
|
+
|
|
496
|
+
const orgId = resolveOrgId(actor);
|
|
497
|
+
|
|
498
|
+
const existingBySlug = await this.repo.findEntityBySlug(orgId, input.slug, ctx);
|
|
499
|
+
if (existingBySlug) {
|
|
500
|
+
return Err(
|
|
501
|
+
new CommerceConflictError(
|
|
502
|
+
`Entity with slug ${input.slug} already exists.`,
|
|
503
|
+
),
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const beforeHooks = this.deps.hooks.resolve(
|
|
508
|
+
"catalog.beforeCreate",
|
|
509
|
+
) as CatalogCreateBeforeHook[];
|
|
510
|
+
const afterHooks = this.deps.hooks.resolve(
|
|
511
|
+
"catalog.afterCreate",
|
|
512
|
+
) as CatalogCreateAfterHook[];
|
|
513
|
+
const logger = createLogger("catalog.create");
|
|
514
|
+
const context: HookContext = createHookContext({
|
|
515
|
+
actor,
|
|
516
|
+
tx: ctx?.tx ?? null,
|
|
517
|
+
logger,
|
|
518
|
+
services: this.deps.services,
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
const processedInput = await runBeforeHooks(
|
|
522
|
+
beforeHooks,
|
|
523
|
+
input,
|
|
524
|
+
"create",
|
|
525
|
+
context,
|
|
526
|
+
);
|
|
527
|
+
const entity = await this.repo.createEntity(
|
|
528
|
+
{
|
|
529
|
+
organizationId: orgId,
|
|
530
|
+
type: processedInput.type,
|
|
531
|
+
slug: processedInput.slug,
|
|
532
|
+
status: "draft",
|
|
533
|
+
isVisible: false,
|
|
534
|
+
metadata: processedInput.metadata ?? {},
|
|
535
|
+
},
|
|
536
|
+
ctx,
|
|
537
|
+
);
|
|
538
|
+
|
|
539
|
+
if (processedInput.attributes) {
|
|
540
|
+
await this.repo.createAttribute(
|
|
541
|
+
{
|
|
542
|
+
entityId: entity.id,
|
|
543
|
+
locale: processedInput.attributes.locale ?? "en",
|
|
544
|
+
title: processedInput.attributes.title,
|
|
545
|
+
subtitle: processedInput.attributes.subtitle,
|
|
546
|
+
description: processedInput.attributes.description,
|
|
547
|
+
richDescription: processedInput.attributes.richDescription,
|
|
548
|
+
seoTitle: processedInput.attributes.seoTitle,
|
|
549
|
+
seoDescription: processedInput.attributes.seoDescription,
|
|
550
|
+
},
|
|
551
|
+
ctx,
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
const customFieldsResult = await this.validateAndCreateCustomFields(
|
|
556
|
+
entity.id,
|
|
557
|
+
entity.type,
|
|
558
|
+
processedInput.customFields,
|
|
559
|
+
ctx,
|
|
560
|
+
);
|
|
561
|
+
if (!customFieldsResult.ok) return customFieldsResult;
|
|
562
|
+
|
|
563
|
+
const hookReport = await runAfterHooks(
|
|
564
|
+
afterHooks,
|
|
565
|
+
null,
|
|
566
|
+
entity,
|
|
567
|
+
"create",
|
|
568
|
+
context,
|
|
569
|
+
);
|
|
570
|
+
const hydrated = await this.hydrateEntity(entity, undefined, ctx);
|
|
571
|
+
return Ok(
|
|
572
|
+
hydrated,
|
|
573
|
+
hookReport.hasErrors ? { hookErrors: hookReport.errors } : undefined,
|
|
574
|
+
);
|
|
575
|
+
} catch (error) {
|
|
576
|
+
return Err(toCommerceError(error));
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
async update(
|
|
581
|
+
id: string,
|
|
582
|
+
input: UpdateEntityInput,
|
|
583
|
+
actor: Actor | null,
|
|
584
|
+
ctx?: TxContext,
|
|
585
|
+
): Promise<Result<CatalogEntityHydrated>> {
|
|
586
|
+
try {
|
|
587
|
+
assertPermission(actor, "catalog:update");
|
|
588
|
+
const existing = await this.repo.findEntityById(id, ctx);
|
|
589
|
+
if (!existing) return Err(new CommerceNotFoundError("Entity not found."));
|
|
590
|
+
|
|
591
|
+
const beforeHooks = this.deps.hooks.resolve(
|
|
592
|
+
"catalog.beforeUpdate",
|
|
593
|
+
) as CatalogUpdateBeforeHook[];
|
|
594
|
+
const afterHooks = this.deps.hooks.resolve(
|
|
595
|
+
"catalog.afterUpdate",
|
|
596
|
+
) as CatalogUpdateAfterHook[];
|
|
597
|
+
const context: HookContext = createHookContext({
|
|
598
|
+
actor,
|
|
599
|
+
tx: ctx?.tx ?? null,
|
|
600
|
+
logger: createLogger("catalog.update"),
|
|
601
|
+
services: this.deps.services,
|
|
602
|
+
});
|
|
603
|
+
const processed = await runBeforeHooks(
|
|
604
|
+
beforeHooks,
|
|
605
|
+
input,
|
|
606
|
+
"update",
|
|
607
|
+
context,
|
|
608
|
+
);
|
|
609
|
+
|
|
610
|
+
const updated = await this.repo.updateEntity(
|
|
611
|
+
id,
|
|
612
|
+
{
|
|
613
|
+
...(processed.slug !== undefined ? { slug: processed.slug } : {}),
|
|
614
|
+
...(processed.status !== undefined
|
|
615
|
+
? { status: processed.status as SellableEntity["status"] }
|
|
616
|
+
: {}),
|
|
617
|
+
...(processed.metadata !== undefined
|
|
618
|
+
? { metadata: processed.metadata }
|
|
619
|
+
: {}),
|
|
620
|
+
...(processed.isVisible !== undefined
|
|
621
|
+
? { isVisible: processed.isVisible }
|
|
622
|
+
: {}),
|
|
623
|
+
},
|
|
624
|
+
ctx,
|
|
625
|
+
);
|
|
626
|
+
|
|
627
|
+
if (!updated) return Err(new CommerceNotFoundError("Entity not found."));
|
|
628
|
+
|
|
629
|
+
const hookReport = await runAfterHooks(
|
|
630
|
+
afterHooks,
|
|
631
|
+
existing,
|
|
632
|
+
updated,
|
|
633
|
+
"update",
|
|
634
|
+
context,
|
|
635
|
+
);
|
|
636
|
+
const hydrated = await this.hydrateEntity(updated, undefined, ctx);
|
|
637
|
+
return Ok(
|
|
638
|
+
hydrated,
|
|
639
|
+
hookReport.hasErrors ? { hookErrors: hookReport.errors } : undefined,
|
|
640
|
+
);
|
|
641
|
+
} catch (error) {
|
|
642
|
+
return Err(toCommerceError(error));
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
async delete(
|
|
647
|
+
id: string,
|
|
648
|
+
actor: Actor | null,
|
|
649
|
+
ctx?: TxContext,
|
|
650
|
+
): Promise<Result<void>> {
|
|
651
|
+
try {
|
|
652
|
+
assertPermission(actor, "catalog:delete");
|
|
653
|
+
const existing = await this.repo.findEntityById(id, ctx);
|
|
654
|
+
if (!existing) return Err(new CommerceNotFoundError("Entity not found."));
|
|
655
|
+
|
|
656
|
+
// Clean up related data (cascade handles most, but be explicit)
|
|
657
|
+
await this.repo.deleteAttributesByEntityId(id, ctx);
|
|
658
|
+
await this.repo.deleteCustomFieldsByEntityId(id, ctx);
|
|
659
|
+
await this.repo.deleteEntityCategoriesByEntityId(id, ctx);
|
|
660
|
+
await this.repo.deleteEntityBrandsByEntityId(id, ctx);
|
|
661
|
+
await this.repo.deleteVariantOptionValuesByEntityId(id, ctx);
|
|
662
|
+
await this.repo.deleteVariantsByEntityId(id, ctx);
|
|
663
|
+
await this.repo.deleteEntity(id, ctx);
|
|
664
|
+
|
|
665
|
+
return Ok(undefined);
|
|
666
|
+
} catch (error) {
|
|
667
|
+
return Err(toCommerceError(error));
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
async getById(
|
|
672
|
+
id: string,
|
|
673
|
+
options?: GetOptions,
|
|
674
|
+
actor?: Actor | null,
|
|
675
|
+
ctx?: TxContext,
|
|
676
|
+
): Promise<Result<CatalogEntityHydrated>> {
|
|
677
|
+
const context: HookContext = createHookContext({
|
|
678
|
+
actor: actor ?? null,
|
|
679
|
+
tx: ctx?.tx ?? null,
|
|
680
|
+
logger: createLogger("catalog.read"),
|
|
681
|
+
services: this.deps.services,
|
|
682
|
+
});
|
|
683
|
+
const beforeHooks = this.deps.hooks.resolve(
|
|
684
|
+
"catalog.beforeRead",
|
|
685
|
+
) as CatalogReadBeforeHook[];
|
|
686
|
+
const afterHooks = this.deps.hooks.resolve(
|
|
687
|
+
"catalog.afterRead",
|
|
688
|
+
) as CatalogReadAfterHook[];
|
|
689
|
+
const readInput: CatalogReadHookInput = {
|
|
690
|
+
id,
|
|
691
|
+
...(options !== undefined ? { options } : {}),
|
|
692
|
+
};
|
|
693
|
+
const processed = await runBeforeHooks(
|
|
694
|
+
beforeHooks,
|
|
695
|
+
readInput,
|
|
696
|
+
"read",
|
|
697
|
+
context,
|
|
698
|
+
);
|
|
699
|
+
|
|
700
|
+
const entity = await this.repo.findEntityById(processed.id ?? id, ctx);
|
|
701
|
+
if (!entity) return Err(new CommerceNotFoundError("Entity not found."));
|
|
702
|
+
|
|
703
|
+
// Org boundary check — prevent cross-org access
|
|
704
|
+
if (actor && entity.organizationId) {
|
|
705
|
+
const orgId = resolveOrgId(actor);
|
|
706
|
+
if (entity.organizationId !== orgId) {
|
|
707
|
+
return Err(new CommerceNotFoundError("Entity not found."));
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
const result = await this.hydrateEntity(
|
|
712
|
+
entity,
|
|
713
|
+
processed.options ?? options,
|
|
714
|
+
ctx,
|
|
715
|
+
);
|
|
716
|
+
const report = await runAfterHooks(
|
|
717
|
+
afterHooks,
|
|
718
|
+
null,
|
|
719
|
+
result,
|
|
720
|
+
"read",
|
|
721
|
+
context,
|
|
722
|
+
);
|
|
723
|
+
return Ok(
|
|
724
|
+
result,
|
|
725
|
+
report.hasErrors ? { hookErrors: report.errors } : undefined,
|
|
726
|
+
);
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
async getBySlug(
|
|
730
|
+
slug: string,
|
|
731
|
+
options?: GetOptions,
|
|
732
|
+
actor?: Actor | null,
|
|
733
|
+
ctx?: TxContext,
|
|
734
|
+
): Promise<Result<CatalogEntityHydrated>> {
|
|
735
|
+
const context: HookContext = createHookContext({
|
|
736
|
+
actor: actor ?? null,
|
|
737
|
+
tx: ctx?.tx ?? null,
|
|
738
|
+
logger: createLogger("catalog.read"),
|
|
739
|
+
services: this.deps.services,
|
|
740
|
+
});
|
|
741
|
+
const beforeHooks = this.deps.hooks.resolve(
|
|
742
|
+
"catalog.beforeRead",
|
|
743
|
+
) as CatalogReadBeforeHook[];
|
|
744
|
+
const afterHooks = this.deps.hooks.resolve(
|
|
745
|
+
"catalog.afterRead",
|
|
746
|
+
) as CatalogReadAfterHook[];
|
|
747
|
+
const readInput: CatalogReadHookInput = {
|
|
748
|
+
slug,
|
|
749
|
+
...(options !== undefined ? { options } : {}),
|
|
750
|
+
};
|
|
751
|
+
const processed = await runBeforeHooks(
|
|
752
|
+
beforeHooks,
|
|
753
|
+
readInput,
|
|
754
|
+
"read",
|
|
755
|
+
context,
|
|
756
|
+
);
|
|
757
|
+
|
|
758
|
+
const resolvedSlug = processed.slug ?? slug;
|
|
759
|
+
const orgId = resolveOrgId(actor ?? null);
|
|
760
|
+
const entity = await this.repo.findEntityBySlug(orgId, resolvedSlug, ctx);
|
|
761
|
+
if (!entity) return Err(new CommerceNotFoundError("Entity not found."));
|
|
762
|
+
|
|
763
|
+
const result = await this.hydrateEntity(
|
|
764
|
+
entity,
|
|
765
|
+
processed.options ?? options,
|
|
766
|
+
ctx,
|
|
767
|
+
);
|
|
768
|
+
const report = await runAfterHooks(
|
|
769
|
+
afterHooks,
|
|
770
|
+
null,
|
|
771
|
+
result,
|
|
772
|
+
"read",
|
|
773
|
+
context,
|
|
774
|
+
);
|
|
775
|
+
return Ok(
|
|
776
|
+
result,
|
|
777
|
+
report.hasErrors ? { hookErrors: report.errors } : undefined,
|
|
778
|
+
);
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
async list(
|
|
782
|
+
params: ListParams,
|
|
783
|
+
actor?: Actor | null,
|
|
784
|
+
ctx?: TxContext,
|
|
785
|
+
): Promise<Result<CatalogListResult>> {
|
|
786
|
+
const resolvedActor = actor ?? ctx?.actor ?? null;
|
|
787
|
+
const context: HookContext = createHookContext({
|
|
788
|
+
actor: resolvedActor,
|
|
789
|
+
tx: ctx?.tx ?? null,
|
|
790
|
+
logger: createLogger("catalog.list"),
|
|
791
|
+
services: this.deps.services,
|
|
792
|
+
});
|
|
793
|
+
const beforeHooks = this.deps.hooks.resolve(
|
|
794
|
+
"catalog.beforeList",
|
|
795
|
+
) as CatalogListBeforeHook[];
|
|
796
|
+
const afterHooks = this.deps.hooks.resolve(
|
|
797
|
+
"catalog.afterList",
|
|
798
|
+
) as CatalogListAfterHook[];
|
|
799
|
+
const processed = await runBeforeHooks(
|
|
800
|
+
beforeHooks,
|
|
801
|
+
params,
|
|
802
|
+
"list",
|
|
803
|
+
context,
|
|
804
|
+
);
|
|
805
|
+
|
|
806
|
+
const listOrgId = resolveOrgId(resolvedActor);
|
|
807
|
+
let entities = await this.repo.findEntities(
|
|
808
|
+
listOrgId,
|
|
809
|
+
{
|
|
810
|
+
...(processed.filter?.type ? { type: processed.filter.type } : {}),
|
|
811
|
+
...(processed.filter?.status
|
|
812
|
+
? { status: processed.filter.status }
|
|
813
|
+
: {}),
|
|
814
|
+
},
|
|
815
|
+
ctx,
|
|
816
|
+
);
|
|
817
|
+
|
|
818
|
+
// Category filter (M1 fix: validate input format, return error if not found)
|
|
819
|
+
if (processed.filter?.category) {
|
|
820
|
+
const catInput = processed.filter.category;
|
|
821
|
+
const isUUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
822
|
+
|
|
823
|
+
let category = await this.repo.findCategoryBySlug(listOrgId, catInput, ctx);
|
|
824
|
+
|
|
825
|
+
if (!category && isUUID.test(catInput)) {
|
|
826
|
+
category = await this.repo.findCategoryById(catInput, ctx);
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
if (!category) {
|
|
830
|
+
return Err(
|
|
831
|
+
new CommerceValidationError(`Category not found: "${catInput}".`),
|
|
832
|
+
);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
const entityIds = await this.repo.findEntitiesByCategory(
|
|
836
|
+
category.id,
|
|
837
|
+
ctx,
|
|
838
|
+
);
|
|
839
|
+
const entityIdSet = new Set(entityIds);
|
|
840
|
+
entities = entities.filter((e) => entityIdSet.has(e.id));
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
// Brand filter
|
|
844
|
+
if (processed.filter?.brand) {
|
|
845
|
+
let brand = await this.repo.findBrandBySlug(listOrgId, processed.filter.brand, ctx);
|
|
846
|
+
if (!brand) {
|
|
847
|
+
brand = await this.repo.findBrandById(processed.filter.brand, ctx);
|
|
848
|
+
}
|
|
849
|
+
if (brand) {
|
|
850
|
+
const brandEntityIds: string[] = [];
|
|
851
|
+
for (const entity of entities) {
|
|
852
|
+
const entityBrands = await this.repo.findEntityBrands(entity.id, ctx);
|
|
853
|
+
if (entityBrands.some((eb) => eb.brandId === brand!.id)) {
|
|
854
|
+
brandEntityIds.push(entity.id);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
const brandEntitySet = new Set(brandEntityIds);
|
|
858
|
+
entities = entities.filter((e) => brandEntitySet.has(e.id));
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// Custom field filter
|
|
863
|
+
if (processed.filter?.customField) {
|
|
864
|
+
const filteredIds: string[] = [];
|
|
865
|
+
for (const entity of entities) {
|
|
866
|
+
const fields = await this.repo.findCustomFieldsByEntityId(
|
|
867
|
+
entity.id,
|
|
868
|
+
ctx,
|
|
869
|
+
);
|
|
870
|
+
const matches = fields.some((field) => {
|
|
871
|
+
if (field.fieldName !== processed.filter?.customField?.fieldName)
|
|
872
|
+
return false;
|
|
873
|
+
return (
|
|
874
|
+
getCustomFieldValue(field) === processed.filter.customField.value
|
|
875
|
+
);
|
|
876
|
+
});
|
|
877
|
+
if (matches) filteredIds.push(entity.id);
|
|
878
|
+
}
|
|
879
|
+
const filteredSet = new Set(filteredIds);
|
|
880
|
+
entities = entities.filter((e) => filteredSet.has(e.id));
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
// Sorting
|
|
884
|
+
if (processed.sort) {
|
|
885
|
+
const direction = processed.sort.direction === "asc" ? 1 : -1;
|
|
886
|
+
entities.sort((a, b) => {
|
|
887
|
+
const first = a[processed.sort!.field];
|
|
888
|
+
const second = b[processed.sort!.field];
|
|
889
|
+
if (first instanceof Date && second instanceof Date) {
|
|
890
|
+
return (first.getTime() - second.getTime()) * direction;
|
|
891
|
+
}
|
|
892
|
+
return String(first).localeCompare(String(second)) * direction;
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
const page = processed.pagination?.page ?? 1;
|
|
897
|
+
const limit = processed.pagination?.limit ?? 20;
|
|
898
|
+
const paged = paginate(entities, page, limit);
|
|
899
|
+
|
|
900
|
+
const hydratedItems = await Promise.all(
|
|
901
|
+
paged.items.map((entity) => this.hydrateEntity(entity, undefined, ctx)),
|
|
902
|
+
);
|
|
903
|
+
|
|
904
|
+
const result = { items: hydratedItems, pagination: paged.pagination };
|
|
905
|
+
const report = await runAfterHooks(
|
|
906
|
+
afterHooks,
|
|
907
|
+
null,
|
|
908
|
+
result,
|
|
909
|
+
"list",
|
|
910
|
+
context,
|
|
911
|
+
);
|
|
912
|
+
return Ok(
|
|
913
|
+
result,
|
|
914
|
+
report.hasErrors ? { hookErrors: report.errors } : undefined,
|
|
915
|
+
);
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
private async changeStatus(
|
|
919
|
+
id: string,
|
|
920
|
+
status: SellableEntity["status"],
|
|
921
|
+
actor: Actor | null,
|
|
922
|
+
ctx?: TxContext,
|
|
923
|
+
): Promise<Result<CatalogEntityHydrated>> {
|
|
924
|
+
const entity = await this.repo.findEntityById(id, ctx);
|
|
925
|
+
if (!entity) return Err(new CommerceNotFoundError("Entity not found."));
|
|
926
|
+
|
|
927
|
+
assertPermission(actor, "catalog:update");
|
|
928
|
+
|
|
929
|
+
const updateData: Partial<SellableEntity> = { status };
|
|
930
|
+
if (status === "active") {
|
|
931
|
+
updateData.publishedAt = new Date();
|
|
932
|
+
updateData.isVisible = true;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
const updated = await this.repo.updateEntity(id, updateData, ctx);
|
|
936
|
+
if (!updated) return Err(new CommerceNotFoundError("Entity not found."));
|
|
937
|
+
|
|
938
|
+
return Ok(await this.hydrateEntity(updated, undefined, ctx));
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
publish(
|
|
942
|
+
id: string,
|
|
943
|
+
actor: Actor | null,
|
|
944
|
+
ctx?: TxContext,
|
|
945
|
+
): Promise<Result<CatalogEntityHydrated>> {
|
|
946
|
+
return this.changeStatus(id, "active", actor, ctx);
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
archive(
|
|
950
|
+
id: string,
|
|
951
|
+
actor: Actor | null,
|
|
952
|
+
ctx?: TxContext,
|
|
953
|
+
): Promise<Result<CatalogEntityHydrated>> {
|
|
954
|
+
return this.changeStatus(id, "archived", actor, ctx);
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
discontinue(
|
|
958
|
+
id: string,
|
|
959
|
+
actor: Actor | null,
|
|
960
|
+
ctx?: TxContext,
|
|
961
|
+
): Promise<Result<CatalogEntityHydrated>> {
|
|
962
|
+
return this.changeStatus(id, "discontinued", actor, ctx);
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
async setAttributes(
|
|
966
|
+
entityId: string,
|
|
967
|
+
locale: string,
|
|
968
|
+
attrs: SetAttributesInput,
|
|
969
|
+
ctx?: TxContext,
|
|
970
|
+
): Promise<Result<void>> {
|
|
971
|
+
const entity = await this.repo.findEntityById(entityId, ctx);
|
|
972
|
+
if (!entity) return Err(new CommerceNotFoundError("Entity not found."));
|
|
973
|
+
|
|
974
|
+
await this.repo.upsertAttribute(
|
|
975
|
+
entityId,
|
|
976
|
+
locale,
|
|
977
|
+
{
|
|
978
|
+
title: attrs.title,
|
|
979
|
+
subtitle: attrs.subtitle,
|
|
980
|
+
description: attrs.description,
|
|
981
|
+
richDescription: attrs.richDescription,
|
|
982
|
+
seoTitle: attrs.seoTitle,
|
|
983
|
+
seoDescription: attrs.seoDescription,
|
|
984
|
+
},
|
|
985
|
+
ctx,
|
|
986
|
+
);
|
|
987
|
+
|
|
988
|
+
return Ok(undefined);
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
async getAttributes(
|
|
992
|
+
entityId: string,
|
|
993
|
+
locale: string,
|
|
994
|
+
ctx?: TxContext,
|
|
995
|
+
): Promise<Result<SellableAttribute>> {
|
|
996
|
+
const attr = await this.repo.findAttributeByLocale(entityId, locale, ctx);
|
|
997
|
+
if (!attr)
|
|
998
|
+
return Err(
|
|
999
|
+
new CommerceNotFoundError(`Attributes for locale ${locale} not found.`),
|
|
1000
|
+
);
|
|
1001
|
+
return Ok(attr);
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
async listCategories(ctx?: TxContext): Promise<Result<CategorySummary[]>> {
|
|
1005
|
+
const allCategories = await this.repo.findAllCategories(resolveOrgId(ctx?.actor ?? null), ctx);
|
|
1006
|
+
const sorted = allCategories.sort(
|
|
1007
|
+
(a, b) => a.sortOrder - b.sortOrder || a.slug.localeCompare(b.slug),
|
|
1008
|
+
);
|
|
1009
|
+
return Ok(
|
|
1010
|
+
sorted.map((c) => ({
|
|
1011
|
+
id: c.id,
|
|
1012
|
+
parentId: c.parentId,
|
|
1013
|
+
slug: c.slug,
|
|
1014
|
+
sortOrder: c.sortOrder,
|
|
1015
|
+
metadata: c.metadata ?? {},
|
|
1016
|
+
})),
|
|
1017
|
+
);
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
async createCategory(
|
|
1021
|
+
input: CreateCategoryInput,
|
|
1022
|
+
actor: Actor | null,
|
|
1023
|
+
ctx?: TxContext,
|
|
1024
|
+
): Promise<Result<CategorySummary>> {
|
|
1025
|
+
assertPermission(actor, "catalog:update");
|
|
1026
|
+
|
|
1027
|
+
if (input.id) {
|
|
1028
|
+
const existingById = await this.repo.findCategoryById(input.id, ctx);
|
|
1029
|
+
if (existingById) {
|
|
1030
|
+
return Err(
|
|
1031
|
+
new CommerceConflictError(
|
|
1032
|
+
`Category with id ${input.id} already exists.`,
|
|
1033
|
+
),
|
|
1034
|
+
);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
const orgId = resolveOrgId(actor);
|
|
1039
|
+
const existingBySlug = await this.repo.findCategoryBySlug(orgId, input.slug, ctx);
|
|
1040
|
+
if (existingBySlug) {
|
|
1041
|
+
return Err(
|
|
1042
|
+
new CommerceConflictError(
|
|
1043
|
+
`Category with slug ${input.slug} already exists.`,
|
|
1044
|
+
),
|
|
1045
|
+
);
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
const category = await this.repo.createCategory(
|
|
1049
|
+
{
|
|
1050
|
+
organizationId: orgId,
|
|
1051
|
+
...(input.id ? { id: input.id } : {}),
|
|
1052
|
+
slug: input.slug,
|
|
1053
|
+
sortOrder: input.sortOrder ?? 0,
|
|
1054
|
+
metadata: input.metadata ?? {},
|
|
1055
|
+
...(input.parentId !== undefined ? { parentId: input.parentId } : {}),
|
|
1056
|
+
},
|
|
1057
|
+
ctx,
|
|
1058
|
+
);
|
|
1059
|
+
|
|
1060
|
+
return Ok({
|
|
1061
|
+
id: category.id,
|
|
1062
|
+
parentId: category.parentId,
|
|
1063
|
+
slug: category.slug,
|
|
1064
|
+
sortOrder: category.sortOrder,
|
|
1065
|
+
metadata: category.metadata ?? {},
|
|
1066
|
+
});
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
async updateCategory(
|
|
1070
|
+
id: string,
|
|
1071
|
+
input: UpdateCategoryInput,
|
|
1072
|
+
actor: Actor | null,
|
|
1073
|
+
ctx?: TxContext,
|
|
1074
|
+
): Promise<Result<CategorySummary>> {
|
|
1075
|
+
assertPermission(actor, "catalog:update");
|
|
1076
|
+
|
|
1077
|
+
const existing = await this.repo.findCategoryById(id, ctx);
|
|
1078
|
+
if (!existing) return Err(new CommerceNotFoundError("Category not found."));
|
|
1079
|
+
|
|
1080
|
+
if (input.slug) {
|
|
1081
|
+
const catOrgId = resolveOrgId(actor);
|
|
1082
|
+
const existingBySlug = await this.repo.findCategoryBySlug(
|
|
1083
|
+
catOrgId,
|
|
1084
|
+
input.slug,
|
|
1085
|
+
ctx,
|
|
1086
|
+
);
|
|
1087
|
+
if (existingBySlug && existingBySlug.id !== id) {
|
|
1088
|
+
return Err(
|
|
1089
|
+
new CommerceConflictError(
|
|
1090
|
+
`Category with slug ${input.slug} already exists.`,
|
|
1091
|
+
),
|
|
1092
|
+
);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
const updated = await this.repo.updateCategory(
|
|
1097
|
+
id,
|
|
1098
|
+
{
|
|
1099
|
+
...(input.slug !== undefined ? { slug: input.slug } : {}),
|
|
1100
|
+
...(input.sortOrder !== undefined
|
|
1101
|
+
? { sortOrder: input.sortOrder }
|
|
1102
|
+
: {}),
|
|
1103
|
+
...(input.metadata !== undefined ? { metadata: input.metadata } : {}),
|
|
1104
|
+
...(input.parentId !== undefined ? { parentId: input.parentId } : {}),
|
|
1105
|
+
},
|
|
1106
|
+
ctx,
|
|
1107
|
+
);
|
|
1108
|
+
|
|
1109
|
+
if (!updated) return Err(new CommerceNotFoundError("Category not found."));
|
|
1110
|
+
|
|
1111
|
+
return Ok({
|
|
1112
|
+
id: updated.id,
|
|
1113
|
+
parentId: updated.parentId,
|
|
1114
|
+
slug: updated.slug,
|
|
1115
|
+
sortOrder: updated.sortOrder,
|
|
1116
|
+
metadata: updated.metadata ?? {},
|
|
1117
|
+
});
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
async deleteCategory(
|
|
1121
|
+
id: string,
|
|
1122
|
+
actor: Actor | null,
|
|
1123
|
+
ctx?: TxContext,
|
|
1124
|
+
): Promise<Result<void>> {
|
|
1125
|
+
assertPermission(actor, "catalog:update");
|
|
1126
|
+
|
|
1127
|
+
const existing = await this.repo.findCategoryById(id, ctx);
|
|
1128
|
+
if (!existing) return Err(new CommerceNotFoundError("Category not found."));
|
|
1129
|
+
|
|
1130
|
+
await this.repo.deleteEntityCategoriesByCategoryId(id, ctx);
|
|
1131
|
+
await this.repo.deleteCategory(id, ctx);
|
|
1132
|
+
|
|
1133
|
+
return Ok(undefined);
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
async addToCategory(
|
|
1137
|
+
entityId: string,
|
|
1138
|
+
categoryId: string,
|
|
1139
|
+
ctx?: TxContext,
|
|
1140
|
+
): Promise<Result<void>> {
|
|
1141
|
+
const entity = await this.repo.findEntityById(entityId, ctx);
|
|
1142
|
+
if (!entity) return Err(new CommerceNotFoundError("Entity not found."));
|
|
1143
|
+
|
|
1144
|
+
const addCatOrgId = resolveOrgId(ctx?.actor ?? null);
|
|
1145
|
+
const isUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(categoryId);
|
|
1146
|
+
let category = isUuid ? await this.repo.findCategoryById(categoryId, ctx) : null;
|
|
1147
|
+
if (!category) {
|
|
1148
|
+
category = await this.repo.findCategoryBySlug(addCatOrgId, categoryId, ctx);
|
|
1149
|
+
}
|
|
1150
|
+
if (!category) {
|
|
1151
|
+
category = await this.repo.createCategory(
|
|
1152
|
+
{
|
|
1153
|
+
organizationId: addCatOrgId,
|
|
1154
|
+
slug: categoryId,
|
|
1155
|
+
sortOrder: 0,
|
|
1156
|
+
metadata: {},
|
|
1157
|
+
},
|
|
1158
|
+
ctx,
|
|
1159
|
+
);
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
await this.repo.addEntityToCategory(entityId, category.id, 0, ctx);
|
|
1163
|
+
return Ok(undefined);
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
async removeFromCategory(
|
|
1167
|
+
entityId: string,
|
|
1168
|
+
categoryId: string,
|
|
1169
|
+
ctx?: TxContext,
|
|
1170
|
+
): Promise<Result<void>> {
|
|
1171
|
+
const isCatUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(categoryId);
|
|
1172
|
+
let category = isCatUuid ? await this.repo.findCategoryById(categoryId, ctx) : null;
|
|
1173
|
+
if (!category) {
|
|
1174
|
+
category = await this.repo.findCategoryBySlug(resolveOrgId(ctx?.actor ?? null), categoryId, ctx);
|
|
1175
|
+
}
|
|
1176
|
+
const resolvedCategoryId = category?.id ?? categoryId;
|
|
1177
|
+
|
|
1178
|
+
const removed = await this.repo.removeEntityFromCategory(
|
|
1179
|
+
entityId,
|
|
1180
|
+
resolvedCategoryId,
|
|
1181
|
+
ctx,
|
|
1182
|
+
);
|
|
1183
|
+
if (!removed)
|
|
1184
|
+
return Err(new CommerceNotFoundError("Category assignment not found."));
|
|
1185
|
+
return Ok(undefined);
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
async listBrands(ctx?: TxContext): Promise<Result<Brand[]>> {
|
|
1189
|
+
const allBrands = await this.repo.findAllBrands(resolveOrgId(ctx?.actor ?? null), ctx);
|
|
1190
|
+
return Ok(
|
|
1191
|
+
allBrands.sort((a, b) => a.displayName.localeCompare(b.displayName)),
|
|
1192
|
+
);
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
async createBrand(
|
|
1196
|
+
input: CreateBrandInput,
|
|
1197
|
+
actor: Actor | null,
|
|
1198
|
+
ctx?: TxContext,
|
|
1199
|
+
): Promise<Result<Brand>> {
|
|
1200
|
+
assertPermission(actor, "catalog:update");
|
|
1201
|
+
|
|
1202
|
+
if (input.id) {
|
|
1203
|
+
const existingById = await this.repo.findBrandById(input.id, ctx);
|
|
1204
|
+
if (existingById) {
|
|
1205
|
+
return Err(
|
|
1206
|
+
new CommerceConflictError(
|
|
1207
|
+
`Brand with id ${input.id} already exists.`,
|
|
1208
|
+
),
|
|
1209
|
+
);
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
const orgId = resolveOrgId(actor);
|
|
1214
|
+
const existingBySlug = await this.repo.findBrandBySlug(orgId, input.slug, ctx);
|
|
1215
|
+
if (existingBySlug) {
|
|
1216
|
+
return Err(
|
|
1217
|
+
new CommerceConflictError(
|
|
1218
|
+
`Brand with slug ${input.slug} already exists.`,
|
|
1219
|
+
),
|
|
1220
|
+
);
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
return Ok(
|
|
1224
|
+
await this.repo.createBrand(
|
|
1225
|
+
{
|
|
1226
|
+
organizationId: orgId,
|
|
1227
|
+
...(input.id ? { id: input.id } : {}),
|
|
1228
|
+
slug: input.slug,
|
|
1229
|
+
displayName: input.displayName,
|
|
1230
|
+
metadata: input.metadata ?? {},
|
|
1231
|
+
},
|
|
1232
|
+
ctx,
|
|
1233
|
+
),
|
|
1234
|
+
);
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
async updateBrand(
|
|
1238
|
+
id: string,
|
|
1239
|
+
input: UpdateBrandInput,
|
|
1240
|
+
actor: Actor | null,
|
|
1241
|
+
ctx?: TxContext,
|
|
1242
|
+
): Promise<Result<Brand>> {
|
|
1243
|
+
assertPermission(actor, "catalog:update");
|
|
1244
|
+
|
|
1245
|
+
const existing = await this.repo.findBrandById(id, ctx);
|
|
1246
|
+
if (!existing) return Err(new CommerceNotFoundError("Brand not found."));
|
|
1247
|
+
|
|
1248
|
+
if (input.slug) {
|
|
1249
|
+
const brandOrgId = resolveOrgId(actor);
|
|
1250
|
+
const existingBySlug = await this.repo.findBrandBySlug(brandOrgId, input.slug, ctx);
|
|
1251
|
+
if (existingBySlug && existingBySlug.id !== id) {
|
|
1252
|
+
return Err(
|
|
1253
|
+
new CommerceConflictError(
|
|
1254
|
+
`Brand with slug ${input.slug} already exists.`,
|
|
1255
|
+
),
|
|
1256
|
+
);
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
const updated = await this.repo.updateBrand(
|
|
1261
|
+
id,
|
|
1262
|
+
{
|
|
1263
|
+
...(input.slug !== undefined ? { slug: input.slug } : {}),
|
|
1264
|
+
...(input.displayName !== undefined
|
|
1265
|
+
? { displayName: input.displayName }
|
|
1266
|
+
: {}),
|
|
1267
|
+
...(input.metadata !== undefined ? { metadata: input.metadata } : {}),
|
|
1268
|
+
},
|
|
1269
|
+
ctx,
|
|
1270
|
+
);
|
|
1271
|
+
|
|
1272
|
+
if (!updated) return Err(new CommerceNotFoundError("Brand not found."));
|
|
1273
|
+
return Ok(updated);
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
async deleteBrand(
|
|
1277
|
+
id: string,
|
|
1278
|
+
actor: Actor | null,
|
|
1279
|
+
ctx?: TxContext,
|
|
1280
|
+
): Promise<Result<void>> {
|
|
1281
|
+
assertPermission(actor, "catalog:update");
|
|
1282
|
+
|
|
1283
|
+
const existing = await this.repo.findBrandById(id, ctx);
|
|
1284
|
+
if (!existing) return Err(new CommerceNotFoundError("Brand not found."));
|
|
1285
|
+
|
|
1286
|
+
await this.repo.deleteEntityBrandsByBrandId(id, ctx);
|
|
1287
|
+
await this.repo.deleteBrand(id, ctx);
|
|
1288
|
+
|
|
1289
|
+
return Ok(undefined);
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
async addToBrand(
|
|
1293
|
+
entityId: string,
|
|
1294
|
+
brandId: string,
|
|
1295
|
+
ctx?: TxContext,
|
|
1296
|
+
): Promise<Result<void>> {
|
|
1297
|
+
const entity = await this.repo.findEntityById(entityId, ctx);
|
|
1298
|
+
if (!entity) return Err(new CommerceNotFoundError("Entity not found."));
|
|
1299
|
+
|
|
1300
|
+
const addBrandOrgId = resolveOrgId(ctx?.actor ?? null);
|
|
1301
|
+
const isBrandUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(brandId);
|
|
1302
|
+
let brand = isBrandUuid ? await this.repo.findBrandById(brandId, ctx) : null;
|
|
1303
|
+
if (!brand) {
|
|
1304
|
+
brand = await this.repo.findBrandBySlug(addBrandOrgId, brandId, ctx);
|
|
1305
|
+
}
|
|
1306
|
+
if (!brand) {
|
|
1307
|
+
brand = await this.repo.createBrand(
|
|
1308
|
+
{
|
|
1309
|
+
organizationId: addBrandOrgId,
|
|
1310
|
+
slug: brandId,
|
|
1311
|
+
displayName: brandId,
|
|
1312
|
+
metadata: {},
|
|
1313
|
+
},
|
|
1314
|
+
ctx,
|
|
1315
|
+
);
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
await this.repo.addEntityToBrand(entityId, brand.id, 0, ctx);
|
|
1319
|
+
return Ok(undefined);
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
async removeFromBrand(
|
|
1323
|
+
entityId: string,
|
|
1324
|
+
brandId: string,
|
|
1325
|
+
ctx?: TxContext,
|
|
1326
|
+
): Promise<Result<void>> {
|
|
1327
|
+
const isBrUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(brandId);
|
|
1328
|
+
let brand = isBrUuid ? await this.repo.findBrandById(brandId, ctx) : null;
|
|
1329
|
+
if (!brand) {
|
|
1330
|
+
brand = await this.repo.findBrandBySlug(resolveOrgId(ctx?.actor ?? null), brandId, ctx);
|
|
1331
|
+
}
|
|
1332
|
+
const resolvedBrandId = brand?.id ?? brandId;
|
|
1333
|
+
|
|
1334
|
+
const removed = await this.repo.removeEntityFromBrand(
|
|
1335
|
+
entityId,
|
|
1336
|
+
resolvedBrandId,
|
|
1337
|
+
ctx,
|
|
1338
|
+
);
|
|
1339
|
+
if (!removed)
|
|
1340
|
+
return Err(new CommerceNotFoundError("Brand assignment not found."));
|
|
1341
|
+
return Ok(undefined);
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
async createOptionType(
|
|
1345
|
+
input: CreateOptionTypeInput,
|
|
1346
|
+
actor: Actor | null,
|
|
1347
|
+
ctx?: TxContext,
|
|
1348
|
+
): Promise<Result<OptionType>> {
|
|
1349
|
+
assertPermission(actor, "catalog:update");
|
|
1350
|
+
|
|
1351
|
+
const entity = await this.repo.findEntityById(input.entityId, ctx);
|
|
1352
|
+
if (!entity) return Err(new CommerceNotFoundError("Entity not found."));
|
|
1353
|
+
|
|
1354
|
+
const optionType = await this.repo.createOptionType(
|
|
1355
|
+
{
|
|
1356
|
+
entityId: input.entityId,
|
|
1357
|
+
name: input.name,
|
|
1358
|
+
displayName: input.name,
|
|
1359
|
+
sortOrder: 0,
|
|
1360
|
+
},
|
|
1361
|
+
ctx,
|
|
1362
|
+
);
|
|
1363
|
+
|
|
1364
|
+
// Create initial option values if provided
|
|
1365
|
+
if (input.values) {
|
|
1366
|
+
for (const value of input.values) {
|
|
1367
|
+
await this.repo.createOptionValue(
|
|
1368
|
+
{
|
|
1369
|
+
optionTypeId: optionType.id,
|
|
1370
|
+
value,
|
|
1371
|
+
displayValue: value,
|
|
1372
|
+
sortOrder: 0,
|
|
1373
|
+
metadata: {},
|
|
1374
|
+
},
|
|
1375
|
+
ctx,
|
|
1376
|
+
);
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
|
|
1380
|
+
return Ok(optionType);
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
async createOptionValue(
|
|
1384
|
+
input: CreateOptionValueInput,
|
|
1385
|
+
actor: Actor | null,
|
|
1386
|
+
ctx?: TxContext,
|
|
1387
|
+
): Promise<Result<OptionValue>> {
|
|
1388
|
+
assertPermission(actor, "catalog:update");
|
|
1389
|
+
|
|
1390
|
+
const optionType = await this.repo.findOptionTypeById(
|
|
1391
|
+
input.optionTypeId,
|
|
1392
|
+
ctx,
|
|
1393
|
+
);
|
|
1394
|
+
if (!optionType)
|
|
1395
|
+
return Err(new CommerceNotFoundError("Option type not found."));
|
|
1396
|
+
|
|
1397
|
+
return Ok(
|
|
1398
|
+
await this.repo.createOptionValue(
|
|
1399
|
+
{
|
|
1400
|
+
optionTypeId: input.optionTypeId,
|
|
1401
|
+
value: input.value,
|
|
1402
|
+
displayValue: input.value,
|
|
1403
|
+
sortOrder: 0,
|
|
1404
|
+
metadata: {},
|
|
1405
|
+
},
|
|
1406
|
+
ctx,
|
|
1407
|
+
),
|
|
1408
|
+
);
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
async createVariant(
|
|
1412
|
+
input: CreateVariantInput,
|
|
1413
|
+
actor: Actor | null,
|
|
1414
|
+
ctx?: TxContext,
|
|
1415
|
+
): Promise<Result<Variant>> {
|
|
1416
|
+
assertPermission(actor, "catalog:update");
|
|
1417
|
+
|
|
1418
|
+
const entity = await this.repo.findEntityById(input.entityId, ctx);
|
|
1419
|
+
if (!entity) return Err(new CommerceNotFoundError("Entity not found."));
|
|
1420
|
+
|
|
1421
|
+
// Resolve options record (e.g. { Color: "Red" }) into option value IDs
|
|
1422
|
+
const entityOptionTypes = await this.repo.findOptionTypesByEntityId(
|
|
1423
|
+
input.entityId,
|
|
1424
|
+
ctx,
|
|
1425
|
+
);
|
|
1426
|
+
const optionValueIds: string[] = [];
|
|
1427
|
+
for (const [optName, optVal] of Object.entries(input.options)) {
|
|
1428
|
+
const ot = entityOptionTypes.find((t) => t.name === optName);
|
|
1429
|
+
if (!ot) {
|
|
1430
|
+
return Err(
|
|
1431
|
+
new CommerceValidationError(
|
|
1432
|
+
`Option type "${optName}" does not exist on this entity.`,
|
|
1433
|
+
),
|
|
1434
|
+
);
|
|
1435
|
+
}
|
|
1436
|
+
const typeValues = await this.repo.findOptionValuesByTypeId(ot.id, ctx);
|
|
1437
|
+
const ov = typeValues.find((v) => v.value === optVal);
|
|
1438
|
+
if (!ov) {
|
|
1439
|
+
return Err(
|
|
1440
|
+
new CommerceValidationError(
|
|
1441
|
+
`Option value "${optVal}" does not exist for option type "${optName}".`,
|
|
1442
|
+
),
|
|
1443
|
+
);
|
|
1444
|
+
}
|
|
1445
|
+
optionValueIds.push(ov.id);
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
const variant = await this.repo.createVariant(
|
|
1449
|
+
{
|
|
1450
|
+
entityId: input.entityId,
|
|
1451
|
+
status: "active",
|
|
1452
|
+
sortOrder: 0,
|
|
1453
|
+
metadata: {},
|
|
1454
|
+
...(input.sku !== undefined ? { sku: input.sku } : {}),
|
|
1455
|
+
},
|
|
1456
|
+
ctx,
|
|
1457
|
+
);
|
|
1458
|
+
|
|
1459
|
+
await this.repo.createVariantOptionValues(
|
|
1460
|
+
optionValueIds.map((optionValueId) => ({
|
|
1461
|
+
variantId: variant.id,
|
|
1462
|
+
optionValueId,
|
|
1463
|
+
})),
|
|
1464
|
+
ctx,
|
|
1465
|
+
);
|
|
1466
|
+
|
|
1467
|
+
return Ok(variant);
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
async generateVariants(
|
|
1471
|
+
entityId: string,
|
|
1472
|
+
strategy: VariantGenerationStrategy,
|
|
1473
|
+
actor: Actor | null,
|
|
1474
|
+
ctx?: TxContext,
|
|
1475
|
+
): Promise<Result<Variant[]>> {
|
|
1476
|
+
assertPermission(actor, "catalog:update");
|
|
1477
|
+
|
|
1478
|
+
const entity = await this.repo.findEntityById(entityId, ctx);
|
|
1479
|
+
if (!entity) return Err(new CommerceNotFoundError("Entity not found."));
|
|
1480
|
+
|
|
1481
|
+
const entityOptionTypes = await this.repo.findOptionTypesByEntityId(
|
|
1482
|
+
entityId,
|
|
1483
|
+
ctx,
|
|
1484
|
+
);
|
|
1485
|
+
const sortedOptionTypes = entityOptionTypes.sort(
|
|
1486
|
+
(a, b) => a.sortOrder - b.sortOrder,
|
|
1487
|
+
);
|
|
1488
|
+
|
|
1489
|
+
const optionValueGroups: string[][] = [];
|
|
1490
|
+
for (const optionType of sortedOptionTypes) {
|
|
1491
|
+
const values = await this.repo.findOptionValuesByTypeId(
|
|
1492
|
+
optionType.id,
|
|
1493
|
+
ctx,
|
|
1494
|
+
);
|
|
1495
|
+
const sortedValues = values.sort((a, b) => a.sortOrder - b.sortOrder);
|
|
1496
|
+
optionValueGroups.push(sortedValues.map((v) => v.id));
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
let combinations: string[][] = [];
|
|
1500
|
+
|
|
1501
|
+
if (strategy.mode === "all") {
|
|
1502
|
+
combinations = cartesian(optionValueGroups);
|
|
1503
|
+
} else if (strategy.mode === "manual") {
|
|
1504
|
+
combinations = strategy.combinations;
|
|
1505
|
+
} else {
|
|
1506
|
+
const base = cartesian(optionValueGroups);
|
|
1507
|
+
const include = strategy.matrix.include;
|
|
1508
|
+
const exclude = strategy.matrix.exclude;
|
|
1509
|
+
combinations = base.filter((combo) => {
|
|
1510
|
+
const isExcluded = (exclude ?? []).some((pattern) =>
|
|
1511
|
+
pattern.every((val) => combo.includes(val)),
|
|
1512
|
+
);
|
|
1513
|
+
if (isExcluded) return false;
|
|
1514
|
+
if (!include || include.length === 0) return true;
|
|
1515
|
+
return include.some((pattern) =>
|
|
1516
|
+
pattern.every((val) => combo.includes(val)),
|
|
1517
|
+
);
|
|
1518
|
+
});
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
const created: Variant[] = [];
|
|
1522
|
+
for (const combo of combinations) {
|
|
1523
|
+
const variant = await this.repo.createVariant(
|
|
1524
|
+
{
|
|
1525
|
+
entityId,
|
|
1526
|
+
status: "active",
|
|
1527
|
+
sortOrder: 0,
|
|
1528
|
+
metadata: { generatedBy: strategy.mode },
|
|
1529
|
+
},
|
|
1530
|
+
ctx,
|
|
1531
|
+
);
|
|
1532
|
+
await this.repo.createVariantOptionValues(
|
|
1533
|
+
combo.map((optionValueId) => ({
|
|
1534
|
+
variantId: variant.id,
|
|
1535
|
+
optionValueId,
|
|
1536
|
+
})),
|
|
1537
|
+
ctx,
|
|
1538
|
+
);
|
|
1539
|
+
created.push(variant);
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
return Ok(created);
|
|
1543
|
+
}
|
|
1544
|
+
}
|