feat(promotion,dashboard,types,utils,medusa): Add statuses to promotions (#10950)

what:

- adds a status column to promotion table
- introduce active promotion query
- scope revert, register and compute actions to active promotions
- admin to create and update promotion with statuses

RESOLVES CMRC-845
RESOLVES CMRC-846
RESOLVES CMRC-847
RESOLVES CMRC-848
RESOLVES CMRC-849
RESOLVES CMRC-850
This commit is contained in:
Riqwan Thamir
2025-01-16 19:17:22 +00:00
committed by GitHub
parent effee5c8bb
commit 5eab9e7399
35 changed files with 1208 additions and 1743 deletions
@@ -1,5 +1,9 @@
import { CampaignBudgetType, PromotionType } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
CampaignBudgetType,
PromotionStatus,
PromotionType,
} from "@medusajs/utils"
import { createAdminUser } from "../../../../helpers/create-admin-user"
jest.setTimeout(50000)
@@ -46,6 +50,7 @@ export const campaignsData = [
const promotionData = {
code: "TEST",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
is_automatic: true,
application_method: {
target_type: "items",
@@ -108,6 +113,7 @@ medusaIntegrationTestRunner({
return {
code,
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
is_automatic: true,
application_method: {
target_type: "items",
@@ -5,6 +5,7 @@ import {
PriceListType,
ProductStatus,
PromotionRuleOperator,
PromotionStatus,
PromotionType,
} from "@medusajs/utils"
import {
@@ -176,6 +177,7 @@ medusaIntegrationTestRunner({
{
code: "PROMOTION_APPLIED",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
application_method: {
type: "fixed",
target_type: "items",
@@ -488,6 +490,65 @@ medusaIntegrationTestRunner({
)
})
it("should remove promotions when promotion is no longer in active state", async () => {
let responseBeforePromotionUpdate = await api.post(
`/store/carts/${cart.id}/line-items`,
{
variant_id: product.variants[0].id,
quantity: 1,
},
storeHeaders
)
expect(responseBeforePromotionUpdate.data.cart).toEqual(
expect.objectContaining({
id: cart.id,
items: expect.arrayContaining([
expect.objectContaining({
adjustments: [
{
id: expect.any(String),
code: "PROMOTION_APPLIED",
promotion_id: promotion.id,
amount: 100,
},
],
}),
]),
})
)
await api.post(
`/admin/promotions/${promotion.id}`,
{ status: PromotionStatus.INACTIVE },
adminHeaders
)
let responseAfterPromotionUpdate = await api.post(
`/store/carts/${cart.id}/line-items`,
{
variant_id: product.variants[0].id,
quantity: 1,
},
storeHeaders
)
expect(responseAfterPromotionUpdate.status).toEqual(200)
expect(responseAfterPromotionUpdate.data.cart).toEqual(
expect.objectContaining({
id: cart.id,
items: expect.arrayContaining([
expect.objectContaining({
adjustments: [],
}),
expect.objectContaining({
adjustments: [],
}),
]),
})
)
})
describe("with custom shipping options prices", () => {
beforeEach(async () => {
cart = (
@@ -1205,6 +1266,7 @@ medusaIntegrationTestRunner({
{
code: "PROMOTION_TEST",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
application_method: {
type: "fixed",
target_type: "items",
@@ -1,5 +1,5 @@
import { PromotionType } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { PromotionStatus, PromotionType } from "@medusajs/utils"
import { createAdminUser } from "../../../../helpers/create-admin-user"
jest.setTimeout(50000)
@@ -8,6 +8,43 @@ const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
const standardPromotionPayload = {
code: "TEST",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
is_automatic: true,
campaign: {
name: "test",
campaign_identifier: "test-1",
budget: {
type: "usage",
limit: 100,
},
},
application_method: {
target_type: "items",
type: "fixed",
allocation: "each",
currency_code: "USD",
value: 100,
max_quantity: 100,
target_rules: [
{
attribute: "test.test",
operator: "eq",
values: ["test1", "test2"],
},
],
},
rules: [
{
attribute: "test.test",
operator: "eq",
values: ["test1", "test2"],
},
],
}
medusaIntegrationTestRunner({
testSuite: ({ dbConnection, getContainer, api }) => {
describe("Admin Promotions API", () => {
@@ -34,6 +71,7 @@ medusaIntegrationTestRunner({
{
code: "TEST_ACROSS",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
application_method: {
type: "fixed",
allocation: "across",
@@ -120,6 +158,7 @@ medusaIntegrationTestRunner({
{
code: "first",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
application_method: {
type: "fixed",
target_type: "order",
@@ -183,41 +222,7 @@ medusaIntegrationTestRunner({
it("should create a standard promotion successfully", async () => {
const response = await api.post(
`/admin/promotions`,
{
code: "TEST",
type: PromotionType.STANDARD,
is_automatic: true,
campaign: {
name: "test",
campaign_identifier: "test-1",
budget: {
type: "usage",
limit: 100,
},
},
application_method: {
target_type: "items",
type: "fixed",
allocation: "each",
currency_code: "USD",
value: 100,
max_quantity: 100,
target_rules: [
{
attribute: "test.test",
operator: "eq",
values: ["test1", "test2"],
},
],
},
rules: [
{
attribute: "test.test",
operator: "eq",
values: ["test1", "test2"],
},
],
},
standardPromotionPayload,
adminHeaders
)
@@ -275,6 +280,7 @@ medusaIntegrationTestRunner({
{
code: "TEST",
type: PromotionType.BUYGET,
status: PromotionStatus.ACTIVE,
is_automatic: true,
application_method: {
target_type: "items",
@@ -316,6 +322,7 @@ medusaIntegrationTestRunner({
{
code: "TEST",
type: PromotionType.BUYGET,
status: PromotionStatus.ACTIVE,
is_automatic: true,
application_method: {
target_type: "items",
@@ -356,6 +363,7 @@ medusaIntegrationTestRunner({
{
code: "TEST",
type: PromotionType.BUYGET,
status: PromotionStatus.ACTIVE,
is_automatic: true,
campaign: {
name: "test",
@@ -457,6 +465,23 @@ medusaIntegrationTestRunner({
})
)
})
it("should throw error when an incorrect status is passed", async () => {
const { response } = await api
.post(
`/admin/promotions`,
{ ...standardPromotionPayload, status: "does-not-exist" },
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(400)
expect(response.data).toEqual({
type: "invalid_data",
message:
"Invalid request: Expected: 'draft, active, inactive' for field 'status', but got: 'does-not-exist'",
})
})
})
describe("DELETE /admin/promotions/:id", () => {
@@ -491,7 +516,7 @@ medusaIntegrationTestRunner({
expect(response.status).toEqual(404)
expect(response.data.message).toEqual(
`Promotion with id "does-not-exist" not found`
`Promotion id not found: does-not-exist`
)
})
@@ -1,11 +1,12 @@
import { RemoteLink } from "@medusajs/modules-sdk"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { ICartModuleService, IPromotionModuleService } from "@medusajs/types"
import {
ContainerRegistrationKeys,
Modules,
PromotionStatus,
PromotionType,
} from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
adminHeaders,
createAdminUser,
@@ -48,11 +49,12 @@ medusaIntegrationTestRunner({
await promotionModuleService.createPromotions({
code: "PROMOTION_APPLIED",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
application_method: {
type: "fixed",
target_type: "items",
allocation: "each",
value: "300",
value: 300,
apply_to_quantity: 1,
currency_code: "usd",
max_quantity: 1,
@@ -70,11 +72,12 @@ medusaIntegrationTestRunner({
await promotionModuleService.createPromotions({
code: "PROMOTION_TEST",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
application_method: {
type: "fixed",
target_type: "items",
allocation: "across",
value: "1000",
value: 1000,
apply_to_quantity: 1,
currency_code: "usd",
target_rules: [
@@ -165,6 +168,7 @@ medusaIntegrationTestRunner({
{
code: "PROMOTION_APPLIED",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
rules: [
{
attribute: "customer_id",
@@ -181,7 +185,7 @@ medusaIntegrationTestRunner({
type: "fixed",
target_type: "shipping_methods",
allocation: "each",
value: "100",
value: 100,
max_quantity: 1,
currency_code: "usd",
target_rules: [
@@ -199,6 +203,7 @@ medusaIntegrationTestRunner({
{
code: "PROMOTION_NEW",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
rules: [
{
attribute: "customer_id",
@@ -215,7 +220,7 @@ medusaIntegrationTestRunner({
type: "fixed",
target_type: "shipping_methods",
allocation: "each",
value: "200",
value: 200,
max_quantity: 1,
currency_code: "usd",
target_rules: [
@@ -19,6 +19,7 @@ import {
MedusaError,
Modules,
ProductStatus,
PromotionStatus,
PromotionType,
} from "@medusajs/utils"
import {
@@ -1266,6 +1267,7 @@ medusaIntegrationTestRunner({
{
code: "TEST",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
is_automatic: true,
campaign: {
campaign_identifier: "test",
@@ -1,11 +1,12 @@
import { RemoteLink } from "@medusajs/modules-sdk"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { ICartModuleService, IPromotionModuleService } from "@medusajs/types"
import {
ContainerRegistrationKeys,
Modules,
PromotionStatus,
PromotionType,
} from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
adminHeaders,
createAdminUser,
@@ -48,11 +49,12 @@ medusaIntegrationTestRunner({
await promotionModuleService.createPromotions({
code: "PROMOTION_APPLIED",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
application_method: {
type: "fixed",
target_type: "items",
allocation: "each",
value: "300",
value: 300,
apply_to_quantity: 1,
currency_code: "usd",
max_quantity: 1,
@@ -70,11 +72,12 @@ medusaIntegrationTestRunner({
await promotionModuleService.createPromotions({
code: "PROMOTION_APPLIED_TO_REMOVE",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
application_method: {
type: "fixed",
target_type: "items",
allocation: "each",
value: "300",
value: 300,
apply_to_quantity: 1,
currency_code: "usd",
max_quantity: 1,
@@ -180,6 +183,7 @@ medusaIntegrationTestRunner({
const appliedPromotion =
await promotionModuleService.createPromotions({
code: "PROMOTION_APPLIED",
status: PromotionStatus.ACTIVE,
type: PromotionType.STANDARD,
rules: [
{
@@ -197,7 +201,7 @@ medusaIntegrationTestRunner({
type: "fixed",
target_type: "shipping_methods",
allocation: "each",
value: "100",
value: 100,
max_quantity: 1,
currency_code: "usd",
target_rules: [
@@ -214,6 +218,7 @@ medusaIntegrationTestRunner({
await promotionModuleService.createPromotions({
code: "PROMOTION_APPLIED_TO_REMOVE",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
rules: [
{
attribute: "customer_id",
@@ -230,7 +235,7 @@ medusaIntegrationTestRunner({
type: "fixed",
target_type: "shipping_methods",
allocation: "each",
value: "100",
value: 100,
max_quantity: 1,
currency_code: "usd",
target_rules: [