feat: Convert several batch methods to the standardized format (#7116)

This commit is contained in:
Stevche Radevski
2024-04-22 16:35:10 +02:00
committed by GitHub
parent 0eb68541b8
commit 0df523594f
63 changed files with 614 additions and 919 deletions
@@ -1,83 +0,0 @@
import {
addSalesChannelsToApiKeyWorkflow,
removeSalesChannelsFromApiKeyWorkflow,
} from "@medusajs/core-flows"
import { ApiKeyType, MedusaError } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../types/routing"
import { AdminApiKeySalesChannelType } from "../../../validators"
import { BatchMethodRequest } from "@medusajs/types"
import { refetchApiKey } from "../../../helpers"
export const POST = async (
req: AuthenticatedMedusaRequest<
BatchMethodRequest<AdminApiKeySalesChannelType, AdminApiKeySalesChannelType>
>,
res: MedusaResponse
) => {
const { create, delete: toDelete } = req.validatedBody
const apiKey = await refetchApiKey(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
if (apiKey.type !== ApiKeyType.PUBLISHABLE) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Sales channels can only be associated with publishable API keys"
)
}
if (create?.length) {
const workflowInput = {
data: [
{
api_key_id: req.params.id,
sales_channel_ids: create ?? [],
},
],
}
const { errors } = await addSalesChannelsToApiKeyWorkflow(req.scope).run({
input: workflowInput,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
}
if (toDelete?.length) {
const workflowInput = {
data: [
{
api_key_id: req.params.id,
sales_channel_ids: toDelete,
},
],
}
const { errors } = await removeSalesChannelsFromApiKeyWorkflow(
req.scope
).run({
input: workflowInput,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
}
const newApiKey = await refetchApiKey(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ api_key: newApiKey })
}
@@ -0,0 +1,44 @@
import { ApiKeyType, MedusaError } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
import { refetchApiKey } from "../../helpers"
import { LinkMethodRequest } from "@medusajs/types/src"
import { linkSalesChannelsToApiKeyWorkflow } from "@medusajs/core-flows"
export const POST = async (
req: AuthenticatedMedusaRequest<LinkMethodRequest>,
res: MedusaResponse
) => {
const { add, remove } = req.validatedBody
const apiKey = await refetchApiKey(req.params.id, req.scope, ["id", "type"])
if (apiKey.type !== ApiKeyType.PUBLISHABLE) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Sales channels can only be associated with publishable API keys"
)
}
const { errors } = await linkSalesChannelsToApiKeyWorkflow(req.scope).run({
input: {
id: req.params.id,
add,
remove,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const updatedApiKey = await refetchApiKey(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ api_key: updatedApiKey })
}
@@ -4,7 +4,6 @@ import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
import { validateAndTransformQuery } from "../../utils/validate-query"
import {
AdminApiKeySalesChannel,
AdminCreateApiKey,
AdminGetApiKeyParams,
AdminGetApiKeysParams,
@@ -12,7 +11,7 @@ import {
AdminUpdateApiKey,
} from "./validators"
import { validateAndTransformBody } from "../../utils/validate-body"
import { createBatchBody } from "../../utils/validators"
import { createLinkBody } from "../../utils/validators"
export const adminApiKeyRoutesMiddlewares: MiddlewareRoute[] = [
{
@@ -79,11 +78,9 @@ export const adminApiKeyRoutesMiddlewares: MiddlewareRoute[] = [
},
{
method: ["POST"],
matcher: "/admin/api-keys/:id/sales-channels/batch",
matcher: "/admin/api-keys/:id/sales-channels",
middlewares: [
validateAndTransformBody(
createBatchBody(AdminApiKeySalesChannel, AdminApiKeySalesChannel)
),
validateAndTransformBody(createLinkBody()),
validateAndTransformQuery(
AdminGetApiKeyParams,
QueryConfig.retrieveTransformQueryConfig
@@ -41,8 +41,3 @@ export type AdminRevokeApiKeyType = z.infer<typeof AdminRevokeApiKey>
export const AdminRevokeApiKey = z.object({
revoke_in: z.number().optional(),
})
export type AdminApiKeySalesChannelType = z.infer<
typeof AdminApiKeySalesChannel
>
export const AdminApiKeySalesChannel = z.string()
@@ -1,66 +0,0 @@
import { AdminCustomerGroupResponse, BatchMethodRequest } from "@medusajs/types"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../types/routing"
import { AdminSetCustomersCustomerGroupType } from "../../../validators"
import {
createCustomerGroupCustomersWorkflow,
deleteCustomerGroupCustomersWorkflow,
} from "@medusajs/core-flows"
import { refetchCustomerGroup } from "../../../helpers"
export const POST = async (
req: AuthenticatedMedusaRequest<
BatchMethodRequest<
AdminSetCustomersCustomerGroupType,
AdminSetCustomersCustomerGroupType
>
>,
res: MedusaResponse<AdminCustomerGroupResponse>
) => {
const { id } = req.params
const { create, delete: toDelete } = req.validatedBody
if (create?.length) {
const createCustomers = createCustomerGroupCustomersWorkflow(req.scope)
const { errors } = await createCustomers.run({
input: {
groupCustomers: create.map((c) => ({
customer_id: c,
customer_group_id: id,
})),
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
}
if (toDelete?.length) {
const deleteCustomers = deleteCustomerGroupCustomersWorkflow(req.scope)
const { errors } = await deleteCustomers.run({
input: {
groupCustomers: toDelete.map((c) => ({
customer_id: c,
customer_group_id: id,
})),
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
}
const customerGroup = await refetchCustomerGroup(
id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ customer_group: customerGroup })
}
@@ -0,0 +1,37 @@
import { linkCustomersToCustomerGroupWorkflow } from "@medusajs/core-flows"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
import { LinkMethodRequest } from "@medusajs/types/src"
import { refetchCustomerGroup } from "../../helpers"
export const POST = async (
req: AuthenticatedMedusaRequest<LinkMethodRequest>,
res: MedusaResponse
) => {
const { id } = req.params
const { add, remove } = req.validatedBody
const workflow = linkCustomersToCustomerGroupWorkflow(req.scope)
const { errors } = await workflow.run({
input: {
id,
add,
remove,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const customerGroup = await refetchCustomerGroup(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ customer_group: customerGroup })
}
@@ -6,11 +6,10 @@ import {
AdminCreateCustomerGroup,
AdminGetCustomerGroupParams,
AdminGetCustomerGroupsParams,
AdminSetCustomersCustomerGroup,
AdminUpdateCustomerGroup,
} from "./validators"
import { validateAndTransformBody } from "../../utils/validate-body"
import { createBatchBody } from "../../utils/validators"
import { createLinkBody } from "../../utils/validators"
export const adminCustomerGroupRoutesMiddlewares: MiddlewareRoute[] = [
{
@@ -62,14 +61,9 @@ export const adminCustomerGroupRoutesMiddlewares: MiddlewareRoute[] = [
},
{
method: ["POST"],
matcher: "/admin/customer-groups/:id/customers/batch",
matcher: "/admin/customer-groups/:id/customers",
middlewares: [
validateAndTransformBody(
createBatchBody(
AdminSetCustomersCustomerGroup,
AdminSetCustomersCustomerGroup
)
),
validateAndTransformBody(createLinkBody()),
validateAndTransformQuery(
AdminGetCustomerGroupParams,
QueryConfig.retrieveTransformQueryConfig
@@ -66,8 +66,3 @@ export type AdminUpdateCustomerGroupType = z.infer<
export const AdminUpdateCustomerGroup = z.object({
name: z.string(),
})
export type AdminSetCustomersCustomerGroupType = z.infer<
typeof AdminSetCustomersCustomerGroup
>
export const AdminSetCustomersCustomerGroup = z.string()
@@ -1,38 +0,0 @@
import { addProductsToSalesChannelsWorkflow } from "@medusajs/core-flows"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { refetchSalesChannel } from "../../../../helpers"
import { AdminSetSalesChannelProductsBatchType } from "../../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminSetSalesChannelProductsBatchType>,
res: MedusaResponse
) => {
const workflowInput = {
data: [
{
sales_channel_id: req.params.id,
product_ids: req.validatedBody.product_ids,
},
],
}
const { errors } = await addProductsToSalesChannelsWorkflow(req.scope).run({
input: workflowInput,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const salesChannel = await refetchSalesChannel(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ sales_channel: salesChannel })
}
@@ -1,39 +0,0 @@
import { removeProductsFromSalesChannelsWorkflow } from "@medusajs/core-flows"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { AdminSetSalesChannelProductsBatchType } from "../../../../validators"
import { refetchSalesChannel } from "../../../../helpers"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminSetSalesChannelProductsBatchType>,
res: MedusaResponse
) => {
const workflowInput = {
data: [
{
sales_channel_id: req.params.id,
product_ids: req.validatedBody.product_ids,
},
],
}
const { errors } = await removeProductsFromSalesChannelsWorkflow(
req.scope
).run({
input: workflowInput,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const salesChannel = await refetchSalesChannel(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ sales_channel: salesChannel })
}
@@ -0,0 +1,37 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
import { LinkMethodRequest } from "@medusajs/types/src"
import { refetchSalesChannel } from "../../helpers"
import { linkProductsToSalesChannelWorkflow } from "@medusajs/core-flows"
export const POST = async (
req: AuthenticatedMedusaRequest<LinkMethodRequest>,
res: MedusaResponse
) => {
const { id } = req.params
const { add, remove } = req.validatedBody
const workflow = linkProductsToSalesChannelWorkflow(req.scope)
const { errors } = await workflow.run({
input: {
id,
add,
remove,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const salesChannel = await refetchSalesChannel(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ sales_channel: salesChannel })
}
@@ -3,12 +3,12 @@ import { authenticate } from "../../../utils/authenticate-middleware"
import { maybeApplyLinkFilter } from "../../utils/maybe-apply-link-filter"
import { validateAndTransformBody } from "../../utils/validate-body"
import { validateAndTransformQuery } from "../../utils/validate-query"
import { createLinkBody } from "../../utils/validators"
import * as QueryConfig from "./query-config"
import {
AdminCreateSalesChannel,
AdminGetSalesChannelParams,
AdminGetSalesChannelsParams,
AdminSetSalesChannelProductsBatch,
AdminUpdateSalesChannel,
} from "./validators"
@@ -77,20 +77,9 @@ export const adminSalesChannelRoutesMiddlewares: MiddlewareRoute[] = [
},
{
method: ["POST"],
matcher: "/admin/sales-channels/:id/products/batch/add",
matcher: "/admin/sales-channels/:id/products",
middlewares: [
validateAndTransformBody(AdminSetSalesChannelProductsBatch),
validateAndTransformQuery(
AdminGetSalesChannelParams,
QueryConfig.retrieveTransformQueryConfig
),
],
},
{
method: ["POST"],
matcher: "/admin/sales-channels/:id/products/batch/remove",
middlewares: [
validateAndTransformBody(AdminSetSalesChannelProductsBatch),
validateAndTransformBody(createLinkBody()),
validateAndTransformQuery(
AdminGetSalesChannelParams,
QueryConfig.retrieveTransformQueryConfig
@@ -50,10 +50,3 @@ export const AdminUpdateSalesChannel = z.object({
is_disabled: z.boolean().optional(),
metadata: z.record(z.string(), z.unknown()).optional(),
})
export type AdminSetSalesChannelProductsBatchType = z.infer<
typeof AdminSetSalesChannelProductsBatch
>
export const AdminSetSalesChannelProductsBatch = z.object({
product_ids: z.array(z.string()),
})
@@ -1,37 +0,0 @@
import {
MedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { AdminStockLocationsSalesChannelType } from "../../../../validators"
import { addLocationsToSalesChannelWorkflow } from "@medusajs/core-flows"
import { refetchStockLocation } from "../../../../helpers"
export const POST = async (
req: MedusaRequest<AdminStockLocationsSalesChannelType>,
res: MedusaResponse
) => {
const workflowInput = {
data: req.validatedBody.sales_channel_ids.map((id) => ({
sales_channel_id: id,
location_ids: [req.params.id],
})),
}
const { errors } = await addLocationsToSalesChannelWorkflow(req.scope).run({
input: workflowInput,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const stockLocation = await refetchStockLocation(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ stock_location: stockLocation })
}
@@ -1,39 +0,0 @@
import {
MedusaRequest,
MedusaResponse,
} from "../../../../../../../types/routing"
import { AdminStockLocationsSalesChannelType } from "../../../../validators"
import { removeLocationsFromSalesChannelWorkflow } from "@medusajs/core-flows"
import { refetchStockLocation } from "../../../../helpers"
export const POST = async (
req: MedusaRequest<AdminStockLocationsSalesChannelType>,
res: MedusaResponse
) => {
const workflowInput = {
data: req.validatedBody.sales_channel_ids.map((id) => ({
sales_channel_id: id,
location_ids: [req.params.id],
})),
}
const { errors } = await removeLocationsFromSalesChannelWorkflow(
req.scope
).run({
input: workflowInput,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const stockLocation = await refetchStockLocation(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ stock_location: stockLocation })
}
@@ -0,0 +1,37 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
import { LinkMethodRequest } from "@medusajs/types/src"
import { refetchStockLocation } from "../../helpers"
import { linkSalesChannelsToStockLocationWorkflow } from "@medusajs/core-flows"
export const POST = async (
req: AuthenticatedMedusaRequest<LinkMethodRequest>,
res: MedusaResponse
) => {
const { id } = req.params
const { add, remove } = req.validatedBody
const workflow = linkSalesChannelsToStockLocationWorkflow(req.scope)
const { errors } = await workflow.run({
input: {
id,
add,
remove,
},
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
const stockLocation = await refetchStockLocation(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ stock_location: stockLocation })
}
@@ -3,13 +3,13 @@ import { authenticate } from "../../../utils/authenticate-middleware"
import { maybeApplyLinkFilter } from "../../utils/maybe-apply-link-filter"
import { validateAndTransformBody } from "../../utils/validate-body"
import { validateAndTransformQuery } from "../../utils/validate-query"
import { createLinkBody } from "../../utils/validators"
import * as QueryConfig from "./query-config"
import {
AdminCreateStockLocation,
AdminCreateStockLocationFulfillmentSet,
AdminGetStockLocationParams,
AdminGetStockLocationsParams,
AdminStockLocationsSalesChannel,
AdminUpdateStockLocation,
} from "./validators"
@@ -79,9 +79,9 @@ export const adminStockLocationRoutesMiddlewares: MiddlewareRoute[] = [
},
{
method: ["POST"],
matcher: "/admin/stock-locations/:id/sales-channels/batch*",
matcher: "/admin/stock-locations/:id/sales-channels",
middlewares: [
validateAndTransformBody(AdminStockLocationsSalesChannel),
validateAndTransformBody(createLinkBody()),
validateAndTransformQuery(
AdminGetStockLocationParams,
QueryConfig.retrieveTransformQueryConfig
@@ -60,13 +60,6 @@ export const AdminUpdateStockLocation = z.object({
metadata: z.record(z.unknown()).optional(),
})
export type AdminStockLocationsSalesChannelType = z.infer<
typeof AdminStockLocationsSalesChannel
>
export const AdminStockLocationsSalesChannel = z.object({
sales_channel_ids: z.array(z.string()),
})
export type AdminCreateStockLocationFulfillmentSetType = z.infer<
typeof AdminCreateStockLocationFulfillmentSet
>