Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45e2228
commit a0cf899
Showing
12 changed files
with
540 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types" | ||
import { validateAndTransformBody } from "../../utils/validate-body" | ||
import { validateAndTransformQuery } from "../../utils/validate-query" | ||
import * as QueryConfig from "./query-config" | ||
import { ReturnsParams, StorePostReturnsReqSchema } from "./validators" | ||
|
||
export const storeRegionRoutesMiddlewares: MiddlewareRoute[] = [ | ||
{ | ||
method: ["POST"], | ||
matcher: "/store/returns/create-return", | ||
middlewares: [ | ||
validateAndTransformBody(StorePostReturnsReqSchema), | ||
validateAndTransformQuery( | ||
ReturnsParams, | ||
QueryConfig.retrieveTransformQueryConfig | ||
), | ||
], | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const defaultReturnFields = [ | ||
"id", | ||
"order_id", | ||
"created_at", | ||
"updated_at", | ||
"deleted_at", | ||
"metadata", | ||
] | ||
|
||
export const retrieveTransformQueryConfig = { | ||
defaults: defaultReturnFields, | ||
isList: false, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { CreateOrderReturnDTO } from "@medusajs/types" | ||
import { MedusaRequest, MedusaResponse } from "../../../types/routing" | ||
|
||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { | ||
const input = [req.validatedBody as CreateOrderReturnDTO] | ||
|
||
const { result, errors } = await createReturnsWorkflow(req.scope).run({ | ||
input: { products: input }, | ||
throwOnError: false, | ||
}) | ||
|
||
if (Array.isArray(errors) && errors[0]) { | ||
throw errors[0].error | ||
} | ||
|
||
res.status(200).json({ return: result }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { z } from "zod" | ||
import { createFindParams, createSelectParams } from "../../utils/validators" | ||
|
||
export type ReturnParamsType = z.infer<typeof ReturnParams> | ||
export const ReturnParams = createSelectParams() | ||
|
||
export type ReturnsParamsType = z.infer<typeof ReturnsParams> | ||
export const ReturnsParams = createFindParams().merge( | ||
z.object({ | ||
id: z.union([z.string(), z.array(z.string())]).optional(), | ||
order_id: z.union([z.string(), z.array(z.string())]).optional(), | ||
$and: z.lazy(() => ReturnsParams.array()).optional(), | ||
$or: z.lazy(() => ReturnsParams.array()).optional(), | ||
}) | ||
) | ||
|
||
const ReturnShippingSchema = z.object({ | ||
option_id: z.string(), | ||
}) | ||
|
||
const ItemSchema = z.object({ | ||
item_id: z.string(), | ||
quantity: z.number().min(1), | ||
reason_id: z.string().optional(), | ||
note: z.string().optional(), | ||
}) | ||
|
||
export const StorePostReturnsReqSchema = z.object({ | ||
order_id: z.string(), | ||
items: z.array(ItemSchema), | ||
return_shipping: ReturnShippingSchema.optional(), | ||
}) |
96 changes: 96 additions & 0 deletions
96
packages/modules/order/integration-tests/__tests__/returns.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Modules } from "@medusajs/modules-sdk" | ||
import { IOrderModuleService } from "@medusajs/types" | ||
import { SuiteOptions, moduleIntegrationTestRunner } from "medusa-test-utils" | ||
|
||
jest.setTimeout(100000) | ||
|
||
moduleIntegrationTestRunner({ | ||
moduleName: Modules.ORDER, | ||
testSuite: ({ service }: SuiteOptions<IOrderModuleService>) => { | ||
describe("Order Module Service - Returns", () => { | ||
it("should create return reasons", async function () { | ||
const reason = await service.createReturnReason({ | ||
value: "test", | ||
label: "label test", | ||
description: "description test", | ||
}) | ||
|
||
expect(reason).toEqual({ | ||
id: expect.any(String), | ||
value: "test", | ||
label: "label test", | ||
description: "description test", | ||
return_reason_children: [], | ||
metadata: null, | ||
created_at: expect.any(Date), | ||
updated_at: expect.any(Date), | ||
deleted_at: null, | ||
}) | ||
}) | ||
|
||
it("should create return reasons with parent", async function () { | ||
const reason = await service.createReturnReason({ | ||
value: "test", | ||
label: "label test", | ||
description: "description test", | ||
}) | ||
|
||
const reason2 = await service.createReturnReason({ | ||
value: "test 2.0", | ||
label: "child", | ||
parent_return_reason_id: reason.id, | ||
}) | ||
const reason3 = await service.createReturnReason({ | ||
value: "test 3.0", | ||
label: "child 3", | ||
parent_return_reason_id: reason.id, | ||
}) | ||
|
||
const getChild = await service.retrieveReturnReason(reason2.id, { | ||
relations: ["parent_return_reason"], | ||
}) | ||
expect(getChild).toEqual( | ||
expect.objectContaining({ | ||
id: reason2.id, | ||
value: "test 2.0", | ||
label: "child", | ||
parent_return_reason_id: reason.id, | ||
parent_return_reason: expect.objectContaining({ | ||
id: reason.id, | ||
value: "test", | ||
label: "label test", | ||
description: "description test", | ||
parent_return_reason_id: null, | ||
}), | ||
}) | ||
) | ||
|
||
const getParent = await service.retrieveReturnReason(reason.id, { | ||
relations: ["return_reason_children"], | ||
}) | ||
expect(getParent).toEqual( | ||
expect.objectContaining({ | ||
id: reason.id, | ||
value: "test", | ||
label: "label test", | ||
description: "description test", | ||
return_reason_children: [ | ||
expect.objectContaining({ | ||
id: reason2.id, | ||
value: "test 2.0", | ||
label: "child", | ||
parent_return_reason_id: reason.id, | ||
}), | ||
expect.objectContaining({ | ||
id: reason3.id, | ||
value: "test 3.0", | ||
label: "child 3", | ||
parent_return_reason_id: reason.id, | ||
}), | ||
], | ||
}) | ||
) | ||
}) | ||
}) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.