Feat(link-module, core-flows, medusa): Add sales channel location management (#6905)
* add locations with sales channels * init, not working location based management * working adding sales channel stock locations * remote sales channels from location * remove console log * rm allowedFields * redo errorhandler * make validation take an array * add changeset * fix build * Update packages/core-flows/src/sales-channel/workflows/remove-locations-from-sales-channel.ts Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> * add default cases to return early * cleanup * add sc test and remove associations --------- Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Oli Juhl
parent
27f4f0d724
commit
edafe7db47
@@ -1,10 +1,11 @@
|
||||
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
|
||||
import { AdminPostPromotionsPromotionRulesBatchRemoveReq } from "../../../../validators"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchRemoveReq>,
|
||||
|
||||
@@ -25,7 +25,7 @@ export const GET = async (
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "sales_channels",
|
||||
variables,
|
||||
fields: defaultAdminSalesChannelFields,
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const [sales_channel] = await remoteQuery(queryObject)
|
||||
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
|
||||
import { AdminStockLocationsLocationSalesChannelBatchReq } from "../../../../validators"
|
||||
import { addLocationsToSalesChannelWorkflow } from "@medusajs/core-flows"
|
||||
|
||||
export const POST = async (
|
||||
req: MedusaRequest<AdminStockLocationsLocationSalesChannelBatchReq>,
|
||||
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 remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "stock_locations",
|
||||
variables: { id: req.params.id },
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const [stock_location] = await remoteQuery(queryObject)
|
||||
|
||||
res.status(200).json({ stock_location })
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
|
||||
import { AdminStockLocationsLocationSalesChannelBatchReq } from "../../../../validators"
|
||||
import { removeLocationsFromSalesChannelWorkflow } from "@medusajs/core-flows"
|
||||
|
||||
export const POST = async (
|
||||
req: MedusaRequest<AdminStockLocationsLocationSalesChannelBatchReq>,
|
||||
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 remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "stock_locations",
|
||||
variables: { id: req.params.id },
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const [stock_location] = await remoteQuery(queryObject)
|
||||
|
||||
res.status(200).json({ stock_location })
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
AdminPostStockLocationsLocationReq,
|
||||
AdminPostStockLocationsParams,
|
||||
AdminPostStockLocationsReq,
|
||||
AdminStockLocationsLocationSalesChannelBatchReq,
|
||||
} from "./validators"
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
|
||||
@@ -53,6 +54,17 @@ export const adminStockLocationRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/stock-locations/:id/sales-channels/batch*",
|
||||
middlewares: [
|
||||
transformBody(AdminStockLocationsLocationSalesChannelBatchReq),
|
||||
transformQuery(
|
||||
AdminPostStockLocationsLocationParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/stock-locations/:id",
|
||||
|
||||
@@ -17,7 +17,6 @@ export const defaultAdminStockLocationFields = [
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaults: defaultAdminStockLocationFields,
|
||||
allowed: defaultAdminStockLocationFields,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,6 @@
|
||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
||||
import {
|
||||
DateComparisonOperator,
|
||||
FindParams,
|
||||
NumericalComparisonOperator,
|
||||
StringComparisonOperator,
|
||||
extendedFindParamsMixin,
|
||||
} from "../../../types/common"
|
||||
import {
|
||||
IsBoolean,
|
||||
IsEmail,
|
||||
IsNotEmpty,
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
@@ -290,3 +281,8 @@ export class AdminPostStockLocationsLocationReq {
|
||||
export class AdminPostStockLocationsLocationParams extends FindParams {}
|
||||
|
||||
export class AdminGetStockLocationsLocationParams extends FindParams {}
|
||||
|
||||
export class AdminStockLocationsLocationSalesChannelBatchReq {
|
||||
@IsString({ each: true })
|
||||
sales_channel_ids: string[]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user