chore(core-flows): reorganize folder structure for promotion workflows (#6243)
workflows folder structure:
```
- src/
- promotion/
- workflows/
- create-promotion.ts
- steps/
- prepare-create-promotion-data.ts
```
RESOLVES CORE-1688
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
export * from "./steps"
|
||||
export * from "./workflows"
|
||||
@@ -0,0 +1,31 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { CreateCampaignDTO, IPromotionModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const createCampaignsStepId = "create-campaigns"
|
||||
export const createCampaignsStep = createStep(
|
||||
createCampaignsStepId,
|
||||
async (data: CreateCampaignDTO[], { container }) => {
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const createdCampaigns = await promotionModule.createCampaigns(data)
|
||||
|
||||
return new StepResponse(
|
||||
createdCampaigns,
|
||||
createdCampaigns.map((createdCampaigns) => createdCampaigns.id)
|
||||
)
|
||||
},
|
||||
async (createdCampaignIds, { container }) => {
|
||||
if (!createdCampaignIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
await promotionModule.delete(createdCampaignIds)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,31 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { CreatePromotionDTO, IPromotionModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const createPromotionsStepId = "create-promotions"
|
||||
export const createPromotionsStep = createStep(
|
||||
createPromotionsStepId,
|
||||
async (data: CreatePromotionDTO[], { container }) => {
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const createdPromotions = await promotionModule.create(data)
|
||||
|
||||
return new StepResponse(
|
||||
createdPromotions,
|
||||
createdPromotions.map((createdPromotions) => createdPromotions.id)
|
||||
)
|
||||
},
|
||||
async (createdPromotionIds, { container }) => {
|
||||
if (!createdPromotionIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
await promotionModule.delete(createdPromotionIds)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,28 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const deleteCampaignsStepId = "delete-campaigns"
|
||||
export const deleteCampaignsStep = createStep(
|
||||
deleteCampaignsStepId,
|
||||
async (ids: string[], { container }) => {
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
await promotionModule.softDeleteCampaigns(ids)
|
||||
|
||||
return new StepResponse(void 0, ids)
|
||||
},
|
||||
async (idsToRestore, { container }) => {
|
||||
if (!idsToRestore?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
await promotionModule.restoreCampaigns(idsToRestore)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,28 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const deletePromotionsStepId = "delete-promotions"
|
||||
export const deletePromotionsStep = createStep(
|
||||
deletePromotionsStepId,
|
||||
async (ids: string[], { container }) => {
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
await promotionModule.softDelete(ids)
|
||||
|
||||
return new StepResponse(void 0, ids)
|
||||
},
|
||||
async (idsToRestore, { container }) => {
|
||||
if (!idsToRestore?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
await promotionModule.restore(idsToRestore)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
export * from "./create-campaigns"
|
||||
export * from "./create-promotions"
|
||||
export * from "./delete-campaigns"
|
||||
export * from "./delete-promotions"
|
||||
export * from "./update-campaigns"
|
||||
export * from "./update-promotions"
|
||||
@@ -0,0 +1,37 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService, UpdateCampaignDTO } from "@medusajs/types"
|
||||
import { getSelectsAndRelationsFromObjectArray } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const updateCampaignsStepId = "update-campaigns"
|
||||
export const updateCampaignsStep = createStep(
|
||||
updateCampaignsStepId,
|
||||
async (data: UpdateCampaignDTO[], { container }) => {
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const { selects, relations } = getSelectsAndRelationsFromObjectArray(data)
|
||||
const dataBeforeUpdate = await promotionModule.listCampaigns(
|
||||
{ id: data.map((d) => d.id) },
|
||||
{ relations, select: selects }
|
||||
)
|
||||
|
||||
const updatedCampaigns = await promotionModule.updateCampaigns(data)
|
||||
|
||||
return new StepResponse(updatedCampaigns, dataBeforeUpdate)
|
||||
},
|
||||
async (dataBeforeUpdate, { container }) => {
|
||||
if (!dataBeforeUpdate) {
|
||||
return
|
||||
}
|
||||
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
// TODO: This still requires some sanitation of data and transformation of
|
||||
// shapes for manytomany and oneToMany relations. Create a common util.
|
||||
await promotionModule.updateCampaigns(dataBeforeUpdate)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,37 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService, UpdatePromotionDTO } from "@medusajs/types"
|
||||
import { getSelectsAndRelationsFromObjectArray } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const updatePromotionsStepId = "update-promotions"
|
||||
export const updatePromotionsStep = createStep(
|
||||
updatePromotionsStepId,
|
||||
async (data: UpdatePromotionDTO[], { container }) => {
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const { selects, relations } = getSelectsAndRelationsFromObjectArray(data)
|
||||
const dataBeforeUpdate = await promotionModule.list(
|
||||
{ id: data.map((d) => d.id) },
|
||||
{ relations, select: selects }
|
||||
)
|
||||
|
||||
const updatedPromotions = await promotionModule.update(data)
|
||||
|
||||
return new StepResponse(updatedPromotions, dataBeforeUpdate)
|
||||
},
|
||||
async (dataBeforeUpdate, { container }) => {
|
||||
if (!dataBeforeUpdate) {
|
||||
return
|
||||
}
|
||||
|
||||
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
// TODO: This still requires some sanitation of data and transformation of
|
||||
// shapes for manytomany and oneToMany relations. Create a common util.
|
||||
await promotionModule.update(dataBeforeUpdate)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
import { CampaignDTO, CreateCampaignDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createCampaignsStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { campaignsData: CreateCampaignDTO[] }
|
||||
|
||||
export const createCampaignsWorkflowId = "create-campaigns"
|
||||
export const createCampaignsWorkflow = createWorkflow(
|
||||
createCampaignsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<CampaignDTO[]> => {
|
||||
return createCampaignsStep(input.campaignsData)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
import { CreatePromotionDTO, PromotionDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createPromotionsStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { promotionsData: CreatePromotionDTO[] }
|
||||
|
||||
export const createPromotionsWorkflowId = "create-promotions"
|
||||
export const createPromotionsWorkflow = createWorkflow(
|
||||
createPromotionsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<PromotionDTO[]> => {
|
||||
return createPromotionsStep(input.promotionsData)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
import { createWorkflow, WorkflowData } from "@medusajs/workflows-sdk"
|
||||
import { deleteCampaignsStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { ids: string[] }
|
||||
|
||||
export const deleteCampaignsWorkflowId = "delete-campaigns"
|
||||
export const deleteCampaignsWorkflow = createWorkflow(
|
||||
deleteCampaignsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
return deleteCampaignsStep(input.ids)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
import { createWorkflow, WorkflowData } from "@medusajs/workflows-sdk"
|
||||
import { deletePromotionsStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { ids: string[] }
|
||||
|
||||
export const deletePromotionsWorkflowId = "delete-promotions"
|
||||
export const deletePromotionsWorkflow = createWorkflow(
|
||||
deletePromotionsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
return deletePromotionsStep(input.ids)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
export * from "./create-campaigns"
|
||||
export * from "./create-promotions"
|
||||
export * from "./delete-campaigns"
|
||||
export * from "./delete-promotions"
|
||||
export * from "./update-campaigns"
|
||||
export * from "./update-promotions"
|
||||
@@ -0,0 +1,13 @@
|
||||
import { CampaignDTO, UpdateCampaignDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { updateCampaignsStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { campaignsData: UpdateCampaignDTO[] }
|
||||
|
||||
export const updateCampaignsWorkflowId = "update-campaigns"
|
||||
export const updateCampaignsWorkflow = createWorkflow(
|
||||
updateCampaignsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<CampaignDTO[]> => {
|
||||
return updateCampaignsStep(input.campaignsData)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
import { PromotionDTO, UpdatePromotionDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { updatePromotionsStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { promotionsData: UpdatePromotionDTO[] }
|
||||
|
||||
export const updatePromotionsWorkflowId = "update-promotions"
|
||||
export const updatePromotionsWorkflow = createWorkflow(
|
||||
updatePromotionsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<PromotionDTO[]> => {
|
||||
return updatePromotionsStep(input.promotionsData)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user