@openlup/platform 0.17.0 → 0.18.0
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/CHANGELOG.md +96 -0
- package/README.md +284 -6
- package/dist/catalog/activeSkuSelection.d.ts +2 -0
- package/dist/catalog/activeSkuSelection.js +20 -0
- package/dist/catalog/adminBindings.d.ts +72 -0
- package/dist/catalog/adminBindings.js +1 -0
- package/dist/catalog/adminBundleHandler.d.ts +26 -0
- package/dist/catalog/adminBundleHandler.js +111 -0
- package/dist/catalog/adminBundleReadHandler.d.ts +20 -0
- package/dist/catalog/adminBundleReadHandler.js +185 -0
- package/dist/catalog/adminBundleWritePort.d.ts +85 -0
- package/dist/catalog/adminBundleWritePort.js +1 -0
- package/dist/catalog/bundleAvailability.d.ts +63 -0
- package/dist/catalog/bundleAvailability.js +78 -0
- package/dist/catalog/bundleAvailabilityService.d.ts +65 -0
- package/dist/catalog/bundleAvailabilityService.js +89 -0
- package/dist/catalog/bundleBindings.d.ts +33 -0
- package/dist/catalog/bundleBindings.js +1 -0
- package/dist/catalog/bundleCatalogQueries.d.ts +28 -0
- package/dist/catalog/bundleCatalogQueries.js +84 -0
- package/dist/catalog/bundleCatalogReadPort.d.ts +166 -0
- package/dist/catalog/bundleCatalogReadPort.js +36 -0
- package/dist/catalog/bundleCatalogStore.d.ts +3 -0
- package/dist/catalog/bundleCatalogStore.js +152 -0
- package/dist/catalog/bundleRuleCodes.d.ts +41 -0
- package/dist/catalog/bundleRuleCodes.js +110 -0
- package/dist/catalog/bundleRules.d.ts +69 -0
- package/dist/catalog/bundleRules.js +102 -0
- package/dist/catalog/catalogDraftStore.d.ts +4 -0
- package/dist/catalog/catalogDraftStore.js +63 -0
- package/dist/catalog/catalogSkuEnvelope.d.ts +7 -0
- package/dist/catalog/catalogSkuEnvelope.js +142 -0
- package/dist/catalog/catalogSkuEnvelopeMapper.d.ts +38 -0
- package/dist/catalog/catalogSkuEnvelopeMapper.js +88 -0
- package/dist/catalog/contracts/bundleAdmin.d.ts +143 -0
- package/dist/catalog/contracts/bundleAdmin.js +1 -0
- package/dist/catalog/contracts/bundleAvailability.d.ts +61 -0
- package/dist/catalog/contracts/bundleAvailability.js +1 -0
- package/dist/catalog/contracts/bundleRead.d.ts +223 -0
- package/dist/catalog/contracts/bundleRead.js +1 -0
- package/dist/catalog/contracts/draft.d.ts +79 -0
- package/dist/catalog/contracts/draft.js +47 -0
- package/dist/catalog/contracts/foundation.d.ts +78 -0
- package/dist/catalog/contracts/foundation.js +53 -0
- package/dist/catalog/contracts/index.d.ts +14 -0
- package/dist/catalog/contracts/index.js +6 -0
- package/dist/catalog/contracts/lifecycle.d.ts +77 -0
- package/dist/catalog/contracts/lifecycle.js +1 -0
- package/dist/catalog/contracts/productMediaContracts.d.ts +77 -0
- package/dist/catalog/contracts/productMediaContracts.js +149 -0
- package/dist/catalog/contracts/productTypes.d.ts +54 -0
- package/dist/catalog/contracts/productTypes.js +16 -0
- package/dist/catalog/contracts/resolveProductMedia.d.ts +3 -0
- package/dist/catalog/contracts/resolveProductMedia.js +78 -0
- package/dist/catalog/contracts/sellable.d.ts +23 -0
- package/dist/catalog/contracts/sellable.js +1 -0
- package/dist/catalog/draftHandler.d.ts +25 -0
- package/dist/catalog/draftHandler.js +118 -0
- package/dist/catalog/draftPorts.d.ts +18 -0
- package/dist/catalog/draftPorts.js +8 -0
- package/dist/catalog/draftSchemas.d.ts +423 -0
- package/dist/catalog/draftSchemas.js +89 -0
- package/dist/catalog/draftValidation.d.ts +21 -0
- package/dist/catalog/draftValidation.js +125 -0
- package/dist/catalog/foundationSchemas.d.ts +98 -0
- package/dist/catalog/foundationSchemas.js +110 -0
- package/dist/catalog/index.d.ts +43 -0
- package/dist/catalog/index.js +25 -0
- package/dist/catalog/mediaSourcePack.d.ts +51 -0
- package/dist/catalog/mediaSourcePack.js +247 -0
- package/dist/catalog/productTypeSchemas.d.ts +46 -0
- package/dist/catalog/productTypeSchemas.js +72 -0
- package/dist/catalog/publicationPorts.d.ts +42 -0
- package/dist/catalog/publicationPorts.js +1 -0
- package/dist/catalog/readPorts.d.ts +51 -0
- package/dist/catalog/readPorts.js +1 -0
- package/dist/catalog/referenceCatalogHandler.d.ts +7 -0
- package/dist/catalog/referenceCatalogHandler.js +27 -0
- package/dist/catalog/resolvedSkuProjection.d.ts +88 -0
- package/dist/catalog/resolvedSkuProjection.js +60 -0
- package/dist/catalog/sellableBundleHandler.d.ts +46 -0
- package/dist/catalog/sellableBundleHandler.js +84 -0
- package/dist/catalog/sellableSchemas.d.ts +46 -0
- package/dist/catalog/sellableSchemas.js +27 -0
- package/dist/catalog/sql.d.ts +46 -0
- package/dist/catalog/sql.js +13 -0
- package/dist/catalog/validation.d.ts +2 -0
- package/dist/catalog/validation.js +3 -0
- package/dist/communications/contracts/index.d.ts +1 -0
- package/dist/communications/contracts/marketingSync.d.ts +194 -0
- package/dist/communications/contracts/marketingSync.js +1 -0
- package/dist/communications/index.d.ts +1 -0
- package/dist/communications/klaviyo/consentPush.d.ts +51 -0
- package/dist/communications/klaviyo/consentPush.js +19 -0
- package/dist/communications/klaviyo/index.d.ts +110 -0
- package/dist/communications/klaviyo/index.js +860 -0
- package/dist/communications/newsletterIntegrationContracts.d.ts +3 -3
- package/dist/communications/resend/index.d.ts +40 -0
- package/dist/communications/resend/index.js +310 -0
- package/dist/composition/catalogContribution.d.ts +18 -0
- package/dist/composition/catalogContribution.js +21 -0
- package/dist/composition/index.d.ts +1 -0
- package/dist/composition/index.js +1 -0
- package/dist/identity/customerContracts.d.ts +2 -2
- package/dist/inventory/adminInventory.d.ts +13 -0
- package/dist/inventory/adminInventory.js +214 -0
- package/dist/inventory/contracts/forecast.d.ts +86 -0
- package/dist/inventory/contracts/forecast.js +1 -0
- package/dist/inventory/contracts/index.d.ts +4 -0
- package/dist/inventory/contracts/index.js +1 -0
- package/dist/inventory/contracts/inventory.d.ts +157 -0
- package/dist/inventory/contracts/inventory.js +1 -0
- package/dist/inventory/contracts/newPurchaseStock.d.ts +20 -0
- package/dist/inventory/contracts/newPurchaseStock.js +1 -0
- package/dist/inventory/contracts/reservation.d.ts +18 -0
- package/dist/inventory/contracts/reservation.js +1 -0
- package/dist/inventory/forecast.d.ts +24 -0
- package/dist/inventory/forecast.js +200 -0
- package/dist/inventory/index.d.ts +13 -0
- package/dist/inventory/index.js +10 -0
- package/dist/inventory/inventoryHandlers.d.ts +53 -0
- package/dist/inventory/inventoryHandlers.js +151 -0
- package/dist/inventory/newPurchaseStockAvailability.d.ts +47 -0
- package/dist/inventory/newPurchaseStockAvailability.js +75 -0
- package/dist/inventory/ports.d.ts +21 -0
- package/dist/inventory/ports.js +16 -0
- package/dist/inventory/renewalReservation.d.ts +27 -0
- package/dist/inventory/renewalReservation.js +61 -0
- package/dist/inventory/reservationPolicy.d.ts +6 -0
- package/dist/inventory/reservationPolicy.js +19 -0
- package/dist/inventory/schemas.d.ts +137 -0
- package/dist/inventory/schemas.js +112 -0
- package/dist/inventory/sql.d.ts +14 -0
- package/dist/inventory/sql.js +5 -0
- package/dist/inventory/subscriptionForecastCalculation.d.ts +4 -0
- package/dist/inventory/subscriptionForecastCalculation.js +133 -0
- package/dist/pricing/acquisitionFinancialPolicy.d.ts +26 -0
- package/dist/pricing/acquisitionFinancialPolicy.js +1 -0
- package/dist/pricing/acquisitionOfferPricing.d.ts +27 -0
- package/dist/pricing/acquisitionOfferPricing.js +25 -0
- package/dist/pricing/acquisitionOfferPricingHandler.d.ts +20 -0
- package/dist/pricing/acquisitionOfferPricingHandler.js +105 -0
- package/dist/pricing/acquisitionOfferPricingSchemas.d.ts +31 -0
- package/dist/pricing/acquisitionOfferPricingSchemas.js +33 -0
- package/dist/pricing/acquisitionQuoteBinding.d.ts +12 -0
- package/dist/pricing/acquisitionQuoteBinding.js +12 -0
- package/dist/pricing/acquisitionReadiness.d.ts +31 -0
- package/dist/pricing/acquisitionReadiness.js +117 -0
- package/dist/pricing/acquisitionTerms.d.ts +19 -0
- package/dist/pricing/acquisitionTerms.js +126 -0
- package/dist/pricing/acquisitionTermsSchemas.d.ts +59 -0
- package/dist/pricing/acquisitionTermsSchemas.js +42 -0
- package/dist/pricing/catalogFactsProvenance.d.ts +33 -0
- package/dist/pricing/catalogFactsProvenance.js +64 -0
- package/dist/pricing/catalogFactsSchemas.d.ts +45 -0
- package/dist/pricing/catalogFactsSchemas.js +58 -0
- package/dist/pricing/commerceOfferPricing.d.ts +25 -0
- package/dist/pricing/commerceOfferPricing.js +139 -0
- package/dist/pricing/commerceOfferPricingCache.d.ts +10 -0
- package/dist/pricing/commerceOfferPricingCache.js +44 -0
- package/dist/pricing/commerceOfferPricingHandler.d.ts +21 -0
- package/dist/pricing/commerceOfferPricingHandler.js +78 -0
- package/dist/pricing/commercePriceAuthority.d.ts +26 -0
- package/dist/pricing/commercePriceAuthority.js +79 -0
- package/dist/pricing/commercePricingPolicyErrors.d.ts +40 -0
- package/dist/pricing/commercePricingPolicyErrors.js +67 -0
- package/dist/pricing/commerceQuoteBatchHandler.d.ts +37 -0
- package/dist/pricing/commerceQuoteBatchHandler.js +83 -0
- package/dist/pricing/commerceQuoteCatalogReadPort.d.ts +27 -0
- package/dist/pricing/commerceQuoteCatalogReadPort.js +8 -0
- package/dist/pricing/commerceQuoteHandler.d.ts +31 -0
- package/dist/pricing/commerceQuoteHandler.js +110 -0
- package/dist/pricing/commerceQuoteSnapshotExpect.d.ts +9 -0
- package/dist/pricing/commerceQuoteSnapshotExpect.js +22 -0
- package/dist/pricing/contracts/acquisitionTerms.d.ts +32 -0
- package/dist/pricing/contracts/acquisitionTerms.js +2 -0
- package/dist/pricing/contracts/authority.d.ts +27 -0
- package/dist/pricing/contracts/authority.js +1 -0
- package/dist/pricing/contracts/evidence.d.ts +33 -0
- package/dist/pricing/contracts/evidence.js +1 -0
- package/dist/pricing/contracts/index.d.ts +17 -0
- package/dist/pricing/contracts/index.js +7 -0
- package/dist/pricing/contracts/money.d.ts +27 -0
- package/dist/pricing/contracts/money.js +1 -0
- package/dist/pricing/contracts/offer.d.ts +52 -0
- package/dist/pricing/contracts/offer.js +4 -0
- package/dist/pricing/contracts/offerPolicy.d.ts +16 -0
- package/dist/pricing/contracts/offerPolicy.js +5 -0
- package/dist/pricing/contracts/policy.d.ts +22 -0
- package/dist/pricing/contracts/policy.js +3 -0
- package/dist/pricing/contracts/provenance.d.ts +24 -0
- package/dist/pricing/contracts/provenance.js +2 -0
- package/dist/pricing/contracts/quote.d.ts +94 -0
- package/dist/pricing/contracts/quote.js +1 -0
- package/dist/pricing/dbBackedCommerceQuoteHelpers.d.ts +78 -0
- package/dist/pricing/dbBackedCommerceQuoteHelpers.js +146 -0
- package/dist/pricing/dbBackedCommerceQuotePort.d.ts +66 -0
- package/dist/pricing/dbBackedCommerceQuotePort.js +206 -0
- package/dist/pricing/dbBackedCommerceQuoteSnapshotVerifier.d.ts +4 -0
- package/dist/pricing/dbBackedCommerceQuoteSnapshotVerifier.js +25 -0
- package/dist/pricing/index.d.ts +51 -0
- package/dist/pricing/index.js +36 -0
- package/dist/pricing/offerPolicyReadiness.d.ts +21 -0
- package/dist/pricing/offerPolicyReadiness.js +79 -0
- package/dist/pricing/offerPolicySchemas.d.ts +25 -0
- package/dist/pricing/offerPolicySchemas.js +15 -0
- package/dist/pricing/offerPricingSchemas.d.ts +315 -0
- package/dist/pricing/offerPricingSchemas.js +65 -0
- package/dist/pricing/ports.d.ts +13 -0
- package/dist/pricing/ports.js +9 -0
- package/dist/pricing/pricingPolicyToken.d.ts +39 -0
- package/dist/pricing/pricingPolicyToken.js +84 -0
- package/dist/pricing/quoteAcquisitionTerms.d.ts +2 -0
- package/dist/pricing/quoteAcquisitionTerms.js +4 -0
- package/dist/pricing/quoteCodeRejectionDetails.d.ts +20 -0
- package/dist/pricing/quoteCodeRejectionDetails.js +22 -0
- package/dist/pricing/quotePorts.d.ts +30 -0
- package/dist/pricing/quotePorts.js +20 -0
- package/dist/pricing/quotePrimitives.d.ts +13 -0
- package/dist/pricing/quotePrimitives.js +18 -0
- package/dist/pricing/quotePromotionEvidence.d.ts +39 -0
- package/dist/pricing/quotePromotionEvidence.js +73 -0
- package/dist/pricing/quoteSchemas.d.ts +1176 -0
- package/dist/pricing/quoteSchemas.js +196 -0
- package/dist/pricing/subscriptionPricePolicy.d.ts +52 -0
- package/dist/pricing/subscriptionPricePolicy.js +96 -0
- package/dist/pricing/taxProfileGuard.d.ts +2 -0
- package/dist/pricing/taxProfileGuard.js +24 -0
- package/dist/pricing/validation.d.ts +2 -0
- package/dist/pricing/validation.js +3 -0
- package/dist/promotions/adminBindings.d.ts +72 -0
- package/dist/promotions/adminBindings.js +1 -0
- package/dist/promotions/adminPromotionCodesContracts.d.ts +498 -0
- package/dist/promotions/adminPromotionCodesContracts.js +211 -0
- package/dist/promotions/adminPromotionCodesDataPort.d.ts +47 -0
- package/dist/promotions/adminPromotionCodesDataPort.js +1 -0
- package/dist/promotions/adminPromotionCodesHandler.d.ts +14 -0
- package/dist/promotions/adminPromotionCodesHandler.js +194 -0
- package/dist/promotions/adminPromotionsContracts.d.ts +235 -0
- package/dist/promotions/adminPromotionsContracts.js +99 -0
- package/dist/promotions/adminPromotionsDataPort.d.ts +42 -0
- package/dist/promotions/adminPromotionsDataPort.js +1 -0
- package/dist/promotions/adminPromotionsHandler.d.ts +16 -0
- package/dist/promotions/adminPromotionsHandler.js +47 -0
- package/dist/promotions/contracts/index.d.ts +387 -0
- package/dist/promotions/contracts/index.js +1 -0
- package/dist/promotions/index.d.ts +22 -0
- package/dist/promotions/index.js +21 -0
- package/dist/promotions/promoDataPort.d.ts +47 -0
- package/dist/promotions/promoDataPort.js +14 -0
- package/dist/promotions/promotionAcceptanceTelemetry.d.ts +29 -0
- package/dist/promotions/promotionAcceptanceTelemetry.js +45 -0
- package/dist/promotions/promotionBenefitDefinition.d.ts +37 -0
- package/dist/promotions/promotionBenefitDefinition.js +33 -0
- package/dist/promotions/promotionClaimSweepPort.d.ts +14 -0
- package/dist/promotions/promotionClaimSweepPort.js +8 -0
- package/dist/promotions/promotionClaims.d.ts +5 -0
- package/dist/promotions/promotionClaims.js +57 -0
- package/dist/promotions/promotionCodePreview.d.ts +11 -0
- package/dist/promotions/promotionCodePreview.js +154 -0
- package/dist/promotions/promotionCodeQuotePort.d.ts +25 -0
- package/dist/promotions/promotionCodeQuotePort.js +1 -0
- package/dist/promotions/promotionQuoteAcceptance.d.ts +65 -0
- package/dist/promotions/promotionQuoteAcceptance.js +202 -0
- package/dist/promotions/promotionQuoteBinding.d.ts +71 -0
- package/dist/promotions/promotionQuoteBinding.js +85 -0
- package/dist/promotions/quoteAutomaticPromotions.d.ts +24 -0
- package/dist/promotions/quoteAutomaticPromotions.js +36 -0
- package/dist/promotions/quotePromoDiscounts.d.ts +32 -0
- package/dist/promotions/quotePromoDiscounts.js +140 -0
- package/dist/promotions/quotePromotionCodeAdjustments.d.ts +27 -0
- package/dist/promotions/quotePromotionCodeAdjustments.js +223 -0
- package/dist/promotions/quotePromotionV2.d.ts +29 -0
- package/dist/promotions/quotePromotionV2.js +174 -0
- package/dist/promotions/sql.d.ts +14 -0
- package/dist/promotions/sql.js +5 -0
- package/dist/promotions/validation.d.ts +2 -0
- package/dist/promotions/validation.js +3 -0
- package/dist/shipping/addressCanonLookupHandler.d.ts +7 -0
- package/dist/shipping/addressCanonLookupHandler.js +105 -0
- package/dist/shipping/addressCanonical.d.ts +2 -0
- package/dist/shipping/addressCanonical.js +26 -0
- package/dist/shipping/addressPorts.d.ts +39 -0
- package/dist/shipping/addressPorts.js +1 -0
- package/dist/shipping/addressSchemas.d.ts +30 -0
- package/dist/shipping/addressSchemas.js +1 -0
- package/dist/shipping/adminShippingRateHandler.d.ts +4 -0
- package/dist/shipping/adminShippingRateHandler.js +64 -0
- package/dist/shipping/contracts/address.d.ts +144 -0
- package/dist/shipping/contracts/address.js +1 -0
- package/dist/shipping/contracts/delivery.d.ts +110 -0
- package/dist/shipping/contracts/delivery.js +1 -0
- package/dist/shipping/contracts/index.d.ts +3 -0
- package/dist/shipping/contracts/index.js +1 -0
- package/dist/shipping/contracts/shippingRate.d.ts +11 -0
- package/dist/shipping/contracts/shippingRate.js +1 -0
- package/dist/shipping/customerAddressesHandler.d.ts +5 -0
- package/dist/shipping/customerAddressesHandler.js +80 -0
- package/dist/shipping/customerSelfServiceAddressModels.d.ts +27 -0
- package/dist/shipping/customerSelfServiceAddressModels.js +101 -0
- package/dist/shipping/deliverySelectionHandler.d.ts +10 -0
- package/dist/shipping/deliverySelectionHandler.js +77 -0
- package/dist/shipping/deliverySelectionPort.d.ts +8 -0
- package/dist/shipping/deliverySelectionPort.js +1 -0
- package/dist/shipping/deliverySelectionSummary.d.ts +2 -0
- package/dist/shipping/deliverySelectionSummary.js +53 -0
- package/dist/shipping/handlerSchemas.d.ts +21 -0
- package/dist/shipping/handlerSchemas.js +1 -0
- package/dist/shipping/index.d.ts +17 -0
- package/dist/shipping/index.js +8 -0
- package/dist/shipping/pickupPointSearchPort.d.ts +4 -0
- package/dist/shipping/pickupPointSearchPort.js +1 -0
- package/dist/shipping/readDeliverySelectionEvidence.d.ts +2 -0
- package/dist/shipping/readDeliverySelectionEvidence.js +44 -0
- package/dist/shipping/shippingRatePorts.d.ts +52 -0
- package/dist/shipping/shippingRatePorts.js +1 -0
- package/package.json +62 -2
- package/release-gates.json +218 -10
- package/sql/20260807140000_read_parity_catalog.sql +94 -0
- package/sql/20260810120000_storefront_catalog_attributes.sql +51 -0
- package/sql/20260812140000_sellable_bundle_catalog.sql +124 -0
- package/sql/20260812210000_bundle_operator_write_boundary.sql +567 -0
- package/sql/20260814000000_fulfillment_stock_evidence.sql +239 -0
- package/sql/20260815120000_automatic_subscription_renewal.sql +1083 -0
- package/sql/20260817180000_promotion_claim_lifecycle.sql +591 -0
- package/sql/20260817180100_promotion_claim_lifecycle_validation.sql +23 -0
- package/sql/20260827220000_catalog_document_revision_foundation.sql +226 -0
- package/sql/20260827220001_catalog_document_revision_foundation_validate.sql +10 -0
- package/sql/20260908100000_catalog_draft_foundation.sql +217 -0
- package/src/catalog/activeSkuSelection.ts +20 -0
- package/src/catalog/adminBindings.ts +59 -0
- package/src/catalog/adminBundleHandler.ts +165 -0
- package/src/catalog/adminBundleReadHandler.ts +261 -0
- package/src/catalog/adminBundleWritePort.ts +102 -0
- package/src/catalog/bundleAvailability.ts +129 -0
- package/src/catalog/bundleAvailabilityService.ts +171 -0
- package/src/catalog/bundleBindings.ts +29 -0
- package/src/catalog/bundleCatalogQueries.ts +98 -0
- package/src/catalog/bundleCatalogReadPort.ts +177 -0
- package/src/catalog/bundleCatalogStore.ts +269 -0
- package/src/catalog/bundleRuleCodes.ts +134 -0
- package/src/catalog/bundleRules.ts +153 -0
- package/src/catalog/catalogDraftStore.ts +71 -0
- package/src/catalog/catalogSkuEnvelope.ts +214 -0
- package/src/catalog/catalogSkuEnvelopeMapper.ts +160 -0
- package/src/catalog/contracts/bundleAdmin.ts +20 -0
- package/src/catalog/contracts/bundleAvailability.ts +5 -0
- package/src/catalog/contracts/bundleRead.ts +13 -0
- package/src/catalog/contracts/draft.ts +69 -0
- package/src/catalog/contracts/foundation.ts +104 -0
- package/src/catalog/contracts/index.ts +14 -0
- package/src/catalog/contracts/lifecycle.ts +74 -0
- package/src/catalog/contracts/productMediaContracts.ts +170 -0
- package/src/catalog/contracts/productTypes.ts +35 -0
- package/src/catalog/contracts/resolveProductMedia.ts +85 -0
- package/src/catalog/contracts/sellable.ts +12 -0
- package/src/catalog/draftHandler.ts +105 -0
- package/src/catalog/draftPorts.ts +18 -0
- package/src/catalog/draftSchemas.ts +88 -0
- package/src/catalog/draftValidation.ts +100 -0
- package/src/catalog/foundationSchemas.ts +123 -0
- package/src/catalog/index.ts +43 -0
- package/src/catalog/mediaSourcePack.ts +219 -0
- package/src/catalog/productTypeSchemas.ts +70 -0
- package/src/catalog/publicationPorts.ts +44 -0
- package/src/catalog/readPorts.ts +61 -0
- package/src/catalog/referenceCatalogHandler.ts +39 -0
- package/src/catalog/resolvedSkuProjection.ts +76 -0
- package/src/catalog/sellableBundleHandler.ts +133 -0
- package/src/catalog/sellableSchemas.ts +33 -0
- package/src/catalog/sql.ts +14 -0
- package/src/catalog/validation.ts +3 -0
- package/src/communications/contracts/index.ts +1 -0
- package/src/communications/contracts/marketingSync.ts +109 -0
- package/src/communications/index.ts +1 -0
- package/src/communications/klaviyo/consentPush.ts +19 -0
- package/src/communications/klaviyo/index.ts +649 -0
- package/src/communications/resend/index.ts +350 -0
- package/src/composition/catalogContribution.ts +32 -0
- package/src/composition/index.ts +1 -0
- package/src/inventory/adminInventory.ts +251 -0
- package/src/inventory/contracts/forecast.ts +96 -0
- package/src/inventory/contracts/index.ts +18 -0
- package/src/inventory/contracts/inventory.ts +14 -0
- package/src/inventory/contracts/newPurchaseStock.ts +23 -0
- package/src/inventory/contracts/reservation.ts +20 -0
- package/src/inventory/forecast.ts +239 -0
- package/src/inventory/index.ts +21 -0
- package/src/inventory/inventoryHandlers.ts +215 -0
- package/src/inventory/newPurchaseStockAvailability.ts +120 -0
- package/src/inventory/ports.ts +46 -0
- package/src/inventory/renewalReservation.ts +88 -0
- package/src/inventory/reservationPolicy.ts +27 -0
- package/src/inventory/schemas.ts +128 -0
- package/src/inventory/sql.ts +5 -0
- package/src/inventory/subscriptionForecastCalculation.ts +157 -0
- package/src/pricing/acquisitionFinancialPolicy.ts +26 -0
- package/src/pricing/acquisitionOfferPricing.ts +38 -0
- package/src/pricing/acquisitionOfferPricingHandler.ts +157 -0
- package/src/pricing/acquisitionOfferPricingSchemas.ts +35 -0
- package/src/pricing/acquisitionQuoteBinding.ts +12 -0
- package/src/pricing/acquisitionReadiness.ts +148 -0
- package/src/pricing/acquisitionTerms.ts +125 -0
- package/src/pricing/acquisitionTermsSchemas.ts +52 -0
- package/src/pricing/catalogFactsProvenance.ts +102 -0
- package/src/pricing/catalogFactsSchemas.ts +69 -0
- package/src/pricing/commerceOfferPricing.ts +224 -0
- package/src/pricing/commerceOfferPricingCache.ts +53 -0
- package/src/pricing/commerceOfferPricingHandler.ts +137 -0
- package/src/pricing/commercePriceAuthority.ts +104 -0
- package/src/pricing/commercePricingPolicyErrors.ts +88 -0
- package/src/pricing/commerceQuoteBatchHandler.ts +129 -0
- package/src/pricing/commerceQuoteCatalogReadPort.ts +42 -0
- package/src/pricing/commerceQuoteHandler.ts +184 -0
- package/src/pricing/commerceQuoteSnapshotExpect.ts +48 -0
- package/src/pricing/contracts/acquisitionTerms.ts +26 -0
- package/src/pricing/contracts/authority.ts +44 -0
- package/src/pricing/contracts/evidence.ts +25 -0
- package/src/pricing/contracts/index.ts +17 -0
- package/src/pricing/contracts/money.ts +32 -0
- package/src/pricing/contracts/offer.ts +42 -0
- package/src/pricing/contracts/offerPolicy.ts +17 -0
- package/src/pricing/contracts/policy.ts +26 -0
- package/src/pricing/contracts/provenance.ts +19 -0
- package/src/pricing/contracts/quote.ts +106 -0
- package/src/pricing/dbBackedCommerceQuoteHelpers.ts +256 -0
- package/src/pricing/dbBackedCommerceQuotePort.ts +260 -0
- package/src/pricing/dbBackedCommerceQuoteSnapshotVerifier.ts +49 -0
- package/src/pricing/index.ts +52 -0
- package/src/pricing/offerPolicyReadiness.ts +110 -0
- package/src/pricing/offerPolicySchemas.ts +23 -0
- package/src/pricing/offerPricingSchemas.ts +92 -0
- package/src/pricing/ports.ts +17 -0
- package/src/pricing/pricingPolicyToken.ts +119 -0
- package/src/pricing/quoteAcquisitionTerms.ts +10 -0
- package/src/pricing/quoteCodeRejectionDetails.ts +23 -0
- package/src/pricing/quotePorts.ts +58 -0
- package/src/pricing/quotePrimitives.ts +21 -0
- package/src/pricing/quotePromotionEvidence.ts +82 -0
- package/src/pricing/quoteSchemas.ts +230 -0
- package/src/pricing/subscriptionPricePolicy.ts +113 -0
- package/src/pricing/taxProfileGuard.ts +33 -0
- package/src/pricing/validation.ts +3 -0
- package/src/promotions/adminBindings.ts +53 -0
- package/src/promotions/adminPromotionCodesContracts.ts +244 -0
- package/src/promotions/adminPromotionCodesDataPort.ts +56 -0
- package/src/promotions/adminPromotionCodesHandler.ts +254 -0
- package/src/promotions/adminPromotionsContracts.ts +121 -0
- package/src/promotions/adminPromotionsDataPort.ts +48 -0
- package/src/promotions/adminPromotionsHandler.ts +72 -0
- package/src/promotions/contracts/index.ts +387 -0
- package/src/promotions/index.ts +22 -0
- package/src/promotions/promoDataPort.ts +65 -0
- package/src/promotions/promotionAcceptanceTelemetry.ts +73 -0
- package/src/promotions/promotionBenefitDefinition.ts +48 -0
- package/src/promotions/promotionClaimSweepPort.ts +23 -0
- package/src/promotions/promotionClaims.ts +70 -0
- package/src/promotions/promotionCodePreview.ts +183 -0
- package/src/promotions/promotionCodeQuotePort.ts +35 -0
- package/src/promotions/promotionQuoteAcceptance.ts +284 -0
- package/src/promotions/promotionQuoteBinding.ts +158 -0
- package/src/promotions/quoteAutomaticPromotions.ts +58 -0
- package/src/promotions/quotePromoDiscounts.ts +185 -0
- package/src/promotions/quotePromotionCodeAdjustments.ts +277 -0
- package/src/promotions/quotePromotionV2.ts +223 -0
- package/src/promotions/sql.ts +5 -0
- package/src/promotions/validation.ts +3 -0
- package/src/shipping/addressCanonLookupHandler.ts +114 -0
- package/src/shipping/addressCanonical.ts +31 -0
- package/src/shipping/addressPorts.ts +48 -0
- package/src/shipping/addressSchemas.ts +28 -0
- package/src/shipping/adminShippingRateHandler.ts +72 -0
- package/src/shipping/contracts/address.ts +71 -0
- package/src/shipping/contracts/delivery.ts +114 -0
- package/src/shipping/contracts/index.ts +18 -0
- package/src/shipping/contracts/shippingRate.ts +7 -0
- package/src/shipping/customerAddressesHandler.ts +100 -0
- package/src/shipping/customerSelfServiceAddressModels.ts +124 -0
- package/src/shipping/deliverySelectionHandler.ts +94 -0
- package/src/shipping/deliverySelectionPort.ts +10 -0
- package/src/shipping/deliverySelectionSummary.ts +56 -0
- package/src/shipping/handlerSchemas.ts +19 -0
- package/src/shipping/index.ts +23 -0
- package/src/shipping/pickupPointSearchPort.ts +8 -0
- package/src/shipping/readDeliverySelectionEvidence.ts +51 -0
- package/src/shipping/shippingRatePorts.ts +36 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
export interface InventoryProviderStockLine {
|
|
2
|
+
skuId: string;
|
|
3
|
+
sku: string;
|
|
4
|
+
forSaleQuantity: number;
|
|
5
|
+
lastSyncedAt: string;
|
|
6
|
+
staleAfter: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface InventoryHardReservationLine {
|
|
10
|
+
skuId: string;
|
|
11
|
+
quantity: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Structural inputs for dated subscription stock coverage; validation belongs to the caller. */
|
|
15
|
+
export interface SubscriptionForecastProviderStockLine {
|
|
16
|
+
skuId: string;
|
|
17
|
+
sku: string;
|
|
18
|
+
forSaleQuantity: number;
|
|
19
|
+
lastSyncedAt: string;
|
|
20
|
+
staleAfter: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SubscriptionForecastHardReservationLine {
|
|
24
|
+
skuId: string;
|
|
25
|
+
quantity: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SubscriptionForecastStockLine {
|
|
29
|
+
skuId: string;
|
|
30
|
+
sku: string;
|
|
31
|
+
fulfillable: boolean;
|
|
32
|
+
locationStatus: string;
|
|
33
|
+
safetyStock: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface SubscriptionForecastDemandLine {
|
|
37
|
+
skuId: string;
|
|
38
|
+
sku: string;
|
|
39
|
+
quantity: number;
|
|
40
|
+
dueAt: string;
|
|
41
|
+
status: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface SubscriptionForecastSupplyLine {
|
|
45
|
+
skuId: string;
|
|
46
|
+
quantity: number;
|
|
47
|
+
expectedAt: string;
|
|
48
|
+
status: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface SubscriptionForecastCalculationInput {
|
|
52
|
+
generatedAt: string;
|
|
53
|
+
horizonDays: number;
|
|
54
|
+
protectionDays: number;
|
|
55
|
+
providerCurrent: SubscriptionForecastProviderStockLine[];
|
|
56
|
+
openHardReservations: SubscriptionForecastHardReservationLine[];
|
|
57
|
+
stock: SubscriptionForecastStockLine[];
|
|
58
|
+
demand: SubscriptionForecastDemandLine[];
|
|
59
|
+
supply: SubscriptionForecastSupplyLine[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface SubscriptionForecastCounts {
|
|
63
|
+
activeSubscriptionCount: number;
|
|
64
|
+
excludedSubscriptionCount: number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface SubscriptionForecastSku {
|
|
68
|
+
skuId: string;
|
|
69
|
+
sku: string;
|
|
70
|
+
providerCurrentForSale: number | null;
|
|
71
|
+
providerStockLastSyncedAt: string | null;
|
|
72
|
+
providerStockStaleAfter: string | null;
|
|
73
|
+
stockStatus: "missing" | "stale" | "fresh";
|
|
74
|
+
openHardReservations: number;
|
|
75
|
+
safetyStock: number;
|
|
76
|
+
sellableNow: number;
|
|
77
|
+
activeSubscriptionDemand30: number;
|
|
78
|
+
activeSubscriptionDemand45: number;
|
|
79
|
+
activeSubscriptionDemand60: number;
|
|
80
|
+
plannedSupply: number;
|
|
81
|
+
coverageDays: number | null;
|
|
82
|
+
shortageDate: string | null;
|
|
83
|
+
horizonMissingQty: number;
|
|
84
|
+
protectionMissingQty: number;
|
|
85
|
+
missingQty: number;
|
|
86
|
+
recommendedProductionQty: number;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface SubscriptionForecastTotals extends SubscriptionForecastCounts {
|
|
90
|
+
missingProviderStockSkuCount: number;
|
|
91
|
+
staleProviderStockSkuCount: number;
|
|
92
|
+
horizonShortageSkuCount: number;
|
|
93
|
+
protectionShortageSkuCount: number;
|
|
94
|
+
shortageSkuCount: number;
|
|
95
|
+
recommendedProductionQty: number;
|
|
96
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
InventoryReservationStatus,
|
|
3
|
+
AdminInventoryStockRequest,
|
|
4
|
+
AdminInventoryReservationsRequest,
|
|
5
|
+
AdminInventoryStockAdjustmentRequest,
|
|
6
|
+
AdminInventorySubscriptionForecastRequest,
|
|
7
|
+
InventoryStockListResponse,
|
|
8
|
+
InventoryReservationsListResponse,
|
|
9
|
+
InventoryStockAdjustmentResponse,
|
|
10
|
+
InventorySubscriptionForecastResponse,
|
|
11
|
+
InventorySubscriptionForecastSku,
|
|
12
|
+
InventoryForecastDemandLine,
|
|
13
|
+
InventoryForecastSupplyLine,
|
|
14
|
+
InventoryForecastResponse,
|
|
15
|
+
} from "./inventory.js";
|
|
16
|
+
export type { NewPurchaseStockRow, NewPurchaseStockMode, NewPurchaseStockAvailabilityOptions } from "./newPurchaseStock.js";
|
|
17
|
+
export type { ReservationPolicyInput, ReservationPolicy, AutomaticRenewalReservationResult } from "./reservation.js";
|
|
18
|
+
export type { InventoryProviderStockLine, InventoryHardReservationLine, SubscriptionForecastCalculationInput, SubscriptionForecastCounts, SubscriptionForecastSku, SubscriptionForecastTotals } from "./forecast.js";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Inventory wire DTOs. Runtime validation belongs to the server entry. */
|
|
2
|
+
export type InventoryReservationStatus = "reserved" | "released" | "consumed" | "expired";
|
|
3
|
+
export type AdminInventoryStockRequest = { page: number; pageSize: number; includeZero: boolean; skuId?: string | undefined; locationId?: string | undefined; };
|
|
4
|
+
export type AdminInventoryReservationsRequest = { page: number; pageSize: number; status?: "reserved" | "released" | "consumed" | "expired" | undefined; orderId?: string | undefined; };
|
|
5
|
+
export type AdminInventoryStockAdjustmentRequest = { idempotencyKey: string; skuId: string; locationId: string; quantityDelta: number; reason: string; lotId?: string | null | undefined; note?: string | undefined; };
|
|
6
|
+
export type AdminInventorySubscriptionForecastRequest = { horizonDays: number; protectionDays: number; };
|
|
7
|
+
export type InventoryStockListResponse = { contractVersion: "inventory.v0"; stock: { skuId: string; sku: string; locationId: string; locationCode: string; locationKind: "internal_warehouse" | "third_party_logistics" | "supplier" | "quarantine" | "virtual"; locationStatus: "active" | "inactive"; fulfillable: boolean; lotId: string | null; lotCode: string | null; lotStatus: "expired" | "available" | "quarantined" | "recalled" | null; expiresAt: string | null; onHand: number; reserved: number; unavailable: number; incoming: number; safetyStock: number; availableNow: number; }[]; totalCount: number; page: number; pageSize: number; };
|
|
8
|
+
export type InventoryReservationsListResponse = { contractVersion: "inventory.v0"; reservations: { id: string; orderId: string; subscriptionCycleId: string | null; status: "reserved" | "released" | "consumed" | "expired"; kind: "checkout_payment_window" | "subscription_retry_window" | "manual_ops"; expiresAt: string | null; createdAt: string; }[]; totalCount: number; page: number; pageSize: number; };
|
|
9
|
+
export type InventoryStockAdjustmentResponse = { contractVersion: "inventory.v0"; replayed: boolean; };
|
|
10
|
+
export type InventorySubscriptionForecastResponse = { contractVersion: "inventory.v0"; generatedAt: string; stockMasterProvider: string; horizonDays: number; protectionDays: number; skus: { skuId: string; sku: string; providerCurrentForSale: number | null; providerStockLastSyncedAt: string | null; providerStockStaleAfter: string | null; stockStatus: "fresh" | "stale" | "missing"; openHardReservations: number; safetyStock: number; sellableNow: number; activeSubscriptionDemand30: number; activeSubscriptionDemand45: number; activeSubscriptionDemand60: number; plannedSupply: number; coverageDays: number | null; shortageDate: string | null; horizonMissingQty: number; protectionMissingQty: number; missingQty: number; recommendedProductionQty: number; }[]; totals: { activeSubscriptionCount: number; excludedSubscriptionCount: number; missingProviderStockSkuCount: number; staleProviderStockSkuCount: number; horizonShortageSkuCount: number; protectionShortageSkuCount: number; shortageSkuCount: number; recommendedProductionQty: number; }; };
|
|
11
|
+
export type InventorySubscriptionForecastSku = { skuId: string; sku: string; providerCurrentForSale: number | null; providerStockLastSyncedAt: string | null; providerStockStaleAfter: string | null; stockStatus: "fresh" | "stale" | "missing"; openHardReservations: number; safetyStock: number; sellableNow: number; activeSubscriptionDemand30: number; activeSubscriptionDemand45: number; activeSubscriptionDemand60: number; plannedSupply: number; coverageDays: number | null; shortageDate: string | null; horizonMissingQty: number; protectionMissingQty: number; missingQty: number; recommendedProductionQty: number; };
|
|
12
|
+
export type InventoryForecastDemandLine = { subscriptionId: string; subscriptionCycleId: string | null; skuId: string; sku: string; quantity: number; dueAt: string; status: "planned" | "payment_pending" | "paid" | "retry_scheduled" | "skipped" | "cancelled"; };
|
|
13
|
+
export type InventoryForecastSupplyLine = { skuId: string; quantity: number; expectedAt: string; status: "planned" | "cancelled" | "received"; };
|
|
14
|
+
export type InventoryForecastResponse = { contractVersion: "inventory.v0"; shortages: { skuId: string; sku: string; dueAt: string; demandQuantity: number; availableOrExpectedQuantity: number; missingQuantity: number; }[]; };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface NewPurchaseStockRow {
|
|
2
|
+
sku: string;
|
|
3
|
+
catalogSkuId: string | null;
|
|
4
|
+
source: "provider" | "local" | "missing";
|
|
5
|
+
providerForSale: number | null;
|
|
6
|
+
activeHolds: number;
|
|
7
|
+
safetyStock: number;
|
|
8
|
+
available: number | null;
|
|
9
|
+
stale: boolean;
|
|
10
|
+
renewalDemand: number;
|
|
11
|
+
availableForNewPurchases: number | null;
|
|
12
|
+
/** Holds of the excluded order left out of `activeHolds`; 0 when no order is excluded. */
|
|
13
|
+
excludedOrderHolds: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type NewPurchaseStockMode = "new_purchase" | "renewal";
|
|
17
|
+
|
|
18
|
+
export interface NewPurchaseStockAvailabilityOptions {
|
|
19
|
+
mode: NewPurchaseStockMode;
|
|
20
|
+
lowStockThreshold?: number;
|
|
21
|
+
horizonDays?: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface ReservationPolicyInput {
|
|
2
|
+
reservationKind: string;
|
|
3
|
+
paymentTargetKind: "one_time_order" | "subscription_cycle" | string;
|
|
4
|
+
orderMode: "one_time" | "subscription_cycle" | string;
|
|
5
|
+
now: Date;
|
|
6
|
+
checkoutTtlMinutes?: number | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ReservationPolicy {
|
|
10
|
+
expiresAt: string | null;
|
|
11
|
+
ttlMinutes: number | null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface AutomaticRenewalReservationResult {
|
|
15
|
+
reserved: boolean;
|
|
16
|
+
replayed: boolean;
|
|
17
|
+
reservationId: string | null;
|
|
18
|
+
status: "held" | "committed" | "consumed" | "released" | "expired" | "refused";
|
|
19
|
+
reason: string | null;
|
|
20
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { INVENTORY_CONTRACT_VERSION, availableNow, type InventoryStockLine } from "@openlup/core/inventory";
|
|
3
|
+
import type { InventoryForecastDemandLine, InventoryForecastResponse, InventoryForecastSupplyLine, InventorySubscriptionForecastResponse } from "./contracts/index.js";
|
|
4
|
+
import type { InventoryProviderStockLine, InventoryHardReservationLine } from "./contracts/forecast.js";
|
|
5
|
+
import { createInventorySubscriptionForecastResponseSchema } from "./schemas.js";
|
|
6
|
+
|
|
7
|
+
const uuidSchema = z.guid();
|
|
8
|
+
const datetimeSchema = z.string().datetime({ offset: true });
|
|
9
|
+
const quantitySchema = z.number().int().nonnegative();
|
|
10
|
+
const positiveQuantitySchema = z.number().int().positive();
|
|
11
|
+
|
|
12
|
+
const inventoryForecastShortageSchema = z.object({
|
|
13
|
+
skuId: uuidSchema,
|
|
14
|
+
sku: z.string().trim().min(1),
|
|
15
|
+
dueAt: datetimeSchema,
|
|
16
|
+
demandQuantity: positiveQuantitySchema,
|
|
17
|
+
availableOrExpectedQuantity: quantitySchema,
|
|
18
|
+
missingQuantity: positiveQuantitySchema,
|
|
19
|
+
}).strict();
|
|
20
|
+
|
|
21
|
+
const inventoryForecastResponseSchema = z.object({
|
|
22
|
+
contractVersion: z.literal(INVENTORY_CONTRACT_VERSION),
|
|
23
|
+
shortages: z.array(inventoryForecastShortageSchema),
|
|
24
|
+
}).strict();
|
|
25
|
+
|
|
26
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
27
|
+
|
|
28
|
+
export function forecastSubscriptionInventoryShortages(input: {
|
|
29
|
+
demand: InventoryForecastDemandLine[];
|
|
30
|
+
supply: InventoryForecastSupplyLine[];
|
|
31
|
+
stock: InventoryStockLine[];
|
|
32
|
+
}): InventoryForecastResponse {
|
|
33
|
+
const demand = input.demand
|
|
34
|
+
.filter((line) => !["skipped", "cancelled"].includes(line.status))
|
|
35
|
+
.sort((a, b) => Date.parse(a.dueAt) - Date.parse(b.dueAt));
|
|
36
|
+
const runningAvailable = input.stock.reduce<Record<string, number>>((available, line) => {
|
|
37
|
+
available[line.skuId] = (available[line.skuId] ?? 0) + availableNow(line);
|
|
38
|
+
return available;
|
|
39
|
+
}, {});
|
|
40
|
+
const plannedSupply = input.supply
|
|
41
|
+
.filter((line) => line.status === "planned")
|
|
42
|
+
.sort((a, b) => Date.parse(a.expectedAt) - Date.parse(b.expectedAt));
|
|
43
|
+
const usedSupplyIndexes = new Set<number>();
|
|
44
|
+
|
|
45
|
+
const shortages = demand.flatMap((line) => {
|
|
46
|
+
for (const [index, supply] of plannedSupply.entries()) {
|
|
47
|
+
if (usedSupplyIndexes.has(index)) continue;
|
|
48
|
+
if (supply.skuId !== line.skuId) continue;
|
|
49
|
+
if (Date.parse(supply.expectedAt) > Date.parse(line.dueAt)) continue;
|
|
50
|
+
runningAvailable[line.skuId] = (runningAvailable[line.skuId] ?? 0) + supply.quantity;
|
|
51
|
+
usedSupplyIndexes.add(index);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const available = runningAvailable[line.skuId] ?? 0;
|
|
55
|
+
if (available >= line.quantity) {
|
|
56
|
+
runningAvailable[line.skuId] = available - line.quantity;
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
runningAvailable[line.skuId] = 0;
|
|
61
|
+
return [{
|
|
62
|
+
skuId: line.skuId,
|
|
63
|
+
sku: line.sku,
|
|
64
|
+
dueAt: line.dueAt,
|
|
65
|
+
demandQuantity: line.quantity,
|
|
66
|
+
availableOrExpectedQuantity: available,
|
|
67
|
+
missingQuantity: line.quantity - available,
|
|
68
|
+
}];
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return inventoryForecastResponseSchema.parse({
|
|
72
|
+
contractVersion: INVENTORY_CONTRACT_VERSION,
|
|
73
|
+
shortages,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function buildSubscriptionInventoryForecast(input: {
|
|
78
|
+
generatedAt: string;
|
|
79
|
+
horizonDays: number;
|
|
80
|
+
protectionDays: number;
|
|
81
|
+
providerCurrent: InventoryProviderStockLine[];
|
|
82
|
+
openHardReservations: InventoryHardReservationLine[];
|
|
83
|
+
stock: InventoryStockLine[];
|
|
84
|
+
demand: InventoryForecastDemandLine[];
|
|
85
|
+
supply: InventoryForecastSupplyLine[];
|
|
86
|
+
activeSubscriptionCount: number;
|
|
87
|
+
excludedSubscriptionCount: number;
|
|
88
|
+
}, config: { stockMasterProvider: string; providerSchema: z.ZodType<string> }): InventorySubscriptionForecastResponse {
|
|
89
|
+
const inventorySubscriptionForecastResponseSchema = createInventorySubscriptionForecastResponseSchema(config.providerSchema);
|
|
90
|
+
const generatedAtMs = Date.parse(input.generatedAt);
|
|
91
|
+
const horizonAtMs = generatedAtMs + input.horizonDays * DAY_MS;
|
|
92
|
+
const protectionAtMs = generatedAtMs + input.protectionDays * DAY_MS;
|
|
93
|
+
const demand = input.demand
|
|
94
|
+
.filter((line) => !["skipped", "cancelled"].includes(line.status))
|
|
95
|
+
.filter((line) => Date.parse(line.dueAt) <= horizonAtMs);
|
|
96
|
+
const plannedSupply = input.supply
|
|
97
|
+
.filter((line) => line.status === "planned")
|
|
98
|
+
.filter((line) => Date.parse(line.expectedAt) <= horizonAtMs);
|
|
99
|
+
|
|
100
|
+
const providerBySku = new Map(input.providerCurrent.map((line) => [line.skuId, line]));
|
|
101
|
+
const skuIds = new Set<string>([
|
|
102
|
+
...input.providerCurrent.map((line) => line.skuId),
|
|
103
|
+
...input.openHardReservations.map((line) => line.skuId),
|
|
104
|
+
...input.stock.map((line) => line.skuId),
|
|
105
|
+
...demand.map((line) => line.skuId),
|
|
106
|
+
...plannedSupply.map((line) => line.skuId),
|
|
107
|
+
]);
|
|
108
|
+
|
|
109
|
+
const skus = [...skuIds].map((skuId) => {
|
|
110
|
+
const provider = providerBySku.get(skuId) ?? null;
|
|
111
|
+
const stockLines = input.stock.filter((line) => line.skuId === skuId);
|
|
112
|
+
const demandLines = demand.filter((line) => line.skuId === skuId);
|
|
113
|
+
const supplyLines = plannedSupply.filter((line) => line.skuId === skuId);
|
|
114
|
+
const sku = provider?.sku ?? stockLines[0]?.sku ?? demandLines[0]?.sku ?? "unknown";
|
|
115
|
+
const stockStatus = provider === null
|
|
116
|
+
? "missing"
|
|
117
|
+
: Date.parse(provider.staleAfter) <= generatedAtMs
|
|
118
|
+
? "stale"
|
|
119
|
+
: "fresh";
|
|
120
|
+
const openHardReservations = sum(input.openHardReservations
|
|
121
|
+
.filter((line) => line.skuId === skuId)
|
|
122
|
+
.map((line) => line.quantity));
|
|
123
|
+
const safetyStock = sum(stockLines
|
|
124
|
+
.filter((line) => line.fulfillable && line.locationStatus === "active")
|
|
125
|
+
.map((line) => line.safetyStock));
|
|
126
|
+
const sellableNow = stockStatus === "fresh" && provider
|
|
127
|
+
? Math.max(0, provider.forSaleQuantity - openHardReservations - safetyStock)
|
|
128
|
+
: 0;
|
|
129
|
+
const shortage = simulateSubscriptionCoverage({
|
|
130
|
+
generatedAtMs,
|
|
131
|
+
protectionAtMs,
|
|
132
|
+
sellableNow,
|
|
133
|
+
demand: demandLines,
|
|
134
|
+
supply: supplyLines,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
skuId,
|
|
139
|
+
sku,
|
|
140
|
+
providerCurrentForSale: provider?.forSaleQuantity ?? null,
|
|
141
|
+
providerStockLastSyncedAt: provider?.lastSyncedAt ?? null,
|
|
142
|
+
providerStockStaleAfter: provider?.staleAfter ?? null,
|
|
143
|
+
stockStatus,
|
|
144
|
+
openHardReservations,
|
|
145
|
+
safetyStock,
|
|
146
|
+
sellableNow,
|
|
147
|
+
activeSubscriptionDemand30: demandQuantityWithin(demandLines, generatedAtMs + 30 * DAY_MS),
|
|
148
|
+
activeSubscriptionDemand45: demandQuantityWithin(demandLines, generatedAtMs + 45 * DAY_MS),
|
|
149
|
+
activeSubscriptionDemand60: demandQuantityWithin(demandLines, generatedAtMs + 60 * DAY_MS),
|
|
150
|
+
plannedSupply: sum(supplyLines.map((line) => line.quantity)),
|
|
151
|
+
coverageDays: shortage.shortageDate
|
|
152
|
+
? Math.max(0, Math.ceil((Date.parse(shortage.shortageDate) - generatedAtMs) / DAY_MS))
|
|
153
|
+
: null,
|
|
154
|
+
shortageDate: shortage.shortageDate,
|
|
155
|
+
horizonMissingQty: shortage.horizonMissingQty,
|
|
156
|
+
protectionMissingQty: shortage.protectionMissingQty,
|
|
157
|
+
missingQty: shortage.protectionMissingQty,
|
|
158
|
+
recommendedProductionQty: shortage.protectionMissingQty,
|
|
159
|
+
};
|
|
160
|
+
}).sort((a, b) => a.sku.localeCompare(b.sku));
|
|
161
|
+
|
|
162
|
+
return inventorySubscriptionForecastResponseSchema.parse({
|
|
163
|
+
contractVersion: INVENTORY_CONTRACT_VERSION,
|
|
164
|
+
generatedAt: input.generatedAt,
|
|
165
|
+
stockMasterProvider: config.stockMasterProvider,
|
|
166
|
+
horizonDays: input.horizonDays,
|
|
167
|
+
protectionDays: input.protectionDays,
|
|
168
|
+
skus,
|
|
169
|
+
totals: {
|
|
170
|
+
activeSubscriptionCount: input.activeSubscriptionCount,
|
|
171
|
+
excludedSubscriptionCount: input.excludedSubscriptionCount,
|
|
172
|
+
missingProviderStockSkuCount: skus.filter((line) => line.stockStatus === "missing").length,
|
|
173
|
+
staleProviderStockSkuCount: skus.filter((line) => line.stockStatus === "stale").length,
|
|
174
|
+
horizonShortageSkuCount: skus.filter((line) => line.shortageDate !== null).length,
|
|
175
|
+
protectionShortageSkuCount: skus.filter((line) => line.protectionMissingQty > 0).length,
|
|
176
|
+
shortageSkuCount: skus.filter((line) => line.shortageDate !== null).length,
|
|
177
|
+
recommendedProductionQty: sum(skus.map((line) => line.recommendedProductionQty)),
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function simulateSubscriptionCoverage(input: {
|
|
183
|
+
generatedAtMs: number;
|
|
184
|
+
protectionAtMs: number;
|
|
185
|
+
sellableNow: number;
|
|
186
|
+
demand: InventoryForecastDemandLine[];
|
|
187
|
+
supply: InventoryForecastSupplyLine[];
|
|
188
|
+
}): { shortageDate: string | null; horizonMissingQty: number; protectionMissingQty: number } {
|
|
189
|
+
let available = input.sellableNow;
|
|
190
|
+
let shortageDate: string | null = null;
|
|
191
|
+
let horizonMissingQty = 0;
|
|
192
|
+
let protectionMissingQty = 0;
|
|
193
|
+
const events = [
|
|
194
|
+
...input.supply.map((line) => ({
|
|
195
|
+
type: "supply" as const,
|
|
196
|
+
at: line.expectedAt,
|
|
197
|
+
quantity: line.quantity,
|
|
198
|
+
})),
|
|
199
|
+
...input.demand.map((line) => ({
|
|
200
|
+
type: "demand" as const,
|
|
201
|
+
at: line.dueAt,
|
|
202
|
+
quantity: line.quantity,
|
|
203
|
+
})),
|
|
204
|
+
].sort((a, b) => {
|
|
205
|
+
const dateDelta = Date.parse(a.at) - Date.parse(b.at);
|
|
206
|
+
if (dateDelta !== 0) return dateDelta;
|
|
207
|
+
return a.type === "supply" && b.type === "demand" ? -1 : 1;
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
for (const event of events) {
|
|
211
|
+
if (event.type === "supply") {
|
|
212
|
+
available += event.quantity;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (available >= event.quantity) {
|
|
217
|
+
available -= event.quantity;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const missing = event.quantity - available;
|
|
222
|
+
available = 0;
|
|
223
|
+
shortageDate ??= event.at;
|
|
224
|
+
horizonMissingQty += missing;
|
|
225
|
+
if (Date.parse(event.at) <= input.protectionAtMs) protectionMissingQty += missing;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return { shortageDate, horizonMissingQty, protectionMissingQty };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function demandQuantityWithin(lines: InventoryForecastDemandLine[], dueAtOrBeforeMs: number): number {
|
|
232
|
+
return sum(lines
|
|
233
|
+
.filter((line) => Date.parse(line.dueAt) <= dueAtOrBeforeMs)
|
|
234
|
+
.map((line) => line.quantity));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function sum(values: number[]): number {
|
|
238
|
+
return values.reduce((total, value) => total + value, 0);
|
|
239
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type { InventoryReadPort, InventoryMutationPort } from "./ports.js";
|
|
2
|
+
export { InventoryPersistenceError, InventoryConflictError } from "./ports.js";
|
|
3
|
+
export {
|
|
4
|
+
adminInventoryStockRequestSchema,
|
|
5
|
+
adminInventoryReservationsRequestSchema,
|
|
6
|
+
adminInventoryStockAdjustmentRequestSchema,
|
|
7
|
+
adminInventorySubscriptionForecastRequestSchema,
|
|
8
|
+
inventoryStockListResponseSchema,
|
|
9
|
+
inventoryReservationsListResponseSchema,
|
|
10
|
+
createInventorySubscriptionForecastResponseSchema,
|
|
11
|
+
} from "./schemas.js";
|
|
12
|
+
export { createAdminInventoryStockHandler, createAdminInventoryReservationsHandler, createAdminInventoryAtpCheckHandler, createAdminInventorySubscriptionForecastHandler, createAdminInventoryStockAdjustmentHandler } from "./inventoryHandlers.js";
|
|
13
|
+
export { createPostgresAdminInventoryPort } from "./adminInventory.js";
|
|
14
|
+
export type { NewPurchaseStockRow, NewPurchaseStockMode, NewPurchaseStockAvailabilityOptions } from "./contracts/newPurchaseStock.js";
|
|
15
|
+
export type { NewPurchaseStockReadPort } from "./newPurchaseStockAvailability.js";
|
|
16
|
+
export { createNewPurchaseStockAvailabilityPort } from "./newPurchaseStockAvailability.js";
|
|
17
|
+
export { MIN_CHECKOUT_RESERVATION_TTL_MINUTES, MAX_CHECKOUT_RESERVATION_TTL_MINUTES, DEFAULT_CHECKOUT_RESERVATION_TTL_MINUTES, reservationPolicyFor, normalizedTtlMinutes } from "./reservationPolicy.js";
|
|
18
|
+
export { createPostgresRenewalReservationPort, type AutomaticRenewalInventoryPort } from "./renewalReservation.js";
|
|
19
|
+
export { forecastSubscriptionInventoryShortages, buildSubscriptionInventoryForecast } from "./forecast.js";
|
|
20
|
+
export { calculateSubscriptionForecastSkus, calculateSubscriptionForecastTotals } from "./subscriptionForecastCalculation.js";
|
|
21
|
+
export { INVENTORY_MIGRATIONS } from "./sql.js";
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import type { z } from "zod";
|
|
3
|
+
import { inventoryAtpRequestSchema, inventoryAtpResultSchema } from "@openlup/core/inventory";
|
|
4
|
+
import {
|
|
5
|
+
adminInventoryReservationsRequestSchema,
|
|
6
|
+
adminInventoryStockAdjustmentRequestSchema,
|
|
7
|
+
adminInventoryStockRequestSchema,
|
|
8
|
+
adminInventorySubscriptionForecastRequestSchema,
|
|
9
|
+
inventoryReservationsListResponseSchema,
|
|
10
|
+
inventoryStockListResponseSchema,
|
|
11
|
+
} from "./schemas.js";
|
|
12
|
+
import { InventoryConflictError, type InventoryMutationPort, type InventoryReadPort } from "./ports.js";
|
|
13
|
+
import type { InventorySubscriptionForecastResponse } from "./contracts/index.js";
|
|
14
|
+
|
|
15
|
+
// Structural host bindings keep response context and HTTP composition with the host.
|
|
16
|
+
interface HttpRequest extends IncomingMessage {
|
|
17
|
+
query: Record<string, string | string[] | undefined>;
|
|
18
|
+
cookies?: Record<string, string>;
|
|
19
|
+
body?: unknown;
|
|
20
|
+
}
|
|
21
|
+
interface HttpResponse extends ServerResponse {
|
|
22
|
+
status(code: number): this;
|
|
23
|
+
json(body: unknown): this;
|
|
24
|
+
send(body: string | Buffer | object): this;
|
|
25
|
+
redirect(url: string): this;
|
|
26
|
+
redirect(status: number, url: string): this;
|
|
27
|
+
}
|
|
28
|
+
interface ResponseBindings {
|
|
29
|
+
sendBffSuccess(res: HttpResponse, data: unknown): void;
|
|
30
|
+
sendBffError(res: HttpResponse, code: "UNAUTHORIZED" | "FORBIDDEN" | "BAD_REQUEST" | "INVALID_RESPONSE" | "UPSTREAM_UNAVAILABLE" | "CONFLICT", message: string, options?: { details?: unknown }): void;
|
|
31
|
+
sendMethodNotAllowed(res: HttpResponse, methods: readonly string[]): void;
|
|
32
|
+
}
|
|
33
|
+
type AdminAuthResult =
|
|
34
|
+
| { ok: false; code: "UNAUTHORIZED" | "FORBIDDEN"; message: string }
|
|
35
|
+
| { ok: true; userId: string };
|
|
36
|
+
|
|
37
|
+
interface BaseDeps extends ResponseBindings {
|
|
38
|
+
authorizeAdmin: (req: HttpRequest) => Promise<AdminAuthResult>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createAdminInventoryStockHandler({
|
|
42
|
+
authorizeAdmin,
|
|
43
|
+
sendBffError, sendBffSuccess, sendMethodNotAllowed,
|
|
44
|
+
inventoryPort,
|
|
45
|
+
}: BaseDeps & { inventoryPort: Pick<InventoryReadPort, "listStock"> }) {
|
|
46
|
+
return async function handler(req: HttpRequest, res: HttpResponse): Promise<void> {
|
|
47
|
+
if (req.method !== "GET") return sendMethodNotAllowed(res, ["GET"]);
|
|
48
|
+
if (!(await authorize(req, res, authorizeAdmin, sendBffError))) return;
|
|
49
|
+
|
|
50
|
+
const request = adminInventoryStockRequestSchema.safeParse(req.query);
|
|
51
|
+
if (!request.success) {
|
|
52
|
+
sendBffError(res, "BAD_REQUEST", "Invalid admin inventory stock request", {
|
|
53
|
+
details: request.error.flatten(),
|
|
54
|
+
});
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
const result = await inventoryPort.listStock(request.data);
|
|
60
|
+
const response = inventoryStockListResponseSchema.safeParse(result);
|
|
61
|
+
if (!response.success) return sendBffError(res, "INVALID_RESPONSE", "Inventory stock response invalid");
|
|
62
|
+
sendBffSuccess(res, response.data);
|
|
63
|
+
} catch {
|
|
64
|
+
sendBffError(res, "UPSTREAM_UNAVAILABLE", "Inventory stock read failed");
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function createAdminInventoryReservationsHandler({
|
|
70
|
+
authorizeAdmin,
|
|
71
|
+
sendBffError, sendBffSuccess, sendMethodNotAllowed,
|
|
72
|
+
inventoryPort,
|
|
73
|
+
}: BaseDeps & { inventoryPort: Pick<InventoryReadPort, "listReservations"> }) {
|
|
74
|
+
return async function handler(req: HttpRequest, res: HttpResponse): Promise<void> {
|
|
75
|
+
if (req.method !== "GET") return sendMethodNotAllowed(res, ["GET"]);
|
|
76
|
+
if (!(await authorize(req, res, authorizeAdmin, sendBffError))) return;
|
|
77
|
+
|
|
78
|
+
const request = adminInventoryReservationsRequestSchema.safeParse(req.query);
|
|
79
|
+
if (!request.success) {
|
|
80
|
+
sendBffError(res, "BAD_REQUEST", "Invalid admin inventory reservations request", {
|
|
81
|
+
details: request.error.flatten(),
|
|
82
|
+
});
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
const result = await inventoryPort.listReservations(request.data);
|
|
88
|
+
const response = inventoryReservationsListResponseSchema.safeParse(result);
|
|
89
|
+
if (!response.success) return sendBffError(res, "INVALID_RESPONSE", "Inventory reservations response invalid");
|
|
90
|
+
sendBffSuccess(res, response.data);
|
|
91
|
+
} catch {
|
|
92
|
+
sendBffError(res, "UPSTREAM_UNAVAILABLE", "Inventory reservations read failed");
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function createAdminInventoryAtpCheckHandler({
|
|
98
|
+
authorizeAdmin,
|
|
99
|
+
sendBffError, sendBffSuccess, sendMethodNotAllowed,
|
|
100
|
+
inventoryPort,
|
|
101
|
+
configuredRegion,
|
|
102
|
+
}: BaseDeps & { inventoryPort: Pick<InventoryReadPort, "checkAtp">; configuredRegion?: string }) {
|
|
103
|
+
return async function handler(req: HttpRequest, res: HttpResponse): Promise<void> {
|
|
104
|
+
if (req.method !== "POST") return sendMethodNotAllowed(res, ["POST"]);
|
|
105
|
+
if (!(await authorize(req, res, authorizeAdmin, sendBffError))) return;
|
|
106
|
+
|
|
107
|
+
const request = inventoryAtpRequestSchema.safeParse(req.body ?? {});
|
|
108
|
+
if (!request.success) {
|
|
109
|
+
sendBffError(res, "BAD_REQUEST", "Invalid admin inventory ATP request", {
|
|
110
|
+
details: request.error.flatten(),
|
|
111
|
+
});
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
const result = await inventoryPort.checkAtp({
|
|
117
|
+
...request.data,
|
|
118
|
+
region: request.data.region ?? configuredRegion,
|
|
119
|
+
});
|
|
120
|
+
const response = inventoryAtpResultSchema.safeParse(result);
|
|
121
|
+
if (!response.success) return sendBffError(res, "INVALID_RESPONSE", "Inventory ATP response invalid");
|
|
122
|
+
sendBffSuccess(res, response.data);
|
|
123
|
+
} catch {
|
|
124
|
+
sendBffError(res, "UPSTREAM_UNAVAILABLE", "Inventory ATP check failed");
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function createAdminInventorySubscriptionForecastHandler({
|
|
130
|
+
inventorySubscriptionForecastResponseSchema,
|
|
131
|
+
authorizeAdmin,
|
|
132
|
+
sendBffError, sendBffSuccess, sendMethodNotAllowed,
|
|
133
|
+
inventoryPort,
|
|
134
|
+
}: BaseDeps & { inventoryPort: Pick<InventoryReadPort, "forecastSubscriptions">; inventorySubscriptionForecastResponseSchema: z.ZodType<InventorySubscriptionForecastResponse> }) {
|
|
135
|
+
return async function handler(req: HttpRequest, res: HttpResponse): Promise<void> {
|
|
136
|
+
if (req.method !== "GET") return sendMethodNotAllowed(res, ["GET"]);
|
|
137
|
+
if (!(await authorize(req, res, authorizeAdmin, sendBffError))) return;
|
|
138
|
+
|
|
139
|
+
const request = adminInventorySubscriptionForecastRequestSchema.safeParse(req.query);
|
|
140
|
+
if (!request.success) {
|
|
141
|
+
sendBffError(res, "BAD_REQUEST", "Invalid admin inventory subscription forecast request", {
|
|
142
|
+
details: request.error.flatten(),
|
|
143
|
+
});
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
const result = await inventoryPort.forecastSubscriptions(request.data);
|
|
149
|
+
const response = inventorySubscriptionForecastResponseSchema.safeParse(result);
|
|
150
|
+
if (!response.success) {
|
|
151
|
+
return sendBffError(res, "INVALID_RESPONSE", "Inventory subscription forecast response invalid");
|
|
152
|
+
}
|
|
153
|
+
sendBffSuccess(res, response.data);
|
|
154
|
+
} catch {
|
|
155
|
+
sendBffError(res, "UPSTREAM_UNAVAILABLE", "Inventory subscription forecast read failed");
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function createAdminInventoryStockAdjustmentHandler({
|
|
161
|
+
authorizeAdmin,
|
|
162
|
+
sendBffError, sendBffSuccess, sendMethodNotAllowed,
|
|
163
|
+
inventoryPort,
|
|
164
|
+
mutationsEnabled,
|
|
165
|
+
}: BaseDeps & {
|
|
166
|
+
inventoryPort: Pick<InventoryMutationPort, "adjustStock">;
|
|
167
|
+
mutationsEnabled: () => boolean;
|
|
168
|
+
}) {
|
|
169
|
+
return async function handler(req: HttpRequest, res: HttpResponse): Promise<void> {
|
|
170
|
+
if (req.method !== "POST") return sendMethodNotAllowed(res, ["POST"]);
|
|
171
|
+
const auth = await authorize(req, res, authorizeAdmin, sendBffError);
|
|
172
|
+
if (!auth) return;
|
|
173
|
+
if (!mutationsEnabled()) {
|
|
174
|
+
sendBffError(res, "FORBIDDEN", "Inventory mutations are not enabled");
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const request = adminInventoryStockAdjustmentRequestSchema.safeParse(req.body ?? {});
|
|
179
|
+
if (!request.success) {
|
|
180
|
+
sendBffError(res, "BAD_REQUEST", "Invalid admin inventory stock adjustment request", {
|
|
181
|
+
details: request.error.flatten(),
|
|
182
|
+
});
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
sendBffSuccess(res, await inventoryPort.adjustStock({ ...request.data, actorUserId: auth.userId }));
|
|
188
|
+
} catch (error) {
|
|
189
|
+
if (error instanceof InventoryConflictError) {
|
|
190
|
+
sendBffError(res, "CONFLICT", error.message, { details: error.details });
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
sendBffError(res, "UPSTREAM_UNAVAILABLE", "Inventory stock adjustment failed");
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async function authorize(
|
|
199
|
+
req: HttpRequest,
|
|
200
|
+
res: HttpResponse,
|
|
201
|
+
authorizeAdmin: (req: HttpRequest) => Promise<AdminAuthResult>,
|
|
202
|
+
sendBffError: ResponseBindings["sendBffError"],
|
|
203
|
+
): Promise<{ userId: string } | null> {
|
|
204
|
+
try {
|
|
205
|
+
const authorization = await authorizeAdmin(req);
|
|
206
|
+
if (authorization.ok === false) {
|
|
207
|
+
sendBffError(res, authorization.code, authorization.message);
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
return { userId: authorization.userId };
|
|
211
|
+
} catch {
|
|
212
|
+
sendBffError(res, "UPSTREAM_UNAVAILABLE", "Admin authorization failed");
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
}
|