feat: rma shipping option + unit tests

This commit is contained in:
zakariaelas
2021-09-30 14:52:34 +01:00
parent bf43896d19
commit 77ee0bf186
20 changed files with 577 additions and 20 deletions
@@ -78,6 +78,12 @@ export default async (req, res) => {
.optional(),
})
.optional(),
rma_shipping_options: Validator.array().items({
option_id: Validator.string().optional(),
price: Validator.number()
.integer()
.optional(),
}),
additional_items: Validator.array().items({
variant_id: Validator.string().required(),
quantity: Validator.number().required(),
@@ -138,6 +144,7 @@ export default async (req, res) => {
value.return_items,
value.additional_items,
value.return_shipping,
value.rma_shipping_options,
{
idempotency_key: idempotencyKey.idempotency_key,
no_notification: value.no_notification,
@@ -23,9 +23,9 @@ describe("POST /store/carts/:id/shipping-methods", () => {
jest.clearAllMocks()
})
it("calls CartService addShipping", () => {
expect(CartServiceMock.addShippingMethod).toHaveBeenCalledTimes(1)
expect(CartServiceMock.addShippingMethod).toHaveBeenCalledWith(
it("calls CartService addRMAMethod", () => {
expect(CartServiceMock.addRMAMethod).toHaveBeenCalledTimes(1)
expect(CartServiceMock.addRMAMethod).toHaveBeenCalledWith(
IdMap.getId("fr-cart"),
IdMap.getId("freeShipping"),
{}
@@ -45,6 +45,50 @@ describe("POST /store/carts/:id/shipping-methods", () => {
})
})
describe("successfully adds a RMA shipping method", () => {
let subject
beforeAll(async () => {
const cartId = IdMap.getId("swap-cart")
subject = await request(
"POST",
`/store/carts/${cartId}/shipping-methods`,
{
payload: {
option_id: IdMap.getId("freeShipping"),
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls CartService addRMAMethod", () => {
expect(CartServiceMock.addRMAMethod).toHaveBeenCalledTimes(1)
expect(CartServiceMock.addRMAMethod).toHaveBeenCalledWith(
IdMap.getId("swap-cart"),
IdMap.getId("freeShipping"),
{}
)
})
it("calls CartService retrieve", () => {
expect(CartServiceMock.retrieve).toHaveBeenCalledTimes(2)
})
it("returns 200", () => {
expect(subject.status).toEqual(200)
})
it("returns the cart", () => {
expect(subject.body.cart).toEqual(
expect.objectContaining({ type: "swap", id: IdMap.getId("test-swap") })
)
})
})
describe("successfully adds a shipping method with additional data", () => {
let subject
@@ -68,9 +112,9 @@ describe("POST /store/carts/:id/shipping-methods", () => {
jest.clearAllMocks()
})
it("calls CartService addShipping", () => {
expect(CartServiceMock.addShippingMethod).toHaveBeenCalledTimes(1)
expect(CartServiceMock.addShippingMethod).toHaveBeenCalledWith(
it("calls CartService addRMAMethod", () => {
expect(CartServiceMock.addRMAMethod).toHaveBeenCalledTimes(1)
expect(CartServiceMock.addRMAMethod).toHaveBeenCalledWith(
IdMap.getId("fr-cart"),
IdMap.getId("freeShipping"),
{
@@ -1,6 +1,7 @@
import _ from "lodash"
import { Validator, MedusaError } from "medusa-core-utils"
import { defaultFields, defaultRelations } from "./"
import { CartType } from "../../../../models/cart"
/**
* @oas [post] /carts/{id}/shipping-methods
@@ -44,7 +45,9 @@ export default async (req, res) => {
await manager.transaction(async m => {
const txCartService = cartService.withTransaction(m)
await txCartService.addShippingMethod(id, value.option_id, value.data)
await txCartService.addRMAMethod(id, value.option_id, value.data)
const updated = await txCartService.retrieve(id, {
relations: ["payment_sessions"],
})
@@ -54,12 +57,12 @@ export default async (req, res) => {
}
})
const cart = await cartService.retrieve(id, {
const updatedCart = await cartService.retrieve(id, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ cart })
res.status(200).json({ cart: updatedCart })
} catch (err) {
throw err
}
@@ -4,7 +4,7 @@ import { carts, CartServiceMock } from "../../../../../services/__mocks__/cart"
import { ShippingProfileServiceMock } from "../../../../../services/__mocks__/shipping-profile"
describe("GET /store/shipping-options", () => {
describe("retrieves shipping options", () => {
describe("retrieves shipping options when cart type is not swap and not claim", () => {
let subject
beforeAll(async () => {
@@ -53,4 +53,54 @@ describe("GET /store/shipping-options", () => {
)
})
})
describe("retrieves shipping options when cart type is swap", () => {
let subject
beforeAll(async () => {
subject = await request(
"GET",
`/store/shipping-options/${IdMap.getId("swap-cart")}`
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls CartService retrieve", () => {
expect(CartServiceMock.retrieve).toHaveBeenCalledTimes(1)
expect(CartServiceMock.retrieve).toHaveBeenCalledWith(
IdMap.getId("swap-cart"),
{
select: ["subtotal"],
relations: [
"region",
"items",
"items.variant",
"items.variant.product",
],
}
)
})
it("calls ShippingProfileService fetchRMAOptions", () => {
expect(ShippingProfileServiceMock.fetchRMAOptions).toHaveBeenCalledTimes(
1
)
expect(ShippingProfileServiceMock.fetchRMAOptions).toHaveBeenCalledWith(
carts.testSwapCart
)
})
it("returns 200", () => {
expect(subject.status).toEqual(200)
})
it("returns the RMAshippingOptions", () => {
expect(subject.body.shipping_options[0].id).toEqual(
IdMap.getId("cartRMAShippingOption")
)
})
})
})
@@ -1,4 +1,5 @@
import { Validator, MedusaError } from "medusa-core-utils"
import { CartType } from "../../../../models/cart"
/**
* @oas [get] /shipping-options/{cart_id}
@@ -40,7 +41,12 @@ export default async (req, res) => {
relations: ["region", "items", "items.variant", "items.variant.product"],
})
const options = await shippingProfileService.fetchCartOptions(cart)
let options
if (cart.type === CartType.SWAP || cart.type === CartType.CLAIM) {
options = await shippingProfileService.fetchRMAOptions(cart)
} else {
options = await shippingProfileService.fetchCartOptions(cart)
}
res.status(200).json({ shipping_options: options })
} catch (err) {