feat: Add HTTP endpoints and workflows for price preference management (#7960)
REF CORE-2376 Remaining pieces are adding UI to manage the flag, showing the flag in price editor, plugging it in cart calculations, and https://github.com/medusajs/medusa/pull/7827
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { getOrdersListlWorkflow } from "@medusajs/core-flows"
|
||||
import { getOrdersListWorkflow } from "@medusajs/core-flows"
|
||||
import { OrderDTO } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
@@ -17,7 +17,7 @@ export const GET = async (
|
||||
...req.remoteQueryConfig.pagination,
|
||||
}
|
||||
|
||||
const workflow = getOrdersListlWorkflow(req.scope)
|
||||
const workflow = getOrdersListWorkflow(req.scope)
|
||||
const { result } = await workflow.run({
|
||||
input: {
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import {
|
||||
deletePricePreferencesWorkflow,
|
||||
updatePricePreferencesWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
import { refetchEntity } from "../../../utils/refetch-entity"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const price_preference = await refetchEntity(
|
||||
"price_preference",
|
||||
req.params.id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json({ price_preference })
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<HttpTypes.AdminUpdatePricePreference>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = updatePricePreferencesWorkflow(req.scope)
|
||||
|
||||
await workflow.run({
|
||||
input: { selector: { id: [id] }, update: req.body },
|
||||
})
|
||||
|
||||
const price_preference = await refetchEntity(
|
||||
"price_preference",
|
||||
id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json({ price_preference })
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = deletePricePreferencesWorkflow(req.scope)
|
||||
|
||||
await workflow.run({
|
||||
input: [id],
|
||||
})
|
||||
|
||||
res.status(200).json({
|
||||
id,
|
||||
object: "price_preference",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
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 {
|
||||
AdminCreatePricePreference,
|
||||
AdminGetPricePreferenceParams,
|
||||
AdminGetPricePreferencesParams,
|
||||
AdminUpdatePricePreference,
|
||||
} from "./validators"
|
||||
|
||||
export const adminPricePreferencesRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/price-preferences",
|
||||
middlewares: [
|
||||
validateAndTransformQuery(
|
||||
AdminGetPricePreferencesParams,
|
||||
QueryConfig.listPricePreferenceQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/price-preferences/:id",
|
||||
middlewares: [
|
||||
validateAndTransformQuery(
|
||||
AdminGetPricePreferenceParams,
|
||||
QueryConfig.retrivePricePreferenceQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/price-preferences",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminCreatePricePreference),
|
||||
validateAndTransformQuery(
|
||||
AdminGetPricePreferenceParams,
|
||||
QueryConfig.retrivePricePreferenceQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/price-preferences/:id",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminUpdatePricePreference),
|
||||
validateAndTransformQuery(
|
||||
AdminGetPricePreferenceParams,
|
||||
QueryConfig.retrivePricePreferenceQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,19 @@
|
||||
export const adminPricePreferenceRemoteQueryFields = [
|
||||
"id",
|
||||
"attribute",
|
||||
"value",
|
||||
"is_tax_inclusive",
|
||||
"created_at",
|
||||
"deleted_at",
|
||||
"updated_at",
|
||||
]
|
||||
|
||||
export const retrivePricePreferenceQueryConfig = {
|
||||
defaults: adminPricePreferenceRemoteQueryFields,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listPricePreferenceQueryConfig = {
|
||||
...retrivePricePreferenceQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { refetchEntities, refetchEntity } from "../../utils/refetch-entity"
|
||||
import { createPricePreferencesWorkflow } from "@medusajs/core-flows"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { rows: price_preferences, metadata } = await refetchEntities(
|
||||
"price_preference",
|
||||
req.filterableFields,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields,
|
||||
req.remoteQueryConfig.pagination
|
||||
)
|
||||
res.json({
|
||||
price_preferences: price_preferences,
|
||||
count: metadata.count,
|
||||
offset: metadata.skip,
|
||||
limit: metadata.take,
|
||||
})
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<HttpTypes.AdminCreatePricePreference>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const workflow = createPricePreferencesWorkflow(req.scope)
|
||||
const { result } = await workflow.run({
|
||||
input: [req.validatedBody],
|
||||
})
|
||||
|
||||
const price_preference = await refetchEntity(
|
||||
"price_preference",
|
||||
result[0].id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json({ price_preference })
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { z } from "zod"
|
||||
import { createFindParams, createSelectParams } from "../../utils/validators"
|
||||
|
||||
export const AdminGetPricePreferenceParams = createSelectParams()
|
||||
export const AdminGetPricePreferencesParams = createFindParams({
|
||||
offset: 0,
|
||||
limit: 50,
|
||||
}).merge(
|
||||
z.object({
|
||||
q: z.string().optional(),
|
||||
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
attribute: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
value: z.union([z.string(), z.array(z.string())]).optional(),
|
||||
$and: z.lazy(() => AdminGetPricePreferencesParams.array()).optional(),
|
||||
$or: z.lazy(() => AdminGetPricePreferencesParams.array()).optional(),
|
||||
})
|
||||
)
|
||||
|
||||
export const AdminCreatePricePreference = z.object({
|
||||
attribute: z.string(),
|
||||
value: z.string(),
|
||||
is_tax_inclusive: z.boolean().optional(),
|
||||
})
|
||||
|
||||
export type AdminCreatePricePreferencePriceType = z.infer<
|
||||
typeof AdminCreatePricePreference
|
||||
>
|
||||
|
||||
export const AdminUpdatePricePreference = z.object({
|
||||
attribute: z.string().optional(),
|
||||
value: z.string().optional(),
|
||||
is_tax_inclusive: z.boolean().optional(),
|
||||
})
|
||||
|
||||
export type AdminUpdatePricePreferenceType = z.infer<
|
||||
typeof AdminUpdatePricePreference
|
||||
>
|
||||
@@ -14,6 +14,7 @@ import { adminInviteRoutesMiddlewares } from "./admin/invites/middlewares"
|
||||
import { adminOrderRoutesMiddlewares } from "./admin/orders/middlewares"
|
||||
import { adminPaymentRoutesMiddlewares } from "./admin/payments/middlewares"
|
||||
import { adminPriceListsRoutesMiddlewares } from "./admin/price-lists/middlewares"
|
||||
import { adminPricePreferencesRoutesMiddlewares } from "./admin/price-preferences/middlewares"
|
||||
import { adminProductCategoryRoutesMiddlewares } from "./admin/product-categories/middlewares"
|
||||
import { adminProductTypeRoutesMiddlewares } from "./admin/product-types/middlewares"
|
||||
import { adminProductTagRoutesMiddlewares } from "./admin/product-tags/middlewares"
|
||||
@@ -78,6 +79,7 @@ export const config: MiddlewaresConfig = {
|
||||
...adminProductRoutesMiddlewares,
|
||||
...adminPaymentRoutesMiddlewares,
|
||||
...adminPriceListsRoutesMiddlewares,
|
||||
...adminPricePreferencesRoutesMiddlewares,
|
||||
...adminInventoryRoutesMiddlewares,
|
||||
...adminCollectionRoutesMiddlewares,
|
||||
...adminShippingOptionRoutesMiddlewares,
|
||||
|
||||
Reference in New Issue
Block a user