feat(core-flows,medusa,utils): promotion and campaign create/update endpoint (#6130)
what: - adds create endpoint for promotions including workflows and endpoint (RESOLVES CORE-1678) - adds update endpoint for promotions including workflows and endpoint (RESOLVES CORE-1679) - adds create endpoint for campaigns including workflows and endpoint (RESOLVES CORE-1684) - adds update endpoint for campaigns including workflows and endpoint (RESOLVES CORE-1685)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
export * from "./cart"
|
||||
export * from "./product"
|
||||
export * from "./inventory"
|
||||
export * from "./price-list"
|
||||
export * from "./product"
|
||||
export * from "./promotion"
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { CampaignDTO, CreateCampaignDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createCampaignsStep } from "../../handlers/promotion"
|
||||
|
||||
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,14 @@
|
||||
import { CreatePromotionDTO, PromotionDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createPromotionsStep } from "../../handlers/promotion"
|
||||
|
||||
type WorkflowInput = { promotionsData: CreatePromotionDTO[] }
|
||||
type WorkflowOutput = PromotionDTO[]
|
||||
|
||||
export const createPromotionsWorkflowId = "create-promotions"
|
||||
export const createPromotionsWorkflow = createWorkflow(
|
||||
createPromotionsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<PromotionDTO[]> => {
|
||||
return createPromotionsStep(input.promotionsData)
|
||||
}
|
||||
)
|
||||
4
packages/core-flows/src/definition/promotion/index.ts
Normal file
4
packages/core-flows/src/definition/promotion/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from "./create-campaigns"
|
||||
export * from "./create-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 "../../handlers/promotion"
|
||||
|
||||
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 "../../handlers/promotion"
|
||||
|
||||
type WorkflowInput = { promotionsData: UpdatePromotionDTO[] }
|
||||
|
||||
export const updatePromotionsWorkflowId = "update-promotions"
|
||||
export const updatePromotionsWorkflow = createWorkflow(
|
||||
updatePromotionsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<PromotionDTO[]> => {
|
||||
return updatePromotionsStep(input.promotionsData)
|
||||
}
|
||||
)
|
||||
@@ -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)
|
||||
}
|
||||
)
|
||||
4
packages/core-flows/src/handlers/promotion/index.ts
Normal file
4
packages/core-flows/src/handlers/promotion/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from "./create-campaigns"
|
||||
export * from "./create-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)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user