Merge remote-tracking branch 'origin/develop' into feat/meilisearch-plugin

This commit is contained in:
Sebastian Rindom
2021-09-29 15:20:22 +02:00
271 changed files with 16116 additions and 5647 deletions
@@ -12,6 +12,9 @@ import { Validator, MedusaError } from "medusa-core-utils"
* application/json:
* schema:
* properties:
* email:
* type: string
* description: The Customer's email. Only providable if user not registered.
* first_name:
* type: string
* description: The Customer's first name.
@@ -37,6 +40,7 @@ export default async (req, res) => {
const { id } = req.params
const schema = Validator.object().keys({
email: Validator.string().optional(),
first_name: Validator.string().optional(),
last_name: Validator.string().optional(),
password: Validator.string().optional(),
@@ -50,9 +54,19 @@ export default async (req, res) => {
try {
const customerService = req.scope.resolve("customerService")
let customer = await customerService.retrieve(id)
if (value.email && customer.has_account) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Email cannot be changed when the user has registered their account"
)
}
await customerService.update(id, value)
const customer = await customerService.retrieve(id, {
customer = await customerService.retrieve(id, {
relations: ["orders"],
})
res.status(200).json({ customer })
@@ -22,6 +22,7 @@ import variantRoutes from "./variants"
import draftOrderRoutes from "./draft-orders"
import collectionRoutes from "./collections"
import notificationRoutes from "./notifications"
import noteRoutes from "./notes"
const route = Router()
@@ -68,6 +69,7 @@ export default (app, container, config) => {
collectionRoutes(route)
notificationRoutes(route)
returnReasonRoutes(route)
noteRoutes(route)
return app
}
@@ -0,0 +1,63 @@
import { MedusaError, Validator } from "medusa-core-utils"
/**
* @oas [post] /notes
* operationId: "PostNotes"
* summary: "Creates a Note"
* description: "Creates a Note which can be associated with any resource as required."
* requestBody:
* content:
* application/json:
* schema:
* properties:
* resource_id:
* type: string
* description: The id of the resource which the Note relates to.
* resource_type:
* type: string
* description: The type of resource which the Note relates to.
* value:
* type: string
* description: The content of the Note to create.
* tags:
* - Note
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* note:
* $ref: "#/components/schemas/note"
*
*/
export default async (req, res) => {
const schema = Validator.object().keys({
resource_id: Validator.string(),
resource_type: Validator.string(),
value: Validator.string(),
})
const userId = req.user.id || req.user.userId
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const noteService = req.scope.resolve("noteService")
const result = await noteService.create({
resource_id: value.resource_id,
resource_type: value.resource_type,
value: value.value,
author_id: userId,
})
res.status(200).json({ note: result })
} catch (err) {
throw err
}
}
@@ -0,0 +1,35 @@
/**
* @oas [delete] /notes/{id}
* operationId: "DeleteNotesNote"
* summary: "Deletes a Note"
* description: "Deletes a Note."
* parameters:
* - (path) id=* {string} The id of the Note to delete.
* tags:
* - Note
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* id:
* type: string
* description: The id of the deleted Note.
* deleted:
* type: boolean
* description: Whether or not the Note was deleted.
*/
export default async (req, res) => {
const { id } = req.params
try {
const noteService = req.scope.resolve("noteService")
await noteService.delete(id)
res.status(200).json({ id, deleted: true })
} catch (err) {
throw err
}
}
@@ -0,0 +1,31 @@
/**
* @oas [get] /notes/{id}
* operationId: "GetNoteNote"
* summary: "Get Note"
* description: "Retrieves a single note using its id"
* parameters:
* - (path) id=* {string} The id of the note to retrieve.
* tags:
* - Note
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* note:
* $ref: "#/components/schemas/note"
*/
export default async (req, res) => {
const { id } = req.params
try {
const noteService = req.scope.resolve("noteService")
const note = await noteService.retrieve(id, { relations: ["author"] })
res.status(200).json({ note })
} catch (err) {
throw err
}
}
@@ -0,0 +1,20 @@
import { Router } from "express"
import middlewares from "../../../middlewares"
const route = Router()
export default app => {
app.use("/notes", route)
route.get("/:id", middlewares.wrap(require("./get-note").default))
route.get("/", middlewares.wrap(require("./list-notes").default))
route.post("/", middlewares.wrap(require("./create-note").default))
route.post("/:id", middlewares.wrap(require("./update-note").default))
route.delete("/:id", middlewares.wrap(require("./delete-note").default))
return app
}
@@ -0,0 +1,42 @@
/**
* @oas [get] /notes
* operationId: "GetNotes"
* summary: "List Notes"
* description: "Retrieves a list of notes"
* tags:
* - Note
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* notes:
* type: array
* items:
* $ref: "#/components/schemas/note"
*/
export default async (req, res) => {
try {
const limit = parseInt(req.query.limit) || 50
const offset = parseInt(req.query.offset) || 0
const selector = {}
if ("resource_id" in req.query) {
selector.resource_id = req.query.resource_id
}
const noteService = req.scope.resolve("noteService")
const notes = await noteService.list(selector, {
take: limit,
skip: offset,
relations: ["author"],
})
res.status(200).json({ notes })
} catch (err) {
throw err
}
}
@@ -0,0 +1,51 @@
import { MedusaError, Validator } from "medusa-core-utils"
/**
* @oas [post] /notes/{id}
* operationId: "PostNotesNote"
* summary: "Updates a Note"
* description: "Updates a Note associated with some resource"
* parameters:
* - (path) id=* {string} The id of the Note to update
* requestBody:
* content:
* application/json:
* schema:
* properties:
* value:
* type: string
* description: The updated description of the Note.
* tags:
* - Note
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* note:
* $ref: "#/components/schemas/note"
*
*/
export default async (req, res) => {
const { id } = req.params
const schema = Validator.object().keys({
value: Validator.string(),
})
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const noteService = req.scope.resolve("noteService")
const result = await noteService.update(id, value.value)
res.status(200).json({ note: result })
} catch (err) {
throw err
}
}
@@ -0,0 +1,64 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { ClaimServiceMock } from "../../../../../services/__mocks__/claim"
describe("POST /admin/orders/:id/claims/:claim_id/cancel", () => {
describe("successfully cancels a claim", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order")}/claims/${IdMap.getId(
"test-claim"
)}/cancel`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls ClaimService cancel", () => {
expect(ClaimServiceMock.cancel).toHaveBeenCalledTimes(1)
expect(ClaimServiceMock.cancel).toHaveBeenCalledWith(
IdMap.getId("test-claim")
)
})
})
describe("Trying to cancel a claim unrelated to the order fails", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order2")}/claims/${IdMap.getId(
"test-claim"
)}/cancel`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("returns error", () => {
expect(subject.status).toEqual(404)
})
})
})
@@ -0,0 +1,92 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { ClaimServiceMock } from "../../../../../services/__mocks__/claim"
describe("POST /admin/orders/:id/claims/:claim_id/fulfillments/:fulfillment_id/cancel", () => {
describe("successfully cancels a fulfillment", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order")}/claims/${IdMap.getId(
"test-claim"
)}/fulfillments/${IdMap.getId("claim-fulfillment")}/cancel`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls claimService cancelFulfillment", () => {
expect(ClaimServiceMock.cancelFulfillment).toHaveBeenCalledTimes(1)
expect(ClaimServiceMock.cancelFulfillment).toHaveBeenCalledWith(
IdMap.getId("claim-fulfillment")
)
})
})
describe("Trying to cancel a fulfillment unrelated to the claim fails", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order")}/claims/${IdMap.getId(
"claim-fulfillment2"
)}/fulfillments/${IdMap.getId("claim-fulfillment")}/cancel`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("returns error", () => {
expect(subject.status).toEqual(404)
})
})
describe("Trying to cancel a fulfillment, where claim and order are unrelated", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order2")}/claims/${IdMap.getId(
"test-claim"
)}/fulfillments/${IdMap.getId("claim-fulfillment")}/cancel`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("returns error", () => {
expect(subject.status).toEqual(404)
})
})
})
@@ -0,0 +1,92 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { SwapServiceMock } from "../../../../../services/__mocks__/swap"
describe("POST /admin/orders/:id/swaps/:swap_id/fulfillments/:fulfillment_id/cancel", () => {
describe("successfully cancels a fulfillment", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order")}/swaps/${IdMap.getId(
"test-swap"
)}/fulfillments/${IdMap.getId("swap-fulfillment")}/cancel`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls SwapService cancelFulfillment", () => {
expect(SwapServiceMock.cancelFulfillment).toHaveBeenCalledTimes(1)
expect(SwapServiceMock.cancelFulfillment).toHaveBeenCalledWith(
IdMap.getId("swap-fulfillment")
)
})
})
describe("Trying to cancel a fulfillment unrelated to the swap fails", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order")}/swaps/${IdMap.getId(
"swap-fulfillment2"
)}/fulfillments/${IdMap.getId("swap-fulfillment")}/cancel`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("returns error", () => {
expect(subject.status).toEqual(404)
})
})
describe("Trying to cancel a fulfillment, where swap and order are unrelated", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order2")}/swaps/${IdMap.getId(
"test-swap"
)}/fulfillments/${IdMap.getId("swap-fulfillment")}/cancel`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("returns error", () => {
expect(subject.status).toEqual(404)
})
})
})
@@ -0,0 +1,64 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { OrderServiceMock } from "../../../../../services/__mocks__/order"
describe("POST /admin/orders/:id/fulfillments/:fulfillment_id/cancel", () => {
describe("successfully cancels a fulfillment", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order")}/fulfillments/${IdMap.getId(
"order-fulfillment"
)}/cancel`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls OrderService cancelFulfillment", () => {
expect(OrderServiceMock.cancelFulfillment).toHaveBeenCalledTimes(1)
expect(OrderServiceMock.cancelFulfillment).toHaveBeenCalledWith(
IdMap.getId("order-fulfillment")
)
})
})
describe("Trying to cancel a fulfillment unrelated to the order fails", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order2")}/fulfillments/${IdMap.getId(
"order-fulfillment"
)}/cancel`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("returns error", () => {
expect(subject.status).toEqual(404)
})
})
})
@@ -0,0 +1,64 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { SwapServiceMock } from "../../../../../services/__mocks__/swap"
describe("POST /admin/orders/:id/swaps/:swap_id/cancel", () => {
describe("successfully cancels a claim", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order")}/swaps/${IdMap.getId(
"test-swap"
)}/cancel`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls SwapService cancel", () => {
expect(SwapServiceMock.cancel).toHaveBeenCalledTimes(1)
expect(SwapServiceMock.cancel).toHaveBeenCalledWith(
IdMap.getId("test-swap")
)
})
})
describe("Trying to cancel a claim unrelated to the order fails", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order2")}/swaps/${IdMap.getId(
"test-swap"
)}/cancel`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("returns error", () => {
expect(subject.status).toEqual(404)
})
})
})
@@ -19,6 +19,7 @@ const defaultRelations = [
"gift_card_transactions",
"claims",
"claims.return_order",
"claims.return_order.shipping_method",
"claims.shipping_methods",
"claims.shipping_address",
"claims.additional_items",
@@ -0,0 +1,51 @@
import { MedusaError } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "."
/**
* @oas [post] /orders/{id}/claims/{claim_id}/cancel
* operationId: "PostOrdersClaimCancel"
* summary: "Cancels a Claim"
* description: "Cancels a Claim"
* parameters:
* - (path) id=* {string} The id of the Order.
* . (path) claim_id=* {string} The id of the Claim.
* tags:
* - Claim
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* order:
* $ref: "#/components/schemas/claim"
*/
export default async (req, res) => {
const { id, claim_id } = req.params
try {
const claimService = req.scope.resolve("claimService")
const orderService = req.scope.resolve("orderService")
const claim = await claimService.retrieve(claim_id)
if (claim.order_id !== id) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`no claim was found with the id: ${claim_id} related to order: ${id}`
)
}
await claimService.cancel(claim_id)
const order = await orderService.retrieve(id, {
select: defaultFields,
relations: defaultRelations,
})
res.json({ order })
} catch (error) {
throw error
}
}
@@ -0,0 +1,61 @@
import { MedusaError } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "."
/**
* @oas [post] orders//{id}/claims/{claim_id}/fulfillments/{fulfillment_id}/cancel
* operationId: "PostOrdersClaimFulfillmentsCancel"
* summary: "Cancels a fulfilmment related to a Claim"
* description: "Registers a Fulfillment as canceled."
* parameters:
* - (path) id=* {string} The id of the Order which the Claim relates to.
* - (path) claim_id=* {string} The id of the Claim which the Fulfillment relates to.
* - (path) fulfillment_id=* {string} The id of the Fulfillment.
* tags:
* - Fulfillment
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* fulfillment:
* $ref: "#/components/schemas/fulfillment"
*/
export default async (req, res) => {
const { id, claim_id, fulfillment_id } = req.params
try {
const fulfillmentService = req.scope.resolve("fulfillmentService")
const claimService = req.scope.resolve("claimService")
const orderService = req.scope.resolve("orderService")
const fulfillment = await fulfillmentService.retrieve(fulfillment_id)
if (fulfillment.claim_order_id !== claim_id) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`no fulfillment was found with the id: ${fulfillment_id} related to claim: ${claim_id}`
)
}
const claim = await claimService.retrieve(claim_id)
if (claim.order_id !== id) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`no claim was found with the id: ${claim_id} related to order: ${id}`
)
}
await claimService.cancelFulfillment(fulfillment_id)
const order = await orderService.retrieve(id, {
select: defaultFields,
relations: defaultRelations,
})
res.json({ order })
} catch (error) {
throw error
}
}
@@ -0,0 +1,62 @@
import { MedusaError } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "."
/**
* @oas [post] /orders/{id}/swaps/{swap_id}/fulfillments/{fulfillment_id}/cancel
* operationId: "PostOrdersSwapFulfillmentsCancel"
* summary: "Cancels a fulfilmment related to a Swap"
* description: "Registers a Fulfillment as canceled."
* parameters:
* - (path) id=* {string} The id of the Order which the Swap relates to.
* - (path) swap_id=* {string} The id of the Swap which the Fulfillment relates to.
* - (path) fulfillment_id=* {string} The id of the Fulfillment.
* tags:
* - Fulfillment
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* fulfillment:
* $ref: "#/components/schemas/fulfillment"
*/
export default async (req, res) => {
const { id, swap_id, fulfillment_id } = req.params
try {
const fulfillmentService = req.scope.resolve("fulfillmentService")
const swapService = req.scope.resolve("swapService")
const orderService = req.scope.resolve("orderService")
const fulfillment = await fulfillmentService.retrieve(fulfillment_id)
if (fulfillment.swap_id !== swap_id) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`no fulfillment was found with the id: ${fulfillment_id} related to swap: ${id}`
)
}
const swap = await swapService.retrieve(swap_id)
if (swap.order_id !== id) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`no swap was found with the id: ${swap_id} related to order: ${id}`
)
}
await swapService.cancelFulfillment(fulfillment_id)
const order = await orderService.retrieve(id, {
select: defaultFields,
relations: defaultRelations,
})
res.json({ order })
} catch (error) {
throw error
}
}
@@ -0,0 +1,51 @@
import { MedusaError } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "."
/**
* @oas [post] /orders/{id}/fulfillments/{fulfillment_id}/cancel
* operationId: "PostOrdersOrderFulfillmentsCancel"
* summary: "Cancels a fulfilmment"
* description: "Registers a Fulfillment as canceled."
* parameters:
* - (path) id=* {string} The id of the Order which the Fulfillment relates to.
* - (path) fulfillment_id=* {string} The id of the Fulfillment
* tags:
* - Fulfillment
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* fulfillment:
* $ref: "#/components/schemas/fulfillment"
*/
export default async (req, res) => {
const { id, fulfillment_id } = req.params
try {
const fulfillmentService = req.scope.resolve("fulfillmentService")
const orderService = req.scope.resolve("orderService")
const fulfillment = await fulfillmentService.retrieve(fulfillment_id)
if (fulfillment.order_id !== id) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`no fulfillment was found with the id: ${fulfillment_id} related to order: ${id}`
)
}
await orderService.cancelFulfillment(fulfillment_id)
const order = await orderService.retrieve(id, {
select: defaultFields,
relations: defaultRelations,
})
res.json({ order })
} catch (error) {
throw error
}
}
@@ -1,3 +1,5 @@
import { defaultFields, defaultRelations } from "."
/**
* @oas [post] /orders/{id}/cancel
* operationId: "PostOrdersOrderCancel"
@@ -22,11 +24,11 @@ export default async (req, res) => {
try {
const orderService = req.scope.resolve("orderService")
await orderService.cancel(id)
const order = await orderService.retrieve(id, {
relations: ["region", "customer", "swaps"],
select: defaultFields,
relations: defaultRelations,
})
res.json({ order })
@@ -0,0 +1,51 @@
import { MedusaError } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "."
/**
* @oas [post] /orders/{id}/swaps/{swap_id}/cancel
* operationId: "PostOrdersSwapCancel"
* summary: "Cancels a Swap"
* description: "Cancels a Swap"
* parameters:
* - (path) id=* {string} The id of the Order.
* . (path) swap_id=* {string} The id of the Swap.
* tags:
* - Swap
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* order:
* $ref: "#/components/schemas/swap"
*/
export default async (req, res) => {
const { id, swap_id } = req.params
try {
const swapService = req.scope.resolve("swapService")
const orderService = req.scope.resolve("orderService")
const swap = await swapService.retrieve(swap_id)
if (swap.order_id !== id) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`no swap was found with the id: ${swap_id} related to order: ${id}`
)
}
await swapService.cancel(swap_id)
const order = await orderService.retrieve(id, {
select: defaultFields,
relations: defaultRelations,
})
res.json({ order })
} catch (error) {
throw error
}
}
@@ -48,6 +48,9 @@ import { defaultFields, defaultRelations } from "./"
* no_notification:
* description: If set to true no notification will be send related to this Swap.
* type: boolean
* allow_backorder:
* description: If true, swaps can be completed with items out of stock
* type: boolean
* tags:
* - Order
* responses:
@@ -83,6 +86,7 @@ export default async (req, res) => {
quantity: Validator.number().required(),
}),
no_notification: Validator.boolean().optional(),
allow_backorder: Validator.boolean().default(true),
})
const { value, error } = schema.validate(req.body)
@@ -141,6 +145,7 @@ export default async (req, res) => {
{
idempotency_key: idempotencyKey.idempotency_key,
no_notification: value.no_notification,
allow_backorder: value.allow_backorder,
}
)
@@ -62,6 +62,30 @@ export default app => {
middlewares.wrap(require("./create-fulfillment").default)
)
/**
* Cancel a fulfillment related to an order.
*/
route.post(
"/:id/fulfillments/:fulfillment_id/cancel",
middlewares.wrap(require("./cancel-fulfillment").default)
)
/**
* Cancel a fulfillment related to a swap.
*/
route.post(
"/:id/swaps/:swap_id/fulfillments/:fulfillment_id/cancel",
middlewares.wrap(require("./cancel-fulfillment-swap").default)
)
/**
* Cancel a fulfillment related to a claim.
*/
route.post(
"/:id/claims/:claim_id/fulfillments/:fulfillment_id/cancel",
middlewares.wrap(require("./cancel-fulfillment-claim").default)
)
/**
* Create a shipment.
*/
@@ -104,6 +128,14 @@ export default app => {
*/
route.post("/:id/swaps", middlewares.wrap(require("./create-swap").default))
/**
* Cancels a swap.
*/
route.post(
"/:id/swaps/:swap_id/cancel",
middlewares.wrap(require("./cancel-swap").default)
)
/**
* Receives the inventory to return from a swap
*/
@@ -141,6 +173,14 @@ export default app => {
*/
route.post("/:id/claims", middlewares.wrap(require("./create-claim").default))
/**
* Cancels a claim
*/
route.post(
"/:id/claims/:claim_id/cancel",
middlewares.wrap(require("./cancel-claim").default)
)
/**
* Updates a claim
*/
@@ -193,6 +233,7 @@ export const defaultRelations = [
"gift_card_transactions",
"claims",
"claims.return_order",
"claims.return_order.shipping_method",
"claims.shipping_methods",
"claims.shipping_address",
"claims.additional_items",
@@ -140,7 +140,7 @@ export default async (req, res) => {
if (typeof value.refund !== "undefined" && value.refund < 0) {
returnObj.refund_amount = 0
} else {
if (value.refund) {
if (value.refund >= 0) {
returnObj.refund_amount = value.refund
}
}
@@ -149,7 +149,10 @@ export default async (req, res) => {
.withTransaction(manager)
.retrieve(id)
const evaluatedNoNotification = value.no_notification !== undefined ? value.no_notification : order.no_notification
const evaluatedNoNotification =
value.no_notification !== undefined
? value.no_notification
: order.no_notification
returnObj.no_notification = evaluatedNoNotification
const createdReturn = await returnService
@@ -161,13 +164,13 @@ export default async (req, res) => {
.withTransaction(manager)
.fulfill(createdReturn.id)
}
await eventBus
.withTransaction(manager)
.emit("order.return_requested", {
id,
return_id: createdReturn.id,
no_notification: evaluatedNoNotification
no_notification: evaluatedNoNotification,
})
return {
@@ -1,10 +1,80 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
import { ShippingProfileServiceMock } from "../../../../../services/__mocks__/shipping-profile"
describe("POST /admin/products", () => {
describe("successful creation", () => {
describe("successful creation with variants", () => {
let subject
beforeAll(async () => {
subject = await request("POST", "/admin/products", {
payload: {
title: "Test Product with variants",
description: "Test Description",
tags: [{ id: "test", value: "test" }],
handle: "test-product",
options: [{ title: "Test" }],
variants: [
{
title: "Test",
prices: [
{
currency_code: "USD",
amount: 100,
},
],
options: [
{
value: "100",
},
],
},
],
},
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
})
})
afterAll(async () => {
jest.clearAllMocks()
})
it("returns 200", () => {
expect(subject.status).toEqual(200)
})
it("assigns invokes productVariantService with ranked variants", () => {
expect(ProductVariantServiceMock.create).toHaveBeenCalledTimes(1)
expect(ProductVariantServiceMock.create).toHaveBeenCalledWith(
IdMap.getId("productWithOptions"),
{
title: "Test",
variant_rank: 0,
prices: [
{
currency_code: "USD",
amount: 100,
},
],
options: [
{
option_id: IdMap.getId("option1"),
value: "100",
},
],
inventory_quantity: 0,
}
)
})
})
describe("successful creation test", () => {
let subject
beforeAll(async () => {
@@ -14,6 +84,7 @@ describe("POST /admin/products", () => {
description: "Test Description",
tags: [{ id: "test", value: "test" }],
handle: "test-product",
options: [{ title: "Denominations" }],
},
adminSession: {
jwt: {
@@ -39,7 +110,9 @@ describe("POST /admin/products", () => {
description: "Test Description",
tags: [{ id: "test", value: "test" }],
handle: "test-product",
status: "draft",
is_giftcard: false,
options: [{ title: "Denominations" }],
profile_id: IdMap.getId("default_shipping_profile"),
})
})
@@ -98,6 +171,7 @@ describe("POST /admin/products", () => {
options: [{ title: "Denominations" }],
handle: "test-gift-card",
is_giftcard: true,
status: "draft",
profile_id: IdMap.getId("giftCardProfile"),
})
})
@@ -193,6 +193,9 @@ export default async (req, res) => {
.optional(),
thumbnail: Validator.string().optional(),
handle: Validator.string().optional(),
status: Validator.string()
.valid("proposed", "draft", "published", "rejected")
.default("draft"),
type: Validator.object()
.keys({
id: Validator.string().optional(),
@@ -328,6 +331,8 @@ export default async (req, res) => {
.create({ ...value, profile_id: shippingProfile.id })
if (variants) {
for (const [i, variant] of variants.entries()) variant.variant_rank = i
const optionIds = value.options.map(
o => newProduct.options.find(newO => newO.title === o.title).id
)
@@ -341,6 +346,7 @@ export default async (req, res) => {
option_id: optionIds[index],
})),
}
await productVariantService
.withTransaction(manager)
.create(newProduct.id, variant)
@@ -1,4 +1,5 @@
import _ from "lodash"
import { MedusaError, Validator } from "medusa-core-utils"
import { defaultFields, defaultRelations } from "./"
/**
@@ -56,6 +57,20 @@ export default async (req, res) => {
selector.is_giftcard = req.query.is_giftcard === "true"
}
if ("status" in req.query) {
const schema = Validator.array()
.items(
Validator.string().valid("proposed", "draft", "published", "rejected")
)
.single()
const { value, error } = schema.validate(req.query.status)
if (value && !error) {
selector.status = value
}
}
const listConfig = {
select: includeFields.length ? includeFields : defaultFields,
relations: expandFields.length ? expandFields : defaultRelations,
@@ -193,6 +193,12 @@ export default async (req, res) => {
.allow(null, ""),
description: Validator.string().optional(),
discountable: Validator.boolean().optional(),
status: Validator.string().valid(
"proposed",
"draft",
"published",
"rejected"
),
type: Validator.object()
.keys({
id: Validator.string().optional(),
@@ -39,6 +39,7 @@ export default async (req, res) => {
const schema = Validator.object().keys({
value: Validator.string().required(),
label: Validator.string().required(),
parent_return_reason_id: Validator.string().optional(),
description: Validator.string()
.optional()
.allow(""),
@@ -0,0 +1,41 @@
/**
* @oas [delete] /return-reason/{id}
* operationId: "DeleteReturnReason"
* summary: "Delete a return reason"
* description: "Deletes a return reason."
* parameters:
* - (path) id=* {string} The id of the return reason
* tags:
* - Return Reason
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* id:
* type: string
* description: The id of the deleted return reason
* object:
* type: string
* description: The type of the object that was deleted.
* deleted:
* type: boolean
*/
export default async (req, res) => {
const { id } = req.params
try {
const returnReasonService = req.scope.resolve("returnReasonService")
await returnReasonService.delete(id)
res.json({
id: id,
object: "return_reason",
deleted: true,
})
} catch (err) {
throw err
}
}
@@ -26,6 +26,11 @@ export default app => {
*/
route.post("/:id", middlewares.wrap(require("./update-reason").default))
/**
* Delete a reason
*/
route.delete("/:id", middlewares.wrap(require("./delete-reason").default))
return app
}
@@ -33,10 +38,14 @@ export const defaultFields = [
"id",
"value",
"label",
"parent_return_reason_id",
"description",
"created_at",
"updated_at",
"deleted_at",
]
export const defaultRelations = []
export const defaultRelations = [
"parent_return_reason",
"return_reason_children",
]
@@ -24,7 +24,7 @@ export default async (req, res) => {
try {
const returnReasonService = req.scope.resolve("returnReasonService")
const query = {}
const query = { parent_return_reason_id: null }
const data = await returnReasonService.list(query, {
select: defaultFields,
relations: defaultRelations,
@@ -42,6 +42,7 @@ export default async (req, res) => {
const schema = Validator.object().keys({
label: Validator.string().optional(),
parent_return_reason_id: Validator.string().optional(),
description: Validator.string()
.optional()
.allow(""),
@@ -0,0 +1,50 @@
import { result } from "lodash"
import { MedusaError, Validator } from "medusa-core-utils"
import { defaultFields, defaultRelations } from "../orders"
/**
* @oas [post] /returns/{id}/cancel
* operationId: "PostReturnsReturnCancel"
* summary: "Cancel a Return"
* description: "Registers a Return as canceled."
* parameters:
* - (path) id=* {string} The id of the Return.
* tags:
* - Return
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* return:
* $ref: "#/components/schemas/order"
*/
export default async (req, res) => {
const { id } = req.params
try {
const returnService = req.scope.resolve("returnService")
const orderService = req.scope.resolve("orderService")
let result = await returnService.cancel(id)
if (result.swap_id) {
const swapService = req.scope.resolve("swapService")
result = await swapService.retrieve(result.swap_id)
} else if (result.claim_order_id) {
const claimService = req.scope.resolve("claimService")
result = await claimService.retrieve(result.claim_order_id)
}
const order = await orderService.retrieve(result.order_id, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ order })
} catch (err) {
throw err
}
}
@@ -16,5 +16,10 @@ export default app => {
middlewares.wrap(require("./receive-return").default)
)
route.post(
"/:id/cancel",
middlewares.wrap(require("./cancel-return").default)
)
return app
}
@@ -1,7 +1,7 @@
import { MedusaError, Validator } from "medusa-core-utils"
/**
* @oas [post] /returns/{id}receive
* @oas [post] /returns/{id}/receive
* operationId: "PostReturnsReturnReceive"
* summary: "Receive a Return"
* description: "Registers a Return as received. Updates statuses on Orders and Swaps accordingly."
@@ -56,7 +56,7 @@ export default async (req, res) => {
requirements: Validator.array()
.items(
Validator.object({
id: Validator.string().required(),
id: Validator.string().optional(),
type: Validator.string().required(),
amount: Validator.number()
.integer()
@@ -72,7 +72,7 @@ describe("GET /admin/swaps/:id", () => {
it("returns swap", () => {
expect(subject.status).toEqual(200)
expect(subject.body.swap.id).toEqual(IdMap.getId("test-swap"))
expect(subject.body.swap.id).toEqual("test-swap")
})
})
})
@@ -78,7 +78,7 @@ describe("POST /store/carts/:id", () => {
})
it("returns the created order", () => {
expect(subject.body.data.id).toEqual(IdMap.getId("test-swap"))
expect(subject.body.data.id).toEqual("test-swap")
})
})
@@ -138,18 +138,36 @@ export default async (req, res) => {
// If cart is part of swap, we register swap as complete
switch (cart.type) {
case "swap": {
const swapId = cart.metadata?.swap_id
let swap = await swapService
.withTransaction(manager)
.registerCartCompletion(swapId)
try {
const swapId = cart.metadata?.swap_id
let swap = await swapService
.withTransaction(manager)
.registerCartCompletion(swapId)
swap = await swapService
.withTransaction(manager)
.retrieve(swap.id, { relations: ["shipping_address"] })
swap = await swapService
.withTransaction(manager)
.retrieve(swap.id, { relations: ["shipping_address"] })
return {
response_code: 200,
response_body: { data: swap, type: "swap" },
return {
response_code: 200,
response_body: { data: swap, type: "swap" },
}
} catch (error) {
if (
error &&
error.code === MedusaError.Codes.INSUFFICIENT_INVENTORY
) {
return {
response_code: 409,
response_body: {
message: error.message,
type: error.type,
code: error.code,
},
}
} else {
throw error
}
}
}
// case "payment_link":
@@ -7,21 +7,18 @@ describe("POST /store/customers/:id", () => {
describe("successfully updates a customer", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/store/customers/${IdMap.getId("lebron")}`,
{
payload: {
first_name: "LeBron",
last_name: "James",
subject = await request("POST", `/store/customers/me`, {
payload: {
first_name: "LeBron",
last_name: "James",
email: "test@email.com",
},
clientSession: {
jwt: {
customer_id: IdMap.getId("lebron"),
},
clientSession: {
jwt: {
customer_id: IdMap.getId("lebron"),
},
},
}
)
},
})
})
afterAll(() => {
@@ -35,6 +32,7 @@ describe("POST /store/customers/:id", () => {
{
first_name: "LeBron",
last_name: "James",
email: "test@email.com",
}
)
})
@@ -59,20 +57,16 @@ describe("POST /store/customers/:id", () => {
describe("successfully updates a customer with billing address id", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/store/customers/${IdMap.getId("lebron")}`,
{
payload: {
billing_address: "test",
subject = await request("POST", `/store/customers/me`, {
payload: {
billing_address: "test",
},
clientSession: {
jwt: {
customer_id: IdMap.getId("lebron"),
},
clientSession: {
jwt: {
customer_id: IdMap.getId("lebron"),
},
},
}
)
},
})
})
afterAll(() => {
@@ -97,28 +91,24 @@ describe("POST /store/customers/:id", () => {
describe("successfully updates a customer with billing address object", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/store/customers/${IdMap.getId("lebron")}`,
{
payload: {
billing_address: {
first_name: "Olli",
last_name: "Juhl",
address_1: "Laksegade",
city: "Copenhagen",
country_code: "dk",
postal_code: "2100",
phone: "+1 (222) 333 4444",
},
subject = await request("POST", `/store/customers/me`, {
payload: {
billing_address: {
first_name: "Olli",
last_name: "Juhl",
address_1: "Laksegade",
city: "Copenhagen",
country_code: "dk",
postal_code: "2100",
phone: "+1 (222) 333 4444",
},
clientSession: {
jwt: {
customer_id: IdMap.getId("lebron"),
},
},
clientSession: {
jwt: {
customer_id: IdMap.getId("lebron"),
},
}
)
},
})
})
afterAll(() => {
@@ -147,33 +137,4 @@ describe("POST /store/customers/:id", () => {
expect(subject.status).toEqual(200)
})
})
describe("fails if not authenticated", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/store/customers/${IdMap.getId("customer1")}`,
{
payload: {
first_name: "LeBron",
last_name: "James",
},
clientSession: {
jwt: {
customer_id: IdMap.getId("lebron"),
},
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("status code 400", () => {
expect(subject.status).toEqual(400)
})
})
})
@@ -1,12 +0,0 @@
import { MedusaError } from "medusa-core-utils"
export default async (req, res, next, id) => {
if (!(req.user && req.user.customer_id === id)) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"You must be logged in to update"
)
} else {
next()
}
}
@@ -30,7 +30,7 @@ import { defaultRelations, defaultFields } from "./"
* $ref: "#/components/schemas/customer"
*/
export default async (req, res) => {
const { id } = req.params
const id = req.user.customer_id
const schema = Validator.object().keys({
address: Validator.address().required(),
@@ -21,7 +21,8 @@ import { defaultRelations, defaultFields } from "./"
* $ref: "#/components/schemas/customer"
*/
export default async (req, res) => {
const { id, address_id } = req.params
const id = req.user.customer_id
const { address_id } = req.params
const customerService = req.scope.resolve("customerService")
try {
@@ -1,12 +1,10 @@
import { defaultRelations, defaultFields } from "./"
/**
* @oas [get] /customers/{id}
* @oas [get] /customers/me
* operationId: GetCustomersCustomer
* summary: Retrieves a Customer
* description: "Retrieves a Customer - the Customer must be logged in to retrieve their details."
* parameters:
* - (path) id=* {string} The id of the Customer.
* tags:
* - Customer
* responses:
@@ -20,7 +18,7 @@ import { defaultRelations, defaultFields } from "./"
* $ref: "#/components/schemas/customer"
*/
export default async (req, res) => {
const { id } = req.params
const id = req.user.customer_id
try {
const customerService = req.scope.resolve("customerService")
const customer = await customerService.retrieve(id, {
@@ -1,5 +1,5 @@
/**
* @oas [get] /customers/{id}/payment-methods
* @oas [get] /customers/me/payment-methods
* operationId: GetCustomersCustomerPaymentMethods
* summary: Retrieve saved payment methods
* description: "Retrieves a list of a Customer's saved payment methods. Payment methods are saved with Payment Providers and it is their responsibility to fetch saved methods."
@@ -26,7 +26,7 @@
* description: The data needed for the Payment Provider to use the saved payment method.
*/
export default async (req, res) => {
const { id } = req.params
const id = req.user.customer_id
try {
const storeService = req.scope.resolve("storeService")
const paymentProviderService = req.scope.resolve("paymentProviderService")
@@ -37,11 +37,11 @@ export default async (req, res) => {
const store = await storeService.retrieve(["payment_providers"])
const methods = await Promise.all(
store.payment_providers.map(async next => {
store.payment_providers.map(async (next) => {
const provider = paymentProviderService.retrieveProvider(next)
const pMethods = await provider.retrieveSavedMethods(customer)
return pMethods.map(m => ({
return pMethods.map((m) => ({
provider_id: next,
data: m,
}))
@@ -7,7 +7,6 @@ export default (app, container) => {
const middlewareService = container.resolve("middlewareService")
app.use("/customers", route)
route.param("id", middlewares.wrap(require("./authorize-customer").default))
// Inject plugin routes
const routers = middlewareService.getRouters("store/customers")
@@ -30,28 +29,28 @@ export default (app, container) => {
// Authenticated endpoints
route.use(middlewares.authenticate())
route.get("/:id", middlewares.wrap(require("./get-customer").default))
route.post("/:id", middlewares.wrap(require("./update-customer").default))
route.get("/me", middlewares.wrap(require("./get-customer").default))
route.post("/me", middlewares.wrap(require("./update-customer").default))
route.get("/:id/orders", middlewares.wrap(require("./list-orders").default))
route.get("/me/orders", middlewares.wrap(require("./list-orders").default))
route.post(
"/:id/addresses",
"/me/addresses",
middlewares.wrap(require("./create-address").default)
)
route.post(
"/:id/addresses/:address_id",
"/me/addresses/:address_id",
middlewares.wrap(require("./update-address").default)
)
route.delete(
"/:id/addresses/:address_id",
"/me/addresses/:address_id",
middlewares.wrap(require("./delete-address").default)
)
route.get(
"/:id/payment-methods",
"/me/payment-methods",
middlewares.wrap(require("./get-payment-methods").default)
)
@@ -7,7 +7,7 @@ import {
} from "../orders"
/**
* @oas [get] /customers/{id}/orders
* @oas [get] /customers/me/orders
* operationId: GetCustomersCustomerOrders
* summary: Retrieve Customer Orders
* description: "Retrieves a list of a Customer's Orders."
@@ -28,7 +28,7 @@ import {
* $ref: "#/components/schemas/order"
*/
export default async (req, res) => {
const { id } = req.params
const id = req.user.customer_id
try {
const orderService = req.scope.resolve("orderService")
@@ -42,13 +42,13 @@ export default async (req, res) => {
let includeFields = []
if ("fields" in req.query) {
includeFields = req.query.fields.split(",")
includeFields = includeFields.filter(f => allowedFields.includes(f))
includeFields = includeFields.filter((f) => allowedFields.includes(f))
}
let expandFields = []
if ("expand" in req.query) {
expandFields = req.query.expand.split(",")
expandFields = expandFields.filter(f => allowedRelations.includes(f))
expandFields = expandFields.filter((f) => allowedRelations.includes(f))
}
const listConfig = {
@@ -1,12 +1,10 @@
import { MedusaError, Validator } from "medusa-core-utils"
/**
* @oas [post] /customers/{id}/password-token
* @oas [post] /customers/password-token
* operationId: PostCustomersCustomerPasswordToken
* summary: Creates a reset password token
* description: "Creates a reset password token to be used in a subsequent /reset-password request. The password token should be sent out of band e.g. via email and will not be returned."
* parameters:
* - (path) id=* {string} The id of the Customer.
* tags:
* - Customer
* responses:
@@ -15,9 +13,7 @@ import { MedusaError, Validator } from "medusa-core-utils"
*/
export default async (req, res) => {
const schema = Validator.object().keys({
email: Validator.string()
.email()
.required(),
email: Validator.string().email().required(),
})
const { value, error } = schema.validate(req.body)
@@ -2,12 +2,11 @@ import { MedusaError, Validator } from "medusa-core-utils"
import jwt from "jsonwebtoken"
/**
* @oas [post] /customers/{id}/reset-password
* operationId: PostCustomersCustomerResetPassword
* @oas [post] /customers/reset-password
* operationId: PostCustomersResetPassword
* summary: Resets Customer password
* description: "Resets a Customer's password using a password token created by a previous /password-token request."
* parameters:
* - (path) id=* {string} The id of the Customer.
* - (body) email=* {string} The Customer's email.
* - (body) token=* {string} The password token created by a /password-token request.
* - (body) password=* {string} The new password to set for the Customer.
@@ -25,9 +24,7 @@ import jwt from "jsonwebtoken"
*/
export default async (req, res) => {
const schema = Validator.object().keys({
email: Validator.string()
.email()
.required(),
email: Validator.string().email().required(),
token: Validator.string().required(),
password: Validator.string().required(),
})
@@ -2,12 +2,11 @@ import { Validator, MedusaError } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "./"
/**
* @oas [post] /customers/{id}/addresses/{address_id}
* @oas [post] /customers/me/addresses/{address_id}
* operationId: PostCustomersCustomerAddressesAddress
* summary: "Update a Shipping Address"
* description: "Updates a Customer's saved Shipping Address."
* parameters:
* - (path) id=* {String} The Customer id.
* - (path) address_id=* {String} The id of the Address to update.
* requestBody:
* content:
@@ -31,7 +30,8 @@ import { defaultRelations, defaultFields } from "./"
* $ref: "#/components/schemas/customer"
*/
export default async (req, res) => {
const { id, address_id } = req.params
const id = req.user.customer_id
const { address_id } = req.params
const schema = Validator.object().keys({
address: Validator.address().required(),
@@ -1,13 +1,12 @@
import { optional } from "joi"
import { Validator, MedusaError } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "./"
/**
* @oas [post] /customers/{id}
* @oas [post] /customers/me
* operationId: PostCustomersCustomer
* summary: Update Customer details
* description: "Updates a Customer's saved details."
* parameters:
* - (path) id=* {string} The id of the Customer.
* requestBody:
* content:
* application/json:
@@ -29,6 +28,9 @@ import { defaultRelations, defaultFields } from "./"
* phone:
* description: "The Customer's phone number."
* type: string
* email:
* description: "The email of the customer."
* type: string
* metadata:
* description: "Metadata about the customer."
* type: object
@@ -45,7 +47,7 @@ import { defaultRelations, defaultFields } from "./"
* $ref: "#/components/schemas/customer"
*/
export default async (req, res) => {
const { id } = req.params
const id = req.user.customer_id
const schema = Validator.object().keys({
billing_address: Validator.address().optional(),
@@ -53,6 +55,7 @@ export default async (req, res) => {
last_name: Validator.string().optional(),
password: Validator.string().optional(),
phone: Validator.string().optional(),
email: Validator.string().optional(),
metadata: Validator.object().optional(),
})
@@ -1,6 +1,8 @@
import { request } from "../../../../../helpers/test-request"
import { IdMap } from "medusa-test-utils"
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
import { defaultRelations } from ".."
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
describe("Get product by id", () => {
describe("get product by id successfull", () => {
@@ -20,7 +22,7 @@ describe("Get product by id", () => {
expect(ProductServiceMock.retrieve).toHaveBeenCalledTimes(1)
expect(ProductServiceMock.retrieve).toHaveBeenCalledWith(
IdMap.getId("product1"),
{ relations: ["images", "variants", "options"] }
{ relations: defaultRelations }
)
})
@@ -28,4 +30,37 @@ describe("Get product by id", () => {
expect(subject.body.product.id).toEqual(IdMap.getId("product1"))
})
})
describe("Query products with relations", () => {
let subject
beforeAll(async () => {
subject = await request(
"GET",
`/store/products/${IdMap.getId("variantsWithPrices")}`
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls retrieve() once", () => {
expect(ProductServiceMock.retrieve).toHaveBeenCalledTimes(1)
})
it("endpoint called with defaultRelations", () => {
expect(ProductServiceMock.retrieve).toHaveBeenCalledWith(
IdMap.getId("variantsWithPrices"),
{ relations: defaultRelations }
)
})
it("returns product with variant prices", () => {
expect(
subject.body.product.variants.some(variant => variant.prices)
).toEqual(true)
expect(subject.body.product.variants[0].prices[0].amount).toEqual(100)
})
})
})
@@ -1,4 +1,5 @@
import { IdMap } from "medusa-test-utils"
import { defaultRelations } from ".."
import { request } from "../../../../../helpers/test-request"
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
@@ -17,8 +18,8 @@ describe("GET /store/products", () => {
it("calls get product from productSerice", () => {
expect(ProductServiceMock.list).toHaveBeenCalledTimes(1)
expect(ProductServiceMock.list).toHaveBeenCalledWith(
{},
{ relations: ["variants", "options", "images"], skip: 0, take: 100 }
{ status: ["published"] },
{ relations: defaultRelations, skip: 0, take: 100 }
)
})
@@ -42,8 +43,8 @@ describe("GET /store/products", () => {
it("calls list from productSerice", () => {
expect(ProductServiceMock.list).toHaveBeenCalledTimes(1)
expect(ProductServiceMock.list).toHaveBeenCalledWith(
{ is_giftcard: true },
{ relations: ["variants", "options", "images"], skip: 0, take: 100 }
{ is_giftcard: true, status: ["published"] },
{ relations: defaultRelations, skip: 0, take: 100 }
)
})
})
@@ -1,3 +1,5 @@
import { defaultRelations } from "."
/**
* @oas [get] /products/{id}
* operationId: GetProductsProduct
@@ -22,7 +24,7 @@ export default async (req, res) => {
const productService = req.scope.resolve("productService")
let product = await productService.retrieve(id, {
relations: ["images", "variants", "options"],
relations: defaultRelations,
})
res.json({ product })
@@ -12,3 +12,14 @@ export default app => {
return app
}
export const defaultRelations = [
"variants",
"variants.prices",
"options",
"options.values",
"images",
"tags",
"collection",
"type",
]
@@ -1,3 +1,6 @@
import { MedusaError, Validator } from "medusa-core-utils"
import { defaultRelations } from "."
/**
* @oas [get] /products
* operationId: GetProducts
@@ -39,8 +42,10 @@ export default async (req, res) => {
selector.is_giftcard = req.query.is_giftcard === "true"
}
selector.status = ["published"]
const listConfig = {
relations: ["variants", "options", "images"],
relations: defaultRelations,
skip: offset,
take: limit,
}
@@ -23,10 +23,14 @@ export const defaultFields = [
"id",
"value",
"label",
"parent_return_reason_id",
"description",
"created_at",
"updated_at",
"deleted_at",
]
export const defaultRelations = []
export const defaultRelations = [
"parent_return_reason",
"return_reason_children",
]
@@ -24,7 +24,7 @@ export default async (req, res) => {
try {
const returnReasonService = req.scope.resolve("returnReasonService")
const query = {}
const query = { parent_return_reason_id: null}
const data = await returnReasonService.list(query, {
select: defaultFields,
relations: defaultRelations,
@@ -3,7 +3,7 @@ import middlewares from "../../../middlewares"
const route = Router()
export default (app) => {
export default app => {
app.use("/swaps", route)
route.get(
@@ -19,6 +19,7 @@ export const defaultRelations = [
"order",
"additional_items",
"return_order",
"return_order.shipping_method",
"fulfillments",
"payment",
"shipping_address",
@@ -1,3 +1,4 @@
import { IdMap } from "../../../../../../../medusa-test-utils/dist"
import { request } from "../../../../../helpers/test-request"
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
@@ -23,4 +24,15 @@ describe("Get variant by id", () => {
expect(subject.body.variant.id).toEqual("1")
})
})
describe("get variant with prices", () => {
let subject
beforeAll(async () => {
subject = await request("GET", `/store/variants/variant_with_prices`)
})
it("successfully retrieves variants with prices", async () => {
expect(subject.status).toEqual(200)
expect(subject.body.variant.prices[0].amount).toEqual(100)
})
})
})
@@ -1,3 +1,5 @@
import { defaultRelations } from "."
/**
* @oas [get] /variants/{variant_id}
* operationId: GetVariantsVariant
@@ -22,7 +24,10 @@ export default async (req, res) => {
try {
const variantService = req.scope.resolve("productVariantService")
let variant = await variantService.retrieve(id, { relations: ["prices"] })
let variant = await variantService.retrieve(id, {
relations: defaultRelations,
})
res.json({ variant })
} catch (error) {
throw error
@@ -11,3 +11,5 @@ export default app => {
return app
}
export const defaultRelations = ["prices"]
@@ -1,3 +1,5 @@
import { defaultRelations } from "."
/**
* @oas [get] /variants
* operationId: GetVariants
@@ -23,10 +25,14 @@ export default async (req, res) => {
const limit = parseInt(req.query.limit) || 100
const offset = parseInt(req.query.offset) || 0
let selector = {}
let expandFields = []
if ("expand" in req.query) {
expandFields = req.query.expand.split(",")
}
let selector = {}
const listConfig = {
relations: [],
relations: expandFields.length ? expandFields : defaultRelations,
skip: offset,
take: limit,
}