From 5261999d129474c9599e75323c6284d0b86d6539 Mon Sep 17 00:00:00 2001 From: Vilfred Sikker Date: Fri, 27 Aug 2021 11:22:14 +0200 Subject: [PATCH] grap line item from develop --- packages/medusa/src/services/line-item.js | 69 ++++++++++++----------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/packages/medusa/src/services/line-item.js b/packages/medusa/src/services/line-item.js index 843faae709..759ae2d63a 100644 --- a/packages/medusa/src/services/line-item.js +++ b/packages/medusa/src/services/line-item.js @@ -90,45 +90,50 @@ class LineItemService extends BaseService { } async generate(variantId, regionId, quantity, config = {}) { - const variant = await this.productVariantService_.retrieve(variantId, { - relations: ["product"], - }) + return this.atomicPhase_(async manager => { + const variant = await this.productVariantService_ + .withTransaction(manager) + .retrieve(variantId, { + relations: ["product"], + }) - const region = await this.regionService_.retrieve(regionId) + const region = await this.regionService_ + .withTransaction(manager) + .retrieve(regionId) - let price - let shouldMerge = true + let price + let shouldMerge = true - if (config.unit_price && typeof config.unit_price !== `undefined`) { - // if custom unit_price, we ensure positive values - // and we choose to not merge the items - shouldMerge = false - if (config.unit_price < 0) { - price = 0 + if (config.unit_price && typeof config.unit_price !== `undefined`) { + // if custom unit_price, we ensure positive values + // and we choose to not merge the items + shouldMerge = false + if (config.unit_price < 0) { + price = 0 + } else { + price = config.unit_price + } } else { - price = config.unit_price + price = await this.productVariantService_ + .withTransaction(manager) + .getRegionPrice(variant.id, region.id) } - } else { - price = await this.productVariantService_.getRegionPrice( - variant.id, - region.id - ) - } - const toCreate = { - unit_price: price, - title: variant.product.title, - description: variant.title, - thumbnail: variant.product.thumbnail, - variant_id: variant.id, - quantity: quantity || 1, - allow_discounts: !variant.product.is_giftcard, - is_giftcard: variant.product.is_giftcard, - metadata: config?.metadata || {}, - should_merge: shouldMerge, - } + const toCreate = { + unit_price: price, + title: variant.product.title, + description: variant.title, + thumbnail: variant.product.thumbnail, + variant_id: variant.id, + quantity: quantity || 1, + allow_discounts: variant.product.discountable, + is_giftcard: variant.product.is_giftcard, + metadata: config?.metadata || {}, + should_merge: shouldMerge, + } - return toCreate + return toCreate + }) } /**