Skip to content

Commit

Permalink
feature: add hooks to campaigns (#8450)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage authored Aug 6, 2024
1 parent f415e66 commit 8fb0797
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { CampaignDTO, CreateCampaignDTO } from "@medusajs/types"
import { AdditionalData, CreateCampaignDTO } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
createHook,
createWorkflow,
} from "@medusajs/workflows-sdk"
import { createCampaignsStep } from "../steps"

type WorkflowInput = { campaignsData: CreateCampaignDTO[] }
type WorkflowInput = { campaignsData: CreateCampaignDTO[] } & AdditionalData

export const createCampaignsWorkflowId = "create-campaigns"
export const createCampaignsWorkflow = createWorkflow(
createCampaignsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowResponse<CampaignDTO[]> => {
return new WorkflowResponse(createCampaignsStep(input.campaignsData))
(input: WorkflowData<WorkflowInput>) => {
const createdCampaigns = createCampaignsStep(input.campaignsData)
const campaignsCreated = createHook("campaignsCreated", {
campaigns: createdCampaigns,
additional_data: input.additional_data,
})

return new WorkflowResponse(createdCampaigns, {
hooks: [campaignsCreated],
})
}
)
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { CreatePromotionDTO, PromotionDTO } from "@medusajs/types"
import { AdditionalData, CreatePromotionDTO } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
createHook,
createWorkflow,
} from "@medusajs/workflows-sdk"
import { createPromotionsStep } from "../steps"

type WorkflowInput = { promotionsData: CreatePromotionDTO[] }
type WorkflowInput = { promotionsData: CreatePromotionDTO[] } & AdditionalData

export const createPromotionsWorkflowId = "create-promotions"
export const createPromotionsWorkflow = createWorkflow(
createPromotionsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowResponse<PromotionDTO[]> => {
return new WorkflowResponse(createPromotionsStep(input.promotionsData))
(input: WorkflowData<WorkflowInput>) => {
const createdPromotions = createPromotionsStep(input.promotionsData)
const promotionsCreated = createHook("promotionsCreated", {
promotions: createdPromotions,
additional_data: input.additional_data,
})

return new WorkflowResponse(createdPromotions, {
hooks: [promotionsCreated],
})
}
)
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { createWorkflow, WorkflowData } from "@medusajs/workflows-sdk"
import { createHook, createWorkflow, WorkflowData, WorkflowResponse } 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)
(input: WorkflowData<WorkflowInput>) => {
const deletedCampaigns = deleteCampaignsStep(input.ids)
const campaignsDeleted = createHook("campaignsDeleted", {
ids: input.ids
})

return new WorkflowResponse(deletedCampaigns, {
hooks: [campaignsDeleted]
})
}
)
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { createWorkflow, WorkflowData } from "@medusajs/workflows-sdk"
import { createHook, createWorkflow, WorkflowData, WorkflowResponse } 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)
(input: WorkflowData<WorkflowInput>) => {
const deletedPromotions = deletePromotionsStep(input.ids)
const promotionsDeleted = createHook("promotionsDeleted", {
ids: input.ids
})

return new WorkflowResponse(deletedPromotions, {
hooks: [promotionsDeleted]
})
}
)
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { CampaignDTO, UpdateCampaignDTO } from "@medusajs/types"
import { AdditionalData, UpdateCampaignDTO } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
createHook,
createWorkflow,
} from "@medusajs/workflows-sdk"
import { updateCampaignsStep } from "../steps"

type WorkflowInput = { campaignsData: UpdateCampaignDTO[] }
type WorkflowInput = { campaignsData: UpdateCampaignDTO[] } & AdditionalData

export const updateCampaignsWorkflowId = "update-campaigns"
export const updateCampaignsWorkflow = createWorkflow(
updateCampaignsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowResponse<CampaignDTO[]> => {
return new WorkflowResponse(updateCampaignsStep(input.campaignsData))
(input: WorkflowData<WorkflowInput>) => {
const updatedCampaigns = updateCampaignsStep(input.campaignsData)
const campaignsUpdated = createHook("campaignsUpdated", {
campaigns: updatedCampaigns,
additional_data: input.additional_data,
})

return new WorkflowResponse(updatedCampaigns, {
hooks: [campaignsUpdated],
})
}
)
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { PromotionDTO, UpdatePromotionDTO } from "@medusajs/types"
import {
AdditionalData,
UpdatePromotionDTO,
} from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
createHook,
createWorkflow,
} from "@medusajs/workflows-sdk"
import { updatePromotionsStep } from "../steps"

type WorkflowInput = { promotionsData: UpdatePromotionDTO[] }
type WorkflowInput = { promotionsData: UpdatePromotionDTO[] } & AdditionalData

export const updatePromotionsWorkflowId = "update-promotions"
export const updatePromotionsWorkflow = createWorkflow(
updatePromotionsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowResponse<PromotionDTO[]> => {
return new WorkflowResponse(updatePromotionsStep(input.promotionsData))
(input: WorkflowData<WorkflowInput>) => {
const updatedPromotions = updatePromotionsStep(input.promotionsData)
const promotionsUpdated = createHook("promotionsUpdated", {
promotions: updatedPromotions,
additional_data: input.additional_data,
})

return new WorkflowResponse(updatedPromotions, {
hooks: [promotionsUpdated],
})
}
)
8 changes: 5 additions & 3 deletions packages/medusa/src/api/admin/campaigns/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { refetchCampaign } from "../helpers"
import { AdminUpdateCampaignType } from "../validators"
import { MedusaError } from "@medusajs/utils"
import { AdditionalData } from "@medusajs/types"

export const GET = async (
req: AuthenticatedMedusaRequest,
Expand All @@ -32,19 +33,20 @@ export const GET = async (
}

export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCampaignType>,
req: AuthenticatedMedusaRequest<AdminUpdateCampaignType & AdditionalData>,
res: MedusaResponse
) => {
const { additional_data, ...rest } = req.validatedBody
const updateCampaigns = updateCampaignsWorkflow(req.scope)
const campaignsData = [
{
id: req.params.id,
...req.validatedBody,
...rest,
},
]

await updateCampaigns.run({
input: { campaignsData },
input: { campaignsData, additional_data },
})

const campaign = await refetchCampaign(
Expand Down
14 changes: 8 additions & 6 deletions packages/medusa/src/api/admin/campaigns/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import {createCampaignsWorkflow} from "@medusajs/core-flows"
import { createCampaignsWorkflow } from "@medusajs/core-flows"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import {AdminCreateCampaignType} from "./validators"
import {refetchCampaign} from "./helpers"
import { AdminCreateCampaignType } from "./validators"
import { refetchCampaign } from "./helpers"
import { AdditionalData } from "@medusajs/types"

export const GET = async (
req: AuthenticatedMedusaRequest,
Expand Down Expand Up @@ -36,14 +37,15 @@ export const GET = async (
}

export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreateCampaignType>,
req: AuthenticatedMedusaRequest<AdminCreateCampaignType & AdditionalData>,
res: MedusaResponse
) => {
const { additional_data, ...rest } = req.validatedBody
const createCampaigns = createCampaignsWorkflow(req.scope)
const campaignsData = [req.validatedBody]
const campaignsData = [rest]

const { result } = await createCampaigns.run({
input: { campaignsData },
input: { campaignsData, additional_data },
context: {
requestId: req.requestId,
},
Expand Down
36 changes: 31 additions & 5 deletions packages/medusa/src/api/admin/campaigns/validators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CampaignBudgetType, isPresent } from "@medusajs/utils"
import { z } from "zod"
import { z, ZodObject } from "zod"
import { createFindParams, createSelectParams } from "../../utils/validators"

export const AdminGetCampaignParams = createSelectParams()
Expand Down Expand Up @@ -56,8 +56,8 @@ export const UpdateCampaignBudget = z
})
.strict()

export type AdminCreateCampaignType = z.infer<typeof AdminCreateCampaign>
export const AdminCreateCampaign = z
export type AdminCreateCampaignType = z.infer<typeof CreateCampaign>
export const CreateCampaign = z
.object({
name: z.string(),
campaign_identifier: z.string(),
Expand All @@ -68,9 +68,22 @@ export const AdminCreateCampaign = z
promotions: z.array(z.object({ id: z.string() })).optional(),
})
.strict()
export const AdminCreateCampaign = (
additionalDataValidator?: ZodObject<any, any>
) => {
if (!additionalDataValidator) {
return CreateCampaign.extend({
additional_data: z.record(z.unknown()).nullish(),
})
}

return CreateCampaign.extend({
additional_data: additionalDataValidator,
})
}

export type AdminUpdateCampaignType = z.infer<typeof AdminUpdateCampaign>
export const AdminUpdateCampaign = z.object({
export type AdminUpdateCampaignType = z.infer<typeof UpdateCampaign>
export const UpdateCampaign = z.object({
name: z.string().optional(),
campaign_identifier: z.string().optional(),
description: z.string().nullish(),
Expand All @@ -79,3 +92,16 @@ export const AdminUpdateCampaign = z.object({
ends_at: z.coerce.date().nullish(),
promotions: z.array(z.object({ id: z.string() })).optional(),
})
export const AdminUpdateCampaign = (
additionalDataValidator?: ZodObject<any, any>
) => {
if (!additionalDataValidator) {
return UpdateCampaign.extend({
additional_data: z.record(z.unknown()).nullish(),
})
}

return UpdateCampaign.extend({
additional_data: additionalDataValidator,
})
}
8 changes: 5 additions & 3 deletions packages/medusa/src/api/admin/promotions/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
AdminGetPromotionParamsType,
AdminUpdatePromotionType,
} from "../validators"
import { AdditionalData } from "@medusajs/types"

export const GET = async (
req: AuthenticatedMedusaRequest<AdminGetPromotionParamsType>,
Expand Down Expand Up @@ -43,19 +44,20 @@ export const GET = async (
}

export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdatePromotionType>,
req: AuthenticatedMedusaRequest<AdminUpdatePromotionType & AdditionalData>,
res: MedusaResponse
) => {
const { additional_data, ...rest } = req.validatedBody
const updatePromotions = updatePromotionsWorkflow(req.scope)
const promotionsData = [
{
id: req.params.id,
...req.validatedBody,
...rest,
} as any,
]

await updatePromotions.run({
input: { promotionsData },
input: { promotionsData, additional_data },
})

const promotion = await refetchPromotion(
Expand Down
8 changes: 5 additions & 3 deletions packages/medusa/src/api/admin/promotions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
AdminCreatePromotionType,
AdminGetPromotionsParamsType,
} from "./validators"
import { AdditionalData } from "@medusajs/types"

export const GET = async (
req: AuthenticatedMedusaRequest<AdminGetPromotionsParamsType>,
Expand Down Expand Up @@ -39,14 +40,15 @@ export const GET = async (
}

export const POST = async (
req: AuthenticatedMedusaRequest<AdminCreatePromotionType>,
req: AuthenticatedMedusaRequest<AdminCreatePromotionType & AdditionalData>,
res: MedusaResponse
) => {
const { additional_data, ...rest } = req.validatedBody
const createPromotions = createPromotionsWorkflow(req.scope)
const promotionsData = [req.validatedBody] as any
const promotionsData = [rest] as any

const { result } = await createPromotions.run({
input: { promotionsData },
input: { promotionsData, additional_data },
})

const promotion = await refetchPromotion(
Expand Down
Loading

0 comments on commit 8fb0797

Please sign in to comment.