add update-line-item on draft orders
This commit is contained in:
@@ -29,10 +29,15 @@ export default app => {
|
||||
middlewares.wrap(require("./create-line-item").default)
|
||||
)
|
||||
|
||||
route.post(
|
||||
"/:id/line-items/:line_id",
|
||||
middlewares.wrap(require("./update-line-item").default)
|
||||
)
|
||||
|
||||
route.post("/", middlewares.wrap(require("./create-draft-order").default))
|
||||
|
||||
route.post(
|
||||
"/:id/register-payment",
|
||||
"/:id/pay",
|
||||
middlewares.wrap(require("./register-payment").default)
|
||||
)
|
||||
|
||||
|
||||
@@ -16,9 +16,6 @@ import { defaultCartFields, defaultCartRelations, defaultFields } from "."
|
||||
* region_id:
|
||||
* type: string
|
||||
* description: The id of the Region to create the Draft Order in.
|
||||
* country_code:
|
||||
* type: string
|
||||
* description: "The 2 character ISO country code to create the Draft Order in."
|
||||
* email:
|
||||
* type: string
|
||||
* description: "An email to be used on the Draft Order."
|
||||
@@ -70,6 +67,11 @@ export default async (req, res) => {
|
||||
code: Validator.string(),
|
||||
})
|
||||
.optional(),
|
||||
gift_cards: Validator.array()
|
||||
.items({
|
||||
code: Validator.string(),
|
||||
})
|
||||
.optional(),
|
||||
customer_id: Validator.string().optional(),
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import { MedusaError, Validator } from "medusa-core-utils"
|
||||
import { defaultCartFields, defaultCartRelations, defaultFields } from "."
|
||||
|
||||
/**
|
||||
* @oas [post] /draft-orders/{id}/line-items/{line_id}
|
||||
* operationId: "PostDraftOrdersDraftOrderLineItemsItem"
|
||||
* summary: "Update a Line Item for a Draft Order"
|
||||
* description: "Updates a Line Item for a Draft Order"
|
||||
* requestBody:
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* properties:
|
||||
* unit_price:
|
||||
* description: The potential custom price of the item.
|
||||
* type: integer
|
||||
* title:
|
||||
* description: The potential custom title of the item.
|
||||
* type: string
|
||||
* quantity:
|
||||
* description: The quantity of the Line Item.
|
||||
* type: integer
|
||||
* metadata:
|
||||
* description: The optional key-value map with additional details about the Line Item.
|
||||
* type: object
|
||||
* tags:
|
||||
* - Draft Order
|
||||
* responses:
|
||||
* 200:
|
||||
* description: OK
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* properties:
|
||||
* draft_order:
|
||||
* $ref: "#/components/schemas/draft-order"
|
||||
*/
|
||||
|
||||
export default async (req, res) => {
|
||||
const { id, line_id } = req.params
|
||||
|
||||
const schema = Validator.object().keys({
|
||||
title: Validator.string().optional(),
|
||||
unit_price: Validator.number().optional(),
|
||||
quantity: Validator.number().optional(),
|
||||
metadata: Validator.object().optional(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
if (error) {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||
}
|
||||
|
||||
try {
|
||||
const draftOrderService = req.scope.resolve("draftOrderService")
|
||||
const cartService = req.scope.resolve("cartService")
|
||||
const entityManager = req.scope.resolve("manager")
|
||||
|
||||
await entityManager.transaction(async manager => {
|
||||
const draftOrder = await draftOrderService
|
||||
.withTransaction(manager)
|
||||
.retrieve(id, {
|
||||
select: defaultFields,
|
||||
relations: ["cart", "cart.items"],
|
||||
})
|
||||
|
||||
if (
|
||||
draftOrder.status === "completed" ||
|
||||
draftOrder.status === "awaiting"
|
||||
) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
"You are only allowed to update open draft orders"
|
||||
)
|
||||
}
|
||||
|
||||
if (value.quantity === 0) {
|
||||
await cartService
|
||||
.withTransaction(manager)
|
||||
.removeLineItem(draftOrder.cart.id, line_id)
|
||||
} else {
|
||||
const existing = draftOrder.cart.items.find(i => i.id === line_id)
|
||||
|
||||
if (!existing) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"Could not find the line item"
|
||||
)
|
||||
}
|
||||
|
||||
const lineItemUpdate = {
|
||||
...value,
|
||||
region_id: draftOrder.cart.region_id,
|
||||
}
|
||||
|
||||
if (existing.variant_id) {
|
||||
lineItemUpdate.variant_id = existing.variant_id
|
||||
}
|
||||
|
||||
await cartService
|
||||
.withTransaction(manager)
|
||||
.updateLineItem(draftOrder.cart_id, line_id, lineItemUpdate)
|
||||
}
|
||||
|
||||
draftOrder.cart = await cartService
|
||||
.withTransaction(manager)
|
||||
.retrieve(draftOrder.cart_id, {
|
||||
relations: defaultCartRelations,
|
||||
select: defaultCartFields,
|
||||
})
|
||||
|
||||
res.status(200).json({ draft_order: draftOrder })
|
||||
})
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user