feat: add route for retrieving a swap (#326)

* feat: swap details

* test: add get-swap and list-swaps route tests

* test: add swaps integration tests

* fix: use IdMap.getId in complete-cart unit test

commit 289d191 uses the IdMap.getId in the SwapServiceMock retrieve method, so a unit test in complete-cart needs to be adjusted accordingly

* fix: prefix default fields and totals with 'cart'
This commit is contained in:
Zakaria El Asri
2021-07-29 16:47:33 +01:00
committed by GitHub
parent 8bdccc6db6
commit 821d8be733
9 changed files with 496 additions and 44 deletions
@@ -0,0 +1,78 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { SwapServiceMock } from "../../../../../services/__mocks__/swap"
const defaultRelations = [
"order",
"additional_items",
"return_order",
"fulfillments",
"payment",
"shipping_address",
"shipping_methods",
"cart",
"cart.items",
"cart.region",
"cart.shipping_methods",
"cart.gift_cards",
"cart.discounts",
"cart.payment",
]
const defaultFields = [
"id",
"fulfillment_status",
"payment_status",
"order_id",
"difference_due",
"cart_id",
"created_at",
"updated_at",
"metadata",
"cart.subtotal",
"cart.tax_total",
"cart.shipping_total",
"cart.discount_total",
"cart.gift_card_total",
"cart.total",
]
describe("GET /admin/swaps/:id", () => {
describe("successfully gets a swap", () => {
let subject
beforeAll(async () => {
subject = await request(
"GET",
`/admin/swaps/${IdMap.getId("test-swap")}`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls swapService retrieve", () => {
expect(SwapServiceMock.retrieve).toHaveBeenCalledTimes(1)
expect(SwapServiceMock.retrieve).toHaveBeenCalledWith(
IdMap.getId("test-swap"),
{
select: defaultFields,
relations: defaultRelations,
}
)
})
it("returns swap", () => {
expect(subject.status).toEqual(200)
expect(subject.body.swap.id).toEqual(IdMap.getId("test-swap"))
})
})
})
@@ -0,0 +1,48 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { SwapServiceMock } from "../../../../../services/__mocks__/swap"
const defaultListOptions = {
take: 50,
skip: 0,
order: { created_at: "DESC" },
}
describe("GET /admin/swaps/", () => {
describe("successfully lists swaps", () => {
let subject
beforeAll(async () => {
subject = await request("GET", `/admin/swaps/`, {
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
})
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls swapService list with default pagination and sorting options", () => {
expect(SwapServiceMock.list).toHaveBeenCalledTimes(1)
expect(SwapServiceMock.list).toHaveBeenCalledWith(
{},
{
...defaultListOptions,
}
)
})
it("returns swaps", () => {
expect(subject.status).toEqual(200)
expect(subject.body.count).toBe(2)
expect(subject.body.swaps).toEqual([
{ id: IdMap.getId("test-swap") },
{ id: IdMap.getId("test-swap-1") },
])
})
})
})
@@ -1,23 +1,36 @@
import { defaultFields, defaultRelations } from "./"
/**
* @oas [get] /swaps/{id}
* operationId: "GetSwapsSwap"
* summary: "Retrieve a Swap"
* description: "Retrieves a Swap."
* parameters:
* - (path) id=* {string} The id of the Swap.
* tags:
* - Swap
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* swap:
* $ref: "#/components/schemas/swap"
*/
export default async (req, res) => {
const { id } = req.params
try {
const orderService = req.scope.resolve("orderService")
const swapService = req.scope.resolve("swapService")
const order = await orderService.retrieve(id, {
relations: [
"order",
"additional_items",
"return_order",
"fulfillments",
"payment",
"shipping_address",
"shipping_methods",
"cart",
],
const swap = await swapService.retrieve(id, {
select: defaultFields,
relations: defaultRelations,
})
res.json({ order })
res.json({ swap })
} catch (error) {
throw error
}
@@ -18,3 +18,38 @@ export default app => {
return app
}
export const defaultRelations = [
"order",
"additional_items",
"return_order",
"fulfillments",
"payment",
"shipping_address",
"shipping_methods",
"cart",
"cart.items",
"cart.region",
"cart.shipping_methods",
"cart.gift_cards",
"cart.discounts",
"cart.payment",
]
export const defaultFields = [
"id",
"fulfillment_status",
"payment_status",
"order_id",
"difference_due",
"cart_id",
"created_at",
"updated_at",
"metadata",
"cart.subtotal",
"cart.tax_total",
"cart.shipping_total",
"cart.discount_total",
"cart.gift_card_total",
"cart.total",
]
@@ -78,7 +78,7 @@ describe("POST /store/carts/:id", () => {
})
it("returns the created order", () => {
expect(subject.body.data.id).toEqual("test-swap")
expect(subject.body.data.id).toEqual(IdMap.getId("test-swap"))
})
})