fix pr review

This commit is contained in:
olivermrbl
2021-03-11 11:38:16 +01:00
parent d55c24e28c
commit 83a7d7ec5a
11 changed files with 97 additions and 29 deletions
@@ -27,9 +27,12 @@ export default async (req, res) => {
})
.required(),
region_id: Validator.string().required(),
discounts: Validator.array().optional(),
discounts: Validator.array()
.items({
code: Validator.string().required(),
})
.optional(),
customer_id: Validator.string().optional(),
customer: Validator.string().optional(),
shipping_methods: Validator.array()
.items({
option_id: Validator.string().required(),
@@ -43,8 +43,7 @@ export default async (req, res) => {
value.variant_id,
draftOrder.cart.region_id,
value.quantity,
value.metadata,
value.unit_price
{ metadata: value.metadata, unit_price: value.unit_price }
)
await cartService
@@ -34,7 +34,7 @@ describe("POST /store/carts/:id", () => {
IdMap.getId("testVariant"),
IdMap.getId("testRegion"),
3,
undefined // no metadata
{ metadata: undefined }
)
})
@@ -73,7 +73,7 @@ describe("POST /store/carts/:id", () => {
IdMap.getId("fail"),
IdMap.getId("testRegion"),
3,
undefined // no metdata
{ metadata: undefined }
)
})
@@ -50,7 +50,7 @@ export default async (req, res) => {
value.variant_id,
cart.region_id,
value.quantity,
value.metadata
{ metadata: value.metadata }
)
await cartService.withTransaction(m).addLineItem(id, line)
-1
View File
@@ -17,7 +17,6 @@ import { Cart } from "./cart"
import { Order } from "./order"
import { ClaimOrder } from "./claim-order"
import { ProductVariant } from "./product-variant"
import { DraftOrder } from "./draft-order"
@Check(`"fulfilled_quantity" <= "quantity"`)
@Check(`"shipped_quantity" <= "fulfilled_quantity"`)
@@ -179,8 +179,7 @@ describe("DraftOrderService", () => {
"test-variant",
"test-region",
2,
{},
undefined
{ metadata: {}, unit_price: undefined }
)
expect(lineItemService.create).toHaveBeenCalledTimes(1)
+2 -2
View File
@@ -629,7 +629,7 @@ class CartService extends BaseService {
if ("discounts" in update) {
cart.discounts = []
for (const { code } of update.discounts) {
await this.applyDiscount_(cart, code)
await this.applyDiscount(cart, code)
}
}
@@ -797,7 +797,7 @@ class CartService extends BaseService {
* @param {string} discountCode - the discount code
* @return {Promise} the result of the update operation
*/
async applyDiscount_(cart, discountCode) {
async applyDiscount(cart, discountCode) {
const discount = await this.discountService_.retrieveByCode(discountCode, [
"rule",
"regions",
+22 -9
View File
@@ -249,7 +249,16 @@ class DraftOrderService extends BaseService {
)
}
const { items, shipping_methods, ...rest } = data
const { items, shipping_methods, discounts, ...rest } = data
if (discounts) {
for (const { code } of discounts) {
rest.discounts = []
await this.cartService_
.withTransaction(manager)
.applyDiscount(rest, code)
}
}
const createdCart = await this.cartService_
.withTransaction(manager)
@@ -284,13 +293,10 @@ class DraftOrderService extends BaseService {
if (item.variant_id) {
const line = await this.lineItemService_
.withTransaction(manager)
.generate(
item.variant_id,
data.region_id,
item.quantity,
item.metadata,
item.unit_price
)
.generate(item.variant_id, data.region_id, item.quantity, {
metadata: item?.metadata || {},
unit_price: item.unit_price,
})
const variant = await this.productVariantService_
.withTransaction(manager)
@@ -305,13 +311,20 @@ class DraftOrderService extends BaseService {
...line,
})
} else {
let price
if (typeof item.unit_price === `undefined` || item.unit_price < 0) {
price = 0
} else {
price = item.unit_price
}
// custom line items can be added to a draft order
await this.lineItemService_.withTransaction(manager).create({
cart_id: createdCart.id,
has_shipping: true,
title: item.title || "Custom item",
allow_discounts: false,
unit_price: item.unit_price || 0,
unit_price: price,
quantity: item.quantity,
})
}
+9 -4
View File
@@ -89,7 +89,7 @@ class LineItemService extends BaseService {
return lineItem
}
async generate(variantId, regionId, quantity, metadata = {}, unitPrice) {
async generate(variantId, regionId, quantity, config = {}) {
const variant = await this.productVariantService_.retrieve(variantId, {
relations: ["product"],
})
@@ -97,8 +97,13 @@ class LineItemService extends BaseService {
const region = await this.regionService_.retrieve(regionId)
let price
if (unitPrice) {
price = unitPrice
if (config.unit_price && typeof config.unit_price !== `undefined`) {
if (config.unit_price < 0) {
price = 0
} else {
price = config.unit_price
}
} else {
price = await this.productVariantService_.getRegionPrice(
variant.id,
@@ -115,7 +120,7 @@ class LineItemService extends BaseService {
quantity: quantity || 1,
allow_discounts: !variant.product.is_giftcard,
is_giftcard: variant.product.is_giftcard,
metadata: metadata || {},
metadata: config?.metadata || {},
should_merge: true,
}