Adds delete line item, prepares checkout
This commit is contained in:
@@ -22,7 +22,7 @@ export default async (req, res) => {
|
||||
await cartService.addShippingMethod(id, value.option_id, value.data)
|
||||
|
||||
let cart = await cartService.retrieve(id)
|
||||
cart = await cartService.decorate(cart)
|
||||
cart = await cartService.decorate(cart, [], ["region"])
|
||||
|
||||
res.status(200).json({ cart })
|
||||
} catch (err) {
|
||||
|
||||
@@ -26,7 +26,7 @@ export default async (req, res) => {
|
||||
await cartService.addLineItem(cart._id, lineItem)
|
||||
|
||||
cart = await cartService.retrieve(cart._id)
|
||||
cart = await cartService.decorate(cart)
|
||||
cart = await cartService.decorate(cart, [], ["region"])
|
||||
|
||||
res.status(200).json({ cart })
|
||||
} catch (err) {
|
||||
|
||||
@@ -9,7 +9,7 @@ export default async (req, res) => {
|
||||
|
||||
// return the updated cart
|
||||
let cart = await cartService.retrieve(id)
|
||||
cart = await cartService.decorate(cart)
|
||||
cart = await cartService.decorate(cart, [], ["region"])
|
||||
|
||||
res.status(200).json({ cart })
|
||||
} catch (err) {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
|
||||
export default async (req, res) => {
|
||||
const { id, line_id } = req.params
|
||||
|
||||
try {
|
||||
const cartService = req.scope.resolve("cartService")
|
||||
|
||||
let cart = await cartService.removeLineItem(id, line_id)
|
||||
cart = await cartService.decorate(cart, [], ["region"])
|
||||
|
||||
res.status(200).json({ cart })
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,10 @@ export default app => {
|
||||
"/:id/line-items/:line_id",
|
||||
middlewares.wrap(require("./update-line-item").default)
|
||||
)
|
||||
route.delete(
|
||||
"/:id/line-items/:line_id",
|
||||
middlewares.wrap(require("./delete-line-item").default)
|
||||
)
|
||||
|
||||
// Payment sessions
|
||||
route.post(
|
||||
|
||||
@@ -57,7 +57,7 @@ export default async (req, res) => {
|
||||
}
|
||||
|
||||
let newCart = await cartService.retrieve(id)
|
||||
const data = await cartService.decorate(newCart)
|
||||
const data = await cartService.decorate(newCart, [], ["region"])
|
||||
res.json({ cart: data })
|
||||
} catch (err) {
|
||||
throw err
|
||||
|
||||
@@ -24,10 +24,8 @@ export default async (req, res) => {
|
||||
cart.region_id
|
||||
)
|
||||
|
||||
await cartService.updateLineItem(cart._id, line_id, lineItem)
|
||||
|
||||
cart = await cartService.retrieve(cart._id)
|
||||
cart = await cartService.decorate(cart)
|
||||
cart = await cartService.updateLineItem(cart._id, line_id, lineItem)
|
||||
cart = await cartService.decorate(cart, [], ["region"])
|
||||
|
||||
res.status(200).json({ cart })
|
||||
} catch (err) {
|
||||
|
||||
@@ -22,7 +22,7 @@ export default async (req, res) => {
|
||||
await cartService.setPaymentMethod(id, session)
|
||||
|
||||
let cart = await cartService.retrieve(id)
|
||||
cart = await cartService.decorate(cart)
|
||||
cart = await cartService.decorate(cart, [], ["region"])
|
||||
|
||||
res.status(200).json({ cart })
|
||||
} catch (err) {
|
||||
|
||||
@@ -6,7 +6,10 @@ const route = Router()
|
||||
export default app => {
|
||||
app.use("/shipping-options", route)
|
||||
|
||||
route.get("/", middlewares.wrap(require("./list-shipping-options").default))
|
||||
route.get(
|
||||
"/:cart_id",
|
||||
middlewares.wrap(require("./list-shipping-options").default)
|
||||
)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@ import { Validator, MedusaError } from "medusa-core-utils"
|
||||
|
||||
export default async (req, res) => {
|
||||
const schema = Validator.object().keys({
|
||||
cart_id: Validator.string(),
|
||||
cart_id: Validator.string().required(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
const { value, error } = schema.validate(req.params)
|
||||
if (error) {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||
}
|
||||
|
||||
@@ -226,6 +226,10 @@ class CartService extends BaseService {
|
||||
*/
|
||||
async decorate(cart, fields, expandFields = []) {
|
||||
const c = cart.toObject()
|
||||
c.shipping_total = await this.totalsService_.getShippingTotal(cart)
|
||||
c.discount_total = await this.totalsService_.getDiscountTotal(cart)
|
||||
c.tax_total = await this.totalsService_.getTaxTotal(cart)
|
||||
c.subtotal = await this.totalsService_.getSubtotal(cart)
|
||||
c.total = await this.totalsService_.getTotal(cart)
|
||||
if (expandFields.includes("region")) {
|
||||
c.region = await this.regionService_.retrieve(cart.region_id)
|
||||
@@ -241,10 +245,10 @@ class CartService extends BaseService {
|
||||
*/
|
||||
async removeLineItem(cartId, lineItemId) {
|
||||
const cart = await this.retrieve(cartId)
|
||||
const itemToRemove = cart.items.find(line => line._id === lineItemId)
|
||||
const itemToRemove = cart.items.find(line => line._id.equals(lineItemId))
|
||||
|
||||
if (!itemToRemove) {
|
||||
return Promise.resolve()
|
||||
return Promise.resolve(cart)
|
||||
}
|
||||
|
||||
// If cart has more than one of those line items, we update the quantity
|
||||
@@ -739,7 +743,7 @@ class CartService extends BaseService {
|
||||
)
|
||||
|
||||
const status = await provider.getStatus(paymentMethod.data)
|
||||
if (status !== "authorized") {
|
||||
if (!(status === "authorized" || status === "succeeded")) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
`The payment method was not authorized`
|
||||
|
||||
@@ -37,8 +37,12 @@ class LineItemService extends BaseService {
|
||||
|
||||
const lineItemSchema = Validator.object({
|
||||
title: Validator.string().required(),
|
||||
description: Validator.string(),
|
||||
thumbnail: Validator.string(),
|
||||
description: Validator.string()
|
||||
.allow("")
|
||||
.optional(),
|
||||
thumbnail: Validator.string()
|
||||
.allow("")
|
||||
.optional(),
|
||||
content: Validator.alternatives()
|
||||
.try(content, Validator.array().items(content))
|
||||
.required(),
|
||||
@@ -46,7 +50,7 @@ class LineItemService extends BaseService {
|
||||
.integer()
|
||||
.min(1)
|
||||
.required(),
|
||||
metadata: Validator.object(),
|
||||
metadata: Validator.object().default({}),
|
||||
})
|
||||
|
||||
const { value, error } = lineItemSchema.validate(rawLineItem)
|
||||
|
||||
@@ -136,7 +136,8 @@ class ShippingOptionService extends BaseService {
|
||||
* @return {ShippingOption} the validated shipping option
|
||||
*/
|
||||
async validateCartOption(optionId, cart) {
|
||||
let option = await this.retrieve(optionId)
|
||||
const optionDoc = await this.retrieve(optionId)
|
||||
let option = optionDoc.toObject()
|
||||
|
||||
if (cart.region_id !== option.region_id) {
|
||||
throw new MedusaError(
|
||||
@@ -150,7 +151,7 @@ class ShippingOptionService extends BaseService {
|
||||
if (requirement.type === "max_subtotal") {
|
||||
return requirement.value > subtotal
|
||||
} else if (requirement.type === "min_subtotal") {
|
||||
return requirement.value < subtotal
|
||||
return requirement.value <= subtotal
|
||||
}
|
||||
|
||||
return true // default to true
|
||||
|
||||
Reference in New Issue
Block a user