POST /store/carts/:id to update email, addresses, dicsounts, region

This commit is contained in:
Sebastian Rindom
2020-02-25 16:50:40 +01:00
parent 30785ebb95
commit 245557ac2e
6 changed files with 221 additions and 3 deletions

View File

@@ -10,6 +10,9 @@ export default () => {
case MedusaError.Types.INVALID_DATA:
statusCode = 400
break
case MedusaError.Types.NOT_FOUND:
statusCode = 404
break
case MedusaError.Types.DB_ERROR:
statusCode = 500
break

View File

@@ -0,0 +1,136 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { CartServiceMock } from "../../../../../services/__mocks__/cart"
describe("POST /store/carts/:id", () => {
describe("successfully updates all fields", () => {
let subject
const address = {
first_name: "LeBron",
last_name: "James",
country_code: "US",
address_1: "24 Dunk Dr",
address_2: "Ball Ave",
city: "Los Angeles",
province: "CA",
postal_code: "91092",
}
beforeAll(async () => {
subject = await request(
"POST",
`/store/carts/${IdMap.getId("emptyCart")}`,
{
payload: {
region_id: IdMap.getId("testRegion"),
email: "test@admin.com",
shipping_address: address,
billing_address: address,
discounts: [
{
code: "TESTCODE",
},
],
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("sets new region", () => {
expect(CartServiceMock.setRegion).toHaveBeenCalledTimes(1)
expect(CartServiceMock.setRegion).toHaveBeenCalledWith(
IdMap.getId("emptyCart"),
IdMap.getId("testRegion")
)
})
it("updates email", () => {
expect(CartServiceMock.updateEmail).toHaveBeenCalledTimes(1)
expect(CartServiceMock.updateEmail).toHaveBeenCalledWith(
IdMap.getId("emptyCart"),
"test@admin.com"
)
})
it("updates shipping address", () => {
expect(CartServiceMock.updateShippingAddress).toHaveBeenCalledTimes(1)
expect(CartServiceMock.updateShippingAddress).toHaveBeenCalledWith(
IdMap.getId("emptyCart"),
address
)
})
it("updates billing address", () => {
expect(CartServiceMock.updateBillingAddress).toHaveBeenCalledTimes(1)
expect(CartServiceMock.updateBillingAddress).toHaveBeenCalledWith(
IdMap.getId("emptyCart"),
address
)
})
it("applies promo code", () => {
expect(CartServiceMock.applyPromoCode).toHaveBeenCalledTimes(1)
expect(CartServiceMock.applyPromoCode).toHaveBeenCalledWith(
IdMap.getId("emptyCart"),
"TESTCODE"
)
})
it("returns 200", () => {
expect(subject.status).toEqual(200)
})
it("returns cart", () => {
expect(subject.body._id).toEqual(IdMap.getId("emptyCart"))
})
})
describe("it bubbles errors", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/store/carts/${IdMap.getId("emptyCart")}`,
{
payload: {
region_id: IdMap.getId("fail"),
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("returns 404", () => {
expect(subject.status).toEqual(404)
expect(subject.body.message).toEqual("Region not found")
})
})
describe("returns 404 on undefined cart", () => {
let subject
beforeAll(async () => {
subject = await request("POST", `/store/carts/none`)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls get product from productSerice", () => {
expect(CartServiceMock.retrieve).toHaveBeenCalledTimes(1)
expect(CartServiceMock.retrieve).toHaveBeenCalledWith("none")
})
it("returns 404", () => {
expect(subject.status).toEqual(404)
})
})
})

View File

@@ -1,8 +1,8 @@
export default async (req, res) => {
const { cartId } = req.params
const { id } = req.params
const cartService = req.scope.resolve("cartService")
const cart = await cartService.retrieve(cartId)
const cart = await cartService.retrieve(id)
if (!cart) {
res.sendStatus(404)

View File

@@ -6,8 +6,10 @@ const route = Router()
export default app => {
app.use("/carts", route)
route.get("/:cartId", middlewares.wrap(require("./get-cart").default))
route.get("/:id", middlewares.wrap(require("./get-cart").default))
route.post("/", middlewares.wrap(require("./create-cart").default))
route.post("/:id", middlewares.wrap(require("./update-cart").default))
return app
}

View File

@@ -0,0 +1,59 @@
import _ from "lodash"
import { Validator, MedusaError } from "medusa-core-utils"
export default async (req, res) => {
const { id } = req.params
const schema = Validator.object().keys({
region_id: Validator.string(),
email: Validator.string().email(),
billing_address: Validator.address(),
shipping_address: Validator.address(),
discounts: Validator.array().items({
code: Validator.string(),
}),
})
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
const cartService = req.scope.resolve("cartService")
const oldCart = await cartService.retrieve(id)
if (!oldCart) {
res.sendStatus(404)
return
}
try {
if (value.region_id) {
await cartService.setRegion(id, value.region_id)
}
if (value.email) {
await cartService.updateEmail(id, value.email)
}
if (!_.isEmpty(value.shipping_address)) {
await cartService.updateShippingAddress(id, value.shipping_address)
}
if (!_.isEmpty(value.billing_address)) {
await cartService.updateBillingAddress(id, value.billing_address)
}
if (value.discounts && value.discounts.length) {
await Promise.all(
value.discounts.map(async ({ code }) =>
cartService.applyPromoCode(id, code)
)
)
}
const newCart = await cartService.retrieve(id)
res.json(newCart)
} catch (err) {
throw err
}
}

View File

@@ -34,6 +34,24 @@ export const CartServiceMock = {
addLineItem: jest.fn().mockImplementation((cartId, lineItem) => {
return Promise.resolve()
}),
setRegion: jest.fn().mockImplementation((cartId, regionId) => {
if (regionId === IdMap.getId("fail")) {
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Region not found")
}
return Promise.resolve()
}),
updateEmail: jest.fn().mockImplementation((cartId, email) => {
return Promise.resolve()
}),
updateShippingAddress: jest.fn().mockImplementation((cartId, address) => {
return Promise.resolve()
}),
updateBillingAddress: jest.fn().mockImplementation((cartId, address) => {
return Promise.resolve()
}),
applyPromoCode: jest.fn().mockImplementation((cartId, code) => {
return Promise.resolve()
}),
}
const mock = jest.fn().mockImplementation(() => {