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 authored and GitHub committed 2025-01-16 19:17:22 +00:00
1 parent effee5c8bb
commit 5eab9e7399
35 files changed
+1208 -1743

No files matched your search

@@ -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`
)
})