diff --git a/packages/modules/b2c-core/src/api/vendor/campaigns/[id]/promotions/route.ts b/packages/modules/b2c-core/src/api/vendor/campaigns/[id]/promotions/route.ts new file mode 100644 index 000000000..05fb56ce9 --- /dev/null +++ b/packages/modules/b2c-core/src/api/vendor/campaigns/[id]/promotions/route.ts @@ -0,0 +1,101 @@ +import { + AuthenticatedMedusaRequest, + MedusaResponse, + } from "@medusajs/framework/http" + + import { addOrRemoveCampaignPromotionsWorkflow } from "@medusajs/core-flows" + import { LinkMethodRequest } from "@medusajs/framework/types" + import { refetchCampaign } from "@medusajs/medusa/api/admin/campaigns/helpers" + +/** + * @oas [post] /vendor/campaigns/{id}/promotions + * operationId: VendorPostCampaignsIdPromotions + * summary: Manage the Promotions of a Campaign + * description: Manage the promotions of a campaign, either by adding them or removing them from the campaign. + * x-authenticated: true + * parameters: + * - name: id + * in: path + * description: The campaign's ID. + * required: true + * schema: + * type: string + * - name: fields + * in: query + * description: Comma-separated fields that should be included in the returned data. if a field is prefixed with `+` it will be added to the default fields, using `-` will remove it from the default + * fields. without prefix it will replace the entire default fields. + * required: false + * schema: + * type: string + * title: fields + * description: Comma-separated fields that should be included in the returned data. if a field is prefixed with `+` it will be added to the default fields, using `-` will remove it from the default + * fields. without prefix it will replace the entire default fields. + * externalDocs: + * url: "#select-fields-and-relations" + * security: + * - api_token: [] + * - cookie_auth: [] + * - jwt_token: [] + * requestBody: + * content: + * application/json: + * schema: + * type: object + * description: The promotions to add or remove from the campaign. + * properties: + * add: + * type: array + * description: The promotions to add to the campaign. + * items: + * type: string + * title: add + * description: A promotion's ID. + * remove: + * type: array + * description: The promotions to remove from the campaign. + * items: + * type: string + * title: remove + * description: A promotion's ID. + * tags: + * - Vendor Campaigns + * responses: + * "200": + * description: OK + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/VendorCampaign" + * "400": + * $ref: "#/components/responses/400_error" + * "401": + * $ref: "#/components/responses/unauthorized" + * "404": + * $ref: "#/components/responses/not_found_error" + * "409": + * $ref: "#/components/responses/invalid_state_error" + * "422": + * $ref: "#/components/responses/invalid_request_error" + * "500": + * $ref: "#/components/responses/500_error" + * +*/ + + export const POST = async ( + req: AuthenticatedMedusaRequest, + res: MedusaResponse + ) => { + const { id } = req.params + const { add, remove } = req.validatedBody + await addOrRemoveCampaignPromotionsWorkflow(req.scope).run({ + input: { id, add, remove }, + }) + + const campaign = await refetchCampaign( + req.params.id, + req.scope, + req.queryConfig.fields + ) + + res.status(200).json({ campaign }) + } \ No newline at end of file diff --git a/packages/modules/b2c-core/src/api/vendor/campaigns/middlewares.ts b/packages/modules/b2c-core/src/api/vendor/campaigns/middlewares.ts index f9568dc8b..f09fa1611 100644 --- a/packages/modules/b2c-core/src/api/vendor/campaigns/middlewares.ts +++ b/packages/modules/b2c-core/src/api/vendor/campaigns/middlewares.ts @@ -15,6 +15,7 @@ import { VendorGetCampaignsParams, VendorUpdateCampaign } from './validators' +import { createLinkBody } from '@medusajs/medusa/api/utils/validators' export const vendorCampaignsMiddlewares: MiddlewareRoute[] = [ { @@ -77,5 +78,16 @@ export const vendorCampaignsMiddlewares: MiddlewareRoute[] = [ vendorCampaignQueryConfig.retrieve ) ] - } + }, + { + method: ["POST"], + matcher: "/vendor/campaigns/:id/promotions", + middlewares: [ + validateAndTransformBody(createLinkBody()), + validateAndTransformQuery( + VendorGetCampaignsParams, + vendorCampaignQueryConfig.retrieve + ), + ], + }, ]