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,51 +0,0 @@
import { Modules } from "@medusajs/modules-sdk"
import { ContainerRegistrationKeys } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
interface StepInput {
links: {
api_key_id: string
sales_channel_ids: string[]
}[]
}
export const associateApiKeysWithSalesChannelsStepId =
"associate-sales-channels-with-api-keys"
export const associateApiKeysWithSalesChannelsStep = createStep(
associateApiKeysWithSalesChannelsStepId,
async (input: StepInput, { container }) => {
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
if (!input.links) {
return
}
const links = input.links
.map((link) => {
return link.sales_channel_ids.map((id) => {
return {
[Modules.API_KEY]: {
publishable_key_id: link.api_key_id,
},
[Modules.SALES_CHANNEL]: {
sales_channel_id: id,
},
}
})
})
.flat()
const createdLinks = await remoteLink.create(links)
return new StepResponse(createdLinks, links)
},
async (links, { container }) => {
if (!links) {
return
}
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
await remoteLink.dismiss(links)
}
)
@@ -1,47 +0,0 @@
import { Modules } from "@medusajs/modules-sdk"
import { ContainerRegistrationKeys } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
interface StepInput {
links: {
api_key_id: string
sales_channel_ids: string[]
}[]
}
export const detachApiKeysWithSalesChannelsStepId =
"detach-sales-channels-with-api-keys"
export const detachApiKeysWithSalesChannelsStep = createStep(
detachApiKeysWithSalesChannelsStepId,
async (input: StepInput, { container }) => {
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
const links = input.links
.map((link) => {
return link.sales_channel_ids.map((id) => {
return {
[Modules.API_KEY]: {
publishable_key_id: link.api_key_id,
},
[Modules.SALES_CHANNEL]: {
sales_channel_id: id,
},
}
})
})
.flat()
await remoteLink.dismiss(links)
return new StepResponse(void 0, links)
},
async (links, { container }) => {
if (!links?.length) {
return
}
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
await remoteLink.create(links)
}
)
@@ -1,7 +1,6 @@
export * from "./associate-sales-channels-with-publishable-keys"
export * from "./link-sales-channels-to-publishable-key"
export * from "./create-api-keys"
export * from "./delete-api-keys"
export * from "./detach-sales-channels-from-publishable-keys"
export * from "./revoke-api-keys"
export * from "./update-api-keys"
export * from "./validate-sales-channel-exists"
@@ -0,0 +1,62 @@
import { Modules } from "@medusajs/modules-sdk"
import { LinkWorkflowInput } from "@medusajs/types/src"
import { ContainerRegistrationKeys, promiseAll } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const linkSalesChannelsToApiKeyStepId = "link-sales-channels-to-api-key"
export const linkSalesChannelsToApiKeyStep = createStep(
linkSalesChannelsToApiKeyStepId,
async (input: LinkWorkflowInput, { container }) => {
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
if (!input || (!input.add?.length && !input.remove?.length)) {
return
}
const linksToCreate = (input.add ?? []).map((salesChannelId) => {
return {
[Modules.API_KEY]: {
publishable_key_id: input.id,
},
[Modules.SALES_CHANNEL]: {
sales_channel_id: salesChannelId,
},
}
})
const linksToDismiss = (input.remove ?? []).map((salesChannelId) => {
return {
[Modules.API_KEY]: {
publishable_key_id: input.id,
},
[Modules.SALES_CHANNEL]: {
sales_channel_id: salesChannelId,
},
}
})
const promises: Promise<any>[] = []
if (linksToCreate.length) {
promises.push(remoteLink.create(linksToCreate))
}
if (linksToDismiss.length) {
promises.push(remoteLink.dismiss(linksToDismiss))
}
await promiseAll(promises)
return new StepResponse(void 0, { linksToCreate, linksToDismiss })
},
async (prevData, { container }) => {
if (!prevData) {
return
}
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
if (prevData.linksToCreate.length) {
await remoteLink.dismiss(prevData.linksToCreate)
}
if (prevData.linksToDismiss.length) {
await remoteLink.create(prevData.linksToDismiss)
}
}
)
@@ -1,31 +0,0 @@
import {
WorkflowData,
createWorkflow,
transform,
} from "@medusajs/workflows-sdk"
import {
associateApiKeysWithSalesChannelsStep,
validateSalesChannelsExistStep,
} from "../steps"
type WorkflowInput = {
data: {
api_key_id: string
sales_channel_ids: string[]
}[]
}
export const addSalesChannelsToApiKeyWorkflowId =
"add-sales-channels-to-api-key"
export const addSalesChannelsToApiKeyWorkflow = createWorkflow(
addSalesChannelsToApiKeyWorkflowId,
(input: WorkflowData<WorkflowInput>) => {
const salesChannelIds = transform(input.data, (data) =>
data.map((d) => d.sales_channel_ids).flat()
)
validateSalesChannelsExistStep({
sales_channel_ids: salesChannelIds,
})
associateApiKeysWithSalesChannelsStep({ links: input.data })
}
)
@@ -2,5 +2,4 @@ export * from "./create-api-keys"
export * from "./delete-api-keys"
export * from "./update-api-keys"
export * from "./revoke-api-keys"
export * from "./add-sales-channels-to-publishable-key"
export * from "./remove-sales-channels-from-publishable-key"
export * from "./link-sales-channels-to-publishable-key"
@@ -0,0 +1,19 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import {
linkSalesChannelsToApiKeyStep,
validateSalesChannelsExistStep,
} from "../steps"
import { LinkWorkflowInput } from "@medusajs/types/src"
export const linkSalesChannelsToApiKeyWorkflowId =
"link-sales-channels-to-api-key"
export const linkSalesChannelsToApiKeyWorkflow = createWorkflow(
linkSalesChannelsToApiKeyWorkflowId,
(input: WorkflowData<LinkWorkflowInput>) => {
validateSalesChannelsExistStep({
sales_channel_ids: input.add ?? [],
})
linkSalesChannelsToApiKeyStep(input)
}
)
@@ -1,18 +0,0 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { detachApiKeysWithSalesChannelsStep } from "../steps/detach-sales-channels-from-publishable-keys"
type WorkflowInput = {
data: {
api_key_id: string
sales_channel_ids: string[]
}[]
}
export const removeSalesChannelsFromApiKeyWorkflowId =
"remove-sales-channels-from-api-key"
export const removeSalesChannelsFromApiKeyWorkflow = createWorkflow(
removeSalesChannelsFromApiKeyWorkflowId,
(input: WorkflowData<WorkflowInput>) => {
detachApiKeysWithSalesChannelsStep({ links: input.data })
}
)
@@ -1,29 +0,0 @@
import { GroupCustomerPair, ICustomerModuleService } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const createCustomerGroupCustomersStepId =
"create-customer-group-customers"
export const createCustomerGroupCustomersStep = createStep(
createCustomerGroupCustomersStepId,
async (data: GroupCustomerPair[], { container }) => {
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
const groupPairs = await service.addCustomerToGroup(data)
return new StepResponse(groupPairs, data)
},
async (groupPairs, { container }) => {
if (!groupPairs?.length) {
return
}
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
await service.removeCustomerFromGroup(groupPairs)
}
)
@@ -1,5 +1,4 @@
export * from "./update-customer-groups"
export * from "./delete-customer-groups"
export * from "./create-customer-groups"
export * from "./create-customer-group-customers"
export * from "./delete-customer-group-customers"
export * from "./link-customers-customer-group"
@@ -0,0 +1,56 @@
import { ICustomerModuleService } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { LinkWorkflowInput } from "@medusajs/types/src"
import { promiseAll } from "@medusajs/utils"
export const linkCustomersToCustomerGroupStepId =
"link-customers-to-customer-group"
export const linkCustomersToCustomerGroupStep = createStep(
linkCustomersToCustomerGroupStepId,
async (data: LinkWorkflowInput, { container }) => {
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
const toAdd = (data.add ?? []).map((customerId) => {
return {
customer_id: customerId,
customer_group_id: data.id,
}
})
const toRemove = (data.remove ?? []).map((customerId) => {
return {
customer_id: customerId,
customer_group_id: data.id,
}
})
const promises: Promise<any>[] = []
if (toAdd.length) {
promises.push(service.addCustomerToGroup(toAdd))
}
if (toRemove.length) {
promises.push(service.removeCustomerFromGroup(toRemove))
}
await promiseAll(promises)
return new StepResponse(void 0, { toAdd, toRemove })
},
async (prevData, { container }) => {
if (!prevData) {
return
}
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
if (prevData.toAdd.length) {
await service.removeCustomerFromGroup(prevData.toAdd)
}
if (prevData.toRemove.length) {
await service.addCustomerToGroup(prevData.toRemove)
}
}
)
@@ -1,14 +0,0 @@
import { GroupCustomerPair } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { createCustomerGroupCustomersStep } from "../steps"
type WorkflowInput = { groupCustomers: GroupCustomerPair[] }
export const createCustomerGroupCustomersWorkflowId =
"create-customer-group-customers"
export const createCustomerGroupCustomersWorkflow = createWorkflow(
createCustomerGroupCustomersWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<{ id: string }[]> => {
return createCustomerGroupCustomersStep(input.groupCustomers)
}
)
@@ -1,14 +0,0 @@
import { GroupCustomerPair } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { deleteCustomerGroupCustomersStep } from "../steps"
type WorkflowInput = { groupCustomers: GroupCustomerPair[] }
export const deleteCustomerGroupCustomersWorkflowId =
"delete-customer-group-customers"
export const deleteCustomerGroupCustomersWorkflow = createWorkflow(
deleteCustomerGroupCustomersWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
return deleteCustomerGroupCustomersStep(input.groupCustomers)
}
)
@@ -1,5 +1,4 @@
export * from "./update-customer-groups"
export * from "./delete-customer-groups"
export * from "./create-customer-groups"
export * from "./create-customer-group-customers"
export * from "./delete-customer-group-customers"
export * from "./link-customers-customer-group"
@@ -0,0 +1,12 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { linkCustomersToCustomerGroupStep } from "../steps"
import { LinkWorkflowInput } from "@medusajs/types/src"
export const linkCustomersToCustomerGroupWorkflowId =
"link-customers-to-customer-group"
export const linkCustomersToCustomerGroupWorkflow = createWorkflow(
linkCustomersToCustomerGroupWorkflowId,
(input: WorkflowData<LinkWorkflowInput>): WorkflowData<void> => {
return linkCustomersToCustomerGroupStep(input)
}
)
@@ -1,13 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IProductModuleService } from "@medusajs/types"
import { BatchLinkProductsToCollectionDTO } from "@medusajs/types/src"
import { LinkWorkflowInput } from "@medusajs/types/src"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const batchLinkProductsToCollectionStepId =
"batch-link-products-to-collection"
export const batchLinkProductsToCollectionStep = createStep(
batchLinkProductsToCollectionStepId,
async (data: BatchLinkProductsToCollectionDTO, { container }) => {
async (data: LinkWorkflowInput, { container }) => {
const service = container.resolve<IProductModuleService>(
ModuleRegistrationName.PRODUCT
)
@@ -2,33 +2,33 @@ import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { deleteProductVariantsWorkflow } from "../workflows/delete-product-variants"
import { createProductVariantsWorkflow } from "../workflows/create-product-variants"
import { updateProductVariantsWorkflow } from "../workflows/update-product-variants"
import { PricingTypes, ProductTypes } from "@medusajs/types"
type BatchProductVariantsInput = {
create: (ProductTypes.CreateProductVariantDTO & {
prices?: PricingTypes.CreateMoneyAmountDTO[]
})[]
update: (ProductTypes.UpsertProductVariantDTO & {
prices?: PricingTypes.CreateMoneyAmountDTO[]
})[]
delete: string[]
}
import {
BatchWorkflowInput,
CreateProductVariantWorkflowInputDTO,
UpdateProductVariantWorkflowInputDTO,
} from "@medusajs/types"
export const batchProductVariantsStepId = "batch-product-variants"
export const batchProductVariantsStep = createStep(
batchProductVariantsStepId,
async (data: BatchProductVariantsInput, { container }) => {
async (
data: BatchWorkflowInput<
CreateProductVariantWorkflowInputDTO,
UpdateProductVariantWorkflowInputDTO
>,
{ container }
) => {
const { transaction: createTransaction, result: created } =
await createProductVariantsWorkflow(container).run({
input: { product_variants: data.create },
input: { product_variants: data.create ?? [] },
})
const { transaction: updateTransaction, result: updated } =
await updateProductVariantsWorkflow(container).run({
input: { product_variants: data.update },
input: { product_variants: data.update ?? [] },
})
const { transaction: deleteTransaction } =
await deleteProductVariantsWorkflow(container).run({
input: { ids: data.delete },
input: { ids: data.delete ?? [] },
})
return new StepResponse(
@@ -36,7 +36,7 @@ export const batchProductVariantsStep = createStep(
created,
updated,
deleted: {
ids: data.delete,
ids: data.delete ?? [],
object: "product_variant",
deleted: true,
},
@@ -2,37 +2,34 @@ import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { createProductsWorkflow } from "../workflows/create-products"
import { updateProductsWorkflow } from "../workflows/update-products"
import { deleteProductsWorkflow } from "../workflows/delete-products"
import { PricingTypes, ProductTypes } from "@medusajs/types"
type WorkflowInput = {
create: (Omit<ProductTypes.CreateProductDTO, "variants"> & {
sales_channels?: { id: string }[]
variants?: (ProductTypes.CreateProductVariantDTO & {
prices?: PricingTypes.CreateMoneyAmountDTO[]
})[]
})[]
update: (ProductTypes.UpsertProductDTO & {
sales_channels?: { id: string }[]
})[]
delete: string[]
}
import {
BatchWorkflowInput,
CreateProductWorkflowInputDTO,
UpdateProductWorkflowInputDTO,
} from "@medusajs/types"
export const batchProductsStepId = "batch-products"
export const batchProductsStep = createStep(
batchProductsStepId,
async (data: WorkflowInput, { container }) => {
async (
data: BatchWorkflowInput<
CreateProductWorkflowInputDTO,
UpdateProductWorkflowInputDTO
>,
{ container }
) => {
const { transaction: createTransaction, result: created } =
await createProductsWorkflow(container).run({
input: { products: data.create },
input: { products: data.create ?? [] },
})
const { transaction: updateTransaction, result: updated } =
await updateProductsWorkflow(container).run({
input: { products: data.update },
input: { products: data.update ?? [] },
})
const { transaction: deleteTransaction } = await deleteProductsWorkflow(
container
).run({
input: { ids: data.delete },
input: { ids: data.delete ?? [] },
})
return new StepResponse(
@@ -40,7 +37,7 @@ export const batchProductsStep = createStep(
created,
updated,
deleted: {
ids: data.delete,
ids: data.delete ?? [],
object: "product",
deleted: true,
},
@@ -1,14 +1,12 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { BatchLinkProductsToCollectionDTO } from "@medusajs/types/src"
import { LinkWorkflowInput } from "@medusajs/types/src"
import { batchLinkProductsToCollectionStep } from "../steps/batch-link-products-collection"
export const batchLinkProductsToCollectionWorkflowId =
"batch-link-products-to-collection"
export const batchLinkProductsToCollectionWorkflow = createWorkflow(
batchLinkProductsToCollectionWorkflowId,
(
input: WorkflowData<BatchLinkProductsToCollectionDTO>
): WorkflowData<void> => {
(input: WorkflowData<LinkWorkflowInput>): WorkflowData<void> => {
return batchLinkProductsToCollectionStep(input)
}
)
@@ -1,33 +1,24 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { batchProductVariantsStep } from "../steps/batch-product-variants"
import { PricingTypes, ProductTypes } from "@medusajs/types"
type BatchProductVariantsInput = {
create: (ProductTypes.CreateProductVariantDTO & {
prices?: PricingTypes.CreateMoneyAmountDTO[]
})[]
update: (ProductTypes.UpsertProductVariantDTO & {
prices?: PricingTypes.CreateMoneyAmountDTO[]
})[]
delete: string[]
}
type BatchProductVariantsOutput = {
created: ProductTypes.ProductVariantDTO[]
updated: ProductTypes.ProductVariantDTO[]
deleted: {
ids: string[]
object: string
deleted: boolean
}
}
import {
BatchWorkflowInput,
BatchWorkflowOutput,
ProductTypes,
UpdateProductVariantWorkflowInputDTO,
CreateProductVariantWorkflowInputDTO,
} from "@medusajs/types"
export const batchProductVariantsWorkflowId = "batch-product-variants"
export const batchProductVariantsWorkflow = createWorkflow(
batchProductVariantsWorkflowId,
(
input: WorkflowData<BatchProductVariantsInput>
): WorkflowData<BatchProductVariantsOutput> => {
input: WorkflowData<
BatchWorkflowInput<
CreateProductVariantWorkflowInputDTO,
UpdateProductVariantWorkflowInputDTO
>
>
): WorkflowData<BatchWorkflowOutput<ProductTypes.ProductVariantDTO>> => {
return batchProductVariantsStep(input)
}
)
@@ -1,34 +1,24 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { batchProductsStep } from "../steps/batch-products"
import { PricingTypes, ProductTypes } from "@medusajs/types"
type WorkflowInput = {
create: (Omit<ProductTypes.CreateProductDTO, "variants"> & {
sales_channels?: { id: string }[]
variants?: (ProductTypes.CreateProductVariantDTO & {
prices?: PricingTypes.CreateMoneyAmountDTO[]
})[]
})[]
update: (ProductTypes.UpsertProductDTO & {
sales_channels?: { id: string }[]
})[]
delete: string[]
}
type BatchProductsOutput = {
created: ProductTypes.ProductDTO[]
updated: ProductTypes.ProductDTO[]
deleted: {
ids: string[]
object: string
deleted: boolean
}
}
import {
ProductTypes,
BatchWorkflowInput,
CreateProductWorkflowInputDTO,
UpdateProductWorkflowInputDTO,
} from "@medusajs/types"
import { BatchWorkflowOutput } from "@medusajs/types/src"
export const batchProductsWorkflowId = "batch-products"
export const batchProductsWorkflow = createWorkflow(
batchProductsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<BatchProductsOutput> => {
(
input: WorkflowData<
BatchWorkflowInput<
CreateProductWorkflowInputDTO,
UpdateProductWorkflowInputDTO
>
>
): WorkflowData<BatchWorkflowOutput<ProductTypes.ProductDTO>> => {
return batchProductsStep(input)
}
)
@@ -8,16 +8,10 @@ import { createProductsStep } from "../steps/create-products"
import { createVariantPricingLinkStep } from "../steps/create-variant-pricing-link"
import { createPriceSetsStep } from "../../pricing"
import { associateProductsWithSalesChannelsStep } from "../../sales-channel"
import { CreateProductWorkflowInputDTO } from "@medusajs/types/src"
// TODO: We should have separate types here as input, not the module DTO. Eg. the HTTP request that we are handling
// has different data than the DTO, so that needs to be represented differently.
type WorkflowInput = {
products: (Omit<ProductTypes.CreateProductDTO, "variants"> & {
sales_channels?: { id: string }[]
variants?: (ProductTypes.CreateProductVariantDTO & {
prices?: PricingTypes.CreateMoneyAmountDTO[]
})[]
})[]
products: CreateProductWorkflowInputDTO[]
}
export const createProductsWorkflowId = "create-products"
@@ -6,38 +6,32 @@ import { Modules } from "@medusajs/modules-sdk"
interface StepInput {
links: {
sales_channel_id: string
location_ids: string[]
location_id: string
}[]
}
export const associateLocationsWithChannelStepId =
"associate-locations-with-channel-step"
export const associateLocationsWithChannelStep = createStep(
associateLocationsWithChannelStepId,
export const associateLocationsWithSalesChannelsStepId =
"associate-locations-with-sales-channels-step"
export const associateLocationsWithSalesChannelsStep = createStep(
associateLocationsWithSalesChannelsStepId,
async (data: StepInput, { container }) => {
if (!data.links.length) {
if (!data.links?.length) {
return new StepResponse([], [])
}
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
const links = data.links
.map((link) => {
return link.location_ids.map((id) => {
return {
[Modules.SALES_CHANNEL]: {
sales_channel_id: link.sales_channel_id,
},
[Modules.STOCK_LOCATION]: {
stock_location_id: id,
},
}
})
})
.flat()
const links = data.links.map((link) => {
return {
[Modules.SALES_CHANNEL]: {
sales_channel_id: link.sales_channel_id,
},
[Modules.STOCK_LOCATION]: {
stock_location_id: link.location_id,
},
}
})
const createdLinks = await remoteLink.create(links)
return new StepResponse(createdLinks, links)
},
async (links, { container }) => {
@@ -46,7 +40,6 @@ export const associateLocationsWithChannelStep = createStep(
}
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
await remoteLink.dismiss(links)
}
)
@@ -14,7 +14,7 @@ export const associateProductsWithSalesChannelsStepId =
export const associateProductsWithSalesChannelsStep = createStep(
associateProductsWithSalesChannelsStepId,
async (input: StepInput, { container }) => {
if (!input.links.length) {
if (!input.links?.length) {
return new StepResponse([], [])
}
@@ -0,0 +1,48 @@
import { Modules, RemoteLink } from "@medusajs/modules-sdk"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ContainerRegistrationKeys } from "@medusajs/utils"
interface StepInput {
links: {
sales_channel_id: string
location_id: string
}[]
}
export const detachLocationsFromSalesChannelsStepId =
"detach-locations-from-sales-channels"
export const detachLocationsFromSalesChannelsStep = createStep(
detachLocationsFromSalesChannelsStepId,
async (data: StepInput, { container }) => {
if (!data.links?.length) {
return new StepResponse([], [])
}
const remoteLink = container.resolve<RemoteLink>(
ContainerRegistrationKeys.REMOTE_LINK
)
const links = data.links.map((link) => {
return {
[Modules.SALES_CHANNEL]: {
sales_channel_id: link.sales_channel_id,
},
[Modules.STOCK_LOCATION]: {
stock_location_id: link.location_id,
},
}
})
await remoteLink.dismiss(links)
return new StepResponse(void 0, links)
},
async (links, { container }) => {
if (!links?.length) {
return
}
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
await remoteLink.create(links)
}
)
@@ -14,7 +14,7 @@ export const detachProductsFromSalesChannelsStepId =
export const detachProductsFromSalesChannelsStep = createStep(
detachProductsFromSalesChannelsStepId,
async (input: StepInput, { container }) => {
if (!input.links.length) {
if (!input.links?.length) {
return new StepResponse(void 0, [])
}
@@ -4,5 +4,5 @@ export * from "./create-sales-channels"
export * from "./delete-sales-channels"
export * from "./detach-products-from-sales-channels"
export * from "./update-sales-channels"
export * from "./associate-locations-with-channel"
export * from "./remove-locations-from-channels"
export * from "./associate-locations-with-channels"
export * from "./detach-locations-from-channels"
@@ -1,71 +0,0 @@
import { Modules, RemoteLink } from "@medusajs/modules-sdk"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ContainerRegistrationKeys } from "@medusajs/utils"
export const removeLocationsFromSalesChannelStepId =
"remove-locations-from-sales-channel"
export const removeLocationsFromSalesChannelStep = createStep(
removeLocationsFromSalesChannelStepId,
async (
data: {
sales_channel_id: string
location_ids: string[]
}[],
{ container }
) => {
if (!data.length) {
return new StepResponse([], [])
}
const remoteLink = container.resolve<RemoteLink>(
ContainerRegistrationKeys.REMOTE_LINK
)
const linkModule = remoteLink.getLinkModule(
Modules.SALES_CHANNEL,
"sales_channel_id",
Modules.STOCK_LOCATION,
"stock_location_id"
)
if (!linkModule) {
return new StepResponse([], [])
}
const links = data
.map((d) =>
d.location_ids.map((locId) => ({
sales_channel_id: d.sales_channel_id,
stock_location_id: locId,
}))
)
.flat()
await linkModule.softDelete(links)
return new StepResponse(void 0, links)
},
async (links, { container }) => {
if (!links?.length) {
return
}
const remoteLink = container.resolve<RemoteLink>(
ContainerRegistrationKeys.REMOTE_LINK
)
const linkModule = remoteLink.getLinkModule(
Modules.SALES_CHANNEL,
"sales_channel_id",
Modules.STOCK_LOCATION,
"stock_location_id"
)
if (!linkModule) {
return
}
await linkModule.restore(links)
}
)
@@ -1,20 +0,0 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { SalesChannelDTO } from "@medusajs/types"
import { associateLocationsWithChannelStep } from "../steps"
interface WorkflowInput {
data: {
sales_channel_id: string
location_ids: string[]
}[]
}
export const addLocationsToSalesChannelWorkflowId =
"add-locations-to-sales-channel"
export const addLocationsToSalesChannelWorkflow = createWorkflow(
addLocationsToSalesChannelWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<SalesChannelDTO[]> => {
return associateLocationsWithChannelStep({ links: input.data })
}
)
@@ -1,33 +0,0 @@
import { SalesChannelDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { associateProductsWithSalesChannelsStep } from "../steps/associate-products-with-channels"
import { transform } from "@medusajs/workflows-sdk"
type WorkflowInput = {
data: {
sales_channel_id: string
product_ids: string[]
}[]
}
export const addProductsToSalesChannelsWorkflowId =
"add-products-to-sales-channels"
export const addProductsToSalesChannelsWorkflow = createWorkflow(
addProductsToSalesChannelsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<SalesChannelDTO[]> => {
const links = transform({ input }, (data) => {
return data.input.data
.map(({ sales_channel_id, product_ids }) => {
return product_ids.map((product_id) => {
return {
sales_channel_id,
product_id,
}
})
})
.flat()
})
return associateProductsWithSalesChannelsStep({ links })
}
)
@@ -1,7 +1,4 @@
export * from "./add-products-to-sales-channels"
export * from "./link-products-to-sales-channel"
export * from "./create-sales-channels"
export * from "./delete-sales-channels"
export * from "./remove-products-from-sales-channels"
export * from "./update-sales-channels"
export * from "./add-locations-to-sales-channel"
export * from "./remove-locations-from-sales-channel"
@@ -0,0 +1,29 @@
import { LinkWorkflowInput } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { associateProductsWithSalesChannelsStep } from "../steps/associate-products-with-channels"
import { transform } from "@medusajs/workflows-sdk"
import { detachProductsFromSalesChannelsStep } from "../steps"
export const linkProductsToSalesChannelWorkflowId =
"link-products-to-sales-channel"
export const linkProductsToSalesChannelWorkflow = createWorkflow(
linkProductsToSalesChannelWorkflowId,
(input: WorkflowData<LinkWorkflowInput>): WorkflowData<void> => {
const toAdd = transform({ input }, (data) => {
return data.input.add?.map((productId) => ({
sales_channel_id: data.input.id,
product_id: productId,
}))
})
const toRemove = transform({ input }, (data) => {
return data.input.remove?.map((productId) => ({
sales_channel_id: data.input.id,
product_id: productId,
}))
})
associateProductsWithSalesChannelsStep({ links: toAdd })
detachProductsFromSalesChannelsStep({ links: toRemove })
}
)
@@ -1,19 +0,0 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { removeLocationsFromSalesChannelStep } from "../steps"
interface WorkflowInput {
data: {
sales_channel_id: string
location_ids: string[]
}[]
}
export const removeLocationsFromSalesChannelWorkflowId =
"remove-locations-from-sales-channel"
export const removeLocationsFromSalesChannelWorkflow = createWorkflow(
removeLocationsFromSalesChannelWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
removeLocationsFromSalesChannelStep(input.data)
}
)
@@ -1,33 +0,0 @@
import { SalesChannelDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { detachProductsFromSalesChannelsStep } from "../steps/detach-products-from-sales-channels"
import { transform } from "@medusajs/workflows-sdk"
type WorkflowInput = {
data: {
sales_channel_id: string
product_ids: string[]
}[]
}
export const removeProductsFromSalesChannelsWorkflowId =
"remove-products-from-sales-channels"
export const removeProductsFromSalesChannelsWorkflow = createWorkflow(
removeProductsFromSalesChannelsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<SalesChannelDTO[]> => {
const links = transform({ input }, (data) => {
return data.input.data
.map(({ sales_channel_id, product_ids }) => {
return product_ids.map((product_id) => {
return {
sales_channel_id,
product_id,
}
})
})
.flat()
})
return detachProductsFromSalesChannelsStep({ links })
}
)
@@ -2,3 +2,4 @@ export * from "./create-location-fulfillment-set"
export * from "./create-stock-locations"
export * from "./delete-stock-locations"
export * from "./update-stock-locations"
export * from "./link-sales-channels-to-stock-location"
@@ -0,0 +1,31 @@
import { LinkWorkflowInput } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { transform } from "@medusajs/workflows-sdk"
import {
associateLocationsWithSalesChannelsStep,
detachLocationsFromSalesChannelsStep,
} from "../../sales-channel"
export const linkSalesChannelsToStockLocationWorkflowId =
"link-sales-channels-to-stock-location"
export const linkSalesChannelsToStockLocationWorkflow = createWorkflow(
linkSalesChannelsToStockLocationWorkflowId,
(input: WorkflowData<LinkWorkflowInput>): WorkflowData<void> => {
const toAdd = transform({ input }, (data) => {
return data.input.add?.map((salesChannelId) => ({
sales_channel_id: salesChannelId,
location_id: data.input.id,
}))
})
const toRemove = transform({ input }, (data) => {
return data.input.remove?.map((salesChannelId) => ({
sales_channel_id: salesChannelId,
location_id: data.input.id,
}))
})
associateLocationsWithSalesChannelsStep({ links: toAdd })
detachLocationsFromSalesChannelsStep({ links: toRemove })
}
)