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:
@@ -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,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