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