@medusajs/medusa 1.2.2-dev.56 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -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/error-handler.js +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/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-discount.d.ts +11 -2
- package/dist/api/routes/admin/discounts/create-discount.js +66 -12
- 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-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-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 +1 -3
- package/dist/api/routes/admin/discounts/index.js +1 -6
- 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-discount.d.ts +12 -2
- package/dist/api/routes/admin/discounts/update-discount.js +70 -15
- 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/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-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/index.d.ts +7 -0
- package/dist/api/routes/admin/price-lists/index.js +3 -0
- 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 +246 -0
- package/dist/api/routes/admin/price-lists/list-price-list-products.js.map +1 -0
- package/dist/api/routes/admin/price-lists/update-price-list.d.ts +2 -2
- package/dist/api/routes/admin/price-lists/update-price-list.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/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/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/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 +7 -7
- 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 +1 -1
- package/dist/commands/develop.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 +15 -7
- package/dist/helpers/test-request.js.map +1 -1
- package/dist/index.d.ts +3 -59
- package/dist/index.js +3 -62
- package/dist/index.js.map +1 -1
- package/dist/interfaces/base-service.d.ts +66 -0
- package/dist/interfaces/base-service.js +378 -0
- package/dist/interfaces/base-service.js.map +1 -0
- package/dist/interfaces/index.d.ts +1 -0
- package/dist/interfaces/index.js +1 -0
- package/dist/interfaces/index.js.map +1 -1
- 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/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 +18 -10
- package/dist/loaders/plugins.js +27 -24
- 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/{api/routes/admin/discounts/remove-valid-product.js → migrations/1648641130007-tax_line_constraints.js} +39 -44
- 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/{api/routes/admin/discounts/add-valid-product.js → migrations/1649775522087-add_batch_job_model.js} +39 -44
- package/dist/migrations/1649775522087-add_batch_job_model.js.map +1 -0
- package/dist/models/batch-job.d.ts +58 -0
- package/dist/models/batch-job.js +119 -0
- package/dist/models/batch-job.js.map +1 -0
- 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/customer-group.js +3 -1
- package/dist/models/customer-group.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 +68 -0
- package/dist/models/discount-condition.js +208 -0
- package/dist/models/discount-condition.js.map +1 -0
- package/dist/models/discount-rule.d.ts +5 -5
- package/dist/models/discount-rule.js +6 -17
- package/dist/models/discount-rule.js.map +1 -1
- package/dist/models/discount.js.map +1 -1
- package/dist/models/index.d.ts +66 -0
- package/dist/models/index.js +79 -0
- package/dist/models/index.js.map +1 -0
- package/dist/models/line-item-adjustment.d.ts +13 -0
- package/dist/models/line-item-adjustment.js +79 -0
- package/dist/models/line-item-adjustment.js.map +1 -0
- package/dist/models/line-item-tax-line.js +2 -1
- package/dist/models/line-item-tax-line.js.map +1 -1
- package/dist/models/line-item.d.ts +2 -0
- package/dist/models/line-item.js +5 -0
- package/dist/models/line-item.js.map +1 -1
- package/dist/models/money-amount.js +4 -1
- package/dist/models/money-amount.js.map +1 -1
- package/dist/models/price-list.js +6 -2
- package/dist/models/price-list.js.map +1 -1
- package/dist/models/product-variant.js +9 -3
- package/dist/models/product-variant.js.map +1 -1
- package/dist/models/shipping-method-tax-line.js +2 -1
- package/dist/models/shipping-method-tax-line.js.map +1 -1
- package/dist/models/swap.d.ts +4 -4
- package/dist/models/swap.js +23 -23
- package/dist/models/swap.js.map +1 -1
- 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 +34 -0
- package/dist/repositories/discount-condition.js +384 -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 +1 -0
- package/dist/repositories/money-amount.js +49 -2
- package/dist/repositories/money-amount.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 +177 -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/cart.d.ts +58 -47
- package/dist/services/cart.js +1306 -1391
- package/dist/services/cart.js.map +1 -1
- package/dist/services/customer.js +4 -2
- package/dist/services/customer.js.map +1 -1
- package/dist/services/discount.d.ts +50 -58
- package/dist/services/discount.js +363 -152
- 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 +436 -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.js +4 -4
- 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/product-variant.d.ts +26 -9
- package/dist/services/product-variant.js +222 -76
- package/dist/services/product-variant.js.map +1 -1
- package/dist/services/product.d.ts +22 -4
- package/dist/services/product.js +236 -48
- 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 +0 -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/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 +6 -0
- package/dist/types/batch-job.js +11 -0
- package/dist/types/batch-job.js.map +1 -0
- package/dist/types/common.d.ts +1 -3
- package/dist/types/discount.d.ts +69 -1
- package/dist/types/discount.js +129 -5
- package/dist/types/discount.js.map +1 -1
- package/dist/types/global.d.ts +34 -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/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 +16 -0
- package/dist/types/product.js +86 -1
- package/dist/types/product.js.map +1 -1
- 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/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/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/package.json +7 -8
- 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/store/carts/update-payment-method.d.ts +0 -38
- package/dist/api/routes/store/carts/update-payment-method.js +0 -122
- 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
|
@@ -104,17 +104,19 @@ 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
106
|
var medusa_interfaces_1 = require("medusa-interfaces");
|
|
107
|
+
var typeorm_1 = require("typeorm");
|
|
107
108
|
/* Provides layer to manipulate carts.
|
|
108
109
|
* @implements BaseService
|
|
109
110
|
*/
|
|
110
111
|
var CartService = /** @class */ (function (_super) {
|
|
111
112
|
__extends(CartService, _super);
|
|
112
113
|
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 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;
|
|
114
115
|
var _this = _super.call(this) || this;
|
|
115
116
|
_this.manager_ = manager;
|
|
116
117
|
_this.shippingMethodRepository_ = shippingMethodRepository;
|
|
117
118
|
_this.cartRepository_ = cartRepository;
|
|
119
|
+
_this.lineItemRepository_ = lineItemRepository;
|
|
118
120
|
_this.eventBus_ = eventBusService;
|
|
119
121
|
_this.productVariantService_ = productVariantService;
|
|
120
122
|
_this.productService_ = productService;
|
|
@@ -131,6 +133,8 @@ var CartService = /** @class */ (function (_super) {
|
|
|
131
133
|
_this.inventoryService_ = inventoryService;
|
|
132
134
|
_this.customShippingOptionService_ = customShippingOptionService;
|
|
133
135
|
_this.taxProviderService_ = taxProviderService;
|
|
136
|
+
_this.lineItemAdjustmentService_ = lineItemAdjustmentService;
|
|
137
|
+
_this.priceSelectionStrategy_ = priceSelectionStrategy;
|
|
134
138
|
return _this;
|
|
135
139
|
}
|
|
136
140
|
CartService.prototype.withTransaction = function (transactionManager) {
|
|
@@ -141,6 +145,7 @@ var CartService = /** @class */ (function (_super) {
|
|
|
141
145
|
manager: transactionManager,
|
|
142
146
|
taxProviderService: this.taxProviderService_,
|
|
143
147
|
cartRepository: this.cartRepository_,
|
|
148
|
+
lineItemRepository: this.lineItemRepository_,
|
|
144
149
|
eventBusService: this.eventBus_,
|
|
145
150
|
paymentProviderService: this.paymentProviderService_,
|
|
146
151
|
paymentSessionRepository: this.paymentSessionRepository_,
|
|
@@ -157,6 +162,8 @@ var CartService = /** @class */ (function (_super) {
|
|
|
157
162
|
giftCardService: this.giftCardService_,
|
|
158
163
|
inventoryService: this.inventoryService_,
|
|
159
164
|
customShippingOptionService: this.customShippingOptionService_,
|
|
165
|
+
lineItemAdjustmentService: this.lineItemAdjustmentService_,
|
|
166
|
+
priceSelectionStrategy: this.priceSelectionStrategy_,
|
|
160
167
|
});
|
|
161
168
|
cloned.transactionManager_ = transactionManager;
|
|
162
169
|
return cloned;
|
|
@@ -188,7 +195,6 @@ var CartService = /** @class */ (function (_super) {
|
|
|
188
195
|
relationSet.add("gift_cards");
|
|
189
196
|
relationSet.add("discounts");
|
|
190
197
|
relationSet.add("discounts.rule");
|
|
191
|
-
relationSet.add("discounts.rule.valid_for");
|
|
192
198
|
// relationSet.add("discounts.parent_discount")
|
|
193
199
|
// relationSet.add("discounts.parent_discount.rule")
|
|
194
200
|
// relationSet.add("discounts.parent_discount.regions")
|
|
@@ -288,13 +294,21 @@ var CartService = /** @class */ (function (_super) {
|
|
|
288
294
|
CartService.prototype.list = function (selector, config) {
|
|
289
295
|
if (config === void 0) { config = {}; }
|
|
290
296
|
return __awaiter(this, void 0, void 0, function () {
|
|
291
|
-
var
|
|
297
|
+
var _this = this;
|
|
292
298
|
return __generator(this, function (_a) {
|
|
293
299
|
switch (_a.label) {
|
|
294
|
-
case 0:
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
300
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
301
|
+
var cartRepo, query;
|
|
302
|
+
return __generator(this, function (_a) {
|
|
303
|
+
switch (_a.label) {
|
|
304
|
+
case 0:
|
|
305
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
306
|
+
query = this.buildQuery_(selector, config);
|
|
307
|
+
return [4 /*yield*/, cartRepo.find(query)];
|
|
308
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}); })];
|
|
298
312
|
case 1: return [2 /*return*/, _a.sent()];
|
|
299
313
|
}
|
|
300
314
|
});
|
|
@@ -311,33 +325,41 @@ var CartService = /** @class */ (function (_super) {
|
|
|
311
325
|
if (options === void 0) { options = {}; }
|
|
312
326
|
if (totalsConfig === void 0) { totalsConfig = {}; }
|
|
313
327
|
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
|
-
|
|
328
|
+
var _this = this;
|
|
329
|
+
return __generator(this, function (_a) {
|
|
330
|
+
switch (_a.label) {
|
|
331
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
332
|
+
var cartRepo, validatedId, _a, select, relations, totalsToSelect, query, queryRelations, raw;
|
|
333
|
+
return __generator(this, function (_b) {
|
|
334
|
+
switch (_b.label) {
|
|
335
|
+
case 0:
|
|
336
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
337
|
+
validatedId = this.validateId_(cartId);
|
|
338
|
+
_a = this.transformQueryForTotals_(options), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
|
|
339
|
+
query = this.buildQuery_({ id: validatedId }, __assign(__assign({}, options), { select: select, relations: relations }));
|
|
340
|
+
if (relations && relations.length > 0) {
|
|
341
|
+
query.relations = relations;
|
|
342
|
+
}
|
|
343
|
+
if (select && select.length > 0) {
|
|
344
|
+
query.select = select;
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
query.select = undefined;
|
|
348
|
+
}
|
|
349
|
+
queryRelations = query.relations;
|
|
350
|
+
query.relations = undefined;
|
|
351
|
+
return [4 /*yield*/, cartRepo.findOneWithRelations(queryRelations, query)];
|
|
352
|
+
case 1:
|
|
353
|
+
raw = _b.sent();
|
|
354
|
+
if (!raw) {
|
|
355
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Cart with ".concat(cartId, " was not found"));
|
|
356
|
+
}
|
|
357
|
+
return [4 /*yield*/, this.decorateTotals_(raw, totalsToSelect, totalsConfig)];
|
|
358
|
+
case 2: return [2 /*return*/, _b.sent()];
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
}); })];
|
|
362
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
341
363
|
}
|
|
342
364
|
});
|
|
343
365
|
});
|
|
@@ -351,101 +373,114 @@ var CartService = /** @class */ (function (_super) {
|
|
|
351
373
|
return __awaiter(this, void 0, void 0, function () {
|
|
352
374
|
var _this = this;
|
|
353
375
|
return __generator(this, function (_a) {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
376
|
+
switch (_a.label) {
|
|
377
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
378
|
+
var cartRepo, addressRepo, region_id, rawCart, customer, region, regCountries, shippingAddress, _a, remainingFields, remainingFields_1, remainingFields_1_1, remainingField, createdCart, cart;
|
|
379
|
+
var e_2, _b;
|
|
380
|
+
return __generator(this, function (_c) {
|
|
381
|
+
switch (_c.label) {
|
|
382
|
+
case 0:
|
|
383
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
384
|
+
addressRepo = transactionManager.getCustomRepository(this.addressRepository_);
|
|
385
|
+
region_id = data.region_id;
|
|
386
|
+
if (!region_id) {
|
|
387
|
+
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");
|
|
388
|
+
}
|
|
389
|
+
rawCart = {};
|
|
390
|
+
if (!data.email) return [3 /*break*/, 2];
|
|
391
|
+
return [4 /*yield*/, this.createOrFetchUserFromEmail_(data.email)];
|
|
392
|
+
case 1:
|
|
393
|
+
customer = _c.sent();
|
|
394
|
+
rawCart.customer = customer;
|
|
395
|
+
rawCart.customer_id = customer.id;
|
|
396
|
+
rawCart.email = customer.email;
|
|
397
|
+
_c.label = 2;
|
|
398
|
+
case 2: return [4 /*yield*/, this.regionService_
|
|
399
|
+
.withTransaction(transactionManager)
|
|
400
|
+
.retrieve(region_id, {
|
|
367
401
|
relations: ["countries"],
|
|
368
402
|
})];
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
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) {
|
|
397
|
-
if (region.countries.length === 1) {
|
|
398
|
-
// Preselect the country if the region only has 1
|
|
399
|
-
// and create address entity
|
|
400
|
-
toCreate.shipping_address = addressRepo.create({
|
|
401
|
-
country_code: regCountries[0],
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
else {
|
|
406
|
-
if (!regCountries.includes(data.shipping_address.country_code)) {
|
|
403
|
+
case 3:
|
|
404
|
+
region = _c.sent();
|
|
405
|
+
regCountries = region.countries.map(function (_a) {
|
|
406
|
+
var iso_2 = _a.iso_2;
|
|
407
|
+
return iso_2;
|
|
408
|
+
});
|
|
409
|
+
rawCart.region_id = region.id;
|
|
410
|
+
if (!(data.shipping_address_id !== undefined)) return [3 /*break*/, 7];
|
|
411
|
+
if (!data.shipping_address_id) return [3 /*break*/, 5];
|
|
412
|
+
return [4 /*yield*/, addressRepo.findOne(data.shipping_address_id)];
|
|
413
|
+
case 4:
|
|
414
|
+
_a = _c.sent();
|
|
415
|
+
return [3 /*break*/, 6];
|
|
416
|
+
case 5:
|
|
417
|
+
_a = null;
|
|
418
|
+
_c.label = 6;
|
|
419
|
+
case 6:
|
|
420
|
+
shippingAddress = _a;
|
|
421
|
+
if (shippingAddress &&
|
|
422
|
+
!regCountries.includes(shippingAddress.country_code)) {
|
|
407
423
|
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Shipping country not in region");
|
|
408
424
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
try {
|
|
420
|
-
for (remainingFields_1 = __values(remainingFields), remainingFields_1_1 = remainingFields_1.next(); !remainingFields_1_1.done; remainingFields_1_1 = remainingFields_1.next()) {
|
|
421
|
-
k = remainingFields_1_1.value;
|
|
422
|
-
if (typeof data[k] !== "undefined" && k !== "object") {
|
|
423
|
-
toCreate[k] = data[k];
|
|
425
|
+
rawCart.shipping_address = shippingAddress;
|
|
426
|
+
_c.label = 7;
|
|
427
|
+
case 7:
|
|
428
|
+
if (!data.shipping_address) {
|
|
429
|
+
if (region.countries.length === 1) {
|
|
430
|
+
// Preselect the country if the region only has 1
|
|
431
|
+
// and create address entity
|
|
432
|
+
rawCart.shipping_address = addressRepo.create({
|
|
433
|
+
country_code: regCountries[0],
|
|
434
|
+
});
|
|
424
435
|
}
|
|
425
436
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
437
|
+
else {
|
|
438
|
+
if (!regCountries.includes(data.shipping_address.country_code)) {
|
|
439
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Shipping country not in region");
|
|
440
|
+
}
|
|
441
|
+
rawCart.shipping_address = data.shipping_address;
|
|
442
|
+
}
|
|
443
|
+
remainingFields = [
|
|
444
|
+
"billing_address_id",
|
|
445
|
+
"context",
|
|
446
|
+
"type",
|
|
447
|
+
"metadata",
|
|
448
|
+
"discounts",
|
|
449
|
+
"gift_cards",
|
|
450
|
+
];
|
|
429
451
|
try {
|
|
430
|
-
|
|
452
|
+
for (remainingFields_1 = __values(remainingFields), remainingFields_1_1 = remainingFields_1.next(); !remainingFields_1_1.done; remainingFields_1_1 = remainingFields_1.next()) {
|
|
453
|
+
remainingField = remainingFields_1_1.value;
|
|
454
|
+
if (typeof data[remainingField] !== "undefined" &&
|
|
455
|
+
remainingField !== "object") {
|
|
456
|
+
rawCart[remainingField] = data[remainingField];
|
|
457
|
+
}
|
|
458
|
+
}
|
|
431
459
|
}
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
460
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
461
|
+
finally {
|
|
462
|
+
try {
|
|
463
|
+
if (remainingFields_1_1 && !remainingFields_1_1.done && (_b = remainingFields_1.return)) _b.call(remainingFields_1);
|
|
464
|
+
}
|
|
465
|
+
finally { if (e_2) throw e_2.error; }
|
|
466
|
+
}
|
|
467
|
+
createdCart = cartRepo.create(rawCart);
|
|
468
|
+
return [4 /*yield*/, cartRepo.save(createdCart)];
|
|
469
|
+
case 8:
|
|
470
|
+
cart = _c.sent();
|
|
471
|
+
return [4 /*yield*/, this.eventBus_
|
|
472
|
+
.withTransaction(transactionManager)
|
|
473
|
+
.emit(CartService.Events.CREATED, {
|
|
474
|
+
id: cart.id,
|
|
475
|
+
})];
|
|
476
|
+
case 9:
|
|
477
|
+
_c.sent();
|
|
478
|
+
return [2 /*return*/, cart];
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
}); })];
|
|
482
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
483
|
+
}
|
|
449
484
|
});
|
|
450
485
|
});
|
|
451
486
|
};
|
|
@@ -459,102 +494,72 @@ var CartService = /** @class */ (function (_super) {
|
|
|
459
494
|
return __awaiter(this, void 0, void 0, function () {
|
|
460
495
|
var _this = this;
|
|
461
496
|
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,
|
|
497
|
+
switch (_a.label) {
|
|
498
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
499
|
+
var cart, lineItem, lineItemRepository, result;
|
|
500
|
+
var _a;
|
|
501
|
+
return __generator(this, function (_b) {
|
|
502
|
+
switch (_b.label) {
|
|
503
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
504
|
+
relations: [
|
|
505
|
+
"items",
|
|
506
|
+
"items.variant",
|
|
507
|
+
"items.variant.product",
|
|
508
|
+
"payment_sessions",
|
|
509
|
+
],
|
|
519
510
|
})];
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
511
|
+
case 1:
|
|
512
|
+
cart = _b.sent();
|
|
513
|
+
lineItem = cart.items.find(function (item) { return item.id === lineItemId; });
|
|
514
|
+
if (!lineItem) {
|
|
515
|
+
return [2 /*return*/, cart];
|
|
516
|
+
}
|
|
517
|
+
if (!((_a = cart.shipping_methods) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 3];
|
|
518
|
+
return [4 /*yield*/, this.shippingOptionService_
|
|
519
|
+
.withTransaction(transactionManager)
|
|
520
|
+
.deleteShippingMethods(cart.shipping_methods)];
|
|
521
|
+
case 2:
|
|
522
|
+
_b.sent();
|
|
523
|
+
_b.label = 3;
|
|
524
|
+
case 3:
|
|
525
|
+
lineItemRepository = transactionManager.getCustomRepository(this.lineItemRepository_);
|
|
526
|
+
return [4 /*yield*/, lineItemRepository.update({
|
|
527
|
+
id: (0, typeorm_1.In)(cart.items.map(function (item) { return item.id; })),
|
|
528
|
+
}, {
|
|
529
|
+
has_shipping: false,
|
|
530
|
+
})];
|
|
531
|
+
case 4:
|
|
532
|
+
_b.sent();
|
|
533
|
+
return [4 /*yield*/, this.lineItemService_
|
|
534
|
+
.withTransaction(transactionManager)
|
|
535
|
+
.delete(lineItem.id)];
|
|
536
|
+
case 5:
|
|
537
|
+
_b.sent();
|
|
538
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
539
|
+
relations: ["items", "discounts", "discounts.rule"],
|
|
540
|
+
})];
|
|
541
|
+
case 6:
|
|
542
|
+
result = _b.sent();
|
|
543
|
+
return [4 /*yield*/, this.refreshAdjustments_(result)
|
|
544
|
+
// Notify subscribers
|
|
545
|
+
];
|
|
546
|
+
case 7:
|
|
547
|
+
_b.sent();
|
|
541
548
|
// Notify subscribers
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
});
|
|
557
|
-
}); })];
|
|
549
|
+
return [4 /*yield*/, this.eventBus_
|
|
550
|
+
.withTransaction(transactionManager)
|
|
551
|
+
.emit(CartService.Events.UPDATED, {
|
|
552
|
+
id: cart.id,
|
|
553
|
+
})];
|
|
554
|
+
case 8:
|
|
555
|
+
// Notify subscribers
|
|
556
|
+
_b.sent();
|
|
557
|
+
return [2 /*return*/, this.retrieve(cartId)];
|
|
558
|
+
}
|
|
559
|
+
});
|
|
560
|
+
}); })];
|
|
561
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
562
|
+
}
|
|
558
563
|
});
|
|
559
564
|
});
|
|
560
565
|
};
|
|
@@ -593,128 +598,86 @@ var CartService = /** @class */ (function (_super) {
|
|
|
593
598
|
return __awaiter(this, void 0, void 0, function () {
|
|
594
599
|
var _this = this;
|
|
595
600
|
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,
|
|
601
|
+
switch (_a.label) {
|
|
602
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
603
|
+
var cart, currentItem, quantity, lineItemRepository, result;
|
|
604
|
+
return __generator(this, function (_a) {
|
|
605
|
+
switch (_a.label) {
|
|
606
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
607
|
+
relations: [
|
|
608
|
+
"shipping_methods",
|
|
609
|
+
"items",
|
|
610
|
+
"items.adjustments",
|
|
611
|
+
"payment_sessions",
|
|
612
|
+
"items.variant",
|
|
613
|
+
"items.variant.product",
|
|
614
|
+
"discounts",
|
|
615
|
+
"discounts.rule",
|
|
616
|
+
],
|
|
658
617
|
})];
|
|
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
|
-
|
|
618
|
+
case 1:
|
|
619
|
+
cart = _a.sent();
|
|
620
|
+
if (lineItem.should_merge) {
|
|
621
|
+
currentItem = cart.items.find(function (item) {
|
|
622
|
+
if (item.should_merge && item.variant_id === lineItem.variant_id) {
|
|
623
|
+
return lodash_1.default.isEqual(item.metadata, lineItem.metadata);
|
|
624
|
+
}
|
|
625
|
+
return false;
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
quantity = currentItem
|
|
629
|
+
? (currentItem.quantity += lineItem.quantity)
|
|
630
|
+
: lineItem.quantity;
|
|
631
|
+
// Confirm inventory or throw error
|
|
632
|
+
return [4 /*yield*/, this.inventoryService_
|
|
633
|
+
.withTransaction(transactionManager)
|
|
634
|
+
.confirmInventory(lineItem.variant_id, quantity)];
|
|
635
|
+
case 2:
|
|
636
|
+
// Confirm inventory or throw error
|
|
637
|
+
_a.sent();
|
|
638
|
+
if (!currentItem) return [3 /*break*/, 4];
|
|
639
|
+
return [4 /*yield*/, this.lineItemService_
|
|
640
|
+
.withTransaction(transactionManager)
|
|
641
|
+
.update(currentItem.id, {
|
|
642
|
+
quantity: currentItem.quantity,
|
|
643
|
+
})];
|
|
644
|
+
case 3:
|
|
645
|
+
_a.sent();
|
|
646
|
+
return [3 /*break*/, 6];
|
|
647
|
+
case 4: return [4 /*yield*/, this.lineItemService_
|
|
648
|
+
.withTransaction(transactionManager)
|
|
649
|
+
.create(__assign(__assign({}, lineItem), { has_shipping: false, cart_id: cartId }))];
|
|
650
|
+
case 5:
|
|
651
|
+
_a.sent();
|
|
652
|
+
_a.label = 6;
|
|
653
|
+
case 6:
|
|
654
|
+
lineItemRepository = transactionManager.getCustomRepository(this.lineItemRepository_);
|
|
655
|
+
return [4 /*yield*/, lineItemRepository.update({
|
|
656
|
+
id: (0, typeorm_1.In)(cart.items.map(function (item) { return item.id; })),
|
|
657
|
+
}, {
|
|
658
|
+
has_shipping: false,
|
|
659
|
+
})];
|
|
660
|
+
case 7:
|
|
661
|
+
_a.sent();
|
|
662
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
663
|
+
relations: ["items", "discounts", "discounts.rule"],
|
|
664
|
+
})];
|
|
665
|
+
case 8:
|
|
666
|
+
result = _a.sent();
|
|
667
|
+
return [4 /*yield*/, this.refreshAdjustments_(result)];
|
|
668
|
+
case 9:
|
|
669
|
+
_a.sent();
|
|
670
|
+
return [4 /*yield*/, this.eventBus_
|
|
671
|
+
.withTransaction(transactionManager)
|
|
672
|
+
.emit(CartService.Events.UPDATED, result)];
|
|
673
|
+
case 10:
|
|
674
|
+
_a.sent();
|
|
675
|
+
return [2 /*return*/, result];
|
|
676
|
+
}
|
|
677
|
+
});
|
|
678
|
+
}); })];
|
|
679
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
680
|
+
}
|
|
718
681
|
});
|
|
719
682
|
});
|
|
720
683
|
};
|
|
@@ -729,50 +692,60 @@ var CartService = /** @class */ (function (_super) {
|
|
|
729
692
|
return __awaiter(this, void 0, void 0, function () {
|
|
730
693
|
var _this = this;
|
|
731
694
|
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
|
-
|
|
695
|
+
switch (_a.label) {
|
|
696
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
697
|
+
var cart, lineItemExists, hasInventory, updatedCart;
|
|
698
|
+
return __generator(this, function (_a) {
|
|
699
|
+
switch (_a.label) {
|
|
700
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
701
|
+
relations: ["items", "items.adjustments", "payment_sessions"],
|
|
702
|
+
})
|
|
703
|
+
// Ensure that the line item exists in the cart
|
|
704
|
+
];
|
|
705
|
+
case 1:
|
|
706
|
+
cart = _a.sent();
|
|
707
|
+
lineItemExists = cart.items.find(function (i) { return i.id === lineItemId; });
|
|
708
|
+
if (!lineItemExists) {
|
|
709
|
+
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");
|
|
710
|
+
}
|
|
711
|
+
if (!lineItemUpdate.quantity) return [3 /*break*/, 3];
|
|
712
|
+
return [4 /*yield*/, this.inventoryService_
|
|
713
|
+
.withTransaction(transactionManager)
|
|
714
|
+
.confirmInventory(lineItemExists.variant_id, lineItemUpdate.quantity)];
|
|
715
|
+
case 2:
|
|
716
|
+
hasInventory = _a.sent();
|
|
717
|
+
if (!hasInventory) {
|
|
718
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Inventory doesn't cover the desired quantity");
|
|
719
|
+
}
|
|
720
|
+
_a.label = 3;
|
|
721
|
+
case 3: return [4 /*yield*/, this.lineItemService_
|
|
722
|
+
.withTransaction(transactionManager)
|
|
723
|
+
.update(lineItemId, lineItemUpdate)];
|
|
724
|
+
case 4:
|
|
725
|
+
_a.sent();
|
|
726
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
727
|
+
relations: ["items", "discounts", "discounts.rule"],
|
|
728
|
+
})];
|
|
729
|
+
case 5:
|
|
730
|
+
updatedCart = _a.sent();
|
|
731
|
+
return [4 /*yield*/, this.refreshAdjustments_(updatedCart)
|
|
732
|
+
// Update the line item
|
|
733
|
+
];
|
|
734
|
+
case 6:
|
|
735
|
+
_a.sent();
|
|
736
|
+
// Update the line item
|
|
737
|
+
return [4 /*yield*/, this.eventBus_
|
|
738
|
+
.withTransaction(transactionManager)
|
|
739
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
740
|
+
case 7:
|
|
741
|
+
// Update the line item
|
|
742
|
+
_a.sent();
|
|
743
|
+
return [2 /*return*/, updatedCart];
|
|
744
|
+
}
|
|
745
|
+
});
|
|
746
|
+
}); })];
|
|
747
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
748
|
+
}
|
|
776
749
|
});
|
|
777
750
|
});
|
|
778
751
|
};
|
|
@@ -788,258 +761,190 @@ var CartService = /** @class */ (function (_super) {
|
|
|
788
761
|
CartService.prototype.adjustFreeShipping_ = function (cart, shouldAdd) {
|
|
789
762
|
var _a;
|
|
790
763
|
return __awaiter(this, void 0, void 0, function () {
|
|
764
|
+
var shippingMethodRepository_1;
|
|
791
765
|
var _this = this;
|
|
792
766
|
return __generator(this, function (_b) {
|
|
793
767
|
switch (_b.label) {
|
|
794
768
|
case 0:
|
|
795
|
-
if (!((_a = cart.shipping_methods) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/,
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
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
|
-
}); }))];
|
|
816
|
-
case 1:
|
|
817
|
-
_b.sent();
|
|
818
|
-
return [3 /*break*/, 4];
|
|
819
|
-
case 2: return [4 /*yield*/, Promise.all(cart.shipping_methods.map(function (sm) { return __awaiter(_this, void 0, void 0, function () {
|
|
820
|
-
var smRepo;
|
|
769
|
+
if (!((_a = cart.shipping_methods) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 3];
|
|
770
|
+
shippingMethodRepository_1 = this.transactionManager_.getCustomRepository(this.shippingMethodRepository_);
|
|
771
|
+
if (!shouldAdd) return [3 /*break*/, 1];
|
|
772
|
+
return [2 /*return*/, shippingMethodRepository_1.update({
|
|
773
|
+
id: (0, typeorm_1.In)(cart.shipping_methods.map(function (shippingMethod) { return shippingMethod.id; })),
|
|
774
|
+
}, {
|
|
775
|
+
price: 0,
|
|
776
|
+
})];
|
|
777
|
+
case 1: return [4 /*yield*/, Promise.all(cart.shipping_methods.map(function (shippingMethod) { return __awaiter(_this, void 0, void 0, function () {
|
|
821
778
|
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
|
-
}
|
|
779
|
+
// if free shipping discount is removed, we adjust the shipping
|
|
780
|
+
// back to its original amount
|
|
781
|
+
shippingMethod.price = shippingMethod.shipping_option.amount;
|
|
782
|
+
return [2 /*return*/, shippingMethodRepository_1.save(shippingMethod)];
|
|
833
783
|
});
|
|
834
784
|
}); }))];
|
|
835
|
-
case
|
|
785
|
+
case 2:
|
|
836
786
|
_b.sent();
|
|
837
|
-
_b.label =
|
|
838
|
-
case
|
|
787
|
+
_b.label = 3;
|
|
788
|
+
case 3: return [2 /*return*/];
|
|
839
789
|
}
|
|
840
790
|
});
|
|
841
791
|
});
|
|
842
792
|
};
|
|
843
|
-
CartService.prototype.update = function (cartId,
|
|
793
|
+
CartService.prototype.update = function (cartId, data) {
|
|
844
794
|
return __awaiter(this, void 0, void 0, function () {
|
|
845
795
|
var _this = this;
|
|
846
796
|
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
|
-
}); })];
|
|
797
|
+
switch (_a.label) {
|
|
798
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
799
|
+
var cartRepo, cart, customer, countryCode, addrRepo, billingAddress, shippingAddress, previousDiscounts, hasFreeShipping, prevContext, updatedCart;
|
|
800
|
+
var _this = this;
|
|
801
|
+
var _a, _b, _c, _d, _e, _f;
|
|
802
|
+
return __generator(this, function (_g) {
|
|
803
|
+
switch (_g.label) {
|
|
804
|
+
case 0:
|
|
805
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
806
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
807
|
+
select: [
|
|
808
|
+
"subtotal",
|
|
809
|
+
"tax_total",
|
|
810
|
+
"shipping_total",
|
|
811
|
+
"discount_total",
|
|
812
|
+
"total",
|
|
813
|
+
],
|
|
814
|
+
relations: [
|
|
815
|
+
"items",
|
|
816
|
+
"shipping_methods",
|
|
817
|
+
"shipping_address",
|
|
818
|
+
"billing_address",
|
|
819
|
+
"gift_cards",
|
|
820
|
+
"customer",
|
|
821
|
+
"region",
|
|
822
|
+
"payment_sessions",
|
|
823
|
+
"region.countries",
|
|
824
|
+
"discounts",
|
|
825
|
+
"discounts.rule",
|
|
826
|
+
"discounts.regions",
|
|
827
|
+
],
|
|
828
|
+
})];
|
|
829
|
+
case 1:
|
|
830
|
+
cart = _g.sent();
|
|
831
|
+
if (!data.customer_id) return [3 /*break*/, 3];
|
|
832
|
+
return [4 /*yield*/, this.updateCustomerId_(cart, data.customer_id)];
|
|
833
|
+
case 2:
|
|
834
|
+
_g.sent();
|
|
835
|
+
return [3 /*break*/, 5];
|
|
836
|
+
case 3:
|
|
837
|
+
if (!(typeof data.email !== "undefined")) return [3 /*break*/, 5];
|
|
838
|
+
return [4 /*yield*/, this.createOrFetchUserFromEmail_(data.email)];
|
|
839
|
+
case 4:
|
|
840
|
+
customer = _g.sent();
|
|
841
|
+
cart.customer = customer;
|
|
842
|
+
cart.customer_id = customer.id;
|
|
843
|
+
cart.email = customer.email;
|
|
844
|
+
_g.label = 5;
|
|
845
|
+
case 5:
|
|
846
|
+
if (!(typeof data.region_id !== "undefined")) return [3 /*break*/, 7];
|
|
847
|
+
countryCode = (_b = (data.country_code || ((_a = data.shipping_address) === null || _a === void 0 ? void 0 : _a.country_code))) !== null && _b !== void 0 ? _b : null;
|
|
848
|
+
return [4 /*yield*/, this.setRegion_(cart, data.region_id, countryCode)];
|
|
849
|
+
case 6:
|
|
850
|
+
_g.sent();
|
|
851
|
+
_g.label = 7;
|
|
852
|
+
case 7:
|
|
853
|
+
if (!(typeof data.customer_id !== "undefined" ||
|
|
854
|
+
typeof data.region_id !== "undefined")) return [3 /*break*/, 9];
|
|
855
|
+
return [4 /*yield*/, this.updateUnitPrices_(cart, data.region_id, data.customer_id)];
|
|
856
|
+
case 8:
|
|
857
|
+
_g.sent();
|
|
858
|
+
_g.label = 9;
|
|
859
|
+
case 9:
|
|
860
|
+
addrRepo = transactionManager.getCustomRepository(this.addressRepository_);
|
|
861
|
+
billingAddress = (_c = data.billing_address_id) !== null && _c !== void 0 ? _c : data.billing_address;
|
|
862
|
+
if (!(billingAddress !== undefined)) return [3 /*break*/, 11];
|
|
863
|
+
return [4 /*yield*/, this.updateBillingAddress_(cart, billingAddress, addrRepo)];
|
|
864
|
+
case 10:
|
|
865
|
+
_g.sent();
|
|
866
|
+
_g.label = 11;
|
|
867
|
+
case 11:
|
|
868
|
+
shippingAddress = (_d = data.shipping_address_id) !== null && _d !== void 0 ? _d : data.shipping_address;
|
|
869
|
+
if (!(shippingAddress !== undefined)) return [3 /*break*/, 13];
|
|
870
|
+
return [4 /*yield*/, this.updateShippingAddress_(cart, shippingAddress, addrRepo)];
|
|
871
|
+
case 12:
|
|
872
|
+
_g.sent();
|
|
873
|
+
_g.label = 13;
|
|
874
|
+
case 13:
|
|
875
|
+
if (!((_e = data.discounts) === null || _e === void 0 ? void 0 : _e.length)) return [3 /*break*/, 18];
|
|
876
|
+
previousDiscounts = __spreadArray([], __read(cart.discounts), false);
|
|
877
|
+
cart.discounts.length = 0;
|
|
878
|
+
return [4 /*yield*/, Promise.all(data.discounts.map(function (_a) {
|
|
879
|
+
var code = _a.code;
|
|
880
|
+
return _this.applyDiscount(cart, code);
|
|
881
|
+
}))];
|
|
882
|
+
case 14:
|
|
883
|
+
_g.sent();
|
|
884
|
+
hasFreeShipping = cart.discounts.some(function (_a) {
|
|
885
|
+
var rule = _a.rule;
|
|
886
|
+
return (rule === null || rule === void 0 ? void 0 : rule.type) === "free_shipping";
|
|
887
|
+
});
|
|
888
|
+
if (!(previousDiscounts.some(function (_a) {
|
|
889
|
+
var rule = _a.rule;
|
|
890
|
+
return (rule === null || rule === void 0 ? void 0 : rule.type) === "free_shipping";
|
|
891
|
+
}) &&
|
|
892
|
+
!hasFreeShipping)) return [3 /*break*/, 16];
|
|
893
|
+
return [4 /*yield*/, this.adjustFreeShipping_(cart, false)];
|
|
894
|
+
case 15:
|
|
895
|
+
_g.sent();
|
|
896
|
+
_g.label = 16;
|
|
897
|
+
case 16:
|
|
898
|
+
if (!hasFreeShipping) return [3 /*break*/, 18];
|
|
899
|
+
return [4 /*yield*/, this.adjustFreeShipping_(cart, true)];
|
|
900
|
+
case 17:
|
|
901
|
+
_g.sent();
|
|
902
|
+
_g.label = 18;
|
|
903
|
+
case 18:
|
|
904
|
+
if (!("gift_cards" in data)) return [3 /*break*/, 20];
|
|
905
|
+
cart.gift_cards = [];
|
|
906
|
+
return [4 /*yield*/, Promise.all(((_f = data.gift_cards) !== null && _f !== void 0 ? _f : []).map(function (_a) {
|
|
907
|
+
var code = _a.code;
|
|
908
|
+
return _this.applyGiftCard_(cart, code);
|
|
909
|
+
}))];
|
|
910
|
+
case 19:
|
|
911
|
+
_g.sent();
|
|
912
|
+
_g.label = 20;
|
|
913
|
+
case 20:
|
|
914
|
+
if ("metadata" in data) {
|
|
915
|
+
cart.metadata = this.setMetadata_(cart, data.metadata);
|
|
916
|
+
}
|
|
917
|
+
if ("context" in data) {
|
|
918
|
+
prevContext = cart.context || {};
|
|
919
|
+
cart.context = __assign(__assign({}, prevContext), data.context);
|
|
920
|
+
}
|
|
921
|
+
if ("completed_at" in data) {
|
|
922
|
+
cart.completed_at = data.completed_at;
|
|
923
|
+
}
|
|
924
|
+
if ("payment_authorized_at" in data) {
|
|
925
|
+
cart.payment_authorized_at = data.payment_authorized_at;
|
|
926
|
+
}
|
|
927
|
+
return [4 /*yield*/, cartRepo.save(cart)];
|
|
928
|
+
case 21:
|
|
929
|
+
updatedCart = _g.sent();
|
|
930
|
+
if (!("email" in data || "customer_id" in data)) return [3 /*break*/, 23];
|
|
931
|
+
return [4 /*yield*/, this.eventBus_
|
|
932
|
+
.withTransaction(transactionManager)
|
|
933
|
+
.emit(CartService.Events.CUSTOMER_UPDATED, updatedCart.id)];
|
|
934
|
+
case 22:
|
|
935
|
+
_g.sent();
|
|
936
|
+
_g.label = 23;
|
|
937
|
+
case 23: return [4 /*yield*/, this.eventBus_
|
|
938
|
+
.withTransaction(transactionManager)
|
|
939
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
940
|
+
case 24:
|
|
941
|
+
_g.sent();
|
|
942
|
+
return [2 /*return*/, updatedCart];
|
|
943
|
+
}
|
|
944
|
+
});
|
|
945
|
+
}); })];
|
|
946
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
947
|
+
}
|
|
1043
948
|
});
|
|
1044
949
|
});
|
|
1045
950
|
};
|
|
@@ -1111,27 +1016,27 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1111
1016
|
CartService.prototype.updateBillingAddress_ = function (cart, addressOrId, addrRepo) {
|
|
1112
1017
|
var _a, _b;
|
|
1113
1018
|
return __awaiter(this, void 0, void 0, function () {
|
|
1114
|
-
var address,
|
|
1115
|
-
return __generator(this, function (
|
|
1116
|
-
switch (
|
|
1019
|
+
var address, _c, addr;
|
|
1020
|
+
return __generator(this, function (_d) {
|
|
1021
|
+
switch (_d.label) {
|
|
1117
1022
|
case 0:
|
|
1118
1023
|
if (!(typeof addressOrId === "string")) return [3 /*break*/, 2];
|
|
1119
1024
|
return [4 /*yield*/, addrRepo.findOne({
|
|
1120
1025
|
where: { id: addressOrId },
|
|
1121
1026
|
})];
|
|
1122
1027
|
case 1:
|
|
1123
|
-
address = (
|
|
1028
|
+
address = (_d.sent());
|
|
1124
1029
|
return [3 /*break*/, 3];
|
|
1125
1030
|
case 2:
|
|
1126
1031
|
address = addressOrId;
|
|
1127
|
-
|
|
1032
|
+
_d.label = 3;
|
|
1128
1033
|
case 3:
|
|
1129
1034
|
address.country_code = (_b = (_a = address.country_code) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : null;
|
|
1130
1035
|
if (!address.id) return [3 /*break*/, 5];
|
|
1036
|
+
_c = cart;
|
|
1131
1037
|
return [4 /*yield*/, addrRepo.save(address)];
|
|
1132
1038
|
case 4:
|
|
1133
|
-
|
|
1134
|
-
cart.billing_address = updated;
|
|
1039
|
+
_c.billing_address = _d.sent();
|
|
1135
1040
|
return [3 /*break*/, 9];
|
|
1136
1041
|
case 5:
|
|
1137
1042
|
if (!cart.billing_address_id) return [3 /*break*/, 8];
|
|
@@ -1139,15 +1044,14 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1139
1044
|
where: { id: cart.billing_address_id },
|
|
1140
1045
|
})];
|
|
1141
1046
|
case 6:
|
|
1142
|
-
addr =
|
|
1047
|
+
addr = _d.sent();
|
|
1143
1048
|
return [4 /*yield*/, addrRepo.save(__assign(__assign({}, addr), address))];
|
|
1144
1049
|
case 7:
|
|
1145
|
-
|
|
1050
|
+
_d.sent();
|
|
1146
1051
|
return [3 /*break*/, 9];
|
|
1147
1052
|
case 8:
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
_c.label = 9;
|
|
1053
|
+
cart.billing_address = addrRepo.create(__assign({}, address));
|
|
1054
|
+
_d.label = 9;
|
|
1151
1055
|
case 9: return [2 /*return*/];
|
|
1152
1056
|
}
|
|
1153
1057
|
});
|
|
@@ -1163,9 +1067,9 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1163
1067
|
CartService.prototype.updateShippingAddress_ = function (cart, addressOrId, addrRepo) {
|
|
1164
1068
|
var _a, _b;
|
|
1165
1069
|
return __awaiter(this, void 0, void 0, function () {
|
|
1166
|
-
var address,
|
|
1167
|
-
return __generator(this, function (
|
|
1168
|
-
switch (
|
|
1070
|
+
var address, _c, addr;
|
|
1071
|
+
return __generator(this, function (_d) {
|
|
1072
|
+
switch (_d.label) {
|
|
1169
1073
|
case 0:
|
|
1170
1074
|
if (addressOrId === null) {
|
|
1171
1075
|
cart.shipping_address = null;
|
|
@@ -1176,11 +1080,11 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1176
1080
|
where: { id: addressOrId },
|
|
1177
1081
|
})];
|
|
1178
1082
|
case 1:
|
|
1179
|
-
address = (
|
|
1083
|
+
address = (_d.sent());
|
|
1180
1084
|
return [3 /*break*/, 3];
|
|
1181
1085
|
case 2:
|
|
1182
1086
|
address = addressOrId;
|
|
1183
|
-
|
|
1087
|
+
_d.label = 3;
|
|
1184
1088
|
case 3:
|
|
1185
1089
|
address.country_code = (_b = (_a = address.country_code) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : null;
|
|
1186
1090
|
if (address.country_code &&
|
|
@@ -1191,10 +1095,10 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1191
1095
|
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Shipping country must be in the cart region");
|
|
1192
1096
|
}
|
|
1193
1097
|
if (!address.id) return [3 /*break*/, 5];
|
|
1098
|
+
_c = cart;
|
|
1194
1099
|
return [4 /*yield*/, addrRepo.save(address)];
|
|
1195
1100
|
case 4:
|
|
1196
|
-
|
|
1197
|
-
cart.shipping_address = updated;
|
|
1101
|
+
_c.shipping_address = _d.sent();
|
|
1198
1102
|
return [3 /*break*/, 9];
|
|
1199
1103
|
case 5:
|
|
1200
1104
|
if (!cart.shipping_address_id) return [3 /*break*/, 8];
|
|
@@ -1202,15 +1106,14 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1202
1106
|
where: { id: cart.shipping_address_id },
|
|
1203
1107
|
})];
|
|
1204
1108
|
case 6:
|
|
1205
|
-
addr =
|
|
1109
|
+
addr = _d.sent();
|
|
1206
1110
|
return [4 /*yield*/, addrRepo.save(__assign(__assign({}, addr), address))];
|
|
1207
1111
|
case 7:
|
|
1208
|
-
|
|
1112
|
+
_d.sent();
|
|
1209
1113
|
return [3 /*break*/, 9];
|
|
1210
1114
|
case 8:
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
_c.label = 9;
|
|
1115
|
+
cart.shipping_address = addrRepo.create(__assign({}, address));
|
|
1116
|
+
_d.label = 9;
|
|
1214
1117
|
case 9: return [2 /*return*/];
|
|
1215
1118
|
}
|
|
1216
1119
|
});
|
|
@@ -1221,7 +1124,9 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1221
1124
|
var giftCard;
|
|
1222
1125
|
return __generator(this, function (_a) {
|
|
1223
1126
|
switch (_a.label) {
|
|
1224
|
-
case 0: return [4 /*yield*/, this.giftCardService_
|
|
1127
|
+
case 0: return [4 /*yield*/, this.giftCardService_
|
|
1128
|
+
.withTransaction(this.transactionManager_)
|
|
1129
|
+
.retrieveByCode(code)];
|
|
1225
1130
|
case 1:
|
|
1226
1131
|
giftCard = _a.sent();
|
|
1227
1132
|
if (giftCard.is_disabled) {
|
|
@@ -1235,7 +1140,7 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1235
1140
|
var id = _a.id;
|
|
1236
1141
|
return id === giftCard.id;
|
|
1237
1142
|
})) {
|
|
1238
|
-
return [2 /*return
|
|
1143
|
+
return [2 /*return*/];
|
|
1239
1144
|
}
|
|
1240
1145
|
cart.gift_cards = __spreadArray(__spreadArray([], __read(cart.gift_cards), false), [giftCard], false);
|
|
1241
1146
|
return [2 /*return*/];
|
|
@@ -1254,80 +1159,65 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1254
1159
|
*/
|
|
1255
1160
|
CartService.prototype.applyDiscount = function (cart, discountCode) {
|
|
1256
1161
|
return __awaiter(this, void 0, void 0, function () {
|
|
1257
|
-
var
|
|
1162
|
+
var _this = this;
|
|
1258
1163
|
return __generator(this, function (_a) {
|
|
1259
1164
|
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;
|
|
1165
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1166
|
+
var discount, rule, toParse, sawNotShipping, newDiscounts;
|
|
1167
|
+
return __generator(this, function (_a) {
|
|
1168
|
+
switch (_a.label) {
|
|
1169
|
+
case 0: return [4 /*yield*/, this.discountService_
|
|
1170
|
+
.withTransaction(transactionManager)
|
|
1171
|
+
.retrieveByCode(discountCode, ["rule", "regions"])];
|
|
1172
|
+
case 1:
|
|
1173
|
+
discount = _a.sent();
|
|
1174
|
+
return [4 /*yield*/, this.discountService_
|
|
1175
|
+
.withTransaction(transactionManager)
|
|
1176
|
+
.validateDiscountForCartOrThrow(cart, discount)];
|
|
1177
|
+
case 2:
|
|
1178
|
+
_a.sent();
|
|
1179
|
+
rule = discount.rule;
|
|
1180
|
+
// if discount is already there, we simply resolve
|
|
1181
|
+
if (cart.discounts.find(function (_a) {
|
|
1182
|
+
var id = _a.id;
|
|
1183
|
+
return id === discount.id;
|
|
1184
|
+
})) {
|
|
1185
|
+
return [2 /*return*/];
|
|
1323
1186
|
}
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1187
|
+
toParse = __spreadArray(__spreadArray([], __read(cart.discounts), false), [discount], false);
|
|
1188
|
+
sawNotShipping = false;
|
|
1189
|
+
newDiscounts = toParse.map(function (discountToParse) {
|
|
1190
|
+
var _a;
|
|
1191
|
+
switch ((_a = discountToParse.rule) === null || _a === void 0 ? void 0 : _a.type) {
|
|
1192
|
+
case "free_shipping":
|
|
1193
|
+
if (discountToParse.rule.type === rule.type) {
|
|
1194
|
+
return discount;
|
|
1195
|
+
}
|
|
1196
|
+
return discountToParse;
|
|
1197
|
+
default:
|
|
1198
|
+
if (!sawNotShipping) {
|
|
1199
|
+
sawNotShipping = true;
|
|
1200
|
+
if ((rule === null || rule === void 0 ? void 0 : rule.type) !== "free_shipping") {
|
|
1201
|
+
return discount;
|
|
1202
|
+
}
|
|
1203
|
+
return discountToParse;
|
|
1204
|
+
}
|
|
1205
|
+
return null;
|
|
1206
|
+
}
|
|
1207
|
+
});
|
|
1208
|
+
cart.discounts = newDiscounts.filter(function (newDiscount) {
|
|
1209
|
+
return !!newDiscount;
|
|
1210
|
+
});
|
|
1211
|
+
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];
|
|
1212
|
+
return [4 /*yield*/, this.refreshAdjustments_(cart)];
|
|
1213
|
+
case 3:
|
|
1214
|
+
_a.sent();
|
|
1215
|
+
_a.label = 4;
|
|
1216
|
+
case 4: return [2 /*return*/];
|
|
1217
|
+
}
|
|
1218
|
+
});
|
|
1219
|
+
}); })];
|
|
1220
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1331
1221
|
}
|
|
1332
1222
|
});
|
|
1333
1223
|
});
|
|
@@ -1342,94 +1232,101 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1342
1232
|
return __awaiter(this, void 0, void 0, function () {
|
|
1343
1233
|
var _this = this;
|
|
1344
1234
|
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
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1235
|
+
switch (_a.label) {
|
|
1236
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1237
|
+
var cart, cartRepo, updatedCart;
|
|
1238
|
+
var _a;
|
|
1239
|
+
return __generator(this, function (_b) {
|
|
1240
|
+
switch (_b.label) {
|
|
1241
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
1242
|
+
relations: [
|
|
1243
|
+
"discounts",
|
|
1244
|
+
"discounts.rule",
|
|
1245
|
+
"payment_sessions",
|
|
1246
|
+
"shipping_methods",
|
|
1247
|
+
],
|
|
1248
|
+
})];
|
|
1249
|
+
case 1:
|
|
1250
|
+
cart = _b.sent();
|
|
1251
|
+
if (!cart.discounts.some(function (_a) {
|
|
1252
|
+
var rule = _a.rule;
|
|
1253
|
+
return rule.type === "free_shipping";
|
|
1254
|
+
})) return [3 /*break*/, 3];
|
|
1255
|
+
return [4 /*yield*/, this.adjustFreeShipping_(cart, false)];
|
|
1256
|
+
case 2:
|
|
1257
|
+
_b.sent();
|
|
1258
|
+
_b.label = 3;
|
|
1259
|
+
case 3:
|
|
1260
|
+
cart.discounts = cart.discounts.filter(function (discount) { return discount.code !== discountCode; });
|
|
1261
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
1262
|
+
return [4 /*yield*/, cartRepo.save(cart)];
|
|
1263
|
+
case 4:
|
|
1264
|
+
updatedCart = _b.sent();
|
|
1265
|
+
if (!((_a = updatedCart.payment_sessions) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 6];
|
|
1266
|
+
return [4 /*yield*/, this.setPaymentSessions(cartId)];
|
|
1267
|
+
case 5:
|
|
1268
|
+
_b.sent();
|
|
1269
|
+
_b.label = 6;
|
|
1270
|
+
case 6: return [4 /*yield*/, this.eventBus_
|
|
1271
|
+
.withTransaction(transactionManager)
|
|
1272
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
1273
|
+
case 7:
|
|
1274
|
+
_b.sent();
|
|
1275
|
+
return [2 /*return*/, updatedCart];
|
|
1276
|
+
}
|
|
1277
|
+
});
|
|
1278
|
+
}); })];
|
|
1279
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1280
|
+
}
|
|
1281
|
+
});
|
|
1282
|
+
});
|
|
1283
|
+
};
|
|
1284
|
+
/**
|
|
1285
|
+
* Updates the currently selected payment session.
|
|
1286
|
+
* @param cartId - the id of the cart to update the payment session for
|
|
1287
|
+
* @param update - the data to update the payment session with
|
|
1288
|
+
* @return the resulting cart
|
|
1289
|
+
*/
|
|
1290
|
+
CartService.prototype.updatePaymentSession = function (cartId, update) {
|
|
1291
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1292
|
+
var _this = this;
|
|
1293
|
+
return __generator(this, function (_a) {
|
|
1294
|
+
switch (_a.label) {
|
|
1295
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1296
|
+
var cart, updatedCart;
|
|
1297
|
+
return __generator(this, function (_a) {
|
|
1298
|
+
switch (_a.label) {
|
|
1299
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
1300
|
+
relations: ["payment_sessions"],
|
|
1301
|
+
})];
|
|
1302
|
+
case 1:
|
|
1303
|
+
cart = _a.sent();
|
|
1304
|
+
if (!cart.payment_session) return [3 /*break*/, 3];
|
|
1305
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
1306
|
+
.withTransaction(transactionManager)
|
|
1307
|
+
.updateSessionData(cart.payment_session, update)];
|
|
1308
|
+
case 2:
|
|
1309
|
+
_a.sent();
|
|
1310
|
+
_a.label = 3;
|
|
1311
|
+
case 3: return [4 /*yield*/, this.retrieve(cart.id)];
|
|
1312
|
+
case 4:
|
|
1313
|
+
updatedCart = _a.sent();
|
|
1314
|
+
return [4 /*yield*/, this.eventBus_
|
|
1315
|
+
.withTransaction(transactionManager)
|
|
1316
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
1317
|
+
case 5:
|
|
1318
|
+
_a.sent();
|
|
1319
|
+
return [2 /*return*/, updatedCart];
|
|
1320
|
+
}
|
|
1321
|
+
});
|
|
1322
|
+
}); })];
|
|
1323
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1324
|
+
}
|
|
1325
|
+
});
|
|
1326
|
+
});
|
|
1327
|
+
};
|
|
1328
|
+
/**
|
|
1329
|
+
* Authorizes a payment for a cart.
|
|
1433
1330
|
* Will authorize with chosen payment provider. This will return
|
|
1434
1331
|
* a payment object, that we will use to update our cart payment with.
|
|
1435
1332
|
* Additionally, if the payment does not require more or fails, we will
|
|
@@ -1445,58 +1342,64 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1445
1342
|
return __awaiter(this, void 0, void 0, function () {
|
|
1446
1343
|
var _this = this;
|
|
1447
1344
|
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
|
-
|
|
1345
|
+
switch (_a.label) {
|
|
1346
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1347
|
+
var cartRepository, cart, session, freshCart, _a, updatedCart;
|
|
1348
|
+
return __generator(this, function (_b) {
|
|
1349
|
+
switch (_b.label) {
|
|
1350
|
+
case 0:
|
|
1351
|
+
cartRepository = transactionManager.getCustomRepository(this.cartRepository_);
|
|
1352
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
1353
|
+
select: ["total"],
|
|
1354
|
+
relations: ["region", "payment_sessions"],
|
|
1355
|
+
})];
|
|
1356
|
+
case 1:
|
|
1357
|
+
cart = _b.sent();
|
|
1358
|
+
if (typeof cart.total === "undefined") {
|
|
1359
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "cart.total should be defined");
|
|
1360
|
+
}
|
|
1361
|
+
// If cart total is 0, we don't perform anything payment related
|
|
1362
|
+
if (cart.total <= 0) {
|
|
1363
|
+
cart.payment_authorized_at = new Date();
|
|
1364
|
+
return [2 /*return*/, cartRepository.save(cart)];
|
|
1365
|
+
}
|
|
1366
|
+
if (!cart.payment_session) {
|
|
1367
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "You cannot complete a cart without a payment session.");
|
|
1368
|
+
}
|
|
1369
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
1370
|
+
.withTransaction(transactionManager)
|
|
1371
|
+
.authorizePayment(cart.payment_session, context)];
|
|
1372
|
+
case 2:
|
|
1373
|
+
session = _b.sent();
|
|
1374
|
+
return [4 /*yield*/, this.retrieve(cart.id, {
|
|
1375
|
+
select: ["total"],
|
|
1376
|
+
relations: ["payment_sessions"],
|
|
1377
|
+
})];
|
|
1378
|
+
case 3:
|
|
1379
|
+
freshCart = _b.sent();
|
|
1380
|
+
if (!(session.status === "authorized")) return [3 /*break*/, 5];
|
|
1381
|
+
_a = freshCart;
|
|
1382
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
1383
|
+
.withTransaction(transactionManager)
|
|
1384
|
+
.createPayment(freshCart)];
|
|
1385
|
+
case 4:
|
|
1386
|
+
_a.payment = _b.sent();
|
|
1387
|
+
freshCart.payment_authorized_at = new Date();
|
|
1388
|
+
_b.label = 5;
|
|
1389
|
+
case 5: return [4 /*yield*/, cartRepository.save(freshCart)];
|
|
1390
|
+
case 6:
|
|
1391
|
+
updatedCart = _b.sent();
|
|
1392
|
+
return [4 /*yield*/, this.eventBus_
|
|
1393
|
+
.withTransaction(transactionManager)
|
|
1394
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
1395
|
+
case 7:
|
|
1396
|
+
_b.sent();
|
|
1397
|
+
return [2 /*return*/, updatedCart];
|
|
1398
|
+
}
|
|
1399
|
+
});
|
|
1400
|
+
}); })];
|
|
1401
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1402
|
+
}
|
|
1500
1403
|
});
|
|
1501
1404
|
});
|
|
1502
1405
|
};
|
|
@@ -1510,66 +1413,66 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1510
1413
|
return __awaiter(this, void 0, void 0, function () {
|
|
1511
1414
|
var _this = this;
|
|
1512
1415
|
return __generator(this, function (_a) {
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1416
|
+
switch (_a.label) {
|
|
1417
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1418
|
+
var psRepo, cart, paymentSession, updatedCart;
|
|
1419
|
+
var _this = this;
|
|
1420
|
+
return __generator(this, function (_a) {
|
|
1421
|
+
switch (_a.label) {
|
|
1422
|
+
case 0:
|
|
1423
|
+
psRepo = transactionManager.getCustomRepository(this.paymentSessionRepository_);
|
|
1424
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
1425
|
+
select: [
|
|
1426
|
+
"total",
|
|
1427
|
+
"subtotal",
|
|
1428
|
+
"tax_total",
|
|
1429
|
+
"discount_total",
|
|
1430
|
+
"gift_card_total",
|
|
1431
|
+
],
|
|
1432
|
+
relations: ["region", "region.payment_providers", "payment_sessions"],
|
|
1433
|
+
})
|
|
1434
|
+
// The region must have the provider id in its providers array
|
|
1435
|
+
];
|
|
1436
|
+
case 1:
|
|
1437
|
+
cart = _a.sent();
|
|
1530
1438
|
// 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")];
|
|
1439
|
+
if (providerId !== "system" &&
|
|
1440
|
+
!(cart.region.payment_providers.length &&
|
|
1441
|
+
cart.region.payment_providers.find(function (_a) {
|
|
1442
|
+
var id = _a.id;
|
|
1443
|
+
return providerId === id;
|
|
1444
|
+
}))) {
|
|
1445
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "The payment method is not available in this region");
|
|
1446
|
+
}
|
|
1447
|
+
return [4 /*yield*/, Promise.all(cart.payment_sessions.map(function (paymentSession) { return __awaiter(_this, void 0, void 0, function () {
|
|
1448
|
+
return __generator(this, function (_a) {
|
|
1449
|
+
return [2 /*return*/, psRepo.save(__assign(__assign({}, paymentSession), { is_selected: null }))];
|
|
1450
|
+
});
|
|
1451
|
+
}); }))];
|
|
1452
|
+
case 2:
|
|
1453
|
+
_a.sent();
|
|
1454
|
+
paymentSession = cart.payment_sessions.find(function (ps) { return ps.provider_id === providerId; });
|
|
1455
|
+
if (!paymentSession) {
|
|
1456
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Could not find payment session");
|
|
1457
|
+
}
|
|
1458
|
+
paymentSession.is_selected = true;
|
|
1459
|
+
return [4 /*yield*/, psRepo.save(paymentSession)];
|
|
1460
|
+
case 3:
|
|
1461
|
+
_a.sent();
|
|
1462
|
+
return [4 /*yield*/, this.retrieve(cartId)];
|
|
1463
|
+
case 4:
|
|
1464
|
+
updatedCart = _a.sent();
|
|
1465
|
+
return [4 /*yield*/, this.eventBus_
|
|
1466
|
+
.withTransaction(transactionManager)
|
|
1467
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
1468
|
+
case 5:
|
|
1469
|
+
_a.sent();
|
|
1470
|
+
return [2 /*return*/, updatedCart];
|
|
1471
|
+
}
|
|
1472
|
+
});
|
|
1473
|
+
}); }, "SERIALIZABLE")];
|
|
1474
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1475
|
+
}
|
|
1573
1476
|
});
|
|
1574
1477
|
});
|
|
1575
1478
|
};
|
|
@@ -1586,147 +1489,104 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1586
1489
|
return __awaiter(this, void 0, void 0, function () {
|
|
1587
1490
|
var _this = this;
|
|
1588
1491
|
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
|
-
return
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
if (!(total <= 0 ||
|
|
1492
|
+
switch (_a.label) {
|
|
1493
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1494
|
+
var psRepo, cartId, cart, total, region, seen, paymentProvider, paymentSession;
|
|
1495
|
+
var _this = this;
|
|
1496
|
+
var _a;
|
|
1497
|
+
return __generator(this, function (_b) {
|
|
1498
|
+
switch (_b.label) {
|
|
1499
|
+
case 0:
|
|
1500
|
+
psRepo = transactionManager.getCustomRepository(this.paymentSessionRepository_);
|
|
1501
|
+
cartId = typeof cartOrCartId === "string" ? cartOrCartId : cartOrCartId.id;
|
|
1502
|
+
return [4 /*yield*/, this.retrieve(cartId, {
|
|
1503
|
+
select: [
|
|
1504
|
+
"total",
|
|
1505
|
+
"subtotal",
|
|
1506
|
+
"tax_total",
|
|
1507
|
+
"discount_total",
|
|
1508
|
+
"shipping_total",
|
|
1509
|
+
"gift_card_total",
|
|
1510
|
+
],
|
|
1511
|
+
relations: [
|
|
1512
|
+
"items",
|
|
1513
|
+
"discounts",
|
|
1514
|
+
"discounts.rule",
|
|
1515
|
+
"gift_cards",
|
|
1516
|
+
"shipping_methods",
|
|
1517
|
+
"billing_address",
|
|
1518
|
+
"shipping_address",
|
|
1519
|
+
"region",
|
|
1520
|
+
"region.tax_rates",
|
|
1521
|
+
"region.payment_providers",
|
|
1522
|
+
"payment_sessions",
|
|
1523
|
+
"customer",
|
|
1524
|
+
],
|
|
1525
|
+
}, { force_taxes: true })];
|
|
1526
|
+
case 1:
|
|
1527
|
+
cart = _b.sent();
|
|
1528
|
+
total = cart.total, region = cart.region;
|
|
1529
|
+
if (typeof total === "undefined") {
|
|
1530
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "cart.total must be defined");
|
|
1531
|
+
}
|
|
1532
|
+
seen = [];
|
|
1533
|
+
if (!((_a = cart.payment_sessions) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 3];
|
|
1534
|
+
return [4 /*yield*/, Promise.all(cart.payment_sessions.map(function (paymentSession) { return __awaiter(_this, void 0, void 0, function () {
|
|
1535
|
+
return __generator(this, function (_a) {
|
|
1536
|
+
if (total <= 0 ||
|
|
1635
1537
|
!region.payment_providers.find(function (_a) {
|
|
1636
1538
|
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
|
-
}); })];
|
|
1539
|
+
return id === paymentSession.provider_id;
|
|
1540
|
+
})) {
|
|
1541
|
+
return [2 /*return*/, this.paymentProviderService_
|
|
1542
|
+
.withTransaction(transactionManager)
|
|
1543
|
+
.deleteSession(paymentSession)];
|
|
1544
|
+
}
|
|
1545
|
+
else {
|
|
1546
|
+
seen.push(paymentSession.provider_id);
|
|
1547
|
+
return [2 /*return*/, this.paymentProviderService_
|
|
1548
|
+
.withTransaction(transactionManager)
|
|
1549
|
+
.updateSession(paymentSession, cart)];
|
|
1550
|
+
}
|
|
1551
|
+
return [2 /*return*/];
|
|
1552
|
+
});
|
|
1553
|
+
}); }))];
|
|
1554
|
+
case 2:
|
|
1555
|
+
_b.sent();
|
|
1556
|
+
_b.label = 3;
|
|
1557
|
+
case 3:
|
|
1558
|
+
if (!(total > 0)) return [3 /*break*/, 8];
|
|
1559
|
+
if (!(region.payment_providers.length === 1 && !cart.payment_session)) return [3 /*break*/, 6];
|
|
1560
|
+
paymentProvider = region.payment_providers[0];
|
|
1561
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
1562
|
+
.withTransaction(transactionManager)
|
|
1563
|
+
.createSession(paymentProvider.id, cart)];
|
|
1564
|
+
case 4:
|
|
1565
|
+
paymentSession = _b.sent();
|
|
1566
|
+
paymentSession.is_selected = true;
|
|
1567
|
+
return [4 /*yield*/, psRepo.save(paymentSession)];
|
|
1568
|
+
case 5:
|
|
1569
|
+
_b.sent();
|
|
1570
|
+
return [3 /*break*/, 8];
|
|
1571
|
+
case 6: return [4 /*yield*/, Promise.all(region.payment_providers.map(function (paymentProvider) { return __awaiter(_this, void 0, void 0, function () {
|
|
1572
|
+
return __generator(this, function (_a) {
|
|
1573
|
+
if (!seen.includes(paymentProvider.id)) {
|
|
1574
|
+
return [2 /*return*/, this.paymentProviderService_
|
|
1575
|
+
.withTransaction(transactionManager)
|
|
1576
|
+
.createSession(paymentProvider.id, cart)];
|
|
1577
|
+
}
|
|
1578
|
+
return [2 /*return*/];
|
|
1579
|
+
});
|
|
1580
|
+
}); }))];
|
|
1581
|
+
case 7:
|
|
1582
|
+
_b.sent();
|
|
1583
|
+
_b.label = 8;
|
|
1584
|
+
case 8: return [2 /*return*/];
|
|
1585
|
+
}
|
|
1586
|
+
});
|
|
1587
|
+
}); })];
|
|
1588
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1589
|
+
}
|
|
1730
1590
|
});
|
|
1731
1591
|
});
|
|
1732
1592
|
};
|
|
@@ -1741,46 +1601,49 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1741
1601
|
return __awaiter(this, void 0, void 0, function () {
|
|
1742
1602
|
var _this = this;
|
|
1743
1603
|
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
|
-
|
|
1604
|
+
switch (_a.label) {
|
|
1605
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1606
|
+
var cart, cartRepo, paymentSession;
|
|
1607
|
+
return __generator(this, function (_a) {
|
|
1608
|
+
switch (_a.label) {
|
|
1609
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
1610
|
+
relations: ["payment_sessions"],
|
|
1611
|
+
})];
|
|
1612
|
+
case 1:
|
|
1613
|
+
cart = _a.sent();
|
|
1614
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
1615
|
+
if (!cart.payment_sessions) return [3 /*break*/, 3];
|
|
1616
|
+
paymentSession = cart.payment_sessions.find(function (_a) {
|
|
1617
|
+
var provider_id = _a.provider_id;
|
|
1618
|
+
return provider_id === providerId;
|
|
1619
|
+
});
|
|
1620
|
+
cart.payment_sessions = cart.payment_sessions.filter(function (_a) {
|
|
1621
|
+
var provider_id = _a.provider_id;
|
|
1622
|
+
return provider_id !== providerId;
|
|
1623
|
+
});
|
|
1624
|
+
if (!paymentSession) return [3 /*break*/, 3];
|
|
1625
|
+
// Delete the session with the provider
|
|
1626
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
1627
|
+
.withTransaction(transactionManager)
|
|
1628
|
+
.deleteSession(paymentSession)];
|
|
1629
|
+
case 2:
|
|
1630
|
+
// Delete the session with the provider
|
|
1631
|
+
_a.sent();
|
|
1632
|
+
_a.label = 3;
|
|
1633
|
+
case 3: return [4 /*yield*/, cartRepo.save(cart)];
|
|
1634
|
+
case 4:
|
|
1635
|
+
_a.sent();
|
|
1636
|
+
return [4 /*yield*/, this.eventBus_
|
|
1637
|
+
.withTransaction(transactionManager)
|
|
1638
|
+
.emit(CartService.Events.UPDATED, cart)];
|
|
1639
|
+
case 5:
|
|
1640
|
+
_a.sent();
|
|
1641
|
+
return [2 /*return*/, cart];
|
|
1642
|
+
}
|
|
1643
|
+
});
|
|
1644
|
+
}); })];
|
|
1645
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1646
|
+
}
|
|
1784
1647
|
});
|
|
1785
1648
|
});
|
|
1786
1649
|
};
|
|
@@ -1795,41 +1658,44 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1795
1658
|
return __awaiter(this, void 0, void 0, function () {
|
|
1796
1659
|
var _this = this;
|
|
1797
1660
|
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
|
-
|
|
1661
|
+
switch (_a.label) {
|
|
1662
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1663
|
+
var cart, paymentSession, updatedCart;
|
|
1664
|
+
return __generator(this, function (_a) {
|
|
1665
|
+
switch (_a.label) {
|
|
1666
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
1667
|
+
relations: ["payment_sessions"],
|
|
1668
|
+
})];
|
|
1669
|
+
case 1:
|
|
1670
|
+
cart = _a.sent();
|
|
1671
|
+
if (!cart.payment_sessions) return [3 /*break*/, 3];
|
|
1672
|
+
paymentSession = cart.payment_sessions.find(function (_a) {
|
|
1673
|
+
var provider_id = _a.provider_id;
|
|
1674
|
+
return provider_id === providerId;
|
|
1675
|
+
});
|
|
1676
|
+
if (!paymentSession) return [3 /*break*/, 3];
|
|
1677
|
+
// Delete the session with the provider
|
|
1678
|
+
return [4 /*yield*/, this.paymentProviderService_
|
|
1679
|
+
.withTransaction(transactionManager)
|
|
1680
|
+
.refreshSession(paymentSession, cart)];
|
|
1681
|
+
case 2:
|
|
1682
|
+
// Delete the session with the provider
|
|
1683
|
+
_a.sent();
|
|
1684
|
+
_a.label = 3;
|
|
1685
|
+
case 3: return [4 /*yield*/, this.retrieve(cartId)];
|
|
1686
|
+
case 4:
|
|
1687
|
+
updatedCart = _a.sent();
|
|
1688
|
+
return [4 /*yield*/, this.eventBus_
|
|
1689
|
+
.withTransaction(transactionManager)
|
|
1690
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
1691
|
+
case 5:
|
|
1692
|
+
_a.sent();
|
|
1693
|
+
return [2 /*return*/, updatedCart];
|
|
1694
|
+
}
|
|
1695
|
+
});
|
|
1696
|
+
}); })];
|
|
1697
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1698
|
+
}
|
|
1833
1699
|
});
|
|
1834
1700
|
});
|
|
1835
1701
|
};
|
|
@@ -1849,133 +1715,119 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1849
1715
|
return __awaiter(this, void 0, void 0, function () {
|
|
1850
1716
|
var _this = this;
|
|
1851
1717
|
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),
|
|
1718
|
+
switch (_a.label) {
|
|
1719
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
1720
|
+
var cart, cartCustomShippingOptions, customShippingOption, shipping_methods, shippingMethodConfig, newShippingMethod, methods, shipping_methods_1, shipping_methods_1_1, shippingMethod, e_3_1, updatedCart;
|
|
1721
|
+
var e_3, _a;
|
|
1722
|
+
var _this = this;
|
|
1723
|
+
var _b;
|
|
1724
|
+
return __generator(this, function (_c) {
|
|
1725
|
+
switch (_c.label) {
|
|
1726
|
+
case 0: return [4 /*yield*/, this.retrieve(cartId, {
|
|
1727
|
+
select: ["subtotal"],
|
|
1728
|
+
relations: [
|
|
1729
|
+
"shipping_methods",
|
|
1730
|
+
"discounts",
|
|
1731
|
+
"discounts.rule",
|
|
1732
|
+
"shipping_methods.shipping_option",
|
|
1733
|
+
"items",
|
|
1734
|
+
"items.variant",
|
|
1735
|
+
"payment_sessions",
|
|
1736
|
+
"items.variant.product",
|
|
1737
|
+
],
|
|
1932
1738
|
})];
|
|
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
|
-
|
|
1739
|
+
case 1:
|
|
1740
|
+
cart = _c.sent();
|
|
1741
|
+
return [4 /*yield*/, this.customShippingOptionService_
|
|
1742
|
+
.withTransaction(transactionManager)
|
|
1743
|
+
.list({ cart_id: cart.id })];
|
|
1744
|
+
case 2:
|
|
1745
|
+
cartCustomShippingOptions = _c.sent();
|
|
1746
|
+
customShippingOption = this.findCustomShippingOption(cartCustomShippingOptions, optionId);
|
|
1747
|
+
shipping_methods = cart.shipping_methods;
|
|
1748
|
+
shippingMethodConfig = customShippingOption
|
|
1749
|
+
? { cart_id: cart.id, price: customShippingOption.price }
|
|
1750
|
+
: { cart: cart };
|
|
1751
|
+
return [4 /*yield*/, this.shippingOptionService_
|
|
1752
|
+
.withTransaction(transactionManager)
|
|
1753
|
+
.createShippingMethod(optionId, data, shippingMethodConfig)];
|
|
1754
|
+
case 3:
|
|
1755
|
+
newShippingMethod = _c.sent();
|
|
1756
|
+
methods = [newShippingMethod];
|
|
1757
|
+
if (!(shipping_methods === null || shipping_methods === void 0 ? void 0 : shipping_methods.length)) return [3 /*break*/, 12];
|
|
1758
|
+
_c.label = 4;
|
|
1759
|
+
case 4:
|
|
1760
|
+
_c.trys.push([4, 10, 11, 12]);
|
|
1761
|
+
shipping_methods_1 = __values(shipping_methods), shipping_methods_1_1 = shipping_methods_1.next();
|
|
1762
|
+
_c.label = 5;
|
|
1763
|
+
case 5:
|
|
1764
|
+
if (!!shipping_methods_1_1.done) return [3 /*break*/, 9];
|
|
1765
|
+
shippingMethod = shipping_methods_1_1.value;
|
|
1766
|
+
if (!(shippingMethod.shipping_option.profile_id ===
|
|
1767
|
+
newShippingMethod.shipping_option.profile_id)) return [3 /*break*/, 7];
|
|
1768
|
+
return [4 /*yield*/, this.shippingOptionService_
|
|
1769
|
+
.withTransaction(transactionManager)
|
|
1770
|
+
.deleteShippingMethods(shippingMethod)];
|
|
1771
|
+
case 6:
|
|
1772
|
+
_c.sent();
|
|
1773
|
+
return [3 /*break*/, 8];
|
|
1774
|
+
case 7:
|
|
1775
|
+
methods.push(shippingMethod);
|
|
1776
|
+
_c.label = 8;
|
|
1777
|
+
case 8:
|
|
1778
|
+
shipping_methods_1_1 = shipping_methods_1.next();
|
|
1779
|
+
return [3 /*break*/, 5];
|
|
1780
|
+
case 9: return [3 /*break*/, 12];
|
|
1781
|
+
case 10:
|
|
1782
|
+
e_3_1 = _c.sent();
|
|
1783
|
+
e_3 = { error: e_3_1 };
|
|
1784
|
+
return [3 /*break*/, 12];
|
|
1785
|
+
case 11:
|
|
1786
|
+
try {
|
|
1787
|
+
if (shipping_methods_1_1 && !shipping_methods_1_1.done && (_a = shipping_methods_1.return)) _a.call(shipping_methods_1);
|
|
1788
|
+
}
|
|
1789
|
+
finally { if (e_3) throw e_3.error; }
|
|
1790
|
+
return [7 /*endfinally*/];
|
|
1791
|
+
case 12:
|
|
1792
|
+
if (!((_b = cart.items) === null || _b === void 0 ? void 0 : _b.length)) return [3 /*break*/, 14];
|
|
1793
|
+
return [4 /*yield*/, Promise.all(cart.items.map(function (item) { return __awaiter(_this, void 0, void 0, function () {
|
|
1794
|
+
return __generator(this, function (_a) {
|
|
1795
|
+
return [2 /*return*/, this.lineItemService_
|
|
1796
|
+
.withTransaction(transactionManager)
|
|
1797
|
+
.update(item.id, {
|
|
1798
|
+
has_shipping: this.validateLineItemShipping_(methods, item),
|
|
1799
|
+
})];
|
|
1800
|
+
});
|
|
1801
|
+
}); }))];
|
|
1802
|
+
case 13:
|
|
1803
|
+
_c.sent();
|
|
1804
|
+
_c.label = 14;
|
|
1805
|
+
case 14: return [4 /*yield*/, this.retrieve(cartId, {
|
|
1806
|
+
relations: ["discounts", "discounts.rule", "shipping_methods"],
|
|
1807
|
+
})
|
|
1808
|
+
// if cart has freeshipping, adjust price
|
|
1809
|
+
];
|
|
1810
|
+
case 15:
|
|
1811
|
+
updatedCart = _c.sent();
|
|
1812
|
+
if (!updatedCart.discounts.some(function (_a) {
|
|
1813
|
+
var rule = _a.rule;
|
|
1814
|
+
return rule.type === "free_shipping";
|
|
1815
|
+
})) return [3 /*break*/, 17];
|
|
1816
|
+
return [4 /*yield*/, this.adjustFreeShipping_(updatedCart, true)];
|
|
1817
|
+
case 16:
|
|
1818
|
+
_c.sent();
|
|
1819
|
+
_c.label = 17;
|
|
1820
|
+
case 17: return [4 /*yield*/, this.eventBus_
|
|
1821
|
+
.withTransaction(transactionManager)
|
|
1822
|
+
.emit(CartService.Events.UPDATED, updatedCart)];
|
|
1823
|
+
case 18:
|
|
1824
|
+
_c.sent();
|
|
1825
|
+
return [2 /*return*/, updatedCart];
|
|
1826
|
+
}
|
|
1827
|
+
});
|
|
1828
|
+
}); }, "SERIALIZABLE")];
|
|
1829
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1830
|
+
}
|
|
1979
1831
|
});
|
|
1980
1832
|
});
|
|
1981
1833
|
};
|
|
@@ -1994,74 +1846,100 @@ var CartService = /** @class */ (function (_super) {
|
|
|
1994
1846
|
}
|
|
1995
1847
|
return customOption;
|
|
1996
1848
|
};
|
|
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) {
|
|
1849
|
+
CartService.prototype.updateUnitPrices_ = function (cart, regionId, customer_id) {
|
|
1850
|
+
var _a;
|
|
2005
1851
|
return __awaiter(this, void 0, void 0, function () {
|
|
2006
|
-
var
|
|
1852
|
+
var region_1, _b;
|
|
2007
1853
|
var _this = this;
|
|
2008
|
-
return __generator(this, function (
|
|
2009
|
-
switch (
|
|
1854
|
+
return __generator(this, function (_c) {
|
|
1855
|
+
switch (_c.label) {
|
|
2010
1856
|
case 0:
|
|
2011
|
-
if (cart.
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
1857
|
+
if (!((_a = cart.items) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 3];
|
|
1858
|
+
return [4 /*yield*/, this.regionService_
|
|
1859
|
+
.withTransaction(this.transactionManager_)
|
|
1860
|
+
.retrieve(regionId || cart.region_id, {
|
|
2015
1861
|
relations: ["countries"],
|
|
2016
1862
|
})];
|
|
2017
1863
|
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 () {
|
|
1864
|
+
region_1 = _c.sent();
|
|
1865
|
+
_b = cart;
|
|
1866
|
+
return [4 /*yield*/, Promise.all(cart.items.map(function (item) { return __awaiter(_this, void 0, void 0, function () {
|
|
2026
1867
|
var availablePrice;
|
|
2027
1868
|
return __generator(this, function (_a) {
|
|
2028
1869
|
switch (_a.label) {
|
|
2029
|
-
case 0: return [4 /*yield*/, this.
|
|
2030
|
-
.
|
|
1870
|
+
case 0: return [4 /*yield*/, this.priceSelectionStrategy_
|
|
1871
|
+
.withTransaction(this.transactionManager_)
|
|
1872
|
+
.calculateVariantPrice(item.variant_id, {
|
|
1873
|
+
region_id: region_1.id,
|
|
1874
|
+
currency_code: region_1.currency_code,
|
|
1875
|
+
quantity: item.quantity,
|
|
1876
|
+
customer_id: customer_id || cart.customer_id,
|
|
1877
|
+
include_discount_prices: true,
|
|
1878
|
+
})
|
|
2031
1879
|
.catch(function () { return undefined; })];
|
|
2032
1880
|
case 1:
|
|
2033
1881
|
availablePrice = _a.sent();
|
|
2034
|
-
if (!(availablePrice !== undefined
|
|
1882
|
+
if (!(availablePrice !== undefined &&
|
|
1883
|
+
availablePrice.calculatedPrice !== null)) return [3 /*break*/, 2];
|
|
2035
1884
|
return [2 /*return*/, this.lineItemService_
|
|
2036
1885
|
.withTransaction(this.transactionManager_)
|
|
2037
1886
|
.update(item.id, {
|
|
2038
1887
|
has_shipping: false,
|
|
2039
|
-
unit_price: availablePrice,
|
|
1888
|
+
unit_price: availablePrice.calculatedPrice,
|
|
2040
1889
|
})];
|
|
2041
1890
|
case 2: return [4 /*yield*/, this.lineItemService_
|
|
2042
1891
|
.withTransaction(this.transactionManager_)
|
|
2043
1892
|
.delete(item.id)];
|
|
2044
1893
|
case 3:
|
|
2045
1894
|
_a.sent();
|
|
2046
|
-
return [2 /*return
|
|
1895
|
+
return [2 /*return*/];
|
|
2047
1896
|
}
|
|
2048
1897
|
});
|
|
2049
|
-
}); })
|
|
2050
|
-
.filter(Boolean))];
|
|
1898
|
+
}); }))];
|
|
2051
1899
|
case 2:
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
case 3:
|
|
1900
|
+
_b.items = (_c.sent()).filter(function (item) { return !!item; });
|
|
1901
|
+
_c.label = 3;
|
|
1902
|
+
case 3: return [2 /*return*/];
|
|
1903
|
+
}
|
|
1904
|
+
});
|
|
1905
|
+
});
|
|
1906
|
+
};
|
|
1907
|
+
/**
|
|
1908
|
+
* Set's the region of a cart.
|
|
1909
|
+
* @param cart - the cart to set region on
|
|
1910
|
+
* @param regionId - the id of the region to set the region to
|
|
1911
|
+
* @param countryCode - the country code to set the country to
|
|
1912
|
+
* @return the result of the update operation
|
|
1913
|
+
*/
|
|
1914
|
+
CartService.prototype.setRegion_ = function (cart, regionId, countryCode) {
|
|
1915
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1916
|
+
var region, addrRepo, shippingAddress, updated, updated, paymentSessionRepo;
|
|
1917
|
+
return __generator(this, function (_a) {
|
|
1918
|
+
switch (_a.label) {
|
|
1919
|
+
case 0:
|
|
1920
|
+
if (cart.completed_at || cart.payment_authorized_at) {
|
|
1921
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cannot change the region of a completed cart");
|
|
1922
|
+
}
|
|
1923
|
+
return [4 /*yield*/, this.regionService_
|
|
1924
|
+
.withTransaction(this.transactionManager_)
|
|
1925
|
+
.retrieve(regionId, {
|
|
1926
|
+
relations: ["countries"],
|
|
1927
|
+
})];
|
|
1928
|
+
case 1:
|
|
1929
|
+
region = _a.sent();
|
|
1930
|
+
addrRepo = this.transactionManager_.getCustomRepository(this.addressRepository_);
|
|
1931
|
+
cart.region = region;
|
|
1932
|
+
cart.region_id = region.id;
|
|
2055
1933
|
shippingAddress = {};
|
|
2056
|
-
if (!cart.shipping_address_id) return [3 /*break*/,
|
|
1934
|
+
if (!cart.shipping_address_id) return [3 /*break*/, 3];
|
|
2057
1935
|
return [4 /*yield*/, addrRepo.findOne({
|
|
2058
1936
|
where: { id: cart.shipping_address_id },
|
|
2059
1937
|
})];
|
|
2060
|
-
case
|
|
2061
|
-
shippingAddress = (
|
|
2062
|
-
|
|
2063
|
-
case
|
|
2064
|
-
if (!(countryCode !== null)) return [3 /*break*/,
|
|
1938
|
+
case 2:
|
|
1939
|
+
shippingAddress = (_a.sent());
|
|
1940
|
+
_a.label = 3;
|
|
1941
|
+
case 3:
|
|
1942
|
+
if (!(countryCode !== null)) return [3 /*break*/, 5];
|
|
2065
1943
|
if (!region.countries.find(function (_a) {
|
|
2066
1944
|
var iso_2 = _a.iso_2;
|
|
2067
1945
|
return iso_2 === countryCode.toLowerCase();
|
|
@@ -2070,10 +1948,10 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2070
1948
|
}
|
|
2071
1949
|
updated = __assign(__assign({}, shippingAddress), { country_code: countryCode.toLowerCase() });
|
|
2072
1950
|
return [4 /*yield*/, addrRepo.save(updated)];
|
|
2073
|
-
case
|
|
2074
|
-
|
|
2075
|
-
return [3 /*break*/,
|
|
2076
|
-
case
|
|
1951
|
+
case 4:
|
|
1952
|
+
_a.sent();
|
|
1953
|
+
return [3 /*break*/, 7];
|
|
1954
|
+
case 5:
|
|
2077
1955
|
updated = __assign({}, shippingAddress);
|
|
2078
1956
|
// If the country code of a shipping address is set we need to clear it
|
|
2079
1957
|
if (!lodash_1.default.isEmpty(shippingAddress) && shippingAddress.country_code) {
|
|
@@ -2084,42 +1962,38 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2084
1962
|
updated = __assign(__assign({}, updated), { country_code: region.countries[0].iso_2 });
|
|
2085
1963
|
}
|
|
2086
1964
|
return [4 /*yield*/, this.updateShippingAddress_(cart, updated, addrRepo)];
|
|
1965
|
+
case 6:
|
|
1966
|
+
_a.sent();
|
|
1967
|
+
_a.label = 7;
|
|
1968
|
+
case 7:
|
|
1969
|
+
if (!(cart.shipping_methods && cart.shipping_methods.length)) return [3 /*break*/, 9];
|
|
1970
|
+
return [4 /*yield*/, this.shippingOptionService_
|
|
1971
|
+
.withTransaction(this.transactionManager_)
|
|
1972
|
+
.deleteShippingMethods(cart.shipping_methods)];
|
|
2087
1973
|
case 8:
|
|
2088
|
-
|
|
2089
|
-
|
|
1974
|
+
_a.sent();
|
|
1975
|
+
_a.label = 9;
|
|
2090
1976
|
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
1977
|
if (cart.discounts && cart.discounts.length) {
|
|
2099
|
-
|
|
2100
|
-
|
|
1978
|
+
cart.discounts = cart.discounts.filter(function (discount) {
|
|
1979
|
+
return discount.regions.find(function (_a) {
|
|
2101
1980
|
var id = _a.id;
|
|
2102
1981
|
return id === regionId;
|
|
2103
|
-
})
|
|
2104
|
-
return d;
|
|
2105
|
-
}
|
|
2106
|
-
return null;
|
|
1982
|
+
});
|
|
2107
1983
|
});
|
|
2108
|
-
cart.discounts = newDiscounts.filter(Boolean);
|
|
2109
1984
|
}
|
|
2110
1985
|
cart.gift_cards = [];
|
|
2111
|
-
if (!(cart.payment_sessions && cart.payment_sessions.length)) return [3 /*break*/,
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
cart.payment_sessions = [];
|
|
1986
|
+
if (!(cart.payment_sessions && cart.payment_sessions.length)) return [3 /*break*/, 11];
|
|
1987
|
+
paymentSessionRepo = this.transactionManager_.getCustomRepository(this.paymentSessionRepository_);
|
|
1988
|
+
return [4 /*yield*/, paymentSessionRepo.delete({
|
|
1989
|
+
id: (0, typeorm_1.In)(cart.payment_sessions.map(function (paymentSession) { return paymentSession.id; })),
|
|
1990
|
+
})];
|
|
1991
|
+
case 10:
|
|
1992
|
+
_a.sent();
|
|
1993
|
+
cart.payment_sessions.length = 0;
|
|
2120
1994
|
cart.payment_session = null;
|
|
2121
|
-
|
|
2122
|
-
case
|
|
1995
|
+
_a.label = 11;
|
|
1996
|
+
case 11: return [2 /*return*/];
|
|
2123
1997
|
}
|
|
2124
1998
|
});
|
|
2125
1999
|
});
|
|
@@ -2134,7 +2008,7 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2134
2008
|
var _this = this;
|
|
2135
2009
|
return __generator(this, function (_a) {
|
|
2136
2010
|
switch (_a.label) {
|
|
2137
|
-
case 0: return [4 /*yield*/, this.atomicPhase_(function (
|
|
2011
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
2138
2012
|
var cart, cartRepo;
|
|
2139
2013
|
return __generator(this, function (_a) {
|
|
2140
2014
|
switch (_a.label) {
|
|
@@ -2143,7 +2017,6 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2143
2017
|
"items",
|
|
2144
2018
|
"discounts",
|
|
2145
2019
|
"discounts.rule",
|
|
2146
|
-
"discounts.rule.valid_for",
|
|
2147
2020
|
"payment_sessions",
|
|
2148
2021
|
],
|
|
2149
2022
|
})];
|
|
@@ -2155,7 +2028,7 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2155
2028
|
if (cart.payment_authorized_at) {
|
|
2156
2029
|
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
2030
|
}
|
|
2158
|
-
cartRepo =
|
|
2031
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
2159
2032
|
return [2 /*return*/, cartRepo.remove(cart)];
|
|
2160
2033
|
}
|
|
2161
2034
|
});
|
|
@@ -2179,29 +2052,32 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2179
2052
|
var _this = this;
|
|
2180
2053
|
return __generator(this, function (_a) {
|
|
2181
2054
|
switch (_a.label) {
|
|
2182
|
-
case 0: return [4 /*yield*/, this.atomicPhase_(function (
|
|
2183
|
-
var cartRepo, validatedId, cart, existing,
|
|
2055
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
2056
|
+
var cartRepo, validatedId, cart, existing, updatedCart;
|
|
2184
2057
|
var _a;
|
|
2185
2058
|
return __generator(this, function (_b) {
|
|
2186
2059
|
switch (_b.label) {
|
|
2187
2060
|
case 0:
|
|
2188
|
-
cartRepo =
|
|
2061
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
2189
2062
|
validatedId = this.validateId_(cartId);
|
|
2190
2063
|
if (typeof key !== "string") {
|
|
2191
2064
|
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
2065
|
}
|
|
2193
2066
|
return [4 /*yield*/, cartRepo.findOne(validatedId)];
|
|
2194
2067
|
case 1:
|
|
2195
|
-
cart =
|
|
2068
|
+
cart = _b.sent();
|
|
2069
|
+
if (!cart) {
|
|
2070
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Unable to find the cart with the given id");
|
|
2071
|
+
}
|
|
2196
2072
|
existing = cart.metadata || {};
|
|
2197
2073
|
cart.metadata = __assign(__assign({}, existing), (_a = {}, _a[key] = value, _a));
|
|
2198
2074
|
return [4 /*yield*/, cartRepo.save(cart)];
|
|
2199
2075
|
case 2:
|
|
2200
|
-
|
|
2076
|
+
updatedCart = _b.sent();
|
|
2201
2077
|
this.eventBus_
|
|
2202
|
-
.withTransaction(
|
|
2203
|
-
.emit(CartService.Events.UPDATED,
|
|
2204
|
-
return [2 /*return*/,
|
|
2078
|
+
.withTransaction(transactionManager)
|
|
2079
|
+
.emit(CartService.Events.UPDATED, updatedCart);
|
|
2080
|
+
return [2 /*return*/, updatedCart];
|
|
2205
2081
|
}
|
|
2206
2082
|
});
|
|
2207
2083
|
}); })];
|
|
@@ -2214,34 +2090,70 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2214
2090
|
return __awaiter(this, void 0, void 0, function () {
|
|
2215
2091
|
var _this = this;
|
|
2216
2092
|
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
|
-
|
|
2093
|
+
switch (_a.label) {
|
|
2094
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
2095
|
+
var cart, calculationContext;
|
|
2096
|
+
return __generator(this, function (_a) {
|
|
2097
|
+
switch (_a.label) {
|
|
2098
|
+
case 0: return [4 /*yield*/, this.retrieve(id, {
|
|
2099
|
+
relations: [
|
|
2100
|
+
"items",
|
|
2101
|
+
"gift_cards",
|
|
2102
|
+
"discounts",
|
|
2103
|
+
"discounts.rule",
|
|
2104
|
+
"shipping_methods",
|
|
2105
|
+
"region",
|
|
2106
|
+
"region.tax_rates",
|
|
2107
|
+
],
|
|
2108
|
+
})];
|
|
2109
|
+
case 1:
|
|
2110
|
+
cart = _a.sent();
|
|
2111
|
+
calculationContext = this.totalsService_
|
|
2112
|
+
.withTransaction(transactionManager)
|
|
2113
|
+
.getCalculationContext(cart);
|
|
2114
|
+
return [4 /*yield*/, this.taxProviderService_
|
|
2115
|
+
.withTransaction(transactionManager)
|
|
2116
|
+
.createTaxLines(cart, calculationContext)];
|
|
2117
|
+
case 2:
|
|
2118
|
+
_a.sent();
|
|
2119
|
+
return [2 /*return*/, cart];
|
|
2120
|
+
}
|
|
2121
|
+
});
|
|
2122
|
+
}); })];
|
|
2123
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2124
|
+
}
|
|
2125
|
+
});
|
|
2126
|
+
});
|
|
2127
|
+
};
|
|
2128
|
+
CartService.prototype.refreshAdjustments_ = function (cart) {
|
|
2129
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2130
|
+
var nonReturnLineIDs;
|
|
2131
|
+
return __generator(this, function (_a) {
|
|
2132
|
+
switch (_a.label) {
|
|
2133
|
+
case 0:
|
|
2134
|
+
nonReturnLineIDs = cart.items
|
|
2135
|
+
.filter(function (item) { return !item.is_return; })
|
|
2136
|
+
.map(function (i) { return i.id; });
|
|
2137
|
+
// delete all old non return line item adjustments
|
|
2138
|
+
return [4 /*yield*/, this.lineItemAdjustmentService_
|
|
2139
|
+
.withTransaction(this.transactionManager_)
|
|
2140
|
+
.delete({
|
|
2141
|
+
item_id: nonReturnLineIDs,
|
|
2142
|
+
})
|
|
2143
|
+
// potentially create/update line item adjustments
|
|
2144
|
+
];
|
|
2145
|
+
case 1:
|
|
2146
|
+
// delete all old non return line item adjustments
|
|
2147
|
+
_a.sent();
|
|
2148
|
+
// potentially create/update line item adjustments
|
|
2149
|
+
return [4 /*yield*/, this.lineItemAdjustmentService_
|
|
2150
|
+
.withTransaction(this.transactionManager_)
|
|
2151
|
+
.createAdjustments(cart)];
|
|
2152
|
+
case 2:
|
|
2153
|
+
// potentially create/update line item adjustments
|
|
2154
|
+
_a.sent();
|
|
2155
|
+
return [2 /*return*/];
|
|
2156
|
+
}
|
|
2245
2157
|
});
|
|
2246
2158
|
});
|
|
2247
2159
|
};
|
|
@@ -2255,35 +2167,38 @@ var CartService = /** @class */ (function (_super) {
|
|
|
2255
2167
|
return __awaiter(this, void 0, void 0, function () {
|
|
2256
2168
|
var _this = this;
|
|
2257
2169
|
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
|
-
|
|
2170
|
+
switch (_a.label) {
|
|
2171
|
+
case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
|
|
2172
|
+
var cartRepo, validatedId, cart, updated, updatedCart;
|
|
2173
|
+
return __generator(this, function (_a) {
|
|
2174
|
+
switch (_a.label) {
|
|
2175
|
+
case 0:
|
|
2176
|
+
cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
|
|
2177
|
+
validatedId = this.validateId_(cartId);
|
|
2178
|
+
if (typeof key !== "string") {
|
|
2179
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_ARGUMENT, "Key type is invalid. Metadata keys must be strings");
|
|
2180
|
+
}
|
|
2181
|
+
return [4 /*yield*/, cartRepo.findOne(validatedId)];
|
|
2182
|
+
case 1:
|
|
2183
|
+
cart = _a.sent();
|
|
2184
|
+
if (!cart) {
|
|
2185
|
+
throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Cart with id: ".concat(validatedId, " was not found"));
|
|
2186
|
+
}
|
|
2187
|
+
updated = cart.metadata || {};
|
|
2188
|
+
delete updated[key];
|
|
2189
|
+
cart.metadata = updated;
|
|
2190
|
+
return [4 /*yield*/, cartRepo.save(cart)];
|
|
2191
|
+
case 2:
|
|
2192
|
+
updatedCart = _a.sent();
|
|
2193
|
+
this.eventBus_
|
|
2194
|
+
.withTransaction(transactionManager)
|
|
2195
|
+
.emit(CartService.Events.UPDATED, updatedCart);
|
|
2196
|
+
return [2 /*return*/, updatedCart];
|
|
2197
|
+
}
|
|
2198
|
+
});
|
|
2199
|
+
}); })];
|
|
2200
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2201
|
+
}
|
|
2287
2202
|
});
|
|
2288
2203
|
});
|
|
2289
2204
|
};
|