@medusajs/medusa 1.2.2-dev.56 → 1.3.1-alpha.72
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 +98 -0
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.js +1 -0
- package/dist/api/index.js.map +1 -1
- package/dist/api/middlewares/batch-job/can-access-batch-job.d.ts +1 -0
- package/dist/api/{routes/admin/discounts/add-valid-product.js → middlewares/batch-job/can-access-batch-job.js} +31 -43
- package/dist/api/middlewares/batch-job/can-access-batch-job.js.map +1 -0
- package/dist/api/middlewares/batch-job/get-requested-batch-job.d.ts +1 -0
- package/dist/api/{routes/admin/products/get-variants.js → middlewares/batch-job/get-requested-batch-job.js} +28 -37
- package/dist/api/middlewares/batch-job/get-requested-batch-job.js.map +1 -0
- package/dist/api/middlewares/error-handler.js +1 -1
- package/dist/api/middlewares/index.d.ts +2 -0
- package/dist/api/middlewares/index.js +5 -0
- package/dist/api/middlewares/index.js.map +1 -1
- package/dist/api/routes/admin/auth/create-session.js +5 -5
- package/dist/api/routes/admin/auth/create-session.js.map +1 -1
- package/dist/api/routes/admin/batch/index.d.ts +13 -0
- package/dist/api/routes/admin/batch/index.js +37 -0
- package/dist/api/routes/admin/batch/index.js.map +1 -0
- package/dist/api/routes/admin/batch/list-batch-jobs.d.ts +47 -0
- package/dist/api/routes/admin/batch/list-batch-jobs.js +245 -0
- package/dist/api/routes/admin/batch/list-batch-jobs.js.map +1 -0
- package/dist/api/routes/admin/customer-groups/list-customer-groups.js +2 -2
- package/dist/api/routes/admin/customer-groups/list-customer-groups.js.map +1 -1
- package/dist/api/routes/admin/discounts/create-condition.d.ts +58 -0
- package/dist/api/routes/admin/discounts/create-condition.js +186 -0
- package/dist/api/routes/admin/discounts/create-condition.js.map +1 -0
- package/dist/api/routes/admin/discounts/create-discount.d.ts +14 -4
- package/dist/api/routes/admin/discounts/create-discount.js +72 -16
- package/dist/api/routes/admin/discounts/create-discount.js.map +1 -1
- package/dist/api/routes/admin/discounts/create-dynamic-code.d.ts +1 -0
- package/dist/api/routes/admin/discounts/create-dynamic-code.js +4 -1
- package/dist/api/routes/admin/discounts/create-dynamic-code.js.map +1 -1
- package/dist/api/routes/admin/discounts/delete-condition.d.ts +36 -0
- package/dist/api/routes/admin/discounts/delete-condition.js +151 -0
- package/dist/api/routes/admin/discounts/delete-condition.js.map +1 -0
- package/dist/api/routes/admin/discounts/delete-dynamic-code.js +3 -1
- package/dist/api/routes/admin/discounts/delete-dynamic-code.js.map +1 -1
- package/dist/api/routes/admin/discounts/get-condition.d.ts +31 -0
- package/dist/api/routes/{store/carts/update-payment-method.js → admin/discounts/get-condition.js} +48 -45
- package/dist/api/routes/admin/discounts/get-condition.js.map +1 -0
- package/dist/api/routes/admin/discounts/get-discount-by-code.d.ts +4 -0
- package/dist/api/routes/admin/discounts/get-discount-by-code.js +40 -6
- package/dist/api/routes/admin/discounts/get-discount-by-code.js.map +1 -1
- package/dist/api/routes/admin/discounts/get-discount.d.ts +7 -0
- package/dist/api/routes/admin/discounts/get-discount.js +43 -9
- package/dist/api/routes/admin/discounts/get-discount.js.map +1 -1
- package/dist/api/routes/admin/discounts/index.d.ts +11 -3
- package/dist/api/routes/admin/discounts/index.js +13 -7
- package/dist/api/routes/admin/discounts/index.js.map +1 -1
- package/dist/api/routes/admin/discounts/list-discounts.d.ts +2 -6
- package/dist/api/routes/admin/discounts/list-discounts.js +15 -28
- package/dist/api/routes/admin/discounts/list-discounts.js.map +1 -1
- package/dist/api/routes/admin/discounts/update-condition.d.ts +55 -0
- package/dist/api/routes/admin/discounts/update-condition.js +184 -0
- package/dist/api/routes/admin/discounts/update-condition.js.map +1 -0
- package/dist/api/routes/admin/discounts/update-discount.d.ts +18 -12
- package/dist/api/routes/admin/discounts/update-discount.js +79 -33
- package/dist/api/routes/admin/discounts/update-discount.js.map +1 -1
- package/dist/api/routes/admin/draft-orders/create-draft-order.d.ts +3 -3
- package/dist/api/routes/admin/draft-orders/create-draft-order.js.map +1 -1
- package/dist/api/routes/admin/draft-orders/create-line-item.d.ts +1 -1
- package/dist/api/routes/admin/draft-orders/create-line-item.js.map +1 -1
- package/dist/api/routes/admin/draft-orders/index.js +7 -1
- package/dist/api/routes/admin/draft-orders/index.js.map +1 -1
- package/dist/api/routes/admin/draft-orders/register-payment.js +0 -1
- package/dist/api/routes/admin/draft-orders/register-payment.js.map +1 -1
- package/dist/api/routes/admin/draft-orders/update-line-item.d.ts +1 -1
- package/dist/api/routes/admin/draft-orders/update-line-item.js.map +1 -1
- package/dist/api/routes/admin/index.js +2 -0
- package/dist/api/routes/admin/index.js.map +1 -1
- package/dist/api/routes/admin/notifications/index.js.map +1 -1
- package/dist/api/routes/admin/notifications/list-notifications.js.map +1 -1
- package/dist/api/routes/admin/notifications/resend-notification.js.map +1 -1
- package/dist/api/routes/admin/orders/add-shipping-method.d.ts +1 -1
- package/dist/api/routes/admin/orders/add-shipping-method.js.map +1 -1
- package/dist/api/routes/admin/orders/create-claim.js +6 -0
- package/dist/api/routes/admin/orders/create-claim.js.map +1 -1
- package/dist/api/routes/admin/orders/create-order.js.map +1 -1
- package/dist/api/routes/admin/orders/create-swap.js +7 -3
- package/dist/api/routes/admin/orders/create-swap.js.map +1 -1
- package/dist/api/routes/admin/orders/index.d.ts +1 -1
- package/dist/api/routes/admin/orders/index.js +1 -3
- package/dist/api/routes/admin/orders/index.js.map +1 -1
- package/dist/api/routes/admin/orders/list-orders.js.map +1 -1
- package/dist/api/routes/admin/orders/update-order.js.map +1 -1
- package/dist/api/routes/admin/price-lists/delete-product-prices.d.ts +32 -0
- package/dist/api/routes/admin/{discounts/remove-valid-product.js → price-lists/delete-product-prices.js} +48 -25
- package/dist/api/routes/admin/price-lists/delete-product-prices.js.map +1 -0
- package/dist/api/routes/admin/price-lists/delete-variant-prices.d.ts +32 -0
- package/dist/api/routes/admin/price-lists/delete-variant-prices.js +105 -0
- package/dist/api/routes/admin/price-lists/delete-variant-prices.js.map +1 -0
- package/dist/api/routes/admin/price-lists/index.d.ts +9 -0
- package/dist/api/routes/admin/price-lists/index.js +6 -1
- package/dist/api/routes/admin/price-lists/index.js.map +1 -1
- package/dist/api/routes/admin/price-lists/list-price-list-products.d.ts +74 -0
- package/dist/api/routes/admin/price-lists/list-price-list-products.js +287 -0
- package/dist/api/routes/admin/price-lists/list-price-list-products.js.map +1 -0
- package/dist/api/routes/admin/price-lists/list-price-lists.js.map +1 -1
- package/dist/api/routes/admin/price-lists/update-price-list.d.ts +4 -4
- package/dist/api/routes/admin/price-lists/update-price-list.js +2 -2
- package/dist/api/routes/admin/price-lists/update-price-list.js.map +1 -1
- package/dist/api/routes/admin/product-tags/list-product-tags.d.ts +2 -1
- package/dist/api/routes/admin/product-tags/list-product-tags.js +6 -3
- package/dist/api/routes/admin/product-tags/list-product-tags.js.map +1 -1
- package/dist/api/routes/admin/product-types/list-product-types.d.ts +2 -1
- package/dist/api/routes/admin/product-types/list-product-types.js +6 -3
- package/dist/api/routes/admin/product-types/list-product-types.js.map +1 -1
- package/dist/api/routes/admin/products/index.d.ts +2 -1
- package/dist/api/routes/admin/products/index.js +7 -2
- package/dist/api/routes/admin/products/index.js.map +1 -1
- package/dist/api/routes/admin/products/list-products.d.ts +2 -1
- package/dist/api/routes/admin/products/list-products.js +31 -65
- package/dist/api/routes/admin/products/list-products.js.map +1 -1
- package/dist/api/routes/admin/products/list-variants.d.ts +35 -0
- package/dist/api/routes/admin/products/list-variants.js +173 -0
- package/dist/api/routes/admin/products/list-variants.js.map +1 -0
- package/dist/api/routes/admin/products/update-variant.js +11 -7
- package/dist/api/routes/admin/products/update-variant.js.map +1 -1
- package/dist/api/routes/admin/regions/index.d.ts +1 -1
- package/dist/api/routes/admin/shipping-options/list-shipping-options.d.ts +2 -2
- package/dist/api/routes/admin/shipping-options/list-shipping-options.js +2 -2
- package/dist/api/routes/admin/shipping-options/list-shipping-options.js.map +1 -1
- package/dist/api/routes/admin/store/get-store.js.map +1 -1
- package/dist/api/routes/admin/swaps/index.js +3 -0
- package/dist/api/routes/admin/swaps/index.js.map +1 -1
- package/dist/api/routes/admin/tax-rates/list-tax-rates.js.map +1 -1
- package/dist/api/routes/admin/tax-rates/utils/get-query-config.d.ts +1 -1
- package/dist/api/routes/admin/tax-rates/utils/get-query-config.js +4 -2
- package/dist/api/routes/admin/tax-rates/utils/get-query-config.js.map +1 -1
- package/dist/api/routes/admin/uploads/index.d.ts +1 -1
- package/dist/api/routes/admin/users/update-user.d.ts +1 -1
- package/dist/api/routes/admin/users/update-user.js.map +1 -1
- package/dist/api/routes/store/auth/create-session.js +4 -5
- package/dist/api/routes/store/auth/create-session.js.map +1 -1
- package/dist/api/routes/store/carts/create-cart.js +8 -5
- package/dist/api/routes/store/carts/create-cart.js.map +1 -1
- package/dist/api/routes/store/carts/create-line-item.d.ts +1 -1
- package/dist/api/routes/store/carts/create-line-item.js +9 -6
- package/dist/api/routes/store/carts/create-line-item.js.map +1 -1
- package/dist/api/routes/store/carts/index.d.ts +0 -1
- package/dist/api/routes/store/carts/index.js +1 -3
- package/dist/api/routes/store/carts/index.js.map +1 -1
- package/dist/api/routes/store/carts/update-cart.js +8 -8
- package/dist/api/routes/store/carts/update-cart.js.map +1 -1
- package/dist/api/routes/store/customers/create-customer.js +3 -4
- package/dist/api/routes/store/customers/create-customer.js.map +1 -1
- package/dist/api/routes/store/orders/index.js +0 -2
- package/dist/api/routes/store/orders/index.js.map +1 -1
- package/dist/api/routes/store/products/get-product.js +17 -5
- package/dist/api/routes/store/products/get-product.js.map +1 -1
- package/dist/api/routes/store/products/index.d.ts +2 -2
- package/dist/api/routes/store/products/list-products.d.ts +2 -1
- package/dist/api/routes/store/products/list-products.js +21 -8
- package/dist/api/routes/store/products/list-products.js.map +1 -1
- package/dist/api/routes/store/products/search.d.ts +1 -1
- package/dist/api/routes/store/products/search.js.map +1 -1
- package/dist/api/routes/store/shipping-options/list-options.js.map +1 -1
- package/dist/api/routes/store/swaps/create-swap.js +7 -3
- package/dist/api/routes/store/swaps/create-swap.js.map +1 -1
- package/dist/api/routes/store/variants/get-variant.js +17 -5
- package/dist/api/routes/store/variants/get-variant.js.map +1 -1
- package/dist/api/routes/store/variants/list-variants.d.ts +2 -1
- package/dist/api/routes/store/variants/list-variants.js +38 -9
- package/dist/api/routes/store/variants/list-variants.js.map +1 -1
- package/dist/commands/develop.js +2 -2
- package/dist/commands/develop.js.map +1 -1
- package/dist/commands/start.js +6 -0
- package/dist/commands/start.js.map +1 -1
- package/dist/controllers/customers/index.d.ts +1 -1
- package/dist/controllers/customers/list-customers.d.ts +1 -1
- package/dist/controllers/customers/list-customers.js +34 -37
- package/dist/controllers/customers/list-customers.js.map +1 -1
- package/dist/controllers/products/admin-list-products.d.ts +16 -0
- package/dist/controllers/products/admin-list-products.js +103 -0
- package/dist/controllers/products/admin-list-products.js.map +1 -0
- package/dist/helpers/test-request.js +26 -17
- package/dist/helpers/test-request.js.map +1 -1
- package/dist/index.d.ts +4 -59
- package/dist/index.js +4 -62
- package/dist/index.js.map +1 -1
- package/dist/interfaces/index.d.ts +3 -0
- package/dist/interfaces/index.js +3 -0
- package/dist/interfaces/index.js.map +1 -1
- package/dist/interfaces/models/base-entity.d.ts +8 -0
- package/dist/interfaces/models/base-entity.js +36 -0
- package/dist/interfaces/models/base-entity.js.map +1 -0
- package/dist/interfaces/models/soft-deletable-entity.d.ts +4 -0
- package/dist/interfaces/models/soft-deletable-entity.js +43 -0
- package/dist/interfaces/models/soft-deletable-entity.js.map +1 -0
- package/dist/interfaces/price-selection-strategy.d.ts +50 -0
- package/dist/interfaces/price-selection-strategy.js +34 -0
- package/dist/interfaces/price-selection-strategy.js.map +1 -0
- package/dist/interfaces/tax-service.d.ts +6 -0
- package/dist/interfaces/tax-service.js +31 -0
- package/dist/interfaces/tax-service.js.map +1 -1
- package/dist/interfaces/transaction-base-service.d.ts +23 -0
- package/dist/interfaces/transaction-base-service.js +200 -0
- package/dist/interfaces/transaction-base-service.js.map +1 -0
- package/dist/loaders/api.d.ts +9 -5
- package/dist/loaders/api.js +2 -6
- package/dist/loaders/api.js.map +1 -1
- package/dist/loaders/config.d.ts +3 -0
- package/dist/loaders/config.js +47 -0
- package/dist/loaders/config.js.map +1 -0
- package/dist/loaders/database.d.ts +8 -4
- package/dist/loaders/database.js.map +1 -1
- package/dist/loaders/defaults.d.ts +4 -3
- package/dist/loaders/defaults.js +39 -31
- package/dist/loaders/defaults.js.map +1 -1
- package/dist/loaders/express.d.ts +7 -4
- package/dist/loaders/express.js +7 -5
- package/dist/loaders/express.js.map +1 -1
- package/dist/loaders/index.d.ts +13 -8
- package/dist/loaders/index.js +29 -54
- package/dist/loaders/index.js.map +1 -1
- package/dist/loaders/logger.d.ts +1 -0
- package/dist/loaders/logger.js.map +1 -1
- package/dist/loaders/models.d.ts +8 -3
- package/dist/loaders/models.js +19 -17
- package/dist/loaders/models.js.map +1 -1
- package/dist/loaders/passport.d.ts +7 -4
- package/dist/loaders/passport.js +23 -21
- package/dist/loaders/passport.js.map +1 -1
- package/dist/loaders/plugins.d.ts +38 -10
- package/dist/loaders/plugins.js +30 -26
- package/dist/loaders/plugins.js.map +1 -1
- package/dist/loaders/redis.d.ts +8 -5
- package/dist/loaders/redis.js +3 -3
- package/dist/loaders/redis.js.map +1 -1
- package/dist/loaders/repositories.d.ts +7 -3
- package/dist/loaders/repositories.js +1 -1
- package/dist/loaders/repositories.js.map +1 -1
- package/dist/loaders/search-index.d.ts +4 -3
- package/dist/loaders/search-index.js.map +1 -1
- package/dist/loaders/services.d.ts +10 -5
- package/dist/loaders/services.js.map +1 -1
- package/dist/loaders/strategies.d.ts +1 -1
- package/dist/loaders/subscribers.d.ts +7 -3
- package/dist/loaders/subscribers.js.map +1 -1
- package/dist/migrations/1646324713514-discount_conditions.d.ts +6 -0
- package/dist/migrations/1646324713514-discount_conditions.js +335 -0
- package/dist/migrations/1646324713514-discount_conditions.js.map +1 -0
- package/dist/migrations/1648600574750-add_line_item_adjustments.d.ts +6 -0
- package/dist/migrations/1648600574750-add_line_item_adjustments.js +101 -0
- package/dist/migrations/1648600574750-add_line_item_adjustments.js.map +1 -0
- package/dist/migrations/1648641130007-tax_line_constraints.d.ts +6 -0
- package/dist/migrations/1648641130007-tax_line_constraints.js +77 -0
- package/dist/migrations/1648641130007-tax_line_constraints.js.map +1 -0
- package/dist/migrations/1649775522087-add_batch_job_model.d.ts +6 -0
- package/dist/migrations/1649775522087-add_batch_job_model.js +77 -0
- package/dist/migrations/1649775522087-add_batch_job_model.js.map +1 -0
- package/dist/models/address.d.ts +3 -6
- package/dist/models/address.js +23 -24
- package/dist/models/address.js.map +1 -1
- package/dist/models/batch-job.d.ts +55 -0
- package/dist/models/batch-job.js +118 -0
- package/dist/models/batch-job.js.map +1 -0
- package/dist/models/cart.d.ts +5 -8
- package/dist/models/cart.js +35 -36
- package/dist/models/cart.js.map +1 -1
- package/dist/models/claim-image.d.ts +3 -6
- package/dist/models/claim-image.js +22 -23
- package/dist/models/claim-image.js.map +1 -1
- package/dist/models/claim-item.d.ts +3 -6
- package/dist/models/claim-item.js +25 -24
- package/dist/models/claim-item.js.map +1 -1
- package/dist/models/claim-order.d.ts +4 -4
- package/dist/models/claim-order.js +28 -13
- package/dist/models/claim-order.js.map +1 -1
- package/dist/models/claim-tag.d.ts +3 -6
- package/dist/models/claim-tag.js +22 -23
- package/dist/models/claim-tag.js.map +1 -1
- package/dist/models/country.d.ts +1 -1
- package/dist/models/country.js +1 -1
- package/dist/models/country.js.map +1 -1
- package/dist/models/custom-shipping-option.d.ts +3 -6
- package/dist/models/custom-shipping-option.js +23 -24
- package/dist/models/custom-shipping-option.js.map +1 -1
- package/dist/models/customer-group.d.ts +3 -6
- package/dist/models/customer-group.js +26 -26
- package/dist/models/customer-group.js.map +1 -1
- package/dist/models/customer.d.ts +3 -6
- package/dist/models/customer.js +23 -25
- package/dist/models/customer.js.map +1 -1
- package/dist/models/discount-condition-customer-group.d.ts +39 -0
- package/dist/models/discount-condition-customer-group.js +84 -0
- package/dist/models/discount-condition-customer-group.js.map +1 -0
- package/dist/models/discount-condition-product-collection.d.ts +39 -0
- package/dist/models/discount-condition-product-collection.js +84 -0
- package/dist/models/discount-condition-product-collection.js.map +1 -0
- package/dist/models/discount-condition-product-tag.d.ts +39 -0
- package/dist/models/discount-condition-product-tag.js +84 -0
- package/dist/models/discount-condition-product-tag.js.map +1 -0
- package/dist/models/discount-condition-product-type.d.ts +39 -0
- package/dist/models/discount-condition-product-type.js +84 -0
- package/dist/models/discount-condition-product-type.js.map +1 -0
- package/dist/models/discount-condition-product.d.ts +39 -0
- package/dist/models/discount-condition-product.js +84 -0
- package/dist/models/discount-condition-product.js.map +1 -0
- package/dist/models/discount-condition.d.ts +65 -0
- package/dist/models/discount-condition.js +209 -0
- package/dist/models/discount-condition.js.map +1 -0
- package/dist/models/discount-rule.d.ts +8 -11
- package/dist/models/discount-rule.js +28 -38
- package/dist/models/discount-rule.js.map +1 -1
- package/dist/models/discount.d.ts +7 -10
- package/dist/models/discount.js +30 -30
- package/dist/models/discount.js.map +1 -1
- package/dist/models/draft-order.d.ts +3 -5
- package/dist/models/draft-order.js +23 -20
- package/dist/models/draft-order.js.map +1 -1
- package/dist/models/fulfillment-item.js.map +1 -1
- package/dist/models/fulfillment-provider.js.map +1 -1
- package/dist/models/fulfillment.d.ts +5 -7
- package/dist/models/fulfillment.js +29 -21
- package/dist/models/fulfillment.js.map +1 -1
- package/dist/models/gift-card-transaction.js +2 -5
- package/dist/models/gift-card-transaction.js.map +1 -1
- package/dist/models/gift-card.d.ts +3 -6
- package/dist/models/gift-card.js +22 -23
- package/dist/models/gift-card.js.map +1 -1
- package/dist/models/idempotency-key.d.ts +2 -2
- package/dist/models/idempotency-key.js +2 -3
- package/dist/models/idempotency-key.js.map +1 -1
- package/dist/models/image.d.ts +3 -6
- package/dist/models/image.js +22 -23
- package/dist/models/image.js.map +1 -1
- package/dist/models/index.d.ts +67 -0
- package/dist/models/index.js +80 -0
- package/dist/models/index.js.map +1 -0
- package/dist/models/invite.d.ts +3 -6
- package/dist/models/invite.js +22 -24
- package/dist/models/invite.js.map +1 -1
- package/dist/models/line-item-adjustment.d.ts +13 -0
- package/dist/models/line-item-adjustment.js +76 -0
- package/dist/models/line-item-adjustment.js.map +1 -0
- package/dist/models/line-item-tax-line.js +4 -6
- package/dist/models/line-item-tax-line.js.map +1 -1
- package/dist/models/line-item.d.ts +5 -5
- package/dist/models/line-item.js +29 -19
- package/dist/models/line-item.js.map +1 -1
- package/dist/models/money-amount.d.ts +2 -5
- package/dist/models/money-amount.js +26 -26
- package/dist/models/money-amount.js.map +1 -1
- package/dist/models/note.d.ts +3 -6
- package/dist/models/note.js +22 -23
- package/dist/models/note.js.map +1 -1
- package/dist/models/notification.d.ts +3 -5
- package/dist/models/notification.js +22 -19
- package/dist/models/notification.js.map +1 -1
- package/dist/models/oauth.d.ts +1 -1
- package/dist/models/oauth.js +2 -5
- package/dist/models/oauth.js.map +1 -1
- package/dist/models/order.d.ts +3 -5
- package/dist/models/order.js +25 -21
- package/dist/models/order.js.map +1 -1
- package/dist/models/payment-provider.js.map +1 -1
- package/dist/models/payment-session.d.ts +3 -5
- package/dist/models/payment-session.js +22 -19
- package/dist/models/payment-session.js.map +1 -1
- package/dist/models/payment.d.ts +4 -6
- package/dist/models/payment.js +22 -19
- package/dist/models/payment.js.map +1 -1
- package/dist/models/price-list.d.ts +4 -7
- package/dist/models/price-list.js +30 -28
- package/dist/models/price-list.js.map +1 -1
- package/dist/models/product-collection.d.ts +4 -7
- package/dist/models/product-collection.js +25 -24
- package/dist/models/product-collection.js.map +1 -1
- package/dist/models/product-option-value.d.ts +3 -6
- package/dist/models/product-option-value.js +24 -26
- package/dist/models/product-option-value.js.map +1 -1
- package/dist/models/product-option.d.ts +3 -6
- package/dist/models/product-option.js +23 -24
- package/dist/models/product-option.js.map +1 -1
- package/dist/models/product-tag.d.ts +3 -6
- package/dist/models/product-tag.js +22 -23
- package/dist/models/product-tag.js.map +1 -1
- package/dist/models/product-tax-rate.d.ts +1 -1
- package/dist/models/product-tax-rate.js.map +1 -1
- package/dist/models/product-type-tax-rate.d.ts +1 -1
- package/dist/models/product-type-tax-rate.js.map +1 -1
- package/dist/models/product-type.d.ts +3 -6
- package/dist/models/product-type.js +22 -23
- package/dist/models/product-type.js.map +1 -1
- package/dist/models/product-variant.d.ts +3 -6
- package/dist/models/product-variant.js +30 -26
- package/dist/models/product-variant.js.map +1 -1
- package/dist/models/product.d.ts +3 -6
- package/dist/models/product.js +25 -22
- package/dist/models/product.js.map +1 -1
- package/dist/models/refund.d.ts +3 -5
- package/dist/models/refund.js +22 -17
- package/dist/models/refund.js.map +1 -1
- package/dist/models/region.d.ts +3 -6
- package/dist/models/region.js +23 -24
- package/dist/models/region.js.map +1 -1
- package/dist/models/return-item.d.ts +1 -1
- package/dist/models/return-item.js.map +1 -1
- package/dist/models/return-reason.d.ts +3 -6
- package/dist/models/return-reason.js +24 -25
- package/dist/models/return-reason.js.map +1 -1
- package/dist/models/return.d.ts +5 -7
- package/dist/models/return.js +29 -21
- package/dist/models/return.js.map +1 -1
- package/dist/models/shipping-method-tax-line.js +4 -6
- package/dist/models/shipping-method-tax-line.js.map +1 -1
- package/dist/models/shipping-method.d.ts +1 -1
- package/dist/models/shipping-method.js +2 -5
- package/dist/models/shipping-method.js.map +1 -1
- package/dist/models/shipping-option-requirement.js +2 -5
- package/dist/models/shipping-option-requirement.js.map +1 -1
- package/dist/models/shipping-option.d.ts +4 -7
- package/dist/models/shipping-option.js +25 -24
- package/dist/models/shipping-option.js.map +1 -1
- package/dist/models/shipping-profile.d.ts +3 -6
- package/dist/models/shipping-profile.js +22 -23
- package/dist/models/shipping-profile.js.map +1 -1
- package/dist/models/shipping-tax-rate.d.ts +1 -1
- package/dist/models/shipping-tax-rate.js.map +1 -1
- package/dist/models/staged-job.d.ts +1 -1
- package/dist/models/staged-job.js +2 -3
- package/dist/models/staged-job.js.map +1 -1
- package/dist/models/store.d.ts +3 -5
- package/dist/models/store.js +22 -17
- package/dist/models/store.js.map +1 -1
- package/dist/models/swap.d.ts +9 -12
- package/dist/models/swap.js +55 -52
- package/dist/models/swap.js.map +1 -1
- package/dist/models/tax-line.d.ts +3 -5
- package/dist/models/tax-line.js +20 -14
- package/dist/models/tax-line.js.map +1 -1
- package/dist/models/tax-rate.d.ts +3 -5
- package/dist/models/tax-rate.js +22 -19
- package/dist/models/tax-rate.js.map +1 -1
- package/dist/models/tracking-link.d.ts +3 -6
- package/dist/models/tracking-link.js +26 -27
- package/dist/models/tracking-link.js.map +1 -1
- package/dist/models/user.d.ts +3 -6
- package/dist/models/user.js +22 -23
- package/dist/models/user.js.map +1 -1
- package/dist/repositories/batch-job.d.ts +4 -0
- package/dist/repositories/batch-job.js +38 -0
- package/dist/repositories/batch-job.js.map +1 -0
- package/dist/repositories/cart.d.ts +2 -2
- package/dist/repositories/cart.js.map +1 -1
- package/dist/repositories/customer.d.ts +1 -1
- package/dist/repositories/customer.js +7 -9
- package/dist/repositories/customer.js.map +1 -1
- package/dist/repositories/discount-condition.d.ts +38 -0
- package/dist/repositories/discount-condition.js +398 -0
- package/dist/repositories/discount-condition.js.map +1 -0
- package/dist/repositories/line-item-adjustment.d.ts +4 -0
- package/dist/repositories/line-item-adjustment.js +38 -0
- package/dist/repositories/line-item-adjustment.js.map +1 -0
- package/dist/repositories/line-item-tax-line.d.ts +2 -0
- package/dist/repositories/line-item-tax-line.js +80 -0
- package/dist/repositories/line-item-tax-line.js.map +1 -1
- package/dist/repositories/line-item.js +1 -0
- package/dist/repositories/line-item.js.map +1 -1
- package/dist/repositories/money-amount.d.ts +2 -0
- package/dist/repositories/money-amount.js +77 -2
- package/dist/repositories/money-amount.js.map +1 -1
- package/dist/repositories/order.d.ts +1 -1
- package/dist/repositories/price-list.d.ts +8 -1
- package/dist/repositories/price-list.js +209 -0
- package/dist/repositories/price-list.js.map +1 -1
- package/dist/repositories/product.d.ts +2 -0
- package/dist/repositories/product.js +35 -17
- package/dist/repositories/product.js.map +1 -1
- package/dist/repositories/shipping-method-tax-line.d.ts +2 -0
- package/dist/repositories/shipping-method-tax-line.js +80 -0
- package/dist/repositories/shipping-method-tax-line.js.map +1 -1
- package/dist/scripts/discount-rule-migration.d.ts +4 -0
- package/dist/scripts/discount-rule-migration.js +189 -0
- package/dist/scripts/discount-rule-migration.js.map +1 -0
- package/dist/scripts/line-item-adjustment-migration.d.ts +4 -0
- package/dist/scripts/line-item-adjustment-migration.js +173 -0
- package/dist/scripts/line-item-adjustment-migration.js.map +1 -0
- package/dist/services/batch-job.d.ts +24 -0
- package/dist/services/batch-job.js +126 -0
- package/dist/services/batch-job.js.map +1 -0
- package/dist/services/cart.d.ts +66 -55
- package/dist/services/cart.js +1301 -1410
- package/dist/services/cart.js.map +1 -1
- package/dist/services/claim.js +62 -19
- package/dist/services/claim.js.map +1 -1
- package/dist/services/customer.d.ts +0 -7
- package/dist/services/customer.js +8 -35
- package/dist/services/customer.js.map +1 -1
- package/dist/services/discount-condition.d.ts +31 -0
- package/dist/services/discount-condition.js +232 -0
- package/dist/services/discount-condition.js.map +1 -0
- package/dist/services/discount.d.ts +50 -59
- package/dist/services/discount.js +272 -174
- package/dist/services/discount.js.map +1 -1
- package/dist/services/draft-order.js +1 -0
- package/dist/services/draft-order.js.map +1 -1
- package/dist/services/fulfillment-provider.js +23 -17
- package/dist/services/fulfillment-provider.js.map +1 -1
- package/dist/services/invite.d.ts +4 -2
- package/dist/services/invite.js +11 -9
- package/dist/services/invite.js.map +1 -1
- package/dist/services/line-item-adjustment.d.ts +87 -0
- package/dist/services/line-item-adjustment.js +434 -0
- package/dist/services/line-item-adjustment.js.map +1 -0
- package/dist/services/line-item.d.ts +48 -40
- package/dist/services/line-item.js +226 -193
- package/dist/services/line-item.js.map +1 -1
- package/dist/services/order.d.ts +0 -13
- package/dist/services/order.js +4 -30
- package/dist/services/order.js.map +1 -1
- package/dist/services/payment-provider.js +23 -17
- package/dist/services/payment-provider.js.map +1 -1
- package/dist/services/price-list.d.ts +35 -14
- package/dist/services/price-list.js +327 -56
- package/dist/services/price-list.js.map +1 -1
- package/dist/services/product-tag.d.ts +2 -2
- package/dist/services/product-tag.js +15 -2
- package/dist/services/product-tag.js.map +1 -1
- package/dist/services/product-type.d.ts +1 -1
- package/dist/services/product-type.js +14 -1
- package/dist/services/product-type.js.map +1 -1
- package/dist/services/product-variant.d.ts +26 -9
- package/dist/services/product-variant.js +223 -77
- package/dist/services/product-variant.js.map +1 -1
- package/dist/services/product.d.ts +24 -5
- package/dist/services/product.js +247 -52
- package/dist/services/product.js.map +1 -1
- package/dist/services/region.js +8 -4
- package/dist/services/region.js.map +1 -1
- package/dist/services/return.js +2 -1
- package/dist/services/return.js.map +1 -1
- package/dist/services/shipping-option.d.ts +2 -2
- package/dist/services/shipping-option.js +7 -4
- package/dist/services/shipping-option.js.map +1 -1
- package/dist/services/swap.d.ts +4 -1
- package/dist/services/swap.js +29 -16
- package/dist/services/swap.js.map +1 -1
- package/dist/services/system-tax.d.ts +2 -3
- package/dist/services/system-tax.js +2 -2
- package/dist/services/system-tax.js.map +1 -1
- package/dist/services/tax-provider.d.ts +2 -0
- package/dist/services/tax-provider.js +96 -7
- package/dist/services/tax-provider.js.map +1 -1
- package/dist/services/tax-rate.d.ts +3 -3
- package/dist/services/tax-rate.js +1 -1
- package/dist/services/tax-rate.js.map +1 -1
- package/dist/services/totals.d.ts +9 -8
- package/dist/services/totals.js +41 -88
- package/dist/services/totals.js.map +1 -1
- package/dist/strategies/cart-completion.js.map +1 -1
- package/dist/strategies/price-selection.d.ts +13 -0
- package/dist/strategies/price-selection.js +185 -0
- package/dist/strategies/price-selection.js.map +1 -0
- package/dist/subscribers/order.js +1 -7
- package/dist/subscribers/order.js.map +1 -1
- package/dist/types/batch-job.d.ts +15 -0
- package/dist/types/batch-job.js +63 -0
- package/dist/types/batch-job.js.map +1 -0
- package/dist/types/cart.d.ts +2 -2
- package/dist/types/common.d.ts +32 -13
- package/dist/types/common.js +1 -0
- package/dist/types/common.js.map +1 -1
- package/dist/types/discount.d.ts +68 -1
- package/dist/types/discount.js +129 -5
- package/dist/types/discount.js.map +1 -1
- package/dist/types/global.d.ts +43 -0
- package/dist/types/global.js +3 -0
- package/dist/types/global.js.map +1 -0
- package/dist/types/line-item-adjustment.d.ts +9 -0
- package/dist/types/line-item-adjustment.js +50 -0
- package/dist/types/line-item-adjustment.js.map +1 -0
- package/dist/types/note.d.ts +1 -1
- package/dist/types/price-list.d.ts +1 -0
- package/dist/types/price-list.js +5 -0
- package/dist/types/price-list.js.map +1 -1
- package/dist/types/price-selection.d.ts +5 -0
- package/dist/types/price-selection.js +35 -0
- package/dist/types/price-selection.js.map +1 -0
- package/dist/types/product-variant.d.ts +6 -0
- package/dist/types/product-variant.js.map +1 -1
- package/dist/types/product.d.ts +18 -0
- package/dist/types/product.js +104 -5
- package/dist/types/product.js.map +1 -1
- package/dist/types/tax-service.d.ts +2 -2
- package/dist/types/user.d.ts +2 -2
- package/dist/types/validators/exactly-one.d.ts +6 -0
- package/dist/types/validators/exactly-one.js +37 -0
- package/dist/types/validators/exactly-one.js.map +1 -0
- package/dist/utils/build-query.d.ts +8 -0
- package/dist/utils/build-query.js +114 -0
- package/dist/utils/build-query.js.map +1 -0
- package/dist/utils/date-helpers.d.ts +2 -0
- package/dist/utils/date-helpers.js +15 -0
- package/dist/utils/date-helpers.js.map +1 -0
- package/dist/utils/generate-entity-id.d.ts +6 -0
- package/dist/utils/generate-entity-id.js +19 -0
- package/dist/utils/generate-entity-id.js.map +1 -0
- package/dist/utils/get-query-config.d.ts +11 -0
- package/dist/utils/get-query-config.js +88 -0
- package/dist/utils/get-query-config.js.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/set-metadata.d.ts +9 -0
- package/dist/utils/set-metadata.js +72 -0
- package/dist/utils/set-metadata.js.map +1 -0
- package/dist/utils/validate-id.d.ts +4 -0
- package/dist/utils/validate-id.js +47 -0
- package/dist/utils/validate-id.js.map +1 -0
- package/jest.config.js +6 -0
- package/package.json +10 -9
- package/tsconfig.spec.json +10 -0
- package/dist/api/routes/admin/discounts/add-valid-product.d.ts +0 -23
- package/dist/api/routes/admin/discounts/add-valid-product.js.map +0 -1
- package/dist/api/routes/admin/discounts/remove-valid-product.d.ts +0 -23
- package/dist/api/routes/admin/discounts/remove-valid-product.js.map +0 -1
- package/dist/api/routes/admin/products/get-variants.d.ts +0 -24
- package/dist/api/routes/admin/products/get-variants.js.map +0 -1
- package/dist/api/routes/store/carts/update-payment-method.d.ts +0 -38
- package/dist/api/routes/store/carts/update-payment-method.js.map +0 -1
- package/dist/config/index.d.ts +0 -14
- package/dist/config/index.js +0 -40
- package/dist/config/index.js.map +0 -1
- package/yarn-error.log +0 -6769
package/dist/services/cart.js
CHANGED
|
@@ -103,18 +103,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
103
103
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
104
104
|
var lodash_1 = __importDefault(require("lodash"));
|
|
105
105
|
var medusa_core_utils_1 = require("medusa-core-utils");
|
|
106
|
-
var
|
|
106
|
+
var typeorm_1 = require("typeorm");
|
|
107
|
+
var interfaces_1 = require("../interfaces");
|
|
108
|
+
var utils_1 = require("../utils");
|
|
107
109
|
/* Provides layer to manipulate carts.
|
|
108
110
|
* @implements BaseService
|
|
109
111
|
*/
|
|
110
112
|
var CartService = /** @class */ (function (_super) {
|
|
111
113
|
__extends(CartService, _super);
|
|
112
114
|
function CartService(_a) {
|
|
113
|
-
var manager = _a.manager, cartRepository = _a.cartRepository, shippingMethodRepository = _a.shippingMethodRepository, eventBusService = _a.eventBusService, paymentProviderService = _a.paymentProviderService, productService = _a.productService, productVariantService = _a.productVariantService, taxProviderService = _a.taxProviderService, regionService = _a.regionService, lineItemService = _a.lineItemService, shippingOptionService = _a.shippingOptionService, customerService = _a.customerService, discountService = _a.discountService, giftCardService = _a.giftCardService, totalsService = _a.totalsService, addressRepository = _a.addressRepository, paymentSessionRepository = _a.paymentSessionRepository, inventoryService = _a.inventoryService, customShippingOptionService = _a.customShippingOptionService;
|
|
114
|
-
var _this =
|
|
115
|
+
var manager = _a.manager, cartRepository = _a.cartRepository, shippingMethodRepository = _a.shippingMethodRepository, lineItemRepository = _a.lineItemRepository, eventBusService = _a.eventBusService, paymentProviderService = _a.paymentProviderService, productService = _a.productService, productVariantService = _a.productVariantService, taxProviderService = _a.taxProviderService, regionService = _a.regionService, lineItemService = _a.lineItemService, shippingOptionService = _a.shippingOptionService, customerService = _a.customerService, discountService = _a.discountService, giftCardService = _a.giftCardService, totalsService = _a.totalsService, addressRepository = _a.addressRepository, paymentSessionRepository = _a.paymentSessionRepository, inventoryService = _a.inventoryService, customShippingOptionService = _a.customShippingOptionService, lineItemAdjustmentService = _a.lineItemAdjustmentService, priceSelectionStrategy = _a.priceSelectionStrategy;
|
|
116
|
+
var _this =
|
|
117
|
+
// eslint-disable-next-line prefer-rest-params
|
|
118
|
+
_super.call(this, arguments[0]) || this;
|
|
115
119
|
_this.manager_ = manager;
|
|
116
120
|
_this.shippingMethodRepository_ = shippingMethodRepository;
|
|
117
121
|
_this.cartRepository_ = cartRepository;
|
|
122
|
+
_this.lineItemRepository_ = lineItemRepository;
|
|
118
123
|
_this.eventBus_ = eventBusService;
|
|
119
124
|
_this.productVariantService_ = productVariantService;
|
|
120
125
|
_this.productService_ = productService;
|
|
@@ -131,36 +136,10 @@ var CartService = /** @class */ (function (_super) {
|
|
|
131
136
|
_this.inventoryService_ = inventoryService;
|
|
132
137
|
_this.customShippingOptionService_ = customShippingOptionService;
|
|
133
138
|
_this.taxProviderService_ = taxProviderService;
|
|
139
|
+
_this.lineItemAdjustmentService_ = lineItemAdjustmentService;
|
|
140
|
+
_this.priceSelectionStrategy_ = priceSelectionStrategy;
|
|
134
141
|
return _this;
|
|
135
142
|
}
|
|
136
|
-
CartService.prototype.withTransaction = function (transactionManager) {
|
|
137
|
-
if (!transactionManager) {
|
|
138
|
-
return this;
|
|
139
|
-
}
|
|
140
|
-
var cloned = new CartService({
|
|
141
|
-
manager: transactionManager,
|
|
142
|
-
taxProviderService: this.taxProviderService_,
|
|
143
|
-
cartRepository: this.cartRepository_,
|
|
144
|
-
eventBusService: this.eventBus_,
|
|
145
|
-
paymentProviderService: this.paymentProviderService_,
|
|
146
|
-
paymentSessionRepository: this.paymentSessionRepository_,
|
|
147
|
-
shippingMethodRepository: this.shippingMethodRepository_,
|
|
148
|
-
productService: this.productService_,
|
|
149
|
-
productVariantService: this.productVariantService_,
|
|
150
|
-
regionService: this.regionService_,
|
|
151
|
-
lineItemService: this.lineItemService_,
|
|
152
|
-
shippingOptionService: this.shippingOptionService_,
|
|
153
|
-
customerService: this.customerService_,
|
|
154
|
-
discountService: this.discountService_,
|
|
155
|
-
totalsService: this.totalsService_,
|
|
156
|
-
addressRepository: this.addressRepository_,
|
|
157
|
-
giftCardService: this.giftCardService_,
|
|
158
|
-
inventoryService: this.inventoryService_,
|
|
159
|
-
customShippingOptionService: this.customShippingOptionService_,
|
|
160
|
-
});
|
|
161
|
-
cloned.transactionManager_ = transactionManager;
|
|
162
|
-
return cloned;
|
|
163
|
-
};
|
|
164
143
|
CartService.prototype.transformQueryForTotals_ = function (config) {
|
|
165
144
|
var select = config.select, relations = config.relations;
|
|
166
145
|
if (!select) {
|
|
@@ -188,7 +167,6 @@ var CartService = /** @class */ (function (_super) {
|
|
|
188
167
|
relationSet.add("gift_cards");
|
|
189
168
|
relationSet.add("discounts");
|
|
190
169
|
relationSet.add("discounts.rule");
|
|
191
|
-
relationSet.add("discounts.rule.valid_for");
|
|
192
170
|
// relationSet.add("discounts.parent_discount")
|
|
193
171
|
// relationSet.add("discounts.parent_discount.rule")
|
|
194
172
|
// relationSet.add("discounts.parent_discount.regions")
|
|
@@ -288,13 +266,21 @@ var CartService = /** @class */ (function (_super) {
|
|
|
288
266
|
CartService.prototype.list = function (selector, config) {
|
|
289
267
|
if (config === void 0) { config = {}; }
|
|
290
268
|
return __awaiter(this, void 0, void 0, function () {
|
|
291
|
-
var
|
|
269
|
+
var _this = this;
|
|
292
270
|
return __generator(this, function (_a) {
|
|
293
271
|
switch (_a.label) {
|
|
294
|
-
case 0:
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
272
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
273
|
+
var cartRepo, query;
|
|
274
|
+
return __generator(this, function (_a) {
|
|
275
|
+
switch (_a.label) {
|
|
276
|
+
case 0:
|
|
277
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
278
|
+
query = (0, utils_1.buildQuery)(selector, config);
|
|
279
|
+
return [4 /*yield*/, cartRepo.find(query)];
|
|
280
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
}); })];
|
|
298
284
|
case 1: return [2 /*return*/, _a.sent()];
|
|
299
285
|
}
|
|
300
286
|
});
|
|
@@ -311,33 +297,41 @@ var CartService = /** @class */ (function (_super) {
|
|
|
311
297
|
if (options === void 0) { options = {}; }
|
|
312
298
|
if (totalsConfig === void 0) { totalsConfig = {}; }
|
|
313
299
|
return __awaiter(this, void 0, void 0, function () {
|
|
314
|
-
var
|
|
315
|
-
return __generator(this, function (
|
|
316
|
-
switch (
|
|
317
|
-
case 0:
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
300
|
+
var _this = this;
|
|
301
|
+
return __generator(this, function (_a) {
|
|
302
|
+
switch (_a.label) {
|
|
303
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
304
|
+
var cartRepo, validatedId, _a, select, relations, totalsToSelect, query, queryRelations, raw;
|
|
305
|
+
return __generator(this, function (_b) {
|
|
306
|
+
switch (_b.label) {
|
|
307
|
+
case 0:
|
|
308
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
309
|
+
validatedId = (0, utils_1.validateId)(cartId);
|
|
310
|
+
_a = this.transformQueryForTotals_(options), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
|
|
311
|
+
query = (0, utils_1.buildQuery)({ id: validatedId }, __assign(__assign({}, options), { select: select, relations: relations }));
|
|
312
|
+
if (relations && relations.length > 0) {
|
|
313
|
+
query.relations = relations;
|
|
314
|
+
}
|
|
315
|
+
if (select && select.length > 0) {
|
|
316
|
+
query.select = select;
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
query.select = undefined;
|
|
320
|
+
}
|
|
321
|
+
queryRelations = query.relations;
|
|
322
|
+
query.relations = undefined;
|
|
323
|
+
return [4 /*yield*/, cartRepo.findOneWithRelations(queryRelations, query)];
|
|
324
|
+
case 1:
|
|
325
|
+
raw = _b.sent();
|
|
326
|
+
if (!raw) {
|
|
327
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Cart with ".concat(cartId, " was not found"));
|
|
328
|
+
}
|
|
329
|
+
return [4 /*yield*/, this.decorateTotals_(raw, totalsToSelect, totalsConfig)];
|
|
330
|
+
case 2: return [2 /*return*/, _b.sent()];
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
}); })];
|
|
334
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
341
335
|
}
|
|
342
336
|
});
|
|
343
337
|
});
|
|
@@ -351,101 +345,108 @@ var CartService = /** @class */ (function (_super) {
|
|
|
351
345
|
return __awaiter(this, void 0, void 0, function () {
|
|
352
346
|
var _this = this;
|
|
353
347
|
return __generator(this, function (_a) {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
348
|
+
switch (_a.label) {
|
|
349
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
350
|
+
var cartRepo, addressRepo, region_id, rawCart, customer, region, regCountries, addr, remainingFields, remainingFields_1, remainingFields_1_1, remainingField, createdCart, cart;
|
|
351
|
+
var e_2, _a;
|
|
352
|
+
return __generator(this, function (_b) {
|
|
353
|
+
switch (_b.label) {
|
|
354
|
+
case 0:
|
|
355
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
356
|
+
addressRepo = transactionManager.getCustomRepository(this.addressRepository_);
|
|
357
|
+
region_id = data.region_id;
|
|
358
|
+
if (!region_id) {
|
|
359
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "A region_id must be provided when creating a cart");
|
|
360
|
+
}
|
|
361
|
+
rawCart = {};
|
|
362
|
+
if (!data.email) return [3 /*break*/, 2];
|
|
363
|
+
return [4 /*yield*/, this.createOrFetchUserFromEmail_(data.email)];
|
|
364
|
+
case 1:
|
|
365
|
+
customer = _b.sent();
|
|
366
|
+
rawCart.customer = customer;
|
|
367
|
+
rawCart.customer_id = customer.id;
|
|
368
|
+
rawCart.email = customer.email;
|
|
369
|
+
_b.label = 2;
|
|
370
|
+
case 2: return [4 /*yield*/, this.regionService_
|
|
371
|
+
.withTransaction(transactionManager)
|
|
372
|
+
.retrieve(region_id, {
|
|
367
373
|
relations: ["countries"],
|
|
368
374
|
})];
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
if (!(typeof data.email !== "undefined")) return [3 /*break*/, 3];
|
|
378
|
-
return [4 /*yield*/, this.createOrFetchUserFromEmail_(data.email)];
|
|
379
|
-
case 2:
|
|
380
|
-
customer = _b.sent();
|
|
381
|
-
toCreate.customer = customer;
|
|
382
|
-
toCreate.customer_id = customer.id;
|
|
383
|
-
toCreate.email = customer.email;
|
|
384
|
-
_b.label = 3;
|
|
385
|
-
case 3:
|
|
386
|
-
if (!(typeof data.shipping_address_id !== "undefined")) return [3 /*break*/, 5];
|
|
387
|
-
return [4 /*yield*/, addressRepo.findOne(data.shipping_address_id)];
|
|
388
|
-
case 4:
|
|
389
|
-
addr = _b.sent();
|
|
390
|
-
if (addr && !regCountries.includes(addr.country_code)) {
|
|
391
|
-
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Shipping country not in region");
|
|
392
|
-
}
|
|
393
|
-
toCreate.shipping_address = addr;
|
|
394
|
-
_b.label = 5;
|
|
395
|
-
case 5:
|
|
396
|
-
if (!data.shipping_address) {
|
|
375
|
+
case 3:
|
|
376
|
+
region = _b.sent();
|
|
377
|
+
regCountries = region.countries.map(function (_a) {
|
|
378
|
+
var iso_2 = _a.iso_2;
|
|
379
|
+
return iso_2;
|
|
380
|
+
});
|
|
381
|
+
rawCart.region_id = region.id;
|
|
382
|
+
if (!(!data.shipping_address && !data.shipping_address_id)) return [3 /*break*/, 4];
|
|
397
383
|
if (region.countries.length === 1) {
|
|
398
|
-
|
|
399
|
-
// and create address entity
|
|
400
|
-
toCreate.shipping_address = addressRepo.create({
|
|
384
|
+
rawCart.shipping_address = addressRepo.create({
|
|
401
385
|
country_code: regCountries[0],
|
|
402
386
|
});
|
|
403
387
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
if (
|
|
388
|
+
return [3 /*break*/, 6];
|
|
389
|
+
case 4:
|
|
390
|
+
if (data.shipping_address) {
|
|
391
|
+
if (!regCountries.includes(data.shipping_address.country_code)) {
|
|
392
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Shipping country not in region");
|
|
393
|
+
}
|
|
394
|
+
rawCart.shipping_address = data.shipping_address;
|
|
395
|
+
}
|
|
396
|
+
if (!data.shipping_address_id) return [3 /*break*/, 6];
|
|
397
|
+
return [4 /*yield*/, addressRepo.findOne(data.shipping_address_id)];
|
|
398
|
+
case 5:
|
|
399
|
+
addr = _b.sent();
|
|
400
|
+
if (addr && !regCountries.includes(addr.country_code)) {
|
|
407
401
|
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Shipping country not in region");
|
|
408
402
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
403
|
+
rawCart.shipping_address_id = data.shipping_address_id;
|
|
404
|
+
_b.label = 6;
|
|
405
|
+
case 6:
|
|
406
|
+
remainingFields = [
|
|
407
|
+
"billing_address_id",
|
|
408
|
+
"context",
|
|
409
|
+
"type",
|
|
410
|
+
"metadata",
|
|
411
|
+
"discounts",
|
|
412
|
+
"gift_cards",
|
|
413
|
+
];
|
|
414
|
+
try {
|
|
415
|
+
for (remainingFields_1 = __values(remainingFields), remainingFields_1_1 = remainingFields_1.next(); !remainingFields_1_1.done; remainingFields_1_1 = remainingFields_1.next()) {
|
|
416
|
+
remainingField = remainingFields_1_1.value;
|
|
417
|
+
if (typeof data[remainingField] !== "undefined" &&
|
|
418
|
+
remainingField !== "object") {
|
|
419
|
+
/* TODO: See how to fix the error TS2590 properly while keeping the DeepPartial type */
|
|
420
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
421
|
+
// @ts-ignore
|
|
422
|
+
rawCart[remainingField] = data[remainingField];
|
|
423
|
+
}
|
|
424
424
|
}
|
|
425
425
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
426
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
427
|
+
finally {
|
|
428
|
+
try {
|
|
429
|
+
if (remainingFields_1_1 && !remainingFields_1_1.done && (_a = remainingFields_1.return)) _a.call(remainingFields_1);
|
|
430
|
+
}
|
|
431
|
+
finally { if (e_2) throw e_2.error; }
|
|
431
432
|
}
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
433
|
+
createdCart = cartRepo.create(rawCart);
|
|
434
|
+
return [4 /*yield*/, cartRepo.save(createdCart)];
|
|
435
|
+
case 7:
|
|
436
|
+
cart = _b.sent();
|
|
437
|
+
return [4 /*yield*/, this.eventBus_
|
|
438
|
+
.withTransaction(transactionManager)
|
|
439
|
+
.emit(CartService.Events.CREATED, {
|
|
440
|
+
id: cart.id,
|
|
441
|
+
})];
|
|
442
|
+
case 8:
|
|
443
|
+
_b.sent();
|
|
444
|
+
return [2 /*return*/, cart];
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
}); })];
|
|
448
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
449
|
+
}
|
|
449
450
|
});
|
|
450
451
|
});
|
|
451
452
|
};
|
|
@@ -459,102 +460,72 @@ var CartService = /** @class */ (function (_super) {
|
|
|
459
460
|
return __awaiter(this, void 0, void 0, function () {
|
|
460
461
|
var _this = this;
|
|
461
462
|
return __generator(this, function (_a) {
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
case 1:
|
|
476
|
-
cart = _g.sent();
|
|
477
|
-
lineItem = cart.items.find(function (li) { return li.id === lineItemId; });
|
|
478
|
-
if (!lineItem) {
|
|
479
|
-
return [2 /*return*/, cart];
|
|
480
|
-
}
|
|
481
|
-
if (!(cart.shipping_methods && cart.shipping_methods.length)) return [3 /*break*/, 9];
|
|
482
|
-
_g.label = 2;
|
|
483
|
-
case 2:
|
|
484
|
-
_g.trys.push([2, 7, 8, 9]);
|
|
485
|
-
_a = __values(cart.shipping_methods), _b = _a.next();
|
|
486
|
-
_g.label = 3;
|
|
487
|
-
case 3:
|
|
488
|
-
if (!!_b.done) return [3 /*break*/, 6];
|
|
489
|
-
method = _b.value;
|
|
490
|
-
return [4 /*yield*/, this.shippingOptionService_
|
|
491
|
-
.withTransaction(manager)
|
|
492
|
-
.deleteShippingMethod(method)];
|
|
493
|
-
case 4:
|
|
494
|
-
_g.sent();
|
|
495
|
-
_g.label = 5;
|
|
496
|
-
case 5:
|
|
497
|
-
_b = _a.next();
|
|
498
|
-
return [3 /*break*/, 3];
|
|
499
|
-
case 6: return [3 /*break*/, 9];
|
|
500
|
-
case 7:
|
|
501
|
-
e_3_1 = _g.sent();
|
|
502
|
-
e_3 = { error: e_3_1 };
|
|
503
|
-
return [3 /*break*/, 9];
|
|
504
|
-
case 8:
|
|
505
|
-
try {
|
|
506
|
-
if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
|
|
507
|
-
}
|
|
508
|
-
finally { if (e_3) throw e_3.error; }
|
|
509
|
-
return [7 /*endfinally*/];
|
|
510
|
-
case 9:
|
|
511
|
-
_g.trys.push([9, 14, 15, 16]);
|
|
512
|
-
_c = __values(cart.items), _d = _c.next();
|
|
513
|
-
_g.label = 10;
|
|
514
|
-
case 10:
|
|
515
|
-
if (!!_d.done) return [3 /*break*/, 13];
|
|
516
|
-
itm = _d.value;
|
|
517
|
-
return [4 /*yield*/, this.lineItemService_.withTransaction(manager).update(itm.id, {
|
|
518
|
-
has_shipping: false,
|
|
463
|
+
switch (_a.label) {
|
|
464
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
465
|
+
var cart, lineItem, lineItemRepository, result;
|
|
466
|
+
var _a;
|
|
467
|
+
return __generator(this, function (_b) {
|
|
468
|
+
switch (_b.label) {
|
|
469
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
470
|
+
relations: [
|
|
471
|
+
"items",
|
|
472
|
+
"items.variant",
|
|
473
|
+
"items.variant.product",
|
|
474
|
+
"payment_sessions",
|
|
475
|
+
],
|
|
519
476
|
})];
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
477
|
+
case 1:
|
|
478
|
+
cart = _b.sent();
|
|
479
|
+
lineItem = cart.items.find(function (item) { return item.id === lineItemId; });
|
|
480
|
+
if (!lineItem) {
|
|
481
|
+
return [2 /*return*/, cart];
|
|
482
|
+
}
|
|
483
|
+
if (!((_a = cart.shipping_methods) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 3];
|
|
484
|
+
return [4 /*yield*/, this.shippingOptionService_
|
|
485
|
+
.withTransaction(transactionManager)
|
|
486
|
+
.deleteShippingMethods(cart.shipping_methods)];
|
|
487
|
+
case 2:
|
|
488
|
+
_b.sent();
|
|
489
|
+
_b.label = 3;
|
|
490
|
+
case 3:
|
|
491
|
+
lineItemRepository = transactionManager.getCustomRepository(this.lineItemRepository_);
|
|
492
|
+
return [4 /*yield*/, lineItemRepository.update({
|
|
493
|
+
id: (0, typeorm_1.In)(cart.items.map(function (item) { return item.id; })),
|
|
494
|
+
}, {
|
|
495
|
+
has_shipping: false,
|
|
496
|
+
})];
|
|
497
|
+
case 4:
|
|
498
|
+
_b.sent();
|
|
499
|
+
return [4 /*yield*/, this.lineItemService_
|
|
500
|
+
.withTransaction(transactionManager)
|
|
501
|
+
.delete(lineItem.id)];
|
|
502
|
+
case 5:
|
|
503
|
+
_b.sent();
|
|
504
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
505
|
+
relations: ["items", "discounts", "discounts.rule"],
|
|
506
|
+
})];
|
|
507
|
+
case 6:
|
|
508
|
+
result = _b.sent();
|
|
509
|
+
return [4 /*yield*/, this.refreshAdjustments_(result)
|
|
510
|
+
// Notify subscribers
|
|
511
|
+
];
|
|
512
|
+
case 7:
|
|
513
|
+
_b.sent();
|
|
541
514
|
// Notify subscribers
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
});
|
|
557
|
-
}); })];
|
|
515
|
+
return [4 /*yield*/, this.eventBus_
|
|
516
|
+
.withTransaction(transactionManager)
|
|
517
|
+
.emit(CartService.Events.UPDATED, {
|
|
518
|
+
id: cart.id,
|
|
519
|
+
})];
|
|
520
|
+
case 8:
|
|
521
|
+
// Notify subscribers
|
|
522
|
+
_b.sent();
|
|
523
|
+
return [2 /*return*/, this.retrieve(cartId)];
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
}); })];
|
|
527
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
528
|
+
}
|
|
558
529
|
});
|
|
559
530
|
});
|
|
560
531
|
};
|
|
@@ -593,128 +564,86 @@ var CartService = /** @class */ (function (_super) {
|
|
|
593
564
|
return __awaiter(this, void 0, void 0, function () {
|
|
594
565
|
var _this = this;
|
|
595
566
|
return __generator(this, function (_a) {
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
if (lineItem.should_merge) {
|
|
613
|
-
currentItem = cart.items.find(function (line) {
|
|
614
|
-
if (line.should_merge && line.variant_id === lineItem.variant_id) {
|
|
615
|
-
return lodash_1.default.isEqual(line.metadata, lineItem.metadata);
|
|
616
|
-
}
|
|
617
|
-
return false;
|
|
618
|
-
});
|
|
619
|
-
}
|
|
620
|
-
if (!currentItem) return [3 /*break*/, 4];
|
|
621
|
-
newQuantity = currentItem.quantity + lineItem.quantity;
|
|
622
|
-
// Confirm inventory or throw error
|
|
623
|
-
return [4 /*yield*/, this.inventoryService_
|
|
624
|
-
.withTransaction(manager)
|
|
625
|
-
.confirmInventory(lineItem.variant_id, newQuantity)];
|
|
626
|
-
case 2:
|
|
627
|
-
// Confirm inventory or throw error
|
|
628
|
-
_g.sent();
|
|
629
|
-
return [4 /*yield*/, this.lineItemService_
|
|
630
|
-
.withTransaction(manager)
|
|
631
|
-
.update(currentItem.id, {
|
|
632
|
-
quantity: newQuantity,
|
|
633
|
-
})];
|
|
634
|
-
case 3:
|
|
635
|
-
_g.sent();
|
|
636
|
-
return [3 /*break*/, 7];
|
|
637
|
-
case 4:
|
|
638
|
-
// Confirm inventory or throw error
|
|
639
|
-
return [4 /*yield*/, this.inventoryService_
|
|
640
|
-
.withTransaction(manager)
|
|
641
|
-
.confirmInventory(lineItem.variant_id, lineItem.quantity)];
|
|
642
|
-
case 5:
|
|
643
|
-
// Confirm inventory or throw error
|
|
644
|
-
_g.sent();
|
|
645
|
-
return [4 /*yield*/, this.lineItemService_.withTransaction(manager).create(__assign(__assign({}, lineItem), { has_shipping: false, cart_id: cartId }))];
|
|
646
|
-
case 6:
|
|
647
|
-
_g.sent();
|
|
648
|
-
_g.label = 7;
|
|
649
|
-
case 7:
|
|
650
|
-
_g.trys.push([7, 12, 13, 14]);
|
|
651
|
-
_a = __values(cart.items), _b = _a.next();
|
|
652
|
-
_g.label = 8;
|
|
653
|
-
case 8:
|
|
654
|
-
if (!!_b.done) return [3 /*break*/, 11];
|
|
655
|
-
itm = _b.value;
|
|
656
|
-
return [4 /*yield*/, this.lineItemService_.withTransaction(manager).update(itm.id, {
|
|
657
|
-
has_shipping: false,
|
|
567
|
+
switch (_a.label) {
|
|
568
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
569
|
+
var cart, currentItem, quantity, lineItemRepository, result;
|
|
570
|
+
return __generator(this, function (_a) {
|
|
571
|
+
switch (_a.label) {
|
|
572
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
573
|
+
relations: [
|
|
574
|
+
"shipping_methods",
|
|
575
|
+
"items",
|
|
576
|
+
"items.adjustments",
|
|
577
|
+
"payment_sessions",
|
|
578
|
+
"items.variant",
|
|
579
|
+
"items.variant.product",
|
|
580
|
+
"discounts",
|
|
581
|
+
"discounts.rule",
|
|
582
|
+
],
|
|
658
583
|
})];
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
584
|
+
case 1:
|
|
585
|
+
cart = _a.sent();
|
|
586
|
+
if (lineItem.should_merge) {
|
|
587
|
+
currentItem = cart.items.find(function (item) {
|
|
588
|
+
if (item.should_merge && item.variant_id === lineItem.variant_id) {
|
|
589
|
+
return lodash_1.default.isEqual(item.metadata, lineItem.metadata);
|
|
590
|
+
}
|
|
591
|
+
return false;
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
quantity = currentItem
|
|
595
|
+
? (currentItem.quantity += lineItem.quantity)
|
|
596
|
+
: lineItem.quantity;
|
|
597
|
+
// Confirm inventory or throw error
|
|
598
|
+
return [4 /*yield*/, this.inventoryService_
|
|
599
|
+
.withTransaction(transactionManager)
|
|
600
|
+
.confirmInventory(lineItem.variant_id, quantity)];
|
|
601
|
+
case 2:
|
|
602
|
+
// Confirm inventory or throw error
|
|
603
|
+
_a.sent();
|
|
604
|
+
if (!currentItem) return [3 /*break*/, 4];
|
|
605
|
+
return [4 /*yield*/, this.lineItemService_
|
|
606
|
+
.withTransaction(transactionManager)
|
|
607
|
+
.update(currentItem.id, {
|
|
608
|
+
quantity: currentItem.quantity,
|
|
609
|
+
})];
|
|
610
|
+
case 3:
|
|
611
|
+
_a.sent();
|
|
612
|
+
return [3 /*break*/, 6];
|
|
613
|
+
case 4: return [4 /*yield*/, this.lineItemService_
|
|
614
|
+
.withTransaction(transactionManager)
|
|
615
|
+
.create(__assign(__assign({}, lineItem), { has_shipping: false, cart_id: cartId }))];
|
|
616
|
+
case 5:
|
|
617
|
+
_a.sent();
|
|
618
|
+
_a.label = 6;
|
|
619
|
+
case 6:
|
|
620
|
+
lineItemRepository = transactionManager.getCustomRepository(this.lineItemRepository_);
|
|
621
|
+
return [4 /*yield*/, lineItemRepository.update({
|
|
622
|
+
id: (0, typeorm_1.In)(cart.items.map(function (item) { return item.id; })),
|
|
623
|
+
}, {
|
|
624
|
+
has_shipping: false,
|
|
625
|
+
})];
|
|
626
|
+
case 7:
|
|
627
|
+
_a.sent();
|
|
628
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
629
|
+
relations: ["items", "discounts", "discounts.rule"],
|
|
630
|
+
})];
|
|
631
|
+
case 8:
|
|
632
|
+
result = _a.sent();
|
|
633
|
+
return [4 /*yield*/, this.refreshAdjustments_(result)];
|
|
634
|
+
case 9:
|
|
635
|
+
_a.sent();
|
|
636
|
+
return [4 /*yield*/, this.eventBus_
|
|
637
|
+
.withTransaction(transactionManager)
|
|
638
|
+
.emit(CartService.Events.UPDATED, result)];
|
|
639
|
+
case 10:
|
|
640
|
+
_a.sent();
|
|
641
|
+
return [2 /*return*/, result];
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
}); })];
|
|
645
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
646
|
+
}
|
|
718
647
|
});
|
|
719
648
|
});
|
|
720
649
|
};
|
|
@@ -729,50 +658,60 @@ var CartService = /** @class */ (function (_super) {
|
|
|
729
658
|
return __awaiter(this, void 0, void 0, function () {
|
|
730
659
|
var _this = this;
|
|
731
660
|
return __generator(this, function (_a) {
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
661
|
+
switch (_a.label) {
|
|
662
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
663
|
+
var cart, lineItemExists, hasInventory, updatedCart;
|
|
664
|
+
return __generator(this, function (_a) {
|
|
665
|
+
switch (_a.label) {
|
|
666
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
667
|
+
relations: ["items", "items.adjustments", "payment_sessions"],
|
|
668
|
+
})
|
|
669
|
+
// Ensure that the line item exists in the cart
|
|
670
|
+
];
|
|
671
|
+
case 1:
|
|
672
|
+
cart = _a.sent();
|
|
673
|
+
lineItemExists = cart.items.find(function (i) { return i.id === lineItemId; });
|
|
674
|
+
if (!lineItemExists) {
|
|
675
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "A line item with the provided id doesn't exist in the cart");
|
|
676
|
+
}
|
|
677
|
+
if (!lineItemUpdate.quantity) return [3 /*break*/, 3];
|
|
678
|
+
return [4 /*yield*/, this.inventoryService_
|
|
679
|
+
.withTransaction(transactionManager)
|
|
680
|
+
.confirmInventory(lineItemExists.variant_id, lineItemUpdate.quantity)];
|
|
681
|
+
case 2:
|
|
682
|
+
hasInventory = _a.sent();
|
|
683
|
+
if (!hasInventory) {
|
|
684
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Inventory doesn't cover the desired quantity");
|
|
685
|
+
}
|
|
686
|
+
_a.label = 3;
|
|
687
|
+
case 3: return [4 /*yield*/, this.lineItemService_
|
|
688
|
+
.withTransaction(transactionManager)
|
|
689
|
+
.update(lineItemId, lineItemUpdate)];
|
|
690
|
+
case 4:
|
|
691
|
+
_a.sent();
|
|
692
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
693
|
+
relations: ["items", "discounts", "discounts.rule"],
|
|
694
|
+
})];
|
|
695
|
+
case 5:
|
|
696
|
+
updatedCart = _a.sent();
|
|
697
|
+
return [4 /*yield*/, this.refreshAdjustments_(updatedCart)
|
|
698
|
+
// Update the line item
|
|
699
|
+
];
|
|
700
|
+
case 6:
|
|
701
|
+
_a.sent();
|
|
702
|
+
// Update the line item
|
|
703
|
+
return [4 /*yield*/, this.eventBus_
|
|
704
|
+
.withTransaction(transactionManager)
|
|
705
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
706
|
+
case 7:
|
|
707
|
+
// Update the line item
|
|
708
|
+
_a.sent();
|
|
709
|
+
return [2 /*return*/, updatedCart];
|
|
710
|
+
}
|
|
711
|
+
});
|
|
712
|
+
}); })];
|
|
713
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
714
|
+
}
|
|
776
715
|
});
|
|
777
716
|
});
|
|
778
717
|
};
|
|
@@ -786,260 +725,196 @@ var CartService = /** @class */ (function (_super) {
|
|
|
786
725
|
* @return void
|
|
787
726
|
*/
|
|
788
727
|
CartService.prototype.adjustFreeShipping_ = function (cart, shouldAdd) {
|
|
789
|
-
var _a;
|
|
728
|
+
var _a, _b;
|
|
790
729
|
return __awaiter(this, void 0, void 0, function () {
|
|
730
|
+
var transactionManager, shippingMethodRepository_1;
|
|
791
731
|
var _this = this;
|
|
792
|
-
return __generator(this, function (
|
|
793
|
-
switch (
|
|
732
|
+
return __generator(this, function (_c) {
|
|
733
|
+
switch (_c.label) {
|
|
794
734
|
case 0:
|
|
795
|
-
|
|
735
|
+
transactionManager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
|
|
736
|
+
if (!((_b = cart.shipping_methods) === null || _b === void 0 ? void 0 : _b.length)) return [3 /*break*/, 4];
|
|
737
|
+
shippingMethodRepository_1 = transactionManager.getCustomRepository(this.shippingMethodRepository_);
|
|
796
738
|
if (!shouldAdd) return [3 /*break*/, 2];
|
|
797
|
-
return [4 /*yield*/,
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
smRepo = this.manager_.getCustomRepository(this.shippingMethodRepository_);
|
|
803
|
-
return [4 /*yield*/, smRepo.findOne({ where: { id: sm.id } })];
|
|
804
|
-
case 1:
|
|
805
|
-
method = _a.sent();
|
|
806
|
-
if (!method) return [3 /*break*/, 3];
|
|
807
|
-
method.price = 0;
|
|
808
|
-
return [4 /*yield*/, smRepo.save(method)];
|
|
809
|
-
case 2:
|
|
810
|
-
_a.sent();
|
|
811
|
-
_a.label = 3;
|
|
812
|
-
case 3: return [2 /*return*/];
|
|
813
|
-
}
|
|
814
|
-
});
|
|
815
|
-
}); }))];
|
|
739
|
+
return [4 /*yield*/, shippingMethodRepository_1.update({
|
|
740
|
+
id: (0, typeorm_1.In)(cart.shipping_methods.map(function (shippingMethod) { return shippingMethod.id; })),
|
|
741
|
+
}, {
|
|
742
|
+
price: 0,
|
|
743
|
+
})];
|
|
816
744
|
case 1:
|
|
817
|
-
|
|
745
|
+
_c.sent();
|
|
818
746
|
return [3 /*break*/, 4];
|
|
819
|
-
case 2: return [4 /*yield*/, Promise.all(cart.shipping_methods.map(function (
|
|
820
|
-
var smRepo;
|
|
747
|
+
case 2: return [4 /*yield*/, Promise.all(cart.shipping_methods.map(function (shippingMethod) { return __awaiter(_this, void 0, void 0, function () {
|
|
821
748
|
return __generator(this, function (_a) {
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
// back to its original amount
|
|
827
|
-
sm.price = sm.shipping_option.amount;
|
|
828
|
-
return [4 /*yield*/, smRepo.save(sm)];
|
|
829
|
-
case 1:
|
|
830
|
-
_a.sent();
|
|
831
|
-
return [2 /*return*/];
|
|
832
|
-
}
|
|
749
|
+
// if free shipping discount is removed, we adjust the shipping
|
|
750
|
+
// back to its original amount
|
|
751
|
+
shippingMethod.price = shippingMethod.shipping_option.amount;
|
|
752
|
+
return [2 /*return*/, shippingMethodRepository_1.save(shippingMethod)];
|
|
833
753
|
});
|
|
834
754
|
}); }))];
|
|
835
755
|
case 3:
|
|
836
|
-
|
|
837
|
-
|
|
756
|
+
_c.sent();
|
|
757
|
+
_c.label = 4;
|
|
838
758
|
case 4: return [2 /*return*/];
|
|
839
759
|
}
|
|
840
760
|
});
|
|
841
761
|
});
|
|
842
762
|
};
|
|
843
|
-
CartService.prototype.update = function (cartId,
|
|
763
|
+
CartService.prototype.update = function (cartId, data) {
|
|
844
764
|
return __awaiter(this, void 0, void 0, function () {
|
|
845
765
|
var _this = this;
|
|
846
766
|
return __generator(this, function (_a) {
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
return [3 /*break*/, 25];
|
|
999
|
-
case 28: return [3 /*break*/, 31];
|
|
1000
|
-
case 29:
|
|
1001
|
-
e_8_1 = _j.sent();
|
|
1002
|
-
e_8 = { error: e_8_1 };
|
|
1003
|
-
return [3 /*break*/, 31];
|
|
1004
|
-
case 30:
|
|
1005
|
-
try {
|
|
1006
|
-
if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
|
|
1007
|
-
}
|
|
1008
|
-
finally { if (e_8) throw e_8.error; }
|
|
1009
|
-
return [7 /*endfinally*/];
|
|
1010
|
-
case 31:
|
|
1011
|
-
if ("metadata" in update) {
|
|
1012
|
-
cart.metadata = this.setMetadata_(cart, update.metadata);
|
|
1013
|
-
}
|
|
1014
|
-
if ("context" in update) {
|
|
1015
|
-
prevContext = cart.context || {};
|
|
1016
|
-
cart.context = __assign(__assign({}, prevContext), update.context);
|
|
1017
|
-
}
|
|
1018
|
-
if ("completed_at" in update) {
|
|
1019
|
-
cart.completed_at = update.completed_at;
|
|
1020
|
-
}
|
|
1021
|
-
if ("payment_authorized_at" in update) {
|
|
1022
|
-
cart.payment_authorized_at = update.payment_authorized_at;
|
|
1023
|
-
}
|
|
1024
|
-
return [4 /*yield*/, cartRepo.save(cart)];
|
|
1025
|
-
case 32:
|
|
1026
|
-
result = _j.sent();
|
|
1027
|
-
if (!("email" in update || "customer_id" in update)) return [3 /*break*/, 34];
|
|
1028
|
-
return [4 /*yield*/, this.eventBus_
|
|
1029
|
-
.withTransaction(manager)
|
|
1030
|
-
.emit(CartService.Events.CUSTOMER_UPDATED, result.id)];
|
|
1031
|
-
case 33:
|
|
1032
|
-
_j.sent();
|
|
1033
|
-
_j.label = 34;
|
|
1034
|
-
case 34: return [4 /*yield*/, this.eventBus_
|
|
1035
|
-
.withTransaction(manager)
|
|
1036
|
-
.emit(CartService.Events.UPDATED, result)];
|
|
1037
|
-
case 35:
|
|
1038
|
-
_j.sent();
|
|
1039
|
-
return [2 /*return*/, result];
|
|
1040
|
-
}
|
|
1041
|
-
});
|
|
1042
|
-
}); })];
|
|
767
|
+
switch (_a.label) {
|
|
768
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
769
|
+
var cartRepo, cart, customer, countryCode, addrRepo, billingAddress, shippingAddress, previousDiscounts, hasFreeShipping, prevContext, updatedCart;
|
|
770
|
+
var _this = this;
|
|
771
|
+
var _a, _b, _c, _d, _e;
|
|
772
|
+
return __generator(this, function (_f) {
|
|
773
|
+
switch (_f.label) {
|
|
774
|
+
case 0:
|
|
775
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
776
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
777
|
+
select: [
|
|
778
|
+
"subtotal",
|
|
779
|
+
"tax_total",
|
|
780
|
+
"shipping_total",
|
|
781
|
+
"discount_total",
|
|
782
|
+
"total",
|
|
783
|
+
],
|
|
784
|
+
relations: [
|
|
785
|
+
"items",
|
|
786
|
+
"shipping_methods",
|
|
787
|
+
"shipping_address",
|
|
788
|
+
"billing_address",
|
|
789
|
+
"gift_cards",
|
|
790
|
+
"customer",
|
|
791
|
+
"region",
|
|
792
|
+
"payment_sessions",
|
|
793
|
+
"region.countries",
|
|
794
|
+
"discounts",
|
|
795
|
+
"discounts.rule",
|
|
796
|
+
"discounts.regions",
|
|
797
|
+
],
|
|
798
|
+
})];
|
|
799
|
+
case 1:
|
|
800
|
+
cart = _f.sent();
|
|
801
|
+
if (!data.customer_id) return [3 /*break*/, 3];
|
|
802
|
+
return [4 /*yield*/, this.updateCustomerId_(cart, data.customer_id)];
|
|
803
|
+
case 2:
|
|
804
|
+
_f.sent();
|
|
805
|
+
return [3 /*break*/, 5];
|
|
806
|
+
case 3:
|
|
807
|
+
if (!(typeof data.email !== "undefined")) return [3 /*break*/, 5];
|
|
808
|
+
return [4 /*yield*/, this.createOrFetchUserFromEmail_(data.email)];
|
|
809
|
+
case 4:
|
|
810
|
+
customer = _f.sent();
|
|
811
|
+
cart.customer = customer;
|
|
812
|
+
cart.customer_id = customer.id;
|
|
813
|
+
cart.email = customer.email;
|
|
814
|
+
_f.label = 5;
|
|
815
|
+
case 5:
|
|
816
|
+
if (!(typeof data.region_id !== "undefined")) return [3 /*break*/, 7];
|
|
817
|
+
countryCode = (_b = (data.country_code || ((_a = data.shipping_address) === null || _a === void 0 ? void 0 : _a.country_code))) !== null && _b !== void 0 ? _b : null;
|
|
818
|
+
return [4 /*yield*/, this.setRegion_(cart, data.region_id, countryCode)];
|
|
819
|
+
case 6:
|
|
820
|
+
_f.sent();
|
|
821
|
+
_f.label = 7;
|
|
822
|
+
case 7:
|
|
823
|
+
if (!(typeof data.customer_id !== "undefined" ||
|
|
824
|
+
typeof data.region_id !== "undefined")) return [3 /*break*/, 9];
|
|
825
|
+
return [4 /*yield*/, this.updateUnitPrices_(cart, data.region_id, data.customer_id)];
|
|
826
|
+
case 8:
|
|
827
|
+
_f.sent();
|
|
828
|
+
_f.label = 9;
|
|
829
|
+
case 9:
|
|
830
|
+
addrRepo = transactionManager.getCustomRepository(this.addressRepository_);
|
|
831
|
+
billingAddress = (_c = data.billing_address_id) !== null && _c !== void 0 ? _c : data.billing_address;
|
|
832
|
+
if (!(billingAddress !== undefined)) return [3 /*break*/, 11];
|
|
833
|
+
return [4 /*yield*/, this.updateBillingAddress_(cart, billingAddress, addrRepo)];
|
|
834
|
+
case 10:
|
|
835
|
+
_f.sent();
|
|
836
|
+
_f.label = 11;
|
|
837
|
+
case 11:
|
|
838
|
+
shippingAddress = (_d = data.shipping_address_id) !== null && _d !== void 0 ? _d : data.shipping_address;
|
|
839
|
+
if (!(shippingAddress !== undefined)) return [3 /*break*/, 13];
|
|
840
|
+
return [4 /*yield*/, this.updateShippingAddress_(cart, shippingAddress, addrRepo)];
|
|
841
|
+
case 12:
|
|
842
|
+
_f.sent();
|
|
843
|
+
_f.label = 13;
|
|
844
|
+
case 13:
|
|
845
|
+
if (!(typeof data.discounts !== "undefined")) return [3 /*break*/, 18];
|
|
846
|
+
previousDiscounts = __spreadArray([], __read(cart.discounts), false);
|
|
847
|
+
cart.discounts.length = 0;
|
|
848
|
+
return [4 /*yield*/, Promise.all(data.discounts.map(function (_a) {
|
|
849
|
+
var code = _a.code;
|
|
850
|
+
return _this.applyDiscount(cart, code);
|
|
851
|
+
}))];
|
|
852
|
+
case 14:
|
|
853
|
+
_f.sent();
|
|
854
|
+
hasFreeShipping = cart.discounts.some(function (_a) {
|
|
855
|
+
var rule = _a.rule;
|
|
856
|
+
return (rule === null || rule === void 0 ? void 0 : rule.type) === "free_shipping";
|
|
857
|
+
});
|
|
858
|
+
if (!(previousDiscounts.some(function (_a) {
|
|
859
|
+
var rule = _a.rule;
|
|
860
|
+
return rule.type === "free_shipping";
|
|
861
|
+
}) &&
|
|
862
|
+
!hasFreeShipping)) return [3 /*break*/, 16];
|
|
863
|
+
return [4 /*yield*/, this.adjustFreeShipping_(cart, false)];
|
|
864
|
+
case 15:
|
|
865
|
+
_f.sent();
|
|
866
|
+
_f.label = 16;
|
|
867
|
+
case 16:
|
|
868
|
+
if (!hasFreeShipping) return [3 /*break*/, 18];
|
|
869
|
+
return [4 /*yield*/, this.adjustFreeShipping_(cart, true)];
|
|
870
|
+
case 17:
|
|
871
|
+
_f.sent();
|
|
872
|
+
_f.label = 18;
|
|
873
|
+
case 18:
|
|
874
|
+
if (!("gift_cards" in data)) return [3 /*break*/, 20];
|
|
875
|
+
cart.gift_cards = [];
|
|
876
|
+
return [4 /*yield*/, Promise.all(((_e = data.gift_cards) !== null && _e !== void 0 ? _e : []).map(function (_a) {
|
|
877
|
+
var code = _a.code;
|
|
878
|
+
return _this.applyGiftCard_(cart, code);
|
|
879
|
+
}))];
|
|
880
|
+
case 19:
|
|
881
|
+
_f.sent();
|
|
882
|
+
_f.label = 20;
|
|
883
|
+
case 20:
|
|
884
|
+
if (data === null || data === void 0 ? void 0 : data.metadata) {
|
|
885
|
+
cart.metadata = (0, utils_1.setMetadata)(cart, data.metadata);
|
|
886
|
+
}
|
|
887
|
+
if ("context" in data) {
|
|
888
|
+
prevContext = cart.context || {};
|
|
889
|
+
cart.context = __assign(__assign({}, prevContext), data.context);
|
|
890
|
+
}
|
|
891
|
+
if ("completed_at" in data) {
|
|
892
|
+
cart.completed_at = data.completed_at;
|
|
893
|
+
}
|
|
894
|
+
if ("payment_authorized_at" in data) {
|
|
895
|
+
cart.payment_authorized_at = data.payment_authorized_at;
|
|
896
|
+
}
|
|
897
|
+
return [4 /*yield*/, cartRepo.save(cart)];
|
|
898
|
+
case 21:
|
|
899
|
+
updatedCart = _f.sent();
|
|
900
|
+
if (!("email" in data || "customer_id" in data)) return [3 /*break*/, 23];
|
|
901
|
+
return [4 /*yield*/, this.eventBus_
|
|
902
|
+
.withTransaction(transactionManager)
|
|
903
|
+
.emit(CartService.Events.CUSTOMER_UPDATED, updatedCart.id)];
|
|
904
|
+
case 22:
|
|
905
|
+
_f.sent();
|
|
906
|
+
_f.label = 23;
|
|
907
|
+
case 23: return [4 /*yield*/, this.eventBus_
|
|
908
|
+
.withTransaction(transactionManager)
|
|
909
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
910
|
+
case 24:
|
|
911
|
+
_f.sent();
|
|
912
|
+
return [2 /*return*/, updatedCart];
|
|
913
|
+
}
|
|
914
|
+
});
|
|
915
|
+
}); })];
|
|
916
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
917
|
+
}
|
|
1043
918
|
});
|
|
1044
919
|
});
|
|
1045
920
|
};
|
|
@@ -1111,27 +986,27 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1111
986
|
CartService.prototype.updateBillingAddress_ = function (cart, addressOrId, addrRepo) {
|
|
1112
987
|
var _a, _b;
|
|
1113
988
|
return __awaiter(this, void 0, void 0, function () {
|
|
1114
|
-
var address,
|
|
1115
|
-
return __generator(this, function (
|
|
1116
|
-
switch (
|
|
989
|
+
var address, _c, addr;
|
|
990
|
+
return __generator(this, function (_d) {
|
|
991
|
+
switch (_d.label) {
|
|
1117
992
|
case 0:
|
|
1118
993
|
if (!(typeof addressOrId === "string")) return [3 /*break*/, 2];
|
|
1119
994
|
return [4 /*yield*/, addrRepo.findOne({
|
|
1120
995
|
where: { id: addressOrId },
|
|
1121
996
|
})];
|
|
1122
997
|
case 1:
|
|
1123
|
-
address = (
|
|
998
|
+
address = (_d.sent());
|
|
1124
999
|
return [3 /*break*/, 3];
|
|
1125
1000
|
case 2:
|
|
1126
1001
|
address = addressOrId;
|
|
1127
|
-
|
|
1002
|
+
_d.label = 3;
|
|
1128
1003
|
case 3:
|
|
1129
1004
|
address.country_code = (_b = (_a = address.country_code) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : null;
|
|
1130
1005
|
if (!address.id) return [3 /*break*/, 5];
|
|
1006
|
+
_c = cart;
|
|
1131
1007
|
return [4 /*yield*/, addrRepo.save(address)];
|
|
1132
1008
|
case 4:
|
|
1133
|
-
|
|
1134
|
-
cart.billing_address = updated;
|
|
1009
|
+
_c.billing_address = _d.sent();
|
|
1135
1010
|
return [3 /*break*/, 9];
|
|
1136
1011
|
case 5:
|
|
1137
1012
|
if (!cart.billing_address_id) return [3 /*break*/, 8];
|
|
@@ -1139,15 +1014,14 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1139
1014
|
where: { id: cart.billing_address_id },
|
|
1140
1015
|
})];
|
|
1141
1016
|
case 6:
|
|
1142
|
-
addr =
|
|
1017
|
+
addr = _d.sent();
|
|
1143
1018
|
return [4 /*yield*/, addrRepo.save(__assign(__assign({}, addr), address))];
|
|
1144
1019
|
case 7:
|
|
1145
|
-
|
|
1020
|
+
_d.sent();
|
|
1146
1021
|
return [3 /*break*/, 9];
|
|
1147
1022
|
case 8:
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
_c.label = 9;
|
|
1023
|
+
cart.billing_address = addrRepo.create(__assign({}, address));
|
|
1024
|
+
_d.label = 9;
|
|
1151
1025
|
case 9: return [2 /*return*/];
|
|
1152
1026
|
}
|
|
1153
1027
|
});
|
|
@@ -1163,9 +1037,9 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1163
1037
|
CartService.prototype.updateShippingAddress_ = function (cart, addressOrId, addrRepo) {
|
|
1164
1038
|
var _a, _b;
|
|
1165
1039
|
return __awaiter(this, void 0, void 0, function () {
|
|
1166
|
-
var address,
|
|
1167
|
-
return __generator(this, function (
|
|
1168
|
-
switch (
|
|
1040
|
+
var address, _c, addr;
|
|
1041
|
+
return __generator(this, function (_d) {
|
|
1042
|
+
switch (_d.label) {
|
|
1169
1043
|
case 0:
|
|
1170
1044
|
if (addressOrId === null) {
|
|
1171
1045
|
cart.shipping_address = null;
|
|
@@ -1176,11 +1050,11 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1176
1050
|
where: { id: addressOrId },
|
|
1177
1051
|
})];
|
|
1178
1052
|
case 1:
|
|
1179
|
-
address = (
|
|
1053
|
+
address = (_d.sent());
|
|
1180
1054
|
return [3 /*break*/, 3];
|
|
1181
1055
|
case 2:
|
|
1182
1056
|
address = addressOrId;
|
|
1183
|
-
|
|
1057
|
+
_d.label = 3;
|
|
1184
1058
|
case 3:
|
|
1185
1059
|
address.country_code = (_b = (_a = address.country_code) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : null;
|
|
1186
1060
|
if (address.country_code &&
|
|
@@ -1191,10 +1065,10 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1191
1065
|
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Shipping country must be in the cart region");
|
|
1192
1066
|
}
|
|
1193
1067
|
if (!address.id) return [3 /*break*/, 5];
|
|
1068
|
+
_c = cart;
|
|
1194
1069
|
return [4 /*yield*/, addrRepo.save(address)];
|
|
1195
1070
|
case 4:
|
|
1196
|
-
|
|
1197
|
-
cart.shipping_address = updated;
|
|
1071
|
+
_c.shipping_address = _d.sent();
|
|
1198
1072
|
return [3 /*break*/, 9];
|
|
1199
1073
|
case 5:
|
|
1200
1074
|
if (!cart.shipping_address_id) return [3 /*break*/, 8];
|
|
@@ -1202,15 +1076,14 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1202
1076
|
where: { id: cart.shipping_address_id },
|
|
1203
1077
|
})];
|
|
1204
1078
|
case 6:
|
|
1205
|
-
addr =
|
|
1079
|
+
addr = _d.sent();
|
|
1206
1080
|
return [4 /*yield*/, addrRepo.save(__assign(__assign({}, addr), address))];
|
|
1207
1081
|
case 7:
|
|
1208
|
-
|
|
1082
|
+
_d.sent();
|
|
1209
1083
|
return [3 /*break*/, 9];
|
|
1210
1084
|
case 8:
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
_c.label = 9;
|
|
1085
|
+
cart.shipping_address = addrRepo.create(__assign({}, address));
|
|
1086
|
+
_d.label = 9;
|
|
1214
1087
|
case 9: return [2 /*return*/];
|
|
1215
1088
|
}
|
|
1216
1089
|
});
|
|
@@ -1221,7 +1094,9 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1221
1094
|
var giftCard;
|
|
1222
1095
|
return __generator(this, function (_a) {
|
|
1223
1096
|
switch (_a.label) {
|
|
1224
|
-
case 0: return [4 /*yield*/, this.giftCardService_
|
|
1097
|
+
case 0: return [4 /*yield*/, this.giftCardService_
|
|
1098
|
+
.withTransaction(this.transactionManager_)
|
|
1099
|
+
.retrieveByCode(code)];
|
|
1225
1100
|
case 1:
|
|
1226
1101
|
giftCard = _a.sent();
|
|
1227
1102
|
if (giftCard.is_disabled) {
|
|
@@ -1235,7 +1110,7 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1235
1110
|
var id = _a.id;
|
|
1236
1111
|
return id === giftCard.id;
|
|
1237
1112
|
})) {
|
|
1238
|
-
return [2 /*return
|
|
1113
|
+
return [2 /*return*/];
|
|
1239
1114
|
}
|
|
1240
1115
|
cart.gift_cards = __spreadArray(__spreadArray([], __read(cart.gift_cards), false), [giftCard], false);
|
|
1241
1116
|
return [2 /*return*/];
|
|
@@ -1254,80 +1129,65 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1254
1129
|
*/
|
|
1255
1130
|
CartService.prototype.applyDiscount = function (cart, discountCode) {
|
|
1256
1131
|
return __awaiter(this, void 0, void 0, function () {
|
|
1257
|
-
var
|
|
1132
|
+
var _this = this;
|
|
1258
1133
|
return __generator(this, function (_a) {
|
|
1259
1134
|
switch (_a.label) {
|
|
1260
|
-
case 0: return [4 /*yield*/, this.
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
}
|
|
1282
|
-
regions = discount.regions;
|
|
1283
|
-
if (!discount.parent_discount_id) return [3 /*break*/, 3];
|
|
1284
|
-
return [4 /*yield*/, this.discountService_.retrieve(discount.parent_discount_id, {
|
|
1285
|
-
relations: ["rule", "regions"],
|
|
1286
|
-
})];
|
|
1287
|
-
case 2:
|
|
1288
|
-
parent = _a.sent();
|
|
1289
|
-
regions = parent.regions;
|
|
1290
|
-
_a.label = 3;
|
|
1291
|
-
case 3:
|
|
1292
|
-
if (discount.is_disabled) {
|
|
1293
|
-
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "The discount code is disabled");
|
|
1294
|
-
}
|
|
1295
|
-
if (!regions.find(function (_a) {
|
|
1296
|
-
var id = _a.id;
|
|
1297
|
-
return id === cart.region_id;
|
|
1298
|
-
})) {
|
|
1299
|
-
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "The discount is not available in current region");
|
|
1300
|
-
}
|
|
1301
|
-
// if discount is already there, we simply resolve
|
|
1302
|
-
if (cart.discounts.find(function (_a) {
|
|
1303
|
-
var id = _a.id;
|
|
1304
|
-
return id === discount.id;
|
|
1305
|
-
})) {
|
|
1306
|
-
return [2 /*return*/, Promise.resolve()];
|
|
1307
|
-
}
|
|
1308
|
-
toParse = __spreadArray(__spreadArray([], __read(cart.discounts), false), [discount], false);
|
|
1309
|
-
sawNotShipping = false;
|
|
1310
|
-
newDiscounts = toParse.map(function (d) {
|
|
1311
|
-
var drule = d.rule;
|
|
1312
|
-
switch (drule.type) {
|
|
1313
|
-
case "free_shipping":
|
|
1314
|
-
if (d.rule.type === rule.type) {
|
|
1315
|
-
return discount;
|
|
1316
|
-
}
|
|
1317
|
-
return d;
|
|
1318
|
-
default:
|
|
1319
|
-
if (!sawNotShipping) {
|
|
1320
|
-
sawNotShipping = true;
|
|
1321
|
-
if (rule.type !== "free_shipping") {
|
|
1322
|
-
return discount;
|
|
1135
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1136
|
+
var discount, rule, toParse, sawNotShipping, newDiscounts;
|
|
1137
|
+
return __generator(this, function (_a) {
|
|
1138
|
+
switch (_a.label) {
|
|
1139
|
+
case 0: return [4 /*yield*/, this.discountService_
|
|
1140
|
+
.withTransaction(transactionManager)
|
|
1141
|
+
.retrieveByCode(discountCode, { relations: ["rule", "regions"] })];
|
|
1142
|
+
case 1:
|
|
1143
|
+
discount = _a.sent();
|
|
1144
|
+
return [4 /*yield*/, this.discountService_
|
|
1145
|
+
.withTransaction(transactionManager)
|
|
1146
|
+
.validateDiscountForCartOrThrow(cart, discount)];
|
|
1147
|
+
case 2:
|
|
1148
|
+
_a.sent();
|
|
1149
|
+
rule = discount.rule;
|
|
1150
|
+
// if discount is already there, we simply resolve
|
|
1151
|
+
if (cart.discounts.find(function (_a) {
|
|
1152
|
+
var id = _a.id;
|
|
1153
|
+
return id === discount.id;
|
|
1154
|
+
})) {
|
|
1155
|
+
return [2 /*return*/];
|
|
1323
1156
|
}
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1157
|
+
toParse = __spreadArray(__spreadArray([], __read(cart.discounts), false), [discount], false);
|
|
1158
|
+
sawNotShipping = false;
|
|
1159
|
+
newDiscounts = toParse.map(function (discountToParse) {
|
|
1160
|
+
var _a;
|
|
1161
|
+
switch ((_a = discountToParse.rule) === null || _a === void 0 ? void 0 : _a.type) {
|
|
1162
|
+
case "free_shipping":
|
|
1163
|
+
if (discountToParse.rule.type === rule.type) {
|
|
1164
|
+
return discount;
|
|
1165
|
+
}
|
|
1166
|
+
return discountToParse;
|
|
1167
|
+
default:
|
|
1168
|
+
if (!sawNotShipping) {
|
|
1169
|
+
sawNotShipping = true;
|
|
1170
|
+
if ((rule === null || rule === void 0 ? void 0 : rule.type) !== "free_shipping") {
|
|
1171
|
+
return discount;
|
|
1172
|
+
}
|
|
1173
|
+
return discountToParse;
|
|
1174
|
+
}
|
|
1175
|
+
return null;
|
|
1176
|
+
}
|
|
1177
|
+
});
|
|
1178
|
+
cart.discounts = newDiscounts.filter(function (newDiscount) {
|
|
1179
|
+
return !!newDiscount;
|
|
1180
|
+
});
|
|
1181
|
+
if (!((rule === null || rule === void 0 ? void 0 : rule.type) !== "free_shipping" && (cart === null || cart === void 0 ? void 0 : cart.items))) return [3 /*break*/, 4];
|
|
1182
|
+
return [4 /*yield*/, this.refreshAdjustments_(cart)];
|
|
1183
|
+
case 3:
|
|
1184
|
+
_a.sent();
|
|
1185
|
+
_a.label = 4;
|
|
1186
|
+
case 4: return [2 /*return*/];
|
|
1187
|
+
}
|
|
1188
|
+
});
|
|
1189
|
+
}); })];
|
|
1190
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1331
1191
|
}
|
|
1332
1192
|
});
|
|
1333
1193
|
});
|
|
@@ -1342,89 +1202,96 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1342
1202
|
return __awaiter(this, void 0, void 0, function () {
|
|
1343
1203
|
var _this = this;
|
|
1344
1204
|
return __generator(this, function (_a) {
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
*
|
|
1396
|
-
* @
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1205
|
+
switch (_a.label) {
|
|
1206
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1207
|
+
var cart, cartRepo, updatedCart;
|
|
1208
|
+
var _a;
|
|
1209
|
+
return __generator(this, function (_b) {
|
|
1210
|
+
switch (_b.label) {
|
|
1211
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
1212
|
+
relations: [
|
|
1213
|
+
"discounts",
|
|
1214
|
+
"discounts.rule",
|
|
1215
|
+
"payment_sessions",
|
|
1216
|
+
"shipping_methods",
|
|
1217
|
+
],
|
|
1218
|
+
})];
|
|
1219
|
+
case 1:
|
|
1220
|
+
cart = _b.sent();
|
|
1221
|
+
if (!cart.discounts.some(function (_a) {
|
|
1222
|
+
var rule = _a.rule;
|
|
1223
|
+
return rule.type === "free_shipping";
|
|
1224
|
+
})) return [3 /*break*/, 3];
|
|
1225
|
+
return [4 /*yield*/, this.adjustFreeShipping_(cart, false)];
|
|
1226
|
+
case 2:
|
|
1227
|
+
_b.sent();
|
|
1228
|
+
_b.label = 3;
|
|
1229
|
+
case 3:
|
|
1230
|
+
cart.discounts = cart.discounts.filter(function (discount) { return discount.code !== discountCode; });
|
|
1231
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
1232
|
+
return [4 /*yield*/, cartRepo.save(cart)];
|
|
1233
|
+
case 4:
|
|
1234
|
+
updatedCart = _b.sent();
|
|
1235
|
+
if (!((_a = updatedCart.payment_sessions) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 6];
|
|
1236
|
+
return [4 /*yield*/, this.setPaymentSessions(cartId)];
|
|
1237
|
+
case 5:
|
|
1238
|
+
_b.sent();
|
|
1239
|
+
_b.label = 6;
|
|
1240
|
+
case 6: return [4 /*yield*/, this.eventBus_
|
|
1241
|
+
.withTransaction(transactionManager)
|
|
1242
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
1243
|
+
case 7:
|
|
1244
|
+
_b.sent();
|
|
1245
|
+
return [2 /*return*/, updatedCart];
|
|
1246
|
+
}
|
|
1247
|
+
});
|
|
1248
|
+
}); })];
|
|
1249
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1250
|
+
}
|
|
1251
|
+
});
|
|
1252
|
+
});
|
|
1253
|
+
};
|
|
1254
|
+
/**
|
|
1255
|
+
* Updates the currently selected payment session.
|
|
1256
|
+
* @param cartId - the id of the cart to update the payment session for
|
|
1257
|
+
* @param update - the data to update the payment session with
|
|
1258
|
+
* @return the resulting cart
|
|
1259
|
+
*/
|
|
1260
|
+
CartService.prototype.updatePaymentSession = function (cartId, update) {
|
|
1261
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1262
|
+
var _this = this;
|
|
1401
1263
|
return __generator(this, function (_a) {
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
return [4 /*yield*/, this.
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1264
|
+
switch (_a.label) {
|
|
1265
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1266
|
+
var cart, updatedCart;
|
|
1267
|
+
return __generator(this, function (_a) {
|
|
1268
|
+
switch (_a.label) {
|
|
1269
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
1270
|
+
relations: ["payment_sessions"],
|
|
1271
|
+
})];
|
|
1272
|
+
case 1:
|
|
1273
|
+
cart = _a.sent();
|
|
1274
|
+
if (!cart.payment_session) return [3 /*break*/, 3];
|
|
1275
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
1276
|
+
.withTransaction(transactionManager)
|
|
1277
|
+
.updateSessionData(cart.payment_session, update)];
|
|
1278
|
+
case 2:
|
|
1279
|
+
_a.sent();
|
|
1280
|
+
_a.label = 3;
|
|
1281
|
+
case 3: return [4 /*yield*/, this.retrieve(cart.id)];
|
|
1282
|
+
case 4:
|
|
1283
|
+
updatedCart = _a.sent();
|
|
1284
|
+
return [4 /*yield*/, this.eventBus_
|
|
1285
|
+
.withTransaction(transactionManager)
|
|
1286
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
1287
|
+
case 5:
|
|
1288
|
+
_a.sent();
|
|
1289
|
+
return [2 /*return*/, updatedCart];
|
|
1290
|
+
}
|
|
1291
|
+
});
|
|
1292
|
+
}); })];
|
|
1293
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1294
|
+
}
|
|
1428
1295
|
});
|
|
1429
1296
|
});
|
|
1430
1297
|
};
|
|
@@ -1445,58 +1312,64 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1445
1312
|
return __awaiter(this, void 0, void 0, function () {
|
|
1446
1313
|
var _this = this;
|
|
1447
1314
|
return __generator(this, function (_a) {
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
cart.
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
.
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1315
|
+
switch (_a.label) {
|
|
1316
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1317
|
+
var cartRepository, cart, session, freshCart, _a, updatedCart;
|
|
1318
|
+
return __generator(this, function (_b) {
|
|
1319
|
+
switch (_b.label) {
|
|
1320
|
+
case 0:
|
|
1321
|
+
cartRepository = transactionManager.getCustomRepository(this.cartRepository_);
|
|
1322
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
1323
|
+
select: ["total"],
|
|
1324
|
+
relations: ["region", "payment_sessions"],
|
|
1325
|
+
})];
|
|
1326
|
+
case 1:
|
|
1327
|
+
cart = _b.sent();
|
|
1328
|
+
if (typeof cart.total === "undefined") {
|
|
1329
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "cart.total should be defined");
|
|
1330
|
+
}
|
|
1331
|
+
// If cart total is 0, we don't perform anything payment related
|
|
1332
|
+
if (cart.total <= 0) {
|
|
1333
|
+
cart.payment_authorized_at = new Date();
|
|
1334
|
+
return [2 /*return*/, cartRepository.save(cart)];
|
|
1335
|
+
}
|
|
1336
|
+
if (!cart.payment_session) {
|
|
1337
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "You cannot complete a cart without a payment session.");
|
|
1338
|
+
}
|
|
1339
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
1340
|
+
.withTransaction(transactionManager)
|
|
1341
|
+
.authorizePayment(cart.payment_session, context)];
|
|
1342
|
+
case 2:
|
|
1343
|
+
session = _b.sent();
|
|
1344
|
+
return [4 /*yield*/, this.retrieve(cart.id, {
|
|
1345
|
+
select: ["total"],
|
|
1346
|
+
relations: ["payment_sessions", "items", "items.adjustments"],
|
|
1347
|
+
})];
|
|
1348
|
+
case 3:
|
|
1349
|
+
freshCart = _b.sent();
|
|
1350
|
+
if (!(session.status === "authorized")) return [3 /*break*/, 5];
|
|
1351
|
+
_a = freshCart;
|
|
1352
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
1353
|
+
.withTransaction(transactionManager)
|
|
1354
|
+
.createPayment(freshCart)];
|
|
1355
|
+
case 4:
|
|
1356
|
+
_a.payment = _b.sent();
|
|
1357
|
+
freshCart.payment_authorized_at = new Date();
|
|
1358
|
+
_b.label = 5;
|
|
1359
|
+
case 5: return [4 /*yield*/, cartRepository.save(freshCart)];
|
|
1360
|
+
case 6:
|
|
1361
|
+
updatedCart = _b.sent();
|
|
1362
|
+
return [4 /*yield*/, this.eventBus_
|
|
1363
|
+
.withTransaction(transactionManager)
|
|
1364
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
1365
|
+
case 7:
|
|
1366
|
+
_b.sent();
|
|
1367
|
+
return [2 /*return*/, updatedCart];
|
|
1368
|
+
}
|
|
1369
|
+
});
|
|
1370
|
+
}); })];
|
|
1371
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1372
|
+
}
|
|
1500
1373
|
});
|
|
1501
1374
|
});
|
|
1502
1375
|
};
|
|
@@ -1510,66 +1383,66 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1510
1383
|
return __awaiter(this, void 0, void 0, function () {
|
|
1511
1384
|
var _this = this;
|
|
1512
1385
|
return __generator(this, function (_a) {
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1386
|
+
switch (_a.label) {
|
|
1387
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1388
|
+
var psRepo, cart, paymentSession, updatedCart;
|
|
1389
|
+
var _this = this;
|
|
1390
|
+
return __generator(this, function (_a) {
|
|
1391
|
+
switch (_a.label) {
|
|
1392
|
+
case 0:
|
|
1393
|
+
psRepo = transactionManager.getCustomRepository(this.paymentSessionRepository_);
|
|
1394
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
1395
|
+
select: [
|
|
1396
|
+
"total",
|
|
1397
|
+
"subtotal",
|
|
1398
|
+
"tax_total",
|
|
1399
|
+
"discount_total",
|
|
1400
|
+
"gift_card_total",
|
|
1401
|
+
],
|
|
1402
|
+
relations: ["region", "region.payment_providers", "payment_sessions"],
|
|
1403
|
+
})
|
|
1404
|
+
// The region must have the provider id in its providers array
|
|
1405
|
+
];
|
|
1406
|
+
case 1:
|
|
1407
|
+
cart = _a.sent();
|
|
1530
1408
|
// The region must have the provider id in its providers array
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
_a.sent();
|
|
1569
|
-
return [2 /*return*/, result];
|
|
1570
|
-
}
|
|
1571
|
-
});
|
|
1572
|
-
}); }, "SERIALIZABLE")];
|
|
1409
|
+
if (providerId !== "system" &&
|
|
1410
|
+
!(cart.region.payment_providers.length &&
|
|
1411
|
+
cart.region.payment_providers.find(function (_a) {
|
|
1412
|
+
var id = _a.id;
|
|
1413
|
+
return providerId === id;
|
|
1414
|
+
}))) {
|
|
1415
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "The payment method is not available in this region");
|
|
1416
|
+
}
|
|
1417
|
+
return [4 /*yield*/, Promise.all(cart.payment_sessions.map(function (paymentSession) { return __awaiter(_this, void 0, void 0, function () {
|
|
1418
|
+
return __generator(this, function (_a) {
|
|
1419
|
+
return [2 /*return*/, psRepo.save(__assign(__assign({}, paymentSession), { is_selected: null }))];
|
|
1420
|
+
});
|
|
1421
|
+
}); }))];
|
|
1422
|
+
case 2:
|
|
1423
|
+
_a.sent();
|
|
1424
|
+
paymentSession = cart.payment_sessions.find(function (ps) { return ps.provider_id === providerId; });
|
|
1425
|
+
if (!paymentSession) {
|
|
1426
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Could not find payment session");
|
|
1427
|
+
}
|
|
1428
|
+
paymentSession.is_selected = true;
|
|
1429
|
+
return [4 /*yield*/, psRepo.save(paymentSession)];
|
|
1430
|
+
case 3:
|
|
1431
|
+
_a.sent();
|
|
1432
|
+
return [4 /*yield*/, this.retrieve(cartId)];
|
|
1433
|
+
case 4:
|
|
1434
|
+
updatedCart = _a.sent();
|
|
1435
|
+
return [4 /*yield*/, this.eventBus_
|
|
1436
|
+
.withTransaction(transactionManager)
|
|
1437
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
1438
|
+
case 5:
|
|
1439
|
+
_a.sent();
|
|
1440
|
+
return [2 /*return*/, updatedCart];
|
|
1441
|
+
}
|
|
1442
|
+
});
|
|
1443
|
+
}); }, "SERIALIZABLE")];
|
|
1444
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1445
|
+
}
|
|
1573
1446
|
});
|
|
1574
1447
|
});
|
|
1575
1448
|
};
|
|
@@ -1586,147 +1459,105 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1586
1459
|
return __awaiter(this, void 0, void 0, function () {
|
|
1587
1460
|
var _this = this;
|
|
1588
1461
|
return __generator(this, function (_a) {
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
if (
|
|
1462
|
+
switch (_a.label) {
|
|
1463
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1464
|
+
var psRepo, cartId, cart, total, region, seen, paymentProvider, paymentSession;
|
|
1465
|
+
var _this = this;
|
|
1466
|
+
var _a;
|
|
1467
|
+
return __generator(this, function (_b) {
|
|
1468
|
+
switch (_b.label) {
|
|
1469
|
+
case 0:
|
|
1470
|
+
psRepo = transactionManager.getCustomRepository(this.paymentSessionRepository_);
|
|
1471
|
+
cartId = typeof cartOrCartId === "string" ? cartOrCartId : cartOrCartId.id;
|
|
1472
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
1473
|
+
select: [
|
|
1474
|
+
"total",
|
|
1475
|
+
"subtotal",
|
|
1476
|
+
"tax_total",
|
|
1477
|
+
"discount_total",
|
|
1478
|
+
"shipping_total",
|
|
1479
|
+
"gift_card_total",
|
|
1480
|
+
],
|
|
1481
|
+
relations: [
|
|
1482
|
+
"items",
|
|
1483
|
+
"items.adjustments",
|
|
1484
|
+
"discounts",
|
|
1485
|
+
"discounts.rule",
|
|
1486
|
+
"gift_cards",
|
|
1487
|
+
"shipping_methods",
|
|
1488
|
+
"billing_address",
|
|
1489
|
+
"shipping_address",
|
|
1490
|
+
"region",
|
|
1491
|
+
"region.tax_rates",
|
|
1492
|
+
"region.payment_providers",
|
|
1493
|
+
"payment_sessions",
|
|
1494
|
+
"customer",
|
|
1495
|
+
],
|
|
1496
|
+
}, { force_taxes: true })];
|
|
1497
|
+
case 1:
|
|
1498
|
+
cart = _b.sent();
|
|
1499
|
+
total = cart.total, region = cart.region;
|
|
1500
|
+
if (typeof total === "undefined") {
|
|
1501
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "cart.total must be defined");
|
|
1502
|
+
}
|
|
1503
|
+
seen = [];
|
|
1504
|
+
if (!((_a = cart.payment_sessions) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 3];
|
|
1505
|
+
return [4 /*yield*/, Promise.all(cart.payment_sessions.map(function (paymentSession) { return __awaiter(_this, void 0, void 0, function () {
|
|
1506
|
+
return __generator(this, function (_a) {
|
|
1507
|
+
if (total <= 0 ||
|
|
1635
1508
|
!region.payment_providers.find(function (_a) {
|
|
1636
1509
|
var id = _a.id;
|
|
1637
|
-
return id ===
|
|
1638
|
-
}))
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
return [4 /*yield*/, this.paymentProviderService_
|
|
1689
|
-
.withTransaction(manager)
|
|
1690
|
-
.createSession(p.id, cart)];
|
|
1691
|
-
case 10:
|
|
1692
|
-
sess = _g.sent();
|
|
1693
|
-
sess.is_selected = true;
|
|
1694
|
-
return [4 /*yield*/, psRepo.save(sess)];
|
|
1695
|
-
case 11:
|
|
1696
|
-
_g.sent();
|
|
1697
|
-
return [3 /*break*/, 19];
|
|
1698
|
-
case 12:
|
|
1699
|
-
_g.trys.push([12, 17, 18, 19]);
|
|
1700
|
-
_c = __values(region.payment_providers), _d = _c.next();
|
|
1701
|
-
_g.label = 13;
|
|
1702
|
-
case 13:
|
|
1703
|
-
if (!!_d.done) return [3 /*break*/, 16];
|
|
1704
|
-
provider = _d.value;
|
|
1705
|
-
if (!!seen.includes(provider.id)) return [3 /*break*/, 15];
|
|
1706
|
-
return [4 /*yield*/, this.paymentProviderService_
|
|
1707
|
-
.withTransaction(manager)
|
|
1708
|
-
.createSession(provider.id, cart)];
|
|
1709
|
-
case 14:
|
|
1710
|
-
_g.sent();
|
|
1711
|
-
_g.label = 15;
|
|
1712
|
-
case 15:
|
|
1713
|
-
_d = _c.next();
|
|
1714
|
-
return [3 /*break*/, 13];
|
|
1715
|
-
case 16: return [3 /*break*/, 19];
|
|
1716
|
-
case 17:
|
|
1717
|
-
e_10_1 = _g.sent();
|
|
1718
|
-
e_10 = { error: e_10_1 };
|
|
1719
|
-
return [3 /*break*/, 19];
|
|
1720
|
-
case 18:
|
|
1721
|
-
try {
|
|
1722
|
-
if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
|
|
1723
|
-
}
|
|
1724
|
-
finally { if (e_10) throw e_10.error; }
|
|
1725
|
-
return [7 /*endfinally*/];
|
|
1726
|
-
case 19: return [2 /*return*/];
|
|
1727
|
-
}
|
|
1728
|
-
});
|
|
1729
|
-
}); })];
|
|
1510
|
+
return id === paymentSession.provider_id;
|
|
1511
|
+
})) {
|
|
1512
|
+
return [2 /*return*/, this.paymentProviderService_
|
|
1513
|
+
.withTransaction(transactionManager)
|
|
1514
|
+
.deleteSession(paymentSession)];
|
|
1515
|
+
}
|
|
1516
|
+
else {
|
|
1517
|
+
seen.push(paymentSession.provider_id);
|
|
1518
|
+
return [2 /*return*/, this.paymentProviderService_
|
|
1519
|
+
.withTransaction(transactionManager)
|
|
1520
|
+
.updateSession(paymentSession, cart)];
|
|
1521
|
+
}
|
|
1522
|
+
return [2 /*return*/];
|
|
1523
|
+
});
|
|
1524
|
+
}); }))];
|
|
1525
|
+
case 2:
|
|
1526
|
+
_b.sent();
|
|
1527
|
+
_b.label = 3;
|
|
1528
|
+
case 3:
|
|
1529
|
+
if (!(total > 0)) return [3 /*break*/, 8];
|
|
1530
|
+
if (!(region.payment_providers.length === 1 && !cart.payment_session)) return [3 /*break*/, 6];
|
|
1531
|
+
paymentProvider = region.payment_providers[0];
|
|
1532
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
1533
|
+
.withTransaction(transactionManager)
|
|
1534
|
+
.createSession(paymentProvider.id, cart)];
|
|
1535
|
+
case 4:
|
|
1536
|
+
paymentSession = _b.sent();
|
|
1537
|
+
paymentSession.is_selected = true;
|
|
1538
|
+
return [4 /*yield*/, psRepo.save(paymentSession)];
|
|
1539
|
+
case 5:
|
|
1540
|
+
_b.sent();
|
|
1541
|
+
return [3 /*break*/, 8];
|
|
1542
|
+
case 6: return [4 /*yield*/, Promise.all(region.payment_providers.map(function (paymentProvider) { return __awaiter(_this, void 0, void 0, function () {
|
|
1543
|
+
return __generator(this, function (_a) {
|
|
1544
|
+
if (!seen.includes(paymentProvider.id)) {
|
|
1545
|
+
return [2 /*return*/, this.paymentProviderService_
|
|
1546
|
+
.withTransaction(transactionManager)
|
|
1547
|
+
.createSession(paymentProvider.id, cart)];
|
|
1548
|
+
}
|
|
1549
|
+
return [2 /*return*/];
|
|
1550
|
+
});
|
|
1551
|
+
}); }))];
|
|
1552
|
+
case 7:
|
|
1553
|
+
_b.sent();
|
|
1554
|
+
_b.label = 8;
|
|
1555
|
+
case 8: return [2 /*return*/];
|
|
1556
|
+
}
|
|
1557
|
+
});
|
|
1558
|
+
}); })];
|
|
1559
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1560
|
+
}
|
|
1730
1561
|
});
|
|
1731
1562
|
});
|
|
1732
1563
|
};
|
|
@@ -1741,46 +1572,49 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1741
1572
|
return __awaiter(this, void 0, void 0, function () {
|
|
1742
1573
|
var _this = this;
|
|
1743
1574
|
return __generator(this, function (_a) {
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1575
|
+
switch (_a.label) {
|
|
1576
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1577
|
+
var cart, cartRepo, paymentSession;
|
|
1578
|
+
return __generator(this, function (_a) {
|
|
1579
|
+
switch (_a.label) {
|
|
1580
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
1581
|
+
relations: ["payment_sessions"],
|
|
1582
|
+
})];
|
|
1583
|
+
case 1:
|
|
1584
|
+
cart = _a.sent();
|
|
1585
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
1586
|
+
if (!cart.payment_sessions) return [3 /*break*/, 3];
|
|
1587
|
+
paymentSession = cart.payment_sessions.find(function (_a) {
|
|
1588
|
+
var provider_id = _a.provider_id;
|
|
1589
|
+
return provider_id === providerId;
|
|
1590
|
+
});
|
|
1591
|
+
cart.payment_sessions = cart.payment_sessions.filter(function (_a) {
|
|
1592
|
+
var provider_id = _a.provider_id;
|
|
1593
|
+
return provider_id !== providerId;
|
|
1594
|
+
});
|
|
1595
|
+
if (!paymentSession) return [3 /*break*/, 3];
|
|
1596
|
+
// Delete the session with the provider
|
|
1597
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
1598
|
+
.withTransaction(transactionManager)
|
|
1599
|
+
.deleteSession(paymentSession)];
|
|
1600
|
+
case 2:
|
|
1601
|
+
// Delete the session with the provider
|
|
1602
|
+
_a.sent();
|
|
1603
|
+
_a.label = 3;
|
|
1604
|
+
case 3: return [4 /*yield*/, cartRepo.save(cart)];
|
|
1605
|
+
case 4:
|
|
1606
|
+
_a.sent();
|
|
1607
|
+
return [4 /*yield*/, this.eventBus_
|
|
1608
|
+
.withTransaction(transactionManager)
|
|
1609
|
+
.emit(CartService.Events.UPDATED, cart)];
|
|
1610
|
+
case 5:
|
|
1611
|
+
_a.sent();
|
|
1612
|
+
return [2 /*return*/, cart];
|
|
1613
|
+
}
|
|
1614
|
+
});
|
|
1615
|
+
}); })];
|
|
1616
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1617
|
+
}
|
|
1784
1618
|
});
|
|
1785
1619
|
});
|
|
1786
1620
|
};
|
|
@@ -1795,41 +1629,44 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1795
1629
|
return __awaiter(this, void 0, void 0, function () {
|
|
1796
1630
|
var _this = this;
|
|
1797
1631
|
return __generator(this, function (_a) {
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1632
|
+
switch (_a.label) {
|
|
1633
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1634
|
+
var cart, paymentSession, updatedCart;
|
|
1635
|
+
return __generator(this, function (_a) {
|
|
1636
|
+
switch (_a.label) {
|
|
1637
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
1638
|
+
relations: ["payment_sessions"],
|
|
1639
|
+
})];
|
|
1640
|
+
case 1:
|
|
1641
|
+
cart = _a.sent();
|
|
1642
|
+
if (!cart.payment_sessions) return [3 /*break*/, 3];
|
|
1643
|
+
paymentSession = cart.payment_sessions.find(function (_a) {
|
|
1644
|
+
var provider_id = _a.provider_id;
|
|
1645
|
+
return provider_id === providerId;
|
|
1646
|
+
});
|
|
1647
|
+
if (!paymentSession) return [3 /*break*/, 3];
|
|
1648
|
+
// Delete the session with the provider
|
|
1649
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
1650
|
+
.withTransaction(transactionManager)
|
|
1651
|
+
.refreshSession(paymentSession, cart)];
|
|
1652
|
+
case 2:
|
|
1653
|
+
// Delete the session with the provider
|
|
1654
|
+
_a.sent();
|
|
1655
|
+
_a.label = 3;
|
|
1656
|
+
case 3: return [4 /*yield*/, this.retrieve(cartId)];
|
|
1657
|
+
case 4:
|
|
1658
|
+
updatedCart = _a.sent();
|
|
1659
|
+
return [4 /*yield*/, this.eventBus_
|
|
1660
|
+
.withTransaction(transactionManager)
|
|
1661
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
1662
|
+
case 5:
|
|
1663
|
+
_a.sent();
|
|
1664
|
+
return [2 /*return*/, updatedCart];
|
|
1665
|
+
}
|
|
1666
|
+
});
|
|
1667
|
+
}); })];
|
|
1668
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1669
|
+
}
|
|
1833
1670
|
});
|
|
1834
1671
|
});
|
|
1835
1672
|
};
|
|
@@ -1849,133 +1686,119 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1849
1686
|
return __awaiter(this, void 0, void 0, function () {
|
|
1850
1687
|
var _this = this;
|
|
1851
1688
|
return __generator(this, function (_a) {
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
"
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
cart = _e.sent();
|
|
1873
|
-
return [4 /*yield*/, this.customShippingOptionService_.list({ cart_id: cart.id })];
|
|
1874
|
-
case 2:
|
|
1875
|
-
cartCustomShippingOptions = _e.sent();
|
|
1876
|
-
customShippingOption = this.findCustomShippingOption(cartCustomShippingOptions, optionId);
|
|
1877
|
-
shipping_methods = cart.shipping_methods;
|
|
1878
|
-
shippingMethodConfig = customShippingOption
|
|
1879
|
-
? { cart_id: cart.id, price: customShippingOption.price }
|
|
1880
|
-
: {
|
|
1881
|
-
cart: cart,
|
|
1882
|
-
};
|
|
1883
|
-
return [4 /*yield*/, this.shippingOptionService_
|
|
1884
|
-
.withTransaction(manager)
|
|
1885
|
-
.createShippingMethod(optionId, data, shippingMethodConfig)];
|
|
1886
|
-
case 3:
|
|
1887
|
-
newMethod = _e.sent();
|
|
1888
|
-
methods = [newMethod];
|
|
1889
|
-
if (!shipping_methods.length) return [3 /*break*/, 12];
|
|
1890
|
-
_e.label = 4;
|
|
1891
|
-
case 4:
|
|
1892
|
-
_e.trys.push([4, 10, 11, 12]);
|
|
1893
|
-
shipping_methods_1 = __values(shipping_methods), shipping_methods_1_1 = shipping_methods_1.next();
|
|
1894
|
-
_e.label = 5;
|
|
1895
|
-
case 5:
|
|
1896
|
-
if (!!shipping_methods_1_1.done) return [3 /*break*/, 9];
|
|
1897
|
-
sm = shipping_methods_1_1.value;
|
|
1898
|
-
if (!(sm.shipping_option.profile_id ===
|
|
1899
|
-
newMethod.shipping_option.profile_id)) return [3 /*break*/, 7];
|
|
1900
|
-
return [4 /*yield*/, this.shippingOptionService_
|
|
1901
|
-
.withTransaction(manager)
|
|
1902
|
-
.deleteShippingMethod(sm)];
|
|
1903
|
-
case 6:
|
|
1904
|
-
_e.sent();
|
|
1905
|
-
return [3 /*break*/, 8];
|
|
1906
|
-
case 7:
|
|
1907
|
-
methods.push(sm);
|
|
1908
|
-
_e.label = 8;
|
|
1909
|
-
case 8:
|
|
1910
|
-
shipping_methods_1_1 = shipping_methods_1.next();
|
|
1911
|
-
return [3 /*break*/, 5];
|
|
1912
|
-
case 9: return [3 /*break*/, 12];
|
|
1913
|
-
case 10:
|
|
1914
|
-
e_11_1 = _e.sent();
|
|
1915
|
-
e_11 = { error: e_11_1 };
|
|
1916
|
-
return [3 /*break*/, 12];
|
|
1917
|
-
case 11:
|
|
1918
|
-
try {
|
|
1919
|
-
if (shipping_methods_1_1 && !shipping_methods_1_1.done && (_c = shipping_methods_1.return)) _c.call(shipping_methods_1);
|
|
1920
|
-
}
|
|
1921
|
-
finally { if (e_11) throw e_11.error; }
|
|
1922
|
-
return [7 /*endfinally*/];
|
|
1923
|
-
case 12:
|
|
1924
|
-
_e.trys.push([12, 17, 18, 19]);
|
|
1925
|
-
_a = __values(cart.items), _b = _a.next();
|
|
1926
|
-
_e.label = 13;
|
|
1927
|
-
case 13:
|
|
1928
|
-
if (!!_b.done) return [3 /*break*/, 16];
|
|
1929
|
-
item = _b.value;
|
|
1930
|
-
return [4 /*yield*/, this.lineItemService_.withTransaction(manager).update(item.id, {
|
|
1931
|
-
has_shipping: this.validateLineItemShipping_(methods, item),
|
|
1689
|
+
switch (_a.label) {
|
|
1690
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1691
|
+
var cart, cartCustomShippingOptions, customShippingOption, shipping_methods, shippingMethodConfig, newShippingMethod, methods, shipping_methods_1, shipping_methods_1_1, shippingMethod, e_3_1, updatedCart;
|
|
1692
|
+
var e_3, _a;
|
|
1693
|
+
var _this = this;
|
|
1694
|
+
var _b;
|
|
1695
|
+
return __generator(this, function (_c) {
|
|
1696
|
+
switch (_c.label) {
|
|
1697
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
1698
|
+
select: ["subtotal"],
|
|
1699
|
+
relations: [
|
|
1700
|
+
"shipping_methods",
|
|
1701
|
+
"discounts",
|
|
1702
|
+
"discounts.rule",
|
|
1703
|
+
"shipping_methods.shipping_option",
|
|
1704
|
+
"items",
|
|
1705
|
+
"items.variant",
|
|
1706
|
+
"payment_sessions",
|
|
1707
|
+
"items.variant.product",
|
|
1708
|
+
],
|
|
1932
1709
|
})];
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
.
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1710
|
+
case 1:
|
|
1711
|
+
cart = _c.sent();
|
|
1712
|
+
return [4 /*yield*/, this.customShippingOptionService_
|
|
1713
|
+
.withTransaction(transactionManager)
|
|
1714
|
+
.list({ cart_id: cart.id })];
|
|
1715
|
+
case 2:
|
|
1716
|
+
cartCustomShippingOptions = _c.sent();
|
|
1717
|
+
customShippingOption = this.findCustomShippingOption(cartCustomShippingOptions, optionId);
|
|
1718
|
+
shipping_methods = cart.shipping_methods;
|
|
1719
|
+
shippingMethodConfig = customShippingOption
|
|
1720
|
+
? { cart_id: cart.id, price: customShippingOption.price }
|
|
1721
|
+
: { cart: cart };
|
|
1722
|
+
return [4 /*yield*/, this.shippingOptionService_
|
|
1723
|
+
.withTransaction(transactionManager)
|
|
1724
|
+
.createShippingMethod(optionId, data, shippingMethodConfig)];
|
|
1725
|
+
case 3:
|
|
1726
|
+
newShippingMethod = _c.sent();
|
|
1727
|
+
methods = [newShippingMethod];
|
|
1728
|
+
if (!(shipping_methods === null || shipping_methods === void 0 ? void 0 : shipping_methods.length)) return [3 /*break*/, 12];
|
|
1729
|
+
_c.label = 4;
|
|
1730
|
+
case 4:
|
|
1731
|
+
_c.trys.push([4, 10, 11, 12]);
|
|
1732
|
+
shipping_methods_1 = __values(shipping_methods), shipping_methods_1_1 = shipping_methods_1.next();
|
|
1733
|
+
_c.label = 5;
|
|
1734
|
+
case 5:
|
|
1735
|
+
if (!!shipping_methods_1_1.done) return [3 /*break*/, 9];
|
|
1736
|
+
shippingMethod = shipping_methods_1_1.value;
|
|
1737
|
+
if (!(shippingMethod.shipping_option.profile_id ===
|
|
1738
|
+
newShippingMethod.shipping_option.profile_id)) return [3 /*break*/, 7];
|
|
1739
|
+
return [4 /*yield*/, this.shippingOptionService_
|
|
1740
|
+
.withTransaction(transactionManager)
|
|
1741
|
+
.deleteShippingMethods(shippingMethod)];
|
|
1742
|
+
case 6:
|
|
1743
|
+
_c.sent();
|
|
1744
|
+
return [3 /*break*/, 8];
|
|
1745
|
+
case 7:
|
|
1746
|
+
methods.push(shippingMethod);
|
|
1747
|
+
_c.label = 8;
|
|
1748
|
+
case 8:
|
|
1749
|
+
shipping_methods_1_1 = shipping_methods_1.next();
|
|
1750
|
+
return [3 /*break*/, 5];
|
|
1751
|
+
case 9: return [3 /*break*/, 12];
|
|
1752
|
+
case 10:
|
|
1753
|
+
e_3_1 = _c.sent();
|
|
1754
|
+
e_3 = { error: e_3_1 };
|
|
1755
|
+
return [3 /*break*/, 12];
|
|
1756
|
+
case 11:
|
|
1757
|
+
try {
|
|
1758
|
+
if (shipping_methods_1_1 && !shipping_methods_1_1.done && (_a = shipping_methods_1.return)) _a.call(shipping_methods_1);
|
|
1759
|
+
}
|
|
1760
|
+
finally { if (e_3) throw e_3.error; }
|
|
1761
|
+
return [7 /*endfinally*/];
|
|
1762
|
+
case 12:
|
|
1763
|
+
if (!((_b = cart.items) === null || _b === void 0 ? void 0 : _b.length)) return [3 /*break*/, 14];
|
|
1764
|
+
return [4 /*yield*/, Promise.all(cart.items.map(function (item) { return __awaiter(_this, void 0, void 0, function () {
|
|
1765
|
+
return __generator(this, function (_a) {
|
|
1766
|
+
return [2 /*return*/, this.lineItemService_
|
|
1767
|
+
.withTransaction(transactionManager)
|
|
1768
|
+
.update(item.id, {
|
|
1769
|
+
has_shipping: this.validateLineItemShipping_(methods, item),
|
|
1770
|
+
})];
|
|
1771
|
+
});
|
|
1772
|
+
}); }))];
|
|
1773
|
+
case 13:
|
|
1774
|
+
_c.sent();
|
|
1775
|
+
_c.label = 14;
|
|
1776
|
+
case 14: return [4 /*yield*/, this.retrieve(cartId, {
|
|
1777
|
+
relations: ["discounts", "discounts.rule", "shipping_methods"],
|
|
1778
|
+
})
|
|
1779
|
+
// if cart has freeshipping, adjust price
|
|
1780
|
+
];
|
|
1781
|
+
case 15:
|
|
1782
|
+
updatedCart = _c.sent();
|
|
1783
|
+
if (!updatedCart.discounts.some(function (_a) {
|
|
1784
|
+
var rule = _a.rule;
|
|
1785
|
+
return rule.type === "free_shipping";
|
|
1786
|
+
})) return [3 /*break*/, 17];
|
|
1787
|
+
return [4 /*yield*/, this.adjustFreeShipping_(updatedCart, true)];
|
|
1788
|
+
case 16:
|
|
1789
|
+
_c.sent();
|
|
1790
|
+
_c.label = 17;
|
|
1791
|
+
case 17: return [4 /*yield*/, this.eventBus_
|
|
1792
|
+
.withTransaction(transactionManager)
|
|
1793
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
1794
|
+
case 18:
|
|
1795
|
+
_c.sent();
|
|
1796
|
+
return [2 /*return*/, updatedCart];
|
|
1797
|
+
}
|
|
1798
|
+
});
|
|
1799
|
+
}); }, "SERIALIZABLE")];
|
|
1800
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1801
|
+
}
|
|
1979
1802
|
});
|
|
1980
1803
|
});
|
|
1981
1804
|
};
|
|
@@ -1994,74 +1817,103 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1994
1817
|
}
|
|
1995
1818
|
return customOption;
|
|
1996
1819
|
};
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
* @param cart - the cart to set region on
|
|
2000
|
-
* @param regionId - the id of the region to set the region to
|
|
2001
|
-
* @param countryCode - the country code to set the country to
|
|
2002
|
-
* @return the result of the update operation
|
|
2003
|
-
*/
|
|
2004
|
-
CartService.prototype.setRegion_ = function (cart, regionId, countryCode) {
|
|
1820
|
+
CartService.prototype.updateUnitPrices_ = function (cart, regionId, customer_id) {
|
|
1821
|
+
var _a, _b;
|
|
2005
1822
|
return __awaiter(this, void 0, void 0, function () {
|
|
2006
|
-
var
|
|
1823
|
+
var transactionManager, region_1, _c;
|
|
2007
1824
|
var _this = this;
|
|
2008
|
-
return __generator(this, function (
|
|
2009
|
-
switch (
|
|
1825
|
+
return __generator(this, function (_d) {
|
|
1826
|
+
switch (_d.label) {
|
|
2010
1827
|
case 0:
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
1828
|
+
transactionManager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
|
|
1829
|
+
if (!((_b = cart.items) === null || _b === void 0 ? void 0 : _b.length)) return [3 /*break*/, 3];
|
|
1830
|
+
return [4 /*yield*/, this.regionService_
|
|
1831
|
+
.withTransaction(this.transactionManager_)
|
|
1832
|
+
.retrieve(regionId || cart.region_id, {
|
|
2015
1833
|
relations: ["countries"],
|
|
2016
1834
|
})];
|
|
2017
1835
|
case 1:
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
cart.
|
|
2021
|
-
cart.region_id = region.id;
|
|
2022
|
-
if (!cart.items.length) return [3 /*break*/, 3];
|
|
2023
|
-
_a = cart;
|
|
2024
|
-
return [4 /*yield*/, Promise.all(cart.items
|
|
2025
|
-
.map(function (item) { return __awaiter(_this, void 0, void 0, function () {
|
|
1836
|
+
region_1 = _d.sent();
|
|
1837
|
+
_c = cart;
|
|
1838
|
+
return [4 /*yield*/, Promise.all(cart.items.map(function (item) { return __awaiter(_this, void 0, void 0, function () {
|
|
2026
1839
|
var availablePrice;
|
|
2027
1840
|
return __generator(this, function (_a) {
|
|
2028
1841
|
switch (_a.label) {
|
|
2029
|
-
case 0: return [4 /*yield*/, this.
|
|
2030
|
-
.
|
|
1842
|
+
case 0: return [4 /*yield*/, this.priceSelectionStrategy_
|
|
1843
|
+
.withTransaction(transactionManager)
|
|
1844
|
+
.calculateVariantPrice(item.variant_id, {
|
|
1845
|
+
region_id: region_1.id,
|
|
1846
|
+
currency_code: region_1.currency_code,
|
|
1847
|
+
quantity: item.quantity,
|
|
1848
|
+
customer_id: customer_id || cart.customer_id,
|
|
1849
|
+
include_discount_prices: true,
|
|
1850
|
+
})
|
|
2031
1851
|
.catch(function () { return undefined; })];
|
|
2032
1852
|
case 1:
|
|
2033
1853
|
availablePrice = _a.sent();
|
|
2034
|
-
if (!(availablePrice !== undefined
|
|
1854
|
+
if (!(availablePrice !== undefined &&
|
|
1855
|
+
availablePrice.calculatedPrice !== null)) return [3 /*break*/, 2];
|
|
2035
1856
|
return [2 /*return*/, this.lineItemService_
|
|
2036
|
-
.withTransaction(
|
|
1857
|
+
.withTransaction(transactionManager)
|
|
2037
1858
|
.update(item.id, {
|
|
2038
1859
|
has_shipping: false,
|
|
2039
|
-
unit_price: availablePrice,
|
|
1860
|
+
unit_price: availablePrice.calculatedPrice,
|
|
2040
1861
|
})];
|
|
2041
1862
|
case 2: return [4 /*yield*/, this.lineItemService_
|
|
2042
|
-
.withTransaction(
|
|
1863
|
+
.withTransaction(transactionManager)
|
|
2043
1864
|
.delete(item.id)];
|
|
2044
1865
|
case 3:
|
|
2045
1866
|
_a.sent();
|
|
2046
|
-
return [2 /*return
|
|
1867
|
+
return [2 /*return*/];
|
|
2047
1868
|
}
|
|
2048
1869
|
});
|
|
2049
|
-
}); })
|
|
2050
|
-
.filter(Boolean))];
|
|
1870
|
+
}); }))];
|
|
2051
1871
|
case 2:
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
case 3:
|
|
1872
|
+
_c.items = (_d.sent()).filter(function (item) { return !!item; });
|
|
1873
|
+
_d.label = 3;
|
|
1874
|
+
case 3: return [2 /*return*/];
|
|
1875
|
+
}
|
|
1876
|
+
});
|
|
1877
|
+
});
|
|
1878
|
+
};
|
|
1879
|
+
/**
|
|
1880
|
+
* Set's the region of a cart.
|
|
1881
|
+
* @param cart - the cart to set region on
|
|
1882
|
+
* @param regionId - the id of the region to set the region to
|
|
1883
|
+
* @param countryCode - the country code to set the country to
|
|
1884
|
+
* @return the result of the update operation
|
|
1885
|
+
*/
|
|
1886
|
+
CartService.prototype.setRegion_ = function (cart, regionId, countryCode) {
|
|
1887
|
+
var _a;
|
|
1888
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1889
|
+
var transactionManager, region, addrRepo, shippingAddress, updated, updated, paymentSessionRepo;
|
|
1890
|
+
return __generator(this, function (_b) {
|
|
1891
|
+
switch (_b.label) {
|
|
1892
|
+
case 0:
|
|
1893
|
+
transactionManager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
|
|
1894
|
+
if (cart.completed_at || cart.payment_authorized_at) {
|
|
1895
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cannot change the region of a completed cart");
|
|
1896
|
+
}
|
|
1897
|
+
return [4 /*yield*/, this.regionService_
|
|
1898
|
+
.withTransaction(this.transactionManager_)
|
|
1899
|
+
.retrieve(regionId, {
|
|
1900
|
+
relations: ["countries"],
|
|
1901
|
+
})];
|
|
1902
|
+
case 1:
|
|
1903
|
+
region = _b.sent();
|
|
1904
|
+
addrRepo = transactionManager.getCustomRepository(this.addressRepository_);
|
|
1905
|
+
cart.region = region;
|
|
1906
|
+
cart.region_id = region.id;
|
|
2055
1907
|
shippingAddress = {};
|
|
2056
|
-
if (!cart.shipping_address_id) return [3 /*break*/,
|
|
1908
|
+
if (!cart.shipping_address_id) return [3 /*break*/, 3];
|
|
2057
1909
|
return [4 /*yield*/, addrRepo.findOne({
|
|
2058
1910
|
where: { id: cart.shipping_address_id },
|
|
2059
1911
|
})];
|
|
2060
|
-
case
|
|
1912
|
+
case 2:
|
|
2061
1913
|
shippingAddress = (_b.sent());
|
|
2062
|
-
_b.label =
|
|
2063
|
-
case
|
|
2064
|
-
if (!(countryCode !== null)) return [3 /*break*/,
|
|
1914
|
+
_b.label = 3;
|
|
1915
|
+
case 3:
|
|
1916
|
+
if (!(countryCode !== null)) return [3 /*break*/, 5];
|
|
2065
1917
|
if (!region.countries.find(function (_a) {
|
|
2066
1918
|
var iso_2 = _a.iso_2;
|
|
2067
1919
|
return iso_2 === countryCode.toLowerCase();
|
|
@@ -2070,10 +1922,10 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2070
1922
|
}
|
|
2071
1923
|
updated = __assign(__assign({}, shippingAddress), { country_code: countryCode.toLowerCase() });
|
|
2072
1924
|
return [4 /*yield*/, addrRepo.save(updated)];
|
|
2073
|
-
case
|
|
1925
|
+
case 4:
|
|
2074
1926
|
_b.sent();
|
|
2075
|
-
return [3 /*break*/,
|
|
2076
|
-
case
|
|
1927
|
+
return [3 /*break*/, 7];
|
|
1928
|
+
case 5:
|
|
2077
1929
|
updated = __assign({}, shippingAddress);
|
|
2078
1930
|
// If the country code of a shipping address is set we need to clear it
|
|
2079
1931
|
if (!lodash_1.default.isEmpty(shippingAddress) && shippingAddress.country_code) {
|
|
@@ -2084,42 +1936,38 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2084
1936
|
updated = __assign(__assign({}, updated), { country_code: region.countries[0].iso_2 });
|
|
2085
1937
|
}
|
|
2086
1938
|
return [4 /*yield*/, this.updateShippingAddress_(cart, updated, addrRepo)];
|
|
1939
|
+
case 6:
|
|
1940
|
+
_b.sent();
|
|
1941
|
+
_b.label = 7;
|
|
1942
|
+
case 7:
|
|
1943
|
+
if (!(cart.shipping_methods && cart.shipping_methods.length)) return [3 /*break*/, 9];
|
|
1944
|
+
return [4 /*yield*/, this.shippingOptionService_
|
|
1945
|
+
.withTransaction(transactionManager)
|
|
1946
|
+
.deleteShippingMethods(cart.shipping_methods)];
|
|
2087
1947
|
case 8:
|
|
2088
1948
|
_b.sent();
|
|
2089
1949
|
_b.label = 9;
|
|
2090
1950
|
case 9:
|
|
2091
|
-
if (!(cart.shipping_methods && cart.shipping_methods.length)) return [3 /*break*/, 11];
|
|
2092
|
-
smRepo = this.manager_.getCustomRepository(this.shippingMethodRepository_);
|
|
2093
|
-
return [4 /*yield*/, smRepo.remove(cart.shipping_methods)];
|
|
2094
|
-
case 10:
|
|
2095
|
-
_b.sent();
|
|
2096
|
-
_b.label = 11;
|
|
2097
|
-
case 11:
|
|
2098
1951
|
if (cart.discounts && cart.discounts.length) {
|
|
2099
|
-
|
|
2100
|
-
|
|
1952
|
+
cart.discounts = cart.discounts.filter(function (discount) {
|
|
1953
|
+
return discount.regions.find(function (_a) {
|
|
2101
1954
|
var id = _a.id;
|
|
2102
1955
|
return id === regionId;
|
|
2103
|
-
})
|
|
2104
|
-
return d;
|
|
2105
|
-
}
|
|
2106
|
-
return null;
|
|
1956
|
+
});
|
|
2107
1957
|
});
|
|
2108
|
-
cart.discounts = newDiscounts.filter(Boolean);
|
|
2109
1958
|
}
|
|
2110
1959
|
cart.gift_cards = [];
|
|
2111
|
-
if (!(cart.payment_sessions && cart.payment_sessions.length)) return [3 /*break*/,
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
case 12:
|
|
1960
|
+
if (!(cart.payment_sessions && cart.payment_sessions.length)) return [3 /*break*/, 11];
|
|
1961
|
+
paymentSessionRepo = transactionManager.getCustomRepository(this.paymentSessionRepository_);
|
|
1962
|
+
return [4 /*yield*/, paymentSessionRepo.delete({
|
|
1963
|
+
id: (0, typeorm_1.In)(cart.payment_sessions.map(function (paymentSession) { return paymentSession.id; })),
|
|
1964
|
+
})];
|
|
1965
|
+
case 10:
|
|
2118
1966
|
_b.sent();
|
|
2119
|
-
cart.payment_sessions =
|
|
1967
|
+
cart.payment_sessions.length = 0;
|
|
2120
1968
|
cart.payment_session = null;
|
|
2121
|
-
_b.label =
|
|
2122
|
-
case
|
|
1969
|
+
_b.label = 11;
|
|
1970
|
+
case 11: return [2 /*return*/];
|
|
2123
1971
|
}
|
|
2124
1972
|
});
|
|
2125
1973
|
});
|
|
@@ -2134,7 +1982,7 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2134
1982
|
var _this = this;
|
|
2135
1983
|
return __generator(this, function (_a) {
|
|
2136
1984
|
switch (_a.label) {
|
|
2137
|
-
case 0: return [4 /*yield*/, this.atomicPhase_(function (
|
|
1985
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
2138
1986
|
var cart, cartRepo;
|
|
2139
1987
|
return __generator(this, function (_a) {
|
|
2140
1988
|
switch (_a.label) {
|
|
@@ -2143,7 +1991,6 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2143
1991
|
"items",
|
|
2144
1992
|
"discounts",
|
|
2145
1993
|
"discounts.rule",
|
|
2146
|
-
"discounts.rule.valid_for",
|
|
2147
1994
|
"payment_sessions",
|
|
2148
1995
|
],
|
|
2149
1996
|
})];
|
|
@@ -2155,7 +2002,7 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2155
2002
|
if (cart.payment_authorized_at) {
|
|
2156
2003
|
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Can't delete a cart with an authorized payment");
|
|
2157
2004
|
}
|
|
2158
|
-
cartRepo =
|
|
2005
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
2159
2006
|
return [2 /*return*/, cartRepo.remove(cart)];
|
|
2160
2007
|
}
|
|
2161
2008
|
});
|
|
@@ -2179,29 +2026,32 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2179
2026
|
var _this = this;
|
|
2180
2027
|
return __generator(this, function (_a) {
|
|
2181
2028
|
switch (_a.label) {
|
|
2182
|
-
case 0: return [4 /*yield*/, this.atomicPhase_(function (
|
|
2183
|
-
var cartRepo, validatedId, cart, existing,
|
|
2029
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
2030
|
+
var cartRepo, validatedId, cart, existing, updatedCart;
|
|
2184
2031
|
var _a;
|
|
2185
2032
|
return __generator(this, function (_b) {
|
|
2186
2033
|
switch (_b.label) {
|
|
2187
2034
|
case 0:
|
|
2188
|
-
cartRepo =
|
|
2189
|
-
validatedId =
|
|
2035
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
2036
|
+
validatedId = (0, utils_1.validateId)(cartId);
|
|
2190
2037
|
if (typeof key !== "string") {
|
|
2191
2038
|
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_ARGUMENT, "Key type is invalid. Metadata keys must be strings");
|
|
2192
2039
|
}
|
|
2193
2040
|
return [4 /*yield*/, cartRepo.findOne(validatedId)];
|
|
2194
2041
|
case 1:
|
|
2195
|
-
cart =
|
|
2042
|
+
cart = _b.sent();
|
|
2043
|
+
if (!cart) {
|
|
2044
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Unable to find the cart with the given id");
|
|
2045
|
+
}
|
|
2196
2046
|
existing = cart.metadata || {};
|
|
2197
2047
|
cart.metadata = __assign(__assign({}, existing), (_a = {}, _a[key] = value, _a));
|
|
2198
2048
|
return [4 /*yield*/, cartRepo.save(cart)];
|
|
2199
2049
|
case 2:
|
|
2200
|
-
|
|
2050
|
+
updatedCart = _b.sent();
|
|
2201
2051
|
this.eventBus_
|
|
2202
|
-
.withTransaction(
|
|
2203
|
-
.emit(CartService.Events.UPDATED,
|
|
2204
|
-
return [2 /*return*/,
|
|
2052
|
+
.withTransaction(transactionManager)
|
|
2053
|
+
.emit(CartService.Events.UPDATED, updatedCart);
|
|
2054
|
+
return [2 /*return*/, updatedCart];
|
|
2205
2055
|
}
|
|
2206
2056
|
});
|
|
2207
2057
|
}); })];
|
|
@@ -2214,34 +2064,72 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2214
2064
|
return __awaiter(this, void 0, void 0, function () {
|
|
2215
2065
|
var _this = this;
|
|
2216
2066
|
return __generator(this, function (_a) {
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
.
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2067
|
+
switch (_a.label) {
|
|
2068
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
2069
|
+
var cart, calculationContext;
|
|
2070
|
+
return __generator(this, function (_a) {
|
|
2071
|
+
switch (_a.label) {
|
|
2072
|
+
case 0: return [4 /*yield*/, this.retrieve(id, {
|
|
2073
|
+
relations: [
|
|
2074
|
+
"items",
|
|
2075
|
+
"gift_cards",
|
|
2076
|
+
"discounts",
|
|
2077
|
+
"discounts.rule",
|
|
2078
|
+
"shipping_methods",
|
|
2079
|
+
"region",
|
|
2080
|
+
"region.tax_rates",
|
|
2081
|
+
],
|
|
2082
|
+
})];
|
|
2083
|
+
case 1:
|
|
2084
|
+
cart = _a.sent();
|
|
2085
|
+
calculationContext = this.totalsService_
|
|
2086
|
+
.withTransaction(transactionManager)
|
|
2087
|
+
.getCalculationContext(cart);
|
|
2088
|
+
return [4 /*yield*/, this.taxProviderService_
|
|
2089
|
+
.withTransaction(transactionManager)
|
|
2090
|
+
.createTaxLines(cart, calculationContext)];
|
|
2091
|
+
case 2:
|
|
2092
|
+
_a.sent();
|
|
2093
|
+
return [2 /*return*/, cart];
|
|
2094
|
+
}
|
|
2095
|
+
});
|
|
2096
|
+
}); })];
|
|
2097
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2098
|
+
}
|
|
2099
|
+
});
|
|
2100
|
+
});
|
|
2101
|
+
};
|
|
2102
|
+
CartService.prototype.refreshAdjustments_ = function (cart) {
|
|
2103
|
+
var _a;
|
|
2104
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2105
|
+
var transactionManager, nonReturnLineIDs;
|
|
2106
|
+
return __generator(this, function (_b) {
|
|
2107
|
+
switch (_b.label) {
|
|
2108
|
+
case 0:
|
|
2109
|
+
transactionManager = (_a = this.transactionManager_) !== null && _a !== void 0 ? _a : this.manager_;
|
|
2110
|
+
nonReturnLineIDs = cart.items
|
|
2111
|
+
.filter(function (item) { return !item.is_return; })
|
|
2112
|
+
.map(function (i) { return i.id; });
|
|
2113
|
+
// delete all old non return line item adjustments
|
|
2114
|
+
return [4 /*yield*/, this.lineItemAdjustmentService_
|
|
2115
|
+
.withTransaction(transactionManager)
|
|
2116
|
+
.delete({
|
|
2117
|
+
item_id: nonReturnLineIDs,
|
|
2118
|
+
})
|
|
2119
|
+
// potentially create/update line item adjustments
|
|
2120
|
+
];
|
|
2121
|
+
case 1:
|
|
2122
|
+
// delete all old non return line item adjustments
|
|
2123
|
+
_b.sent();
|
|
2124
|
+
// potentially create/update line item adjustments
|
|
2125
|
+
return [4 /*yield*/, this.lineItemAdjustmentService_
|
|
2126
|
+
.withTransaction(transactionManager)
|
|
2127
|
+
.createAdjustments(cart)];
|
|
2128
|
+
case 2:
|
|
2129
|
+
// potentially create/update line item adjustments
|
|
2130
|
+
_b.sent();
|
|
2131
|
+
return [2 /*return*/];
|
|
2132
|
+
}
|
|
2245
2133
|
});
|
|
2246
2134
|
});
|
|
2247
2135
|
};
|
|
@@ -2255,35 +2143,38 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2255
2143
|
return __awaiter(this, void 0, void 0, function () {
|
|
2256
2144
|
var _this = this;
|
|
2257
2145
|
return __generator(this, function (_a) {
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
.
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2146
|
+
switch (_a.label) {
|
|
2147
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
2148
|
+
var cartRepo, validatedId, cart, updated, updatedCart;
|
|
2149
|
+
return __generator(this, function (_a) {
|
|
2150
|
+
switch (_a.label) {
|
|
2151
|
+
case 0:
|
|
2152
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
2153
|
+
validatedId = (0, utils_1.validateId)(cartId);
|
|
2154
|
+
if (typeof key !== "string") {
|
|
2155
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_ARGUMENT, "Key type is invalid. Metadata keys must be strings");
|
|
2156
|
+
}
|
|
2157
|
+
return [4 /*yield*/, cartRepo.findOne(validatedId)];
|
|
2158
|
+
case 1:
|
|
2159
|
+
cart = _a.sent();
|
|
2160
|
+
if (!cart) {
|
|
2161
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Cart with id: ".concat(validatedId, " was not found"));
|
|
2162
|
+
}
|
|
2163
|
+
updated = cart.metadata || {};
|
|
2164
|
+
delete updated[key];
|
|
2165
|
+
cart.metadata = updated;
|
|
2166
|
+
return [4 /*yield*/, cartRepo.save(cart)];
|
|
2167
|
+
case 2:
|
|
2168
|
+
updatedCart = _a.sent();
|
|
2169
|
+
this.eventBus_
|
|
2170
|
+
.withTransaction(transactionManager)
|
|
2171
|
+
.emit(CartService.Events.UPDATED, updatedCart);
|
|
2172
|
+
return [2 /*return*/, updatedCart];
|
|
2173
|
+
}
|
|
2174
|
+
});
|
|
2175
|
+
}); })];
|
|
2176
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2177
|
+
}
|
|
2287
2178
|
});
|
|
2288
2179
|
});
|
|
2289
2180
|
};
|
|
@@ -2293,6 +2184,6 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2293
2184
|
UPDATED: "cart.updated",
|
|
2294
2185
|
};
|
|
2295
2186
|
return CartService;
|
|
2296
|
-
}(
|
|
2187
|
+
}(interfaces_1.TransactionBaseService));
|
|
2297
2188
|
exports.default = CartService;
|
|
2298
2189
|
//# sourceMappingURL=cart.js.map
|