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:
1 parent
8bdccc6db6
commit
821d8be733
9 files changed
+496
-44
No files matched your search
@@ -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"))
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -11,7 +11,13 @@ export const SwapServiceMock = {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
retrieve: jest.fn().mockImplementation(data => {
|
||||
return Promise.resolve({ id: "test-swap" })
|
||||
return Promise.resolve({ id: IdMap.getId("test-swap") })
|
||||
}),
|
||||
list: jest.fn().mockImplementation((...args) => {
|
||||
return Promise.resolve([
|
||||
{ id: IdMap.getId("test-swap") },
|
||||
{ id: IdMap.getId("test-swap-1") },
|
||||
])
|
||||
}),
|
||||
}
|
||||
|
||||
|
||||
@@ -92,6 +92,69 @@ class SwapService extends BaseService {
|
||||
return cloned
|
||||
}
|
||||
|
||||
transformQueryForTotals_(config) {
|
||||
let { select, relations } = config
|
||||
|
||||
if (!select) {
|
||||
return {
|
||||
...config,
|
||||
totalsToSelect: [],
|
||||
}
|
||||
}
|
||||
|
||||
const totalFields = [
|
||||
"cart.subtotal",
|
||||
"cart.tax_total",
|
||||
"cart.shipping_total",
|
||||
"cart.discount_total",
|
||||
"cart.gift_card_total",
|
||||
"cart.total",
|
||||
]
|
||||
|
||||
const totalsToSelect = select.filter(v => totalFields.includes(v))
|
||||
if (totalsToSelect.length > 0) {
|
||||
const relationSet = new Set(relations)
|
||||
relationSet.add("cart")
|
||||
relationSet.add("cart.items")
|
||||
relationSet.add("cart.gift_cards")
|
||||
relationSet.add("cart.discounts")
|
||||
relationSet.add("cart.shipping_methods")
|
||||
relationSet.add("cart.region")
|
||||
relations = [...relationSet]
|
||||
|
||||
select = select.filter(v => !totalFields.includes(v))
|
||||
}
|
||||
|
||||
return {
|
||||
...config,
|
||||
relations,
|
||||
select,
|
||||
totalsToSelect,
|
||||
}
|
||||
}
|
||||
|
||||
async decorateTotals_(cart, totalsFields = []) {
|
||||
if (totalsFields.includes("cart.shipping_total")) {
|
||||
cart.shipping_total = await this.totalsService_.getShippingTotal(cart)
|
||||
}
|
||||
if (totalsFields.includes("cart.discount_total")) {
|
||||
cart.discount_total = await this.totalsService_.getDiscountTotal(cart)
|
||||
}
|
||||
if (totalsFields.includes("cart.tax_total")) {
|
||||
cart.tax_total = await this.totalsService_.getTaxTotal(cart)
|
||||
}
|
||||
if (totalsFields.includes("cart.gift_card_total")) {
|
||||
cart.gift_card_total = await this.totalsService_.getGiftCardTotal(cart)
|
||||
}
|
||||
if (totalsFields.includes("cart.subtotal")) {
|
||||
cart.subtotal = await this.totalsService_.getSubtotal(cart)
|
||||
}
|
||||
if (totalsFields.includes("cart.total")) {
|
||||
cart.total = await this.totalsService_.getTotal(cart)
|
||||
}
|
||||
return cart
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a swap with the given id.
|
||||
* @param {string} id - the id of the swap to retrieve
|
||||
@@ -102,7 +165,11 @@ class SwapService extends BaseService {
|
||||
|
||||
const validatedId = this.validateId_(id)
|
||||
|
||||
const query = this.buildQuery_({ id: validatedId }, config)
|
||||
const { totalsToSelect, ...newConfig } = this.transformQueryForTotals_(
|
||||
config
|
||||
)
|
||||
|
||||
const query = this.buildQuery_({ id: validatedId }, newConfig)
|
||||
|
||||
const rels = query.relations
|
||||
delete query.relations
|
||||
@@ -112,6 +179,11 @@ class SwapService extends BaseService {
|
||||
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Swap was not found")
|
||||
}
|
||||
|
||||
if (rels && rels.includes("cart")) {
|
||||
const cart = await this.decorateTotals_(swap.cart, totalsToSelect)
|
||||
swap.cart = cart
|
||||
}
|
||||
|
||||
return swap
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user