Merge branch 'integration/dummy-project' of https://github.com/medusajs/medusa into integration/dummy-project
This commit is contained in:
@@ -22,7 +22,7 @@ export default async (req, res) => {
|
|||||||
await cartService.addShippingMethod(id, value.option_id, value.data)
|
await cartService.addShippingMethod(id, value.option_id, value.data)
|
||||||
|
|
||||||
let cart = await cartService.retrieve(id)
|
let cart = await cartService.retrieve(id)
|
||||||
cart = await cartService.decorate(cart)
|
cart = await cartService.decorate(cart, [], ["region"])
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
res.status(200).json({ cart })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ export default async (req, res) => {
|
|||||||
await cartService.addLineItem(cart._id, lineItem)
|
await cartService.addLineItem(cart._id, lineItem)
|
||||||
|
|
||||||
cart = await cartService.retrieve(cart._id)
|
cart = await cartService.retrieve(cart._id)
|
||||||
cart = await cartService.decorate(cart)
|
cart = await cartService.decorate(cart, [], ["region"])
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
res.status(200).json({ cart })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
// return the updated cart
|
// return the updated cart
|
||||||
let cart = await cartService.retrieve(id)
|
let cart = await cartService.retrieve(id)
|
||||||
cart = await cartService.decorate(cart)
|
cart = await cartService.decorate(cart, [], ["region"])
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
res.status(200).json({ cart })
|
||||||
} catch (err) {
|
} 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",
|
"/:id/line-items/:line_id",
|
||||||
middlewares.wrap(require("./update-line-item").default)
|
middlewares.wrap(require("./update-line-item").default)
|
||||||
)
|
)
|
||||||
|
route.delete(
|
||||||
|
"/:id/line-items/:line_id",
|
||||||
|
middlewares.wrap(require("./delete-line-item").default)
|
||||||
|
)
|
||||||
|
|
||||||
// Payment sessions
|
// Payment sessions
|
||||||
route.post(
|
route.post(
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ export default async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let newCart = await cartService.retrieve(id)
|
let newCart = await cartService.retrieve(id)
|
||||||
const data = await cartService.decorate(newCart)
|
const data = await cartService.decorate(newCart, [], ["region"])
|
||||||
res.json({ cart: data })
|
res.json({ cart: data })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err
|
throw err
|
||||||
|
|||||||
@@ -24,10 +24,8 @@ export default async (req, res) => {
|
|||||||
cart.region_id
|
cart.region_id
|
||||||
)
|
)
|
||||||
|
|
||||||
await cartService.updateLineItem(cart._id, line_id, lineItem)
|
cart = await cartService.updateLineItem(cart._id, line_id, lineItem)
|
||||||
|
cart = await cartService.decorate(cart, [], ["region"])
|
||||||
cart = await cartService.retrieve(cart._id)
|
|
||||||
cart = await cartService.decorate(cart)
|
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
res.status(200).json({ cart })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ export default async (req, res) => {
|
|||||||
await cartService.setPaymentMethod(id, session)
|
await cartService.setPaymentMethod(id, session)
|
||||||
|
|
||||||
let cart = await cartService.retrieve(id)
|
let cart = await cartService.retrieve(id)
|
||||||
cart = await cartService.decorate(cart)
|
cart = await cartService.decorate(cart, [], ["region"])
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
res.status(200).json({ cart })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -6,7 +6,10 @@ const route = Router()
|
|||||||
export default app => {
|
export default app => {
|
||||||
app.use("/shipping-options", route)
|
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
|
return app
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ import { Validator, MedusaError } from "medusa-core-utils"
|
|||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const schema = Validator.object().keys({
|
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) {
|
if (error) {
|
||||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import LineItemSchema from "./schemas/line-item"
|
|||||||
import PaymentMethodSchema from "./schemas/payment-method"
|
import PaymentMethodSchema from "./schemas/payment-method"
|
||||||
import ShippingMethodSchema from "./schemas/shipping-method"
|
import ShippingMethodSchema from "./schemas/shipping-method"
|
||||||
import AddressSchema from "./schemas/address"
|
import AddressSchema from "./schemas/address"
|
||||||
import DiscountModel from "./discount"
|
import DiscountSchema from "./schemas/discount"
|
||||||
|
|
||||||
class OrderModel extends BaseModel {
|
class OrderModel extends BaseModel {
|
||||||
static modelName = "Order"
|
static modelName = "Order"
|
||||||
@@ -23,8 +23,8 @@ class OrderModel extends BaseModel {
|
|||||||
shipping_address: { type: AddressSchema, required: true },
|
shipping_address: { type: AddressSchema, required: true },
|
||||||
items: { type: [LineItemSchema], required: true },
|
items: { type: [LineItemSchema], required: true },
|
||||||
region_id: { type: String, required: true },
|
region_id: { type: String, required: true },
|
||||||
discounts: { type: [DiscountModel.schema], default: [] },
|
discounts: { type: [DiscountSchema], default: [] },
|
||||||
customer_id: { type: String, required: true },
|
customer_id: { type: String },
|
||||||
payment_method: { type: PaymentMethodSchema, required: true },
|
payment_method: { type: PaymentMethodSchema, required: true },
|
||||||
shipping_methods: { type: [ShippingMethodSchema], required: true },
|
shipping_methods: { type: [ShippingMethodSchema], required: true },
|
||||||
metadata: { type: mongoose.Schema.Types.Mixed, default: {} },
|
metadata: { type: mongoose.Schema.Types.Mixed, default: {} },
|
||||||
|
|||||||
@@ -226,6 +226,10 @@ class CartService extends BaseService {
|
|||||||
*/
|
*/
|
||||||
async decorate(cart, fields, expandFields = []) {
|
async decorate(cart, fields, expandFields = []) {
|
||||||
const c = cart.toObject()
|
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)
|
c.total = await this.totalsService_.getTotal(cart)
|
||||||
if (expandFields.includes("region")) {
|
if (expandFields.includes("region")) {
|
||||||
c.region = await this.regionService_.retrieve(cart.region_id)
|
c.region = await this.regionService_.retrieve(cart.region_id)
|
||||||
@@ -241,10 +245,10 @@ class CartService extends BaseService {
|
|||||||
*/
|
*/
|
||||||
async removeLineItem(cartId, lineItemId) {
|
async removeLineItem(cartId, lineItemId) {
|
||||||
const cart = await this.retrieve(cartId)
|
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) {
|
if (!itemToRemove) {
|
||||||
return Promise.resolve()
|
return Promise.resolve(cart)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If cart has more than one of those line items, we update the quantity
|
// 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)
|
const status = await provider.getStatus(paymentMethod.data)
|
||||||
if (status !== "authorized") {
|
if (!(status === "authorized" || status === "succeeded")) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
MedusaError.Types.NOT_ALLOWED,
|
MedusaError.Types.NOT_ALLOWED,
|
||||||
`The payment method was not authorized`
|
`The payment method was not authorized`
|
||||||
|
|||||||
@@ -37,8 +37,12 @@ class LineItemService extends BaseService {
|
|||||||
|
|
||||||
const lineItemSchema = Validator.object({
|
const lineItemSchema = Validator.object({
|
||||||
title: Validator.string().required(),
|
title: Validator.string().required(),
|
||||||
description: Validator.string(),
|
description: Validator.string()
|
||||||
thumbnail: Validator.string(),
|
.allow("")
|
||||||
|
.optional(),
|
||||||
|
thumbnail: Validator.string()
|
||||||
|
.allow("")
|
||||||
|
.optional(),
|
||||||
content: Validator.alternatives()
|
content: Validator.alternatives()
|
||||||
.try(content, Validator.array().items(content))
|
.try(content, Validator.array().items(content))
|
||||||
.required(),
|
.required(),
|
||||||
@@ -46,7 +50,7 @@ class LineItemService extends BaseService {
|
|||||||
.integer()
|
.integer()
|
||||||
.min(1)
|
.min(1)
|
||||||
.required(),
|
.required(),
|
||||||
metadata: Validator.object(),
|
metadata: Validator.object().default({}),
|
||||||
})
|
})
|
||||||
|
|
||||||
const { value, error } = lineItemSchema.validate(rawLineItem)
|
const { value, error } = lineItemSchema.validate(rawLineItem)
|
||||||
|
|||||||
@@ -188,8 +188,20 @@ class OrderService extends BaseService {
|
|||||||
* @return {Promise} resolves to the creation result.
|
* @return {Promise} resolves to the creation result.
|
||||||
*/
|
*/
|
||||||
async createFromCart(cart) {
|
async createFromCart(cart) {
|
||||||
|
const o = {
|
||||||
|
payment_method: cart.payment_method,
|
||||||
|
shipping_methods: cart.shipping_methods,
|
||||||
|
items: cart.items,
|
||||||
|
shipping_address: cart.shipping_address,
|
||||||
|
billing_address: cart.shipping_address,
|
||||||
|
region_id: cart.region_id,
|
||||||
|
email: cart.email,
|
||||||
|
customer_id: cart.customer_id,
|
||||||
|
cart_id: cart._id,
|
||||||
|
}
|
||||||
|
|
||||||
return this.orderModel_
|
return this.orderModel_
|
||||||
.create({ ...cart, metadata: { cart_id: cart._id } })
|
.create(o)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
// Notify subscribers
|
// Notify subscribers
|
||||||
this.eventBus_.emit(OrderService.Events.PLACED, result)
|
this.eventBus_.emit(OrderService.Events.PLACED, result)
|
||||||
|
|||||||
@@ -136,7 +136,8 @@ class ShippingOptionService extends BaseService {
|
|||||||
* @return {ShippingOption} the validated shipping option
|
* @return {ShippingOption} the validated shipping option
|
||||||
*/
|
*/
|
||||||
async validateCartOption(optionId, cart) {
|
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) {
|
if (cart.region_id !== option.region_id) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
@@ -150,7 +151,7 @@ class ShippingOptionService extends BaseService {
|
|||||||
if (requirement.type === "max_subtotal") {
|
if (requirement.type === "max_subtotal") {
|
||||||
return requirement.value > subtotal
|
return requirement.value > subtotal
|
||||||
} else if (requirement.type === "min_subtotal") {
|
} else if (requirement.type === "min_subtotal") {
|
||||||
return requirement.value < subtotal
|
return requirement.value <= subtotal
|
||||||
}
|
}
|
||||||
|
|
||||||
return true // default to true
|
return true // default to true
|
||||||
|
|||||||
Reference in New Issue
Block a user