Merge remote-tracking branch 'origin/develop' into release/next

This commit is contained in:
Sebastian Rindom
2021-03-16 13:58:18 +01:00
51 changed files with 7273 additions and 42 deletions
@@ -11,6 +11,7 @@ import shippingProfileRoutes from "./shipping-profiles"
import discountRoutes from "./discounts"
import giftCardRoutes from "./gift-cards"
import orderRoutes from "./orders"
import returnReasonRoutes from "./return-reasons"
import storeRoutes from "./store"
import uploadRoutes from "./uploads"
import customerRoutes from "./customers"
@@ -64,6 +65,7 @@ export default (app, container, config) => {
variantRoutes(route)
collectionRoutes(route)
notificationRoutes(route)
returnReasonRoutes(route)
return app
}
@@ -13,6 +13,8 @@ const defaultRelations = [
"fulfillments.tracking_links",
"fulfillments.items",
"returns",
"returns.items",
"returns.items.reason",
"gift_cards",
"gift_card_transactions",
"claims",
@@ -195,6 +195,8 @@ export const defaultRelations = [
"fulfillments.tracking_links",
"fulfillments.items",
"returns",
"returns.items",
"returns.items.reason",
"gift_cards",
"gift_card_transactions",
"claims",
@@ -21,6 +21,12 @@ import { defaultRelations, defaultFields } from "./"
* item_id:
* description: The id of the Line Item.
* type: string
* reason_id:
* description: The id of the Return Reason to use.
* type: string
* note:
* description: An optional note with information about the Return.
* type: string
* quantity:
* description: The quantity of the Line Item.
* type: integer
@@ -60,6 +66,8 @@ export default async (req, res) => {
.items({
item_id: Validator.string().required(),
quantity: Validator.number().required(),
reason_id: Validator.string().optional(),
note: Validator.string().optional(),
})
.required(),
return_shipping: Validator.object()
@@ -0,0 +1,66 @@
import { MedusaError, Validator } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "./"
/**
* @oas [post] /return-reasons
* operationId: "PostReturnReasons"
* summary: "Create a Return Reason"
* description: "Creates a Return Reason"
* requestBody:
* content:
* application/json:
* schema:
* properties:
* label:
* description: "The label to display to the Customer."
* type: string
* value:
* description: "The value that the Return Reason will be identified by. Must be unique."
* type: string
* description:
* description: "An optional description to for the Reason."
* type: string
* metadata:
* description: An optional set of key-value pairs with additional information.
* type: object
* tags:
* - Return Reason
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* return_reason:
* $ref: "#/components/schemas/return_reason"
*/
export default async (req, res) => {
const schema = Validator.object().keys({
value: Validator.string().required(),
label: Validator.string().required(),
description: Validator.string()
.optional()
.allow(""),
metadata: Validator.object().optional(),
})
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const returnReasonService = req.scope.resolve("returnReasonService")
const result = await returnReasonService.create(value)
const reason = await returnReasonService.retrieve(result.id, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ return_reason: reason })
} catch (err) {
throw err
}
}
@@ -0,0 +1,37 @@
import { MedusaError, Validator } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "./"
/**
* @oas [get] /return-reasons/{id}
* operationId: "GetReturnReasonsReason"
* summary: "Retrieve a Return Reason"
* description: "Retrieves 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:
* return_reason:
* $ref: "#/components/schemas/return_reason"
*/
export default async (req, res) => {
const { id } = req.params
try {
const returnReasonService = req.scope.resolve("returnReasonService")
const data = await returnReasonService.retrieve(id, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ return_reason: data })
} catch (err) {
throw err
}
}
@@ -0,0 +1,42 @@
import { Router } from "express"
import middlewares from "../../../middlewares"
const route = Router()
export default app => {
app.use("/return-reasons", route)
/**
* List reasons
*/
route.get("/", middlewares.wrap(require("./list-reasons").default))
/**
* Retrieve reason
*/
route.get("/:id", middlewares.wrap(require("./get-reason").default))
/**
* Create a reason
*/
route.post("/", middlewares.wrap(require("./create-reason").default))
/**
* Update a reason
*/
route.post("/:id", middlewares.wrap(require("./update-reason").default))
return app
}
export const defaultFields = [
"id",
"value",
"label",
"description",
"created_at",
"updated_at",
"deleted_at",
]
export const defaultRelations = []
@@ -0,0 +1,37 @@
import { MedusaError, Validator } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "./"
/**
* @oas [get] /return-reasons
* operationId: "GetReturnReasons"
* summary: "List Return Reasons"
* description: "Retrieves a list of Return Reasons."
* tags:
* - Return Reason
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* return_reasons:
* type: array
* items:
* $ref: "#/components/schemas/return_reason"
*/
export default async (req, res) => {
try {
const returnReasonService = req.scope.resolve("returnReasonService")
const query = {}
const data = await returnReasonService.list(query, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ return_reasons: data })
} catch (err) {
throw err
}
}
@@ -0,0 +1,70 @@
import { MedusaError, Validator } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "./"
/**
* @oas [post] /return-reasons/:id
* operationId: "PostReturnReasonsReason"
* summary: "Update a Return Reason"
* description: "Updates a Return Reason"
* parameters:
* - (path) id=* {string} The id of the Return Reason.
* requestBody:
* content:
* application/json:
* schema:
* properties:
* label:
* description: "The label to display to the Customer."
* type: string
* value:
* description: "The value that the Return Reason will be identified by. Must be unique."
* type: string
* description:
* description: "An optional description to for the Reason."
* type: string
* metadata:
* description: An optional set of key-value pairs with additional information.
* type: object
* tags:
* - Return Reason
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* return_reason:
* $ref: "#/components/schemas/return_reason"
*/
export default async (req, res) => {
const { id } = req.params
const schema = Validator.object().keys({
label: Validator.string().optional(),
description: Validator.string()
.optional()
.allow(""),
metadata: Validator.object().optional(),
})
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const returnReasonService = req.scope.resolve("returnReasonService")
await returnReasonService.update(id, value)
const reason = await returnReasonService.retrieve(id, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ return_reason: reason })
} catch (err) {
throw err
}
}
@@ -11,6 +11,7 @@ import customerRoutes from "./customers"
import shippingOptionRoutes from "./shipping-options"
import regionRoutes from "./regions"
import returnRoutes from "./returns"
import returnReasonRoutes from "./return-reasons"
import swapRoutes from "./swaps"
import variantRoutes from "./variants"
import giftCardRoutes from "./gift-cards"
@@ -41,6 +42,7 @@ export default (app, container, config) => {
variantRoutes(route)
returnRoutes(route)
giftCardRoutes(route)
returnReasonRoutes(route)
return app
}
@@ -0,0 +1,37 @@
import { MedusaError, Validator } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "./"
/**
* @oas [get] /return-reasons/{id}
* operationId: "GetReturnReasonsReason"
* summary: "Retrieve a Return Reason"
* description: "Retrieves 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:
* return_reason:
* $ref: "#/components/schemas/return_reason"
*/
export default async (req, res) => {
const { id } = req.params
try {
const returnReasonService = req.scope.resolve("returnReasonService")
const data = await returnReasonService.retrieve(id, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ return_reason: data })
} catch (err) {
throw err
}
}
@@ -0,0 +1,32 @@
import { Router } from "express"
import middlewares from "../../../middlewares"
const route = Router()
export default app => {
app.use("/return-reasons", route)
/**
* List reasons
*/
route.get("/", middlewares.wrap(require("./list-reasons").default))
/**
* Retrieve reason
*/
route.get("/:id", middlewares.wrap(require("./get-reason").default))
return app
}
export const defaultFields = [
"id",
"value",
"label",
"description",
"created_at",
"updated_at",
"deleted_at",
]
export const defaultRelations = []
@@ -0,0 +1,37 @@
import { MedusaError, Validator } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "./"
/**
* @oas [get] /return-reasons
* operationId: "GetReturnReasons"
* summary: "List Return Reasons"
* description: "Retrieves a list of Return Reasons."
* tags:
* - Return Reason
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* return_reasons:
* type: array
* items:
* $ref: "#/components/schemas/return_reason"
*/
export default async (req, res) => {
try {
const returnReasonService = req.scope.resolve("returnReasonService")
const query = {}
const data = await returnReasonService.list(query, {
select: defaultFields,
relations: defaultRelations,
})
res.status(200).json({ return_reasons: data })
} catch (err) {
throw err
}
}
@@ -50,6 +50,8 @@ export default async (req, res) => {
.items({
item_id: Validator.string().required(),
quantity: Validator.number().required(),
reason_id: Validator.string().optional(),
note: Validator.string().optional(),
})
.required(),
return_shipping: Validator.object()
@@ -151,13 +153,14 @@ export default async (req, res) => {
const { key, error } = await idempotencyKeyService.workStage(
idempotencyKey.idempotency_key,
async manager => {
let order = await orderService
.withTransaction(manager)
.retrieve(value.order_id, { relations: ["returns"] })
let ret = await returnService.withTransaction(manager).list({
idempotency_key: idempotencyKey.idempotency_key,
})
let ret = await returnService.withTransaction(manager).list(
{
idempotency_key: idempotencyKey.idempotency_key,
},
{
relations: ["items", "items.reason"],
}
)
if (!ret.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,