feat: Sales Channels API routes + workflows (#6722)
**What** - Admin sales channels API routes - Workflows for creating, updating, and deleting sales channels - Align `ISalesChannelModuleService` interface with update + upsert conventions - Integrate v2 tests into v1 sales channels integration test suite - Add metadata to sales channel entity Sales channel <> Product management will come in a follow-up PR. On the integration tests, creating, updating, and deleting are passing with the V2 flag enabled. You'll have to take my word for it (or run them yourself), as they are not included in the CI.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
|
||||
type DeleteCustomerStepInput = string[]
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export * from "./auth"
|
||||
export * from "./api-key"
|
||||
export * from "./auth"
|
||||
export * from "./customer"
|
||||
export * from "./customer-group"
|
||||
export * from "./definition"
|
||||
@@ -14,6 +14,7 @@ export * from "./pricing"
|
||||
export * from "./product"
|
||||
export * from "./promotion"
|
||||
export * from "./region"
|
||||
export * from "./sales-channel"
|
||||
export * from "./shipping-options"
|
||||
export * from "./store"
|
||||
export * from "./tax"
|
||||
|
||||
2
packages/core-flows/src/sales-channel/index.ts
Normal file
2
packages/core-flows/src/sales-channel/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./steps"
|
||||
export * from "./workflows"
|
||||
@@ -0,0 +1,38 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
CreateSalesChannelDTO,
|
||||
ISalesChannelModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
interface StepInput {
|
||||
data: CreateSalesChannelDTO[]
|
||||
}
|
||||
|
||||
export const createSalesChannelsStepId = "create-sales-channels"
|
||||
export const createSalesChannelsStep = createStep(
|
||||
createSalesChannelsStepId,
|
||||
async (input: StepInput, { container }) => {
|
||||
const salesChannelService = container.resolve<ISalesChannelModuleService>(
|
||||
ModuleRegistrationName.SALES_CHANNEL
|
||||
)
|
||||
|
||||
const salesChannels = await salesChannelService.create(input.data)
|
||||
|
||||
return new StepResponse(
|
||||
salesChannels,
|
||||
salesChannels.map((s) => s.id)
|
||||
)
|
||||
},
|
||||
async (createdIds, { container }) => {
|
||||
if (!createdIds) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<ISalesChannelModuleService>(
|
||||
ModuleRegistrationName.SALES_CHANNEL
|
||||
)
|
||||
|
||||
await service.delete(createdIds)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,30 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ISalesChannelModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type DeleteSalesChannelsInput = string[]
|
||||
|
||||
export const deleteSalesChannelsStepId = "delete-sales-channels"
|
||||
export const deleteSalesChannelsStep = createStep(
|
||||
deleteSalesChannelsStepId,
|
||||
async (ids: DeleteSalesChannelsInput, { container }) => {
|
||||
const service = container.resolve<ISalesChannelModuleService>(
|
||||
ModuleRegistrationName.SALES_CHANNEL
|
||||
)
|
||||
|
||||
await service.softDelete(ids)
|
||||
|
||||
return new StepResponse(void 0, ids)
|
||||
},
|
||||
async (prevSalesChannelIds, { container }) => {
|
||||
if (!prevSalesChannelIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<ISalesChannelModuleService>(
|
||||
ModuleRegistrationName.SALES_CHANNEL
|
||||
)
|
||||
|
||||
await service.restore(prevSalesChannelIds)
|
||||
}
|
||||
)
|
||||
3
packages/core-flows/src/sales-channel/steps/index.ts
Normal file
3
packages/core-flows/src/sales-channel/steps/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./create-sales-channels"
|
||||
export * from "./update-sales-channels"
|
||||
export * from "./delete-sales-channels"
|
||||
@@ -0,0 +1,55 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
FilterableSalesChannelProps,
|
||||
ISalesChannelModuleService,
|
||||
UpdateSalesChannelDTO,
|
||||
} from "@medusajs/types"
|
||||
import { getSelectsAndRelationsFromObjectArray } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type UpdateSalesChannelsStepInput = {
|
||||
selector: FilterableSalesChannelProps
|
||||
update: UpdateSalesChannelDTO
|
||||
}
|
||||
|
||||
export const updateSalesChannelsStepId = "update-sales-channels"
|
||||
export const updateSalesChannelsStep = createStep(
|
||||
updateSalesChannelsStepId,
|
||||
async (data: UpdateSalesChannelsStepInput, { container }) => {
|
||||
const service = container.resolve<ISalesChannelModuleService>(
|
||||
ModuleRegistrationName.SALES_CHANNEL
|
||||
)
|
||||
|
||||
const { selects, relations } = getSelectsAndRelationsFromObjectArray([
|
||||
data.update,
|
||||
])
|
||||
|
||||
const prevData = await service.list(data.selector, {
|
||||
select: selects,
|
||||
relations,
|
||||
})
|
||||
|
||||
const channels = await service.update(data.selector, data.update)
|
||||
|
||||
return new StepResponse(channels, prevData)
|
||||
},
|
||||
async (prevData, { container }) => {
|
||||
if (!prevData?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<ISalesChannelModuleService>(
|
||||
ModuleRegistrationName.SALES_CHANNEL
|
||||
)
|
||||
|
||||
await service.upsert(
|
||||
prevData.map((r) => ({
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
description: r.description,
|
||||
is_disabled: r.is_disabled,
|
||||
metadata: r.metadata,
|
||||
}))
|
||||
)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
import { CreateSalesChannelDTO, SalesChannelDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createSalesChannelsStep } from "../steps/create-sales-channels"
|
||||
|
||||
type WorkflowInput = { salesChannelsData: CreateSalesChannelDTO[] }
|
||||
|
||||
export const createSalesChannelsWorkflowId = "create-sales-channels"
|
||||
export const createSalesChannelsWorkflow = createWorkflow(
|
||||
createSalesChannelsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<SalesChannelDTO[]> => {
|
||||
return createSalesChannelsStep({ data: input.salesChannelsData })
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { deleteSalesChannelsStep } from "../steps/delete-sales-channels"
|
||||
|
||||
type WorkflowInput = { ids: string[] }
|
||||
|
||||
export const deleteSalesChannelsWorkflowId = "delete-sales-channels"
|
||||
export const deleteSalesChannelsWorkflow = createWorkflow(
|
||||
deleteSalesChannelsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
return deleteSalesChannelsStep(input.ids)
|
||||
}
|
||||
)
|
||||
3
packages/core-flows/src/sales-channel/workflows/index.ts
Normal file
3
packages/core-flows/src/sales-channel/workflows/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./create-sales-channels"
|
||||
export * from "./update-sales-channels"
|
||||
export * from "./delete-sales-channels"
|
||||
@@ -0,0 +1,22 @@
|
||||
import {
|
||||
FilterableSalesChannelProps,
|
||||
SalesChannelDTO,
|
||||
UpdateSalesChannelDTO,
|
||||
} from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { updateSalesChannelsStep } from "../steps/update-sales-channels"
|
||||
|
||||
type UpdateSalesChannelsStepInput = {
|
||||
selector: FilterableSalesChannelProps
|
||||
update: UpdateSalesChannelDTO
|
||||
}
|
||||
|
||||
export const updateSalesChannelsWorkflowId = "update-sales-channels"
|
||||
export const updateSalesChannelsWorkflow = createWorkflow(
|
||||
updateSalesChannelsWorkflowId,
|
||||
(
|
||||
input: WorkflowData<UpdateSalesChannelsStepInput>
|
||||
): WorkflowData<SalesChannelDTO[]> => {
|
||||
return updateSalesChannelsStep(input)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user