fix: support wishlist (#325)
This commit is contained in:
1 parent
f4a7138a58
commit
4e003bebaa
1 file changed
+12
-21
@@ -3,14 +3,13 @@ import bodyParser from "body-parser"
|
|||||||
import { Validator, MedusaError } from "medusa-core-utils"
|
import { Validator, MedusaError } from "medusa-core-utils"
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
const app = Router();
|
const app = Router()
|
||||||
|
|
||||||
app.delete("/:id/wishlist", bodyParser.json(), async (req, res) => {
|
app.delete("/:id/wishlist", bodyParser.json(), async (req, res) => {
|
||||||
const schema = Validator.object().keys({
|
const schema = Validator.object().keys({
|
||||||
index: Validator.number().required(),
|
index: Validator.number().required(),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
const { value, error } = schema.validate(req.body)
|
const { value, error } = schema.validate(req.body)
|
||||||
if (error) {
|
if (error) {
|
||||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||||
@@ -20,20 +19,16 @@ export default () => {
|
|||||||
const customerService = req.scope.resolve("customerService")
|
const customerService = req.scope.resolve("customerService")
|
||||||
|
|
||||||
let customer = await customerService.retrieve(req.params.id)
|
let customer = await customerService.retrieve(req.params.id)
|
||||||
const wishlist = customer.metadata && customer.metadata.wishlist || []
|
const wishlist = (customer.metadata && customer.metadata.wishlist) || []
|
||||||
|
|
||||||
const newWishlist = [...wishlist]
|
const newWishlist = [...wishlist]
|
||||||
newWishlist.splice(value.index, 1)
|
newWishlist.splice(value.index, 1)
|
||||||
|
|
||||||
customer = await customerService.setMetadata(customer._id, "wishlist", newWishlist)
|
customer = await customerService.update(customer.id, {
|
||||||
|
metadata: { wishlist: newWishlist },
|
||||||
|
})
|
||||||
|
|
||||||
const data = await customerService.decorate(
|
res.json({ customer })
|
||||||
customer,
|
|
||||||
["email", "first_name", "last_name", "shipping_addresses"],
|
|
||||||
["orders"]
|
|
||||||
)
|
|
||||||
|
|
||||||
res.json({ customer: data })
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
@@ -61,21 +56,17 @@ export default () => {
|
|||||||
if (regions.length) {
|
if (regions.length) {
|
||||||
const lineItem = await lineItemService.generate(
|
const lineItem = await lineItemService.generate(
|
||||||
value.variant_id,
|
value.variant_id,
|
||||||
regions[0]._id,
|
regions[0].id,
|
||||||
value.quantity
|
value.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
const wishlist = customer.metadata && customer.metadata.wishlist || []
|
const wishlist = (customer.metadata && customer.metadata.wishlist) || []
|
||||||
customer = await customerService.setMetadata(customer._id, "wishlist", [...wishlist, lineItem])
|
customer = await customerService.update(customer.id, {
|
||||||
|
metadata: { wishlist: [...wishlist, lineItem] },
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await customerService.decorate(
|
res.json({ customer })
|
||||||
customer,
|
|
||||||
["email", "first_name", "last_name", "shipping_addresses"],
|
|
||||||
["orders"]
|
|
||||||
)
|
|
||||||
|
|
||||||
res.json({ customer: data })
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user