diff --git a/.changeset/new-baboons-wash.md b/.changeset/new-baboons-wash.md new file mode 100644 index 0000000000..b6e8d2ec58 --- /dev/null +++ b/.changeset/new-baboons-wash.md @@ -0,0 +1,5 @@ +--- +"@medusajs/types": patch +--- + +feat(types): promotion module uses big number diff --git a/integration-tests/plugins/__tests__/promotion/admin/list-campaigns.spec.ts b/integration-tests/plugins/__tests__/promotion/admin/list-campaigns.spec.ts index 8e3a4106bb..61501f6984 100644 --- a/integration-tests/plugins/__tests__/promotion/admin/list-campaigns.spec.ts +++ b/integration-tests/plugins/__tests__/promotion/admin/list-campaigns.spec.ts @@ -1,13 +1,13 @@ import { initDb, useDb } from "../../../../environment-helpers/use-db" -import { CampaignBudgetType } from "@medusajs/utils" -import { IPromotionModuleService } from "@medusajs/types" import { ModuleRegistrationName } from "@medusajs/modules-sdk" -import { createAdminUser } from "../../../helpers/create-admin-user" -import { getContainer } from "../../../../environment-helpers/use-container" +import { IPromotionModuleService } from "@medusajs/types" +import { CampaignBudgetType } from "@medusajs/utils" import path from "path" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { useApi } from "../../../../environment-helpers/use-api" +import { getContainer } from "../../../../environment-helpers/use-container" +import { createAdminUser } from "../../../helpers/create-admin-user" jest.setTimeout(50000) @@ -101,6 +101,14 @@ describe("GET /admin/campaigns", () => { type: "spend", limit: 1000, used: 0, + raw_limit: { + precision: 20, + value: "1000", + }, + raw_used: { + precision: 20, + value: "0", + }, created_at: expect.any(String), updated_at: expect.any(String), deleted_at: null, @@ -123,6 +131,14 @@ describe("GET /admin/campaigns", () => { type: "usage", limit: 1000, used: 0, + raw_limit: { + precision: 20, + value: "1000", + }, + raw_used: { + precision: 20, + value: "0", + }, created_at: expect.any(String), updated_at: expect.any(String), deleted_at: null, diff --git a/integration-tests/plugins/__tests__/promotion/admin/retrieve-campaign.spec.ts b/integration-tests/plugins/__tests__/promotion/admin/retrieve-campaign.spec.ts index aa96fa29cc..417c2d7d3e 100644 --- a/integration-tests/plugins/__tests__/promotion/admin/retrieve-campaign.spec.ts +++ b/integration-tests/plugins/__tests__/promotion/admin/retrieve-campaign.spec.ts @@ -1,13 +1,13 @@ import { initDb, useDb } from "../../../../environment-helpers/use-db" -import { CampaignBudgetType } from "@medusajs/utils" -import { IPromotionModuleService } from "@medusajs/types" import { ModuleRegistrationName } from "@medusajs/modules-sdk" -import { createAdminUser } from "../../../helpers/create-admin-user" -import { getContainer } from "../../../../environment-helpers/use-container" +import { IPromotionModuleService } from "@medusajs/types" +import { CampaignBudgetType } from "@medusajs/utils" import path from "path" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { useApi } from "../../../../environment-helpers/use-api" +import { getContainer } from "../../../../environment-helpers/use-container" +import { createAdminUser } from "../../../helpers/create-admin-user" jest.setTimeout(50000) @@ -102,6 +102,14 @@ describe("GET /admin/campaigns", () => { campaign: expect.any(Object), type: "spend", limit: 1000, + raw_limit: { + precision: 20, + value: "1000", + }, + raw_used: { + precision: 20, + value: "0", + }, used: 0, created_at: expect.any(String), updated_at: expect.any(String), diff --git a/packages/promotion/integration-tests/__tests__/services/promotion-module/campaign.spec.ts b/packages/promotion/integration-tests/__tests__/services/promotion-module/campaign.spec.ts index a5d075f9fa..be18d5fc5a 100644 --- a/packages/promotion/integration-tests/__tests__/services/promotion-module/campaign.spec.ts +++ b/packages/promotion/integration-tests/__tests__/services/promotion-module/campaign.spec.ts @@ -1,11 +1,11 @@ +import { Modules } from "@medusajs/modules-sdk" import { IPromotionModuleService } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" +import { initModules } from "medusa-test-utils" import { createCampaigns } from "../../../__fixtures__/campaigns" import { createPromotions } from "../../../__fixtures__/promotion" import { MikroOrmWrapper } from "../../../utils" import { getInitModuleConfig } from "../../../utils/get-init-module-config" -import { initModules } from "medusa-test-utils" -import { Modules } from "@medusajs/modules-sdk" jest.setTimeout(30000) @@ -92,11 +92,11 @@ describe("Promotion Module Service: Campaigns", () => { { id: "campaign-id-1", name: "campaign 1", - budget: { + budget: expect.objectContaining({ id: expect.any(String), campaign: expect.any(Object), limit: 1000, - }, + }), }, ]) }) diff --git a/packages/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts b/packages/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts index 7ba8ffcd66..2c1af38820 100644 --- a/packages/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts +++ b/packages/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts @@ -83,7 +83,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "each", - value: "200", + value: 200, max_quantity: 1, }, }, @@ -126,7 +126,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "each", - value: "200", + value: 200, max_quantity: 1, }, }, @@ -179,7 +179,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "each", - value: "200", + value: 200, max_quantity: 1, target_rules: [ { @@ -256,7 +256,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "each", - value: "30", + value: 30, max_quantity: 2, target_rules: [ { @@ -284,7 +284,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "each", - value: "50", + value: 50, max_quantity: 1, target_rules: [ { @@ -376,7 +376,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "each", - value: "500", + value: 500, max_quantity: 2, target_rules: [ { @@ -404,7 +404,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "each", - value: "50", + value: 50, max_quantity: 1, target_rules: [ { @@ -487,7 +487,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "each", - value: "500", + value: 500, max_quantity: 5, target_rules: [ { @@ -550,7 +550,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "each", - value: "500", + value: 500, max_quantity: 5, target_rules: [ { @@ -612,7 +612,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "each", - value: "10", + value: 10, max_quantity: 1, target_rules: [ { @@ -689,7 +689,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "each", - value: "30", + value: 30, max_quantity: 2, target_rules: [ { @@ -717,7 +717,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "each", - value: "10", + value: 10, max_quantity: 1, target_rules: [ { @@ -809,7 +809,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "each", - value: "100", + value: 100, max_quantity: 10, target_rules: [ { @@ -837,7 +837,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "each", - value: "50", + value: 50, max_quantity: 10, target_rules: [ { @@ -920,7 +920,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "each", - value: "100", + value: 100, max_quantity: 5, target_rules: [ { @@ -978,7 +978,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "each", - value: "10", + value: 10, max_quantity: 5, target_rules: [ { @@ -1042,7 +1042,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "across", - value: "400", + value: 400, target_rules: [ { attribute: "product_category.id", @@ -1119,7 +1119,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "across", - value: "400", + value: 400, target_rules: [ { attribute: "product_category.id", @@ -1195,7 +1195,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "across", - value: "30", + value: 30, target_rules: [ { attribute: "product_category.id", @@ -1222,7 +1222,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "across", - value: "50", + value: 50, target_rules: [ { attribute: "product_category.id", @@ -1313,7 +1313,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "across", - value: "1000", + value: 1000, target_rules: [ { attribute: "product_category.id", @@ -1340,7 +1340,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "across", - value: "50", + value: 50, target_rules: [ { attribute: "product_category.id", @@ -1422,7 +1422,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "across", - value: "1500", + value: 1500, target_rules: [ { attribute: "product_category.id", @@ -1479,7 +1479,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "across", - value: "500", + value: 500, target_rules: [ { attribute: "product_category.id", @@ -1540,7 +1540,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "across", - value: "10", + value: 10, target_rules: [ { attribute: "product_category.id", @@ -1617,7 +1617,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "across", - value: "10", + value: 10, target_rules: [ { attribute: "product_category.id", @@ -1693,7 +1693,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "across", - value: "10", + value: 10, target_rules: [ { attribute: "product_category.id", @@ -1720,7 +1720,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "across", - value: "10", + value: 10, target_rules: [ { attribute: "product_category.id", @@ -1811,7 +1811,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "across", - value: "10", + value: 10, target_rules: [ { attribute: "product_category.id", @@ -1838,7 +1838,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "across", - value: "10", + value: 10, target_rules: [ { attribute: "product_category.id", @@ -1932,7 +1932,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "across", - value: "100", + value: 100, target_rules: [ { attribute: "product_category.id", @@ -1989,7 +1989,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "items", allocation: "across", - value: "10", + value: 10, target_rules: [ { attribute: "product_category.id", @@ -2052,7 +2052,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "shipping_methods", allocation: "each", - value: "200", + value: 200, max_quantity: 2, target_rules: [ { @@ -2129,7 +2129,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "shipping_methods", allocation: "each", - value: "200", + value: 200, max_quantity: 2, target_rules: [ { @@ -2206,7 +2206,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "shipping_methods", allocation: "each", - value: "200", + value: 200, max_quantity: 2, target_rules: [ { @@ -2273,7 +2273,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "shipping_methods", allocation: "each", - value: "200", + value: 200, max_quantity: 2, target_rules: [ { @@ -2301,7 +2301,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "shipping_methods", allocation: "each", - value: "200", + value: 200, max_quantity: 2, target_rules: [ { @@ -2386,7 +2386,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "shipping_methods", allocation: "each", - value: "500", + value: 500, max_quantity: 2, target_rules: [ { @@ -2414,7 +2414,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "shipping_methods", allocation: "each", - value: "200", + value: 200, max_quantity: 2, target_rules: [ { @@ -2496,7 +2496,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "shipping_methods", allocation: "each", - value: "1200", + value: 1200, max_quantity: 2, target_rules: [ { @@ -2550,7 +2550,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "shipping_methods", allocation: "each", - value: "1200", + value: 1200, max_quantity: 2, target_rules: [ { @@ -2608,7 +2608,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "each", - value: "10", + value: 10, max_quantity: 2, target_rules: [ { @@ -2685,7 +2685,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "each", - value: "10", + value: 10, max_quantity: 2, target_rules: [ { @@ -2762,7 +2762,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "each", - value: "10", + value: 10, max_quantity: 2, target_rules: [ { @@ -2829,7 +2829,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "each", - value: "10", + value: 10, max_quantity: 2, target_rules: [ { @@ -2857,7 +2857,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "each", - value: "10", + value: 10, max_quantity: 2, target_rules: [ { @@ -2948,7 +2948,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "each", - value: "10", + value: 10, max_quantity: 2, target_rules: [ { @@ -2976,7 +2976,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "each", - value: "10", + value: 10, max_quantity: 2, target_rules: [ { @@ -3070,7 +3070,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "each", - value: "100", + value: 100, max_quantity: 2, target_rules: [ { @@ -3124,7 +3124,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "each", - value: "10", + value: 10, max_quantity: 2, target_rules: [ { @@ -3184,7 +3184,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.FIXED, target_type: "shipping_methods", allocation: "across", - value: "200", + value: 200, target_rules: [ { attribute: "shipping_option.id", @@ -3260,7 +3260,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.FIXED, target_type: "shipping_methods", allocation: "across", - value: "200", + value: 200, target_rules: [ { attribute: "shipping_option.id", @@ -3335,7 +3335,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.FIXED, target_type: "shipping_methods", allocation: "across", - value: "200", + value: 200, target_rules: [ { attribute: "shipping_option.id", @@ -3362,7 +3362,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.FIXED, target_type: "shipping_methods", allocation: "across", - value: "200", + value: 200, target_rules: [ { attribute: "shipping_option.id", @@ -3452,7 +3452,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.FIXED, target_type: "shipping_methods", allocation: "across", - value: "1000", + value: 1000, target_rules: [ { attribute: "shipping_option.id", @@ -3479,7 +3479,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.FIXED, target_type: "shipping_methods", allocation: "across", - value: "200", + value: 200, target_rules: [ { attribute: "shipping_option.id", @@ -3560,7 +3560,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.FIXED, target_type: "shipping_methods", allocation: "across", - value: "1200", + value: 1200, target_rules: [ { attribute: "shipping_option.id", @@ -3613,7 +3613,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.FIXED, target_type: "shipping_methods", allocation: "across", - value: "1200", + value: 1200, target_rules: [ { attribute: "shipping_option.id", @@ -3670,7 +3670,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "across", - value: "10", + value: 10, target_rules: [ { attribute: "shipping_option.id", @@ -3746,7 +3746,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "across", - value: "10", + value: 10, target_rules: [ { attribute: "shipping_option.id", @@ -3821,7 +3821,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "across", - value: "10", + value: 10, target_rules: [ { attribute: "shipping_option.id", @@ -3848,7 +3848,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "across", - value: "10", + value: 10, target_rules: [ { attribute: "shipping_option.id", @@ -3938,7 +3938,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "across", - value: "100", + value: 100, target_rules: [ { attribute: "shipping_option.id", @@ -3965,7 +3965,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "across", - value: "10", + value: 10, target_rules: [ { attribute: "shipping_option.id", @@ -4046,7 +4046,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "across", - value: "100", + value: 100, target_rules: [ { attribute: "shipping_option.id", @@ -4099,7 +4099,7 @@ describe("Promotion Service: computeActions", () => { type: ApplicationMethodType.PERCENTAGE, target_type: "shipping_methods", allocation: "across", - value: "10", + value: 10, target_rules: [ { attribute: "shipping_option.id", @@ -4156,7 +4156,7 @@ describe("Promotion Service: computeActions", () => { application_method: { type: "fixed", target_type: "order", - value: "200", + value: 200, max_quantity: 2, }, }, @@ -4226,7 +4226,7 @@ describe("Promotion Service: computeActions", () => { application_method: { type: "fixed", target_type: "order", - value: "200", + value: 200, max_quantity: 2, }, }, @@ -4295,7 +4295,7 @@ describe("Promotion Service: computeActions", () => { application_method: { type: "fixed", target_type: "order", - value: "30", + value: 30, max_quantity: 2, }, }, @@ -4315,7 +4315,7 @@ describe("Promotion Service: computeActions", () => { application_method: { type: "fixed", target_type: "order", - value: "50", + value: 50, max_quantity: 1, }, }, @@ -4399,7 +4399,7 @@ describe("Promotion Service: computeActions", () => { application_method: { type: "fixed", target_type: "order", - value: "500", + value: 500, max_quantity: 2, }, }, @@ -4419,7 +4419,7 @@ describe("Promotion Service: computeActions", () => { application_method: { type: "fixed", target_type: "order", - value: "50", + value: 50, max_quantity: 1, }, }, @@ -4501,7 +4501,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "items", allocation: "each", - value: "200", + value: 200, max_quantity: 1, target_rules: [ { @@ -4596,7 +4596,7 @@ describe("Promotion Service: computeActions", () => { type: "fixed", target_type: "shipping_methods", allocation: "across", - value: "200", + value: 200, target_rules: [ { attribute: "shipping_option.id", diff --git a/packages/promotion/src/migrations/.snapshot-medusa-promotion.json b/packages/promotion/src/migrations/.snapshot-medusa-promotion.json index 109ece07d1..157ed8049f 100644 --- a/packages/promotion/src/migrations/.snapshot-medusa-promotion.json +++ b/packages/promotion/src/migrations/.snapshot-medusa-promotion.json @@ -102,7 +102,7 @@ "mappedType": "datetime" } }, - "name": "campaign", + "name": "promotion_campaign", "schema": "public", "indexes": [ { @@ -113,7 +113,7 @@ "unique": true }, { - "keyName": "campaign_pkey", + "keyName": "promotion_campaign_pkey", "columnNames": ["id"], "composite": false, "primary": true, @@ -160,19 +160,35 @@ "autoincrement": false, "primary": false, "nullable": true, - "default": "null", "mappedType": "decimal" }, + "raw_limit": { + "name": "raw_limit", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, "used": { "name": "used", "type": "numeric", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": false, - "default": "0", + "nullable": true, "mappedType": "decimal" }, + "raw_used": { + "name": "raw_used", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, "created_at": { "name": "created_at", "type": "timestamptz", @@ -206,7 +222,7 @@ "mappedType": "datetime" } }, - "name": "campaign_budget", + "name": "promotion_campaign_budget", "schema": "public", "indexes": [ { @@ -219,12 +235,12 @@ { "columnNames": ["campaign_id"], "composite": false, - "keyName": "campaign_budget_campaign_id_unique", + "keyName": "promotion_campaign_budget_campaign_id_unique", "primary": false, "unique": true }, { - "keyName": "campaign_budget_pkey", + "keyName": "promotion_campaign_budget_pkey", "columnNames": ["id"], "composite": false, "primary": true, @@ -233,12 +249,12 @@ ], "checks": [], "foreignKeys": { - "campaign_budget_campaign_id_foreign": { - "constraintName": "campaign_budget_campaign_id_foreign", + "promotion_campaign_budget_campaign_id_foreign": { + "constraintName": "promotion_campaign_budget_campaign_id_foreign", "columnNames": ["campaign_id"], - "localTableName": "public.campaign_budget", + "localTableName": "public.promotion_campaign_budget", "referencedColumnNames": ["id"], - "referencedTableName": "public.campaign", + "referencedTableName": "public.promotion_campaign", "updateRule": "cascade" } } @@ -364,7 +380,7 @@ "columnNames": ["campaign_id"], "localTableName": "public.promotion", "referencedColumnNames": ["id"], - "referencedTableName": "public.campaign", + "referencedTableName": "public.promotion_campaign", "deleteRule": "set null" } } @@ -389,6 +405,15 @@ "nullable": true, "mappedType": "decimal" }, + "raw_value": { + "name": "raw_value", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, "max_quantity": { "name": "max_quantity", "type": "numeric", @@ -488,7 +513,7 @@ "mappedType": "datetime" } }, - "name": "application_method", + "name": "promotion_application_method", "schema": "public", "indexes": [ { @@ -515,12 +540,12 @@ { "columnNames": ["promotion_id"], "composite": false, - "keyName": "application_method_promotion_id_unique", + "keyName": "promotion_application_method_promotion_id_unique", "primary": false, "unique": true }, { - "keyName": "application_method_pkey", + "keyName": "promotion_application_method_pkey", "columnNames": ["id"], "composite": false, "primary": true, @@ -529,10 +554,10 @@ ], "checks": [], "foreignKeys": { - "application_method_promotion_id_foreign": { - "constraintName": "application_method_promotion_id_foreign", + "promotion_application_method_promotion_id_foreign": { + "constraintName": "promotion_application_method_promotion_id_foreign", "columnNames": ["promotion_id"], - "localTableName": "public.application_method", + "localTableName": "public.promotion_application_method", "referencedColumnNames": ["id"], "referencedTableName": "public.promotion", "deleteRule": "cascade", @@ -733,7 +758,7 @@ "columnNames": ["application_method_id"], "localTableName": "public.application_method_target_rules", "referencedColumnNames": ["id"], - "referencedTableName": "public.application_method", + "referencedTableName": "public.promotion_application_method", "deleteRule": "cascade", "updateRule": "cascade" }, @@ -787,7 +812,7 @@ "columnNames": ["application_method_id"], "localTableName": "public.application_method_buy_rules", "referencedColumnNames": ["id"], - "referencedTableName": "public.application_method", + "referencedTableName": "public.promotion_application_method", "deleteRule": "cascade", "updateRule": "cascade" }, diff --git a/packages/promotion/src/migrations/Migration20240122084316.ts b/packages/promotion/src/migrations/Migration20240122084316.ts deleted file mode 100644 index db3a0e841d..0000000000 --- a/packages/promotion/src/migrations/Migration20240122084316.ts +++ /dev/null @@ -1,113 +0,0 @@ -import { Migration } from "@mikro-orm/migrations" - -export class Migration20240122084316 extends Migration { - async up(): Promise { - this.addSql( - 'create table "campaign" ("id" text not null, "name" text not null, "description" text null, "currency" text null, "campaign_identifier" text not null, "starts_at" timestamptz null, "ends_at" timestamptz null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "campaign_pkey" primary key ("id"));' - ) - this.addSql( - 'alter table "campaign" add constraint "IDX_campaign_identifier_unique" unique ("campaign_identifier");' - ) - - this.addSql( - 'create table "campaign_budget" ("id" text not null, "type" text check ("type" in (\'spend\', \'usage\')) not null, "campaign_id" text not null, "limit" numeric null default null, "used" numeric not null default 0, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "campaign_budget_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_campaign_budget_type" on "campaign_budget" ("type");' - ) - this.addSql( - 'alter table "campaign_budget" add constraint "campaign_budget_campaign_id_unique" unique ("campaign_id");' - ) - - this.addSql( - 'create table "promotion" ("id" text not null, "code" text not null, "campaign_id" text null, "is_automatic" boolean not null default false, "type" text check ("type" in (\'standard\', \'buyget\')) not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_pkey" primary key ("id"));' - ) - this.addSql('create index "IDX_promotion_code" on "promotion" ("code");') - this.addSql('create index "IDX_promotion_type" on "promotion" ("type");') - this.addSql( - 'alter table "promotion" add constraint "IDX_promotion_code_unique" unique ("code");' - ) - - this.addSql( - 'create table "application_method" ("id" text not null, "value" numeric null, "max_quantity" numeric null, "apply_to_quantity" numeric null, "buy_rules_min_quantity" numeric null, "type" text check ("type" in (\'fixed\', \'percentage\')) not null, "target_type" text check ("target_type" in (\'order\', \'shipping_methods\', \'items\')) not null, "allocation" text check ("allocation" in (\'each\', \'across\')) null, "promotion_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "application_method_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_application_method_type" on "application_method" ("type");' - ) - this.addSql( - 'create index "IDX_application_method_target_type" on "application_method" ("target_type");' - ) - this.addSql( - 'create index "IDX_application_method_allocation" on "application_method" ("allocation");' - ) - this.addSql( - 'alter table "application_method" add constraint "application_method_promotion_id_unique" unique ("promotion_id");' - ) - - this.addSql( - 'create table "promotion_rule" ("id" text not null, "description" text null, "attribute" text not null, "operator" text check ("operator" in (\'gte\', \'lte\', \'gt\', \'lt\', \'eq\', \'ne\', \'in\')) not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_rule_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_promotion_rule_attribute" on "promotion_rule" ("attribute");' - ) - this.addSql( - 'create index "IDX_promotion_rule_operator" on "promotion_rule" ("operator");' - ) - - this.addSql( - 'create table "promotion_promotion_rule" ("promotion_id" text not null, "promotion_rule_id" text not null, constraint "promotion_promotion_rule_pkey" primary key ("promotion_id", "promotion_rule_id"));' - ) - - this.addSql( - 'create table "application_method_target_rules" ("application_method_id" text not null, "promotion_rule_id" text not null, constraint "application_method_target_rules_pkey" primary key ("application_method_id", "promotion_rule_id"));' - ) - - this.addSql( - 'create table "application_method_buy_rules" ("application_method_id" text not null, "promotion_rule_id" text not null, constraint "application_method_buy_rules_pkey" primary key ("application_method_id", "promotion_rule_id"));' - ) - - this.addSql( - 'create table "promotion_rule_value" ("id" text not null, "promotion_rule_id" text not null, "value" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_rule_value_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_promotion_rule_promotion_rule_value_id" on "promotion_rule_value" ("promotion_rule_id");' - ) - - this.addSql( - 'alter table "campaign_budget" add constraint "campaign_budget_campaign_id_foreign" foreign key ("campaign_id") references "campaign" ("id") on update cascade;' - ) - - this.addSql( - 'alter table "promotion" add constraint "promotion_campaign_id_foreign" foreign key ("campaign_id") references "campaign" ("id") on delete set null;' - ) - - this.addSql( - 'alter table "application_method" add constraint "application_method_promotion_id_foreign" foreign key ("promotion_id") references "promotion" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - 'alter table "promotion_promotion_rule" add constraint "promotion_promotion_rule_promotion_id_foreign" foreign key ("promotion_id") references "promotion" ("id") on update cascade on delete cascade;' - ) - this.addSql( - 'alter table "promotion_promotion_rule" add constraint "promotion_promotion_rule_promotion_rule_id_foreign" foreign key ("promotion_rule_id") references "promotion_rule" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - 'alter table "application_method_target_rules" add constraint "application_method_target_rules_application_method_id_foreign" foreign key ("application_method_id") references "application_method" ("id") on update cascade on delete cascade;' - ) - this.addSql( - 'alter table "application_method_target_rules" add constraint "application_method_target_rules_promotion_rule_id_foreign" foreign key ("promotion_rule_id") references "promotion_rule" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - 'alter table "application_method_buy_rules" add constraint "application_method_buy_rules_application_method_id_foreign" foreign key ("application_method_id") references "application_method" ("id") on update cascade on delete cascade;' - ) - this.addSql( - 'alter table "application_method_buy_rules" add constraint "application_method_buy_rules_promotion_rule_id_foreign" foreign key ("promotion_rule_id") references "promotion_rule" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - 'alter table "promotion_rule_value" add constraint "promotion_rule_value_promotion_rule_id_foreign" foreign key ("promotion_rule_id") references "promotion_rule" ("id") on update cascade on delete cascade;' - ) - } -} diff --git a/packages/promotion/src/migrations/Migration20240227120221.ts b/packages/promotion/src/migrations/Migration20240227120221.ts new file mode 100644 index 0000000000..76c659170c --- /dev/null +++ b/packages/promotion/src/migrations/Migration20240227120221.ts @@ -0,0 +1,117 @@ +import { Migration } from "@mikro-orm/migrations" + +export class Migration20240227120221 extends Migration { + async up(): Promise { + this.addSql( + 'create table if not exists "promotion_campaign" ("id" text not null, "name" text not null, "description" text null, "currency" text null, "campaign_identifier" text not null, "starts_at" timestamptz null, "ends_at" timestamptz null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_campaign_pkey" primary key ("id"));' + ) + this.addSql( + 'alter table if exists "promotion_campaign" add constraint "IDX_campaign_identifier_unique" unique ("campaign_identifier");' + ) + + this.addSql( + 'create table if not exists "promotion_campaign_budget" ("id" text not null, "type" text check ("type" in (\'spend\', \'usage\')) not null, "campaign_id" text not null, "limit" numeric null, "raw_limit" jsonb null, "used" numeric null, "raw_used" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_campaign_budget_pkey" primary key ("id"));' + ) + this.addSql( + 'create index if not exists "IDX_campaign_budget_type" on "promotion_campaign_budget" ("type");' + ) + this.addSql( + 'alter table if exists "promotion_campaign_budget" add constraint "promotion_campaign_budget_campaign_id_unique" unique ("campaign_id");' + ) + + this.addSql( + 'create table if not exists "promotion" ("id" text not null, "code" text not null, "campaign_id" text null, "is_automatic" boolean not null default false, "type" text check ("type" in (\'standard\', \'buyget\')) not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_pkey" primary key ("id"));' + ) + this.addSql( + 'create index if not exists "IDX_promotion_code" on "promotion" ("code");' + ) + this.addSql( + 'create index if not exists "IDX_promotion_type" on "promotion" ("type");' + ) + this.addSql( + 'alter table if exists "promotion" add constraint "IDX_promotion_code_unique" unique ("code");' + ) + + this.addSql( + 'create table if not exists "promotion_application_method" ("id" text not null, "value" numeric null, "raw_value" jsonb null, "max_quantity" numeric null, "apply_to_quantity" numeric null, "buy_rules_min_quantity" numeric null, "type" text check ("type" in (\'fixed\', \'percentage\')) not null, "target_type" text check ("target_type" in (\'order\', \'shipping_methods\', \'items\')) not null, "allocation" text check ("allocation" in (\'each\', \'across\')) null, "promotion_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_application_method_pkey" primary key ("id"));' + ) + this.addSql( + 'create index if not exists "IDX_application_method_type" on "promotion_application_method" ("type");' + ) + this.addSql( + 'create index if not exists "IDX_application_method_target_type" on "promotion_application_method" ("target_type");' + ) + this.addSql( + 'create index if not exists "IDX_application_method_allocation" on "promotion_application_method" ("allocation");' + ) + this.addSql( + 'alter table if exists "promotion_application_method" add constraint "promotion_application_method_promotion_id_unique" unique ("promotion_id");' + ) + + this.addSql( + 'create table if not exists "promotion_rule" ("id" text not null, "description" text null, "attribute" text not null, "operator" text check ("operator" in (\'gte\', \'lte\', \'gt\', \'lt\', \'eq\', \'ne\', \'in\')) not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_rule_pkey" primary key ("id"));' + ) + this.addSql( + 'create index if not exists "IDX_promotion_rule_attribute" on "promotion_rule" ("attribute");' + ) + this.addSql( + 'create index if not exists "IDX_promotion_rule_operator" on "promotion_rule" ("operator");' + ) + + this.addSql( + 'create table if not exists "promotion_promotion_rule" ("promotion_id" text not null, "promotion_rule_id" text not null, constraint "promotion_promotion_rule_pkey" primary key ("promotion_id", "promotion_rule_id"));' + ) + + this.addSql( + 'create table if not exists "application_method_target_rules" ("application_method_id" text not null, "promotion_rule_id" text not null, constraint "application_method_target_rules_pkey" primary key ("application_method_id", "promotion_rule_id"));' + ) + + this.addSql( + 'create table if not exists "application_method_buy_rules" ("application_method_id" text not null, "promotion_rule_id" text not null, constraint "application_method_buy_rules_pkey" primary key ("application_method_id", "promotion_rule_id"));' + ) + + this.addSql( + 'create table if not exists "promotion_rule_value" ("id" text not null, "promotion_rule_id" text not null, "value" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_rule_value_pkey" primary key ("id"));' + ) + this.addSql( + 'create index if not exists "IDX_promotion_rule_promotion_rule_value_id" on "promotion_rule_value" ("promotion_rule_id");' + ) + + this.addSql( + 'alter table if exists "promotion_campaign_budget" add constraint "promotion_campaign_budget_campaign_id_foreign" foreign key ("campaign_id") references "promotion_campaign" ("id") on update cascade;' + ) + + this.addSql( + 'alter table if exists "promotion" add constraint "promotion_campaign_id_foreign" foreign key ("campaign_id") references "promotion_campaign" ("id") on delete set null;' + ) + + this.addSql( + 'alter table if exists "promotion_application_method" add constraint "promotion_application_method_promotion_id_foreign" foreign key ("promotion_id") references "promotion" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table if exists "promotion_promotion_rule" add constraint "promotion_promotion_rule_promotion_id_foreign" foreign key ("promotion_id") references "promotion" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'alter table if exists "promotion_promotion_rule" add constraint "promotion_promotion_rule_promotion_rule_id_foreign" foreign key ("promotion_rule_id") references "promotion_rule" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table if exists "application_method_target_rules" add constraint "application_method_target_rules_application_method_id_foreign" foreign key ("application_method_id") references "promotion_application_method" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'alter table if exists "application_method_target_rules" add constraint "application_method_target_rules_promotion_rule_id_foreign" foreign key ("promotion_rule_id") references "promotion_rule" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table if exists "application_method_buy_rules" add constraint "application_method_buy_rules_application_method_id_foreign" foreign key ("application_method_id") references "promotion_application_method" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'alter table if exists "application_method_buy_rules" add constraint "application_method_buy_rules_promotion_rule_id_foreign" foreign key ("promotion_rule_id") references "promotion_rule" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table if exists "promotion_rule_value" add constraint "promotion_rule_value_promotion_rule_id_foreign" foreign key ("promotion_rule_id") references "promotion_rule" ("id") on update cascade on delete cascade;' + ) + } +} diff --git a/packages/promotion/src/models/application-method.ts b/packages/promotion/src/models/application-method.ts index e4201b53f0..a089d83484 100644 --- a/packages/promotion/src/models/application-method.ts +++ b/packages/promotion/src/models/application-method.ts @@ -2,9 +2,16 @@ import { ApplicationMethodAllocationValues, ApplicationMethodTargetTypeValues, ApplicationMethodTypeValues, + BigNumberRawValue, DAL, } from "@medusajs/types" -import { DALUtils, PromotionUtils, generateEntityId } from "@medusajs/utils" +import { + BigNumber, + DALUtils, + MikroOrmBigNumberProperty, + PromotionUtils, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Collection, @@ -30,7 +37,7 @@ type OptionalFields = | "allocation" | DAL.SoftDeletableEntityDateColumns -@Entity({ tableName: "application_method" }) +@Entity({ tableName: "promotion_application_method" }) @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class ApplicationMethod { [OptionalProps]?: OptionalFields @@ -38,8 +45,11 @@ export default class ApplicationMethod { @PrimaryKey({ columnType: "text" }) id!: string - @Property({ columnType: "numeric", nullable: true, serializer: Number }) - value?: string | null = null + @MikroOrmBigNumberProperty({ nullable: true }) + value: BigNumber | number | null = null + + @Property({ columnType: "jsonb", nullable: true }) + raw_value: BigNumberRawValue | null = null @Property({ columnType: "numeric", nullable: true, serializer: Number }) max_quantity?: number | null = null diff --git a/packages/promotion/src/models/campaign-budget.ts b/packages/promotion/src/models/campaign-budget.ts index 4ecf0f9272..e4aa7def55 100644 --- a/packages/promotion/src/models/campaign-budget.ts +++ b/packages/promotion/src/models/campaign-budget.ts @@ -1,5 +1,15 @@ -import { CampaignBudgetTypeValues, DAL } from "@medusajs/types" -import { DALUtils, PromotionUtils, generateEntityId } from "@medusajs/utils" +import { + BigNumberRawValue, + CampaignBudgetTypeValues, + DAL, +} from "@medusajs/types" +import { + BigNumber, + DALUtils, + MikroOrmBigNumberProperty, + PromotionUtils, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Entity, @@ -20,7 +30,7 @@ type OptionalFields = | "used" | DAL.SoftDeletableEntityDateColumns -@Entity({ tableName: "campaign_budget" }) +@Entity({ tableName: "promotion_campaign_budget" }) @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class CampaignBudget { [OptionalProps]?: OptionalFields @@ -37,20 +47,17 @@ export default class CampaignBudget { }) campaign: Campaign | null = null - @Property({ - columnType: "numeric", - nullable: true, - serializer: Number, - default: null, - }) - limit: number | null = null + @MikroOrmBigNumberProperty({ nullable: true }) + limit: BigNumber | number | null = null - @Property({ - columnType: "numeric", - serializer: Number, - default: 0, - }) - used: number + @Property({ columnType: "jsonb", nullable: true }) + raw_limit: BigNumberRawValue | null = null + + @MikroOrmBigNumberProperty({ nullable: true }) + used: BigNumber | number | null = null + + @Property({ columnType: "jsonb", nullable: true }) + raw_used: BigNumberRawValue | null = null @Property({ onCreate: () => new Date(), diff --git a/packages/promotion/src/models/campaign.ts b/packages/promotion/src/models/campaign.ts index db01139ff9..056f8ea802 100644 --- a/packages/promotion/src/models/campaign.ts +++ b/packages/promotion/src/models/campaign.ts @@ -24,7 +24,7 @@ type OptionalFields = | "ends_at" | DAL.SoftDeletableEntityDateColumns -@Entity({ tableName: "campaign" }) +@Entity({ tableName: "promotion_campaign" }) @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class Campaign { [OptionalProps]?: OptionalFields | OptionalRelations diff --git a/packages/promotion/src/services/promotion-module.ts b/packages/promotion/src/services/promotion-module.ts index 2427fba0bb..6df34ea435 100644 --- a/packages/promotion/src/services/promotion-module.ts +++ b/packages/promotion/src/services/promotion-module.ts @@ -247,7 +247,7 @@ export default class PromotionModuleService< ? [] : await this.list( { is_automatic: true }, - { select: ["code"] }, + { select: ["code"], take: null }, sharedContext ) @@ -303,6 +303,7 @@ export default class PromotionModuleService< "campaign", "campaign.budget", ], + take: null, } ) @@ -462,6 +463,7 @@ export default class PromotionModuleService< "campaign", "campaign.budget", ], + take: null, }, sharedContext ) @@ -674,6 +676,7 @@ export default class PromotionModuleService< "campaign", "campaign.budget", ], + take: null, }, sharedContext ) @@ -1046,6 +1049,7 @@ export default class PromotionModuleService< { id: createdCampaigns.map((p) => p!.id) }, { relations: ["budget", "promotions"], + take: null, }, sharedContext ) @@ -1075,7 +1079,7 @@ export default class PromotionModuleService< const promotionsToAdd = promotions ? await this.list( { id: promotions.map((p) => p.id) }, - {}, + { take: null }, sharedContext ) : [] @@ -1137,13 +1141,13 @@ export default class PromotionModuleService< @MedusaContext() sharedContext: Context = {} ): Promise { const input = Array.isArray(data) ? data : [data] - const updatedCampaigns = await this.updateCampaigns_(input, sharedContext) const campaigns = await this.listCampaigns( { id: updatedCampaigns.map((p) => p!.id) }, { relations: ["budget", "promotions"], + take: null, }, sharedContext ) @@ -1162,7 +1166,7 @@ export default class PromotionModuleService< const existingCampaigns = await this.listCampaigns( { id: campaignIds }, - { relations: ["budget"] }, + { relations: ["budget"], take: null }, sharedContext ) @@ -1186,10 +1190,16 @@ export default class PromotionModuleService< } } - const updatedCampaigns = await this.campaignService_.update(campaignsData) + const updatedCampaigns = await this.campaignService_.update( + campaignsData, + sharedContext + ) if (campaignBudgetsData.length) { - await this.campaignBudgetService_.update(campaignBudgetsData) + await this.campaignBudgetService_.update( + campaignBudgetsData, + sharedContext + ) } return updatedCampaigns diff --git a/packages/promotion/src/types/application-method.ts b/packages/promotion/src/types/application-method.ts index 3ba19ad906..374659f642 100644 --- a/packages/promotion/src/types/application-method.ts +++ b/packages/promotion/src/types/application-method.ts @@ -11,7 +11,7 @@ export interface CreateApplicationMethodDTO { type: ApplicationMethodTypeValues target_type: ApplicationMethodTargetTypeValues allocation?: ApplicationMethodAllocationValues - value?: string | null + value?: number promotion: Promotion | string | PromotionDTO max_quantity?: number | null buy_rules_min_quantity?: number | null @@ -23,7 +23,7 @@ export interface UpdateApplicationMethodDTO { type?: ApplicationMethodTypeValues target_type?: ApplicationMethodTargetTypeValues allocation?: ApplicationMethodAllocationValues - value?: string | null + value?: number promotion?: Promotion | string | PromotionDTO max_quantity?: number | null buy_rules_min_quantity?: number | null diff --git a/packages/promotion/src/utils/compute-actions/items.ts b/packages/promotion/src/utils/compute-actions/items.ts index 4168559aac..77b55c8634 100644 --- a/packages/promotion/src/utils/compute-actions/items.ts +++ b/packages/promotion/src/utils/compute-actions/items.ts @@ -75,9 +75,8 @@ export function applyPromotionToItems( ) const totalItemValue = (method.subtotal / method.quantity) * quantityMultiplier - let promotionValue = parseFloat(applicationMethod!.value!) + let promotionValue = applicationMethod?.value ?? 0 const applicableTotal = totalItemValue - appliedPromoValue - if (applicationMethod?.type === ApplicationMethodType.PERCENTAGE) { promotionValue = (promotionValue / 100) * applicableTotal } @@ -130,7 +129,7 @@ export function applyPromotionToItems( } const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0 - const promotionValue = parseFloat(applicationMethod!.value!) + const promotionValue = applicationMethod?.value ?? 0 const applicableTotal = (method.subtotal / method.quantity) * method.quantity - appliedPromoValue diff --git a/packages/promotion/src/utils/compute-actions/shipping-methods.ts b/packages/promotion/src/utils/compute-actions/shipping-methods.ts index 75f0c06cd9..ec3fadbc21 100644 --- a/packages/promotion/src/utils/compute-actions/shipping-methods.ts +++ b/packages/promotion/src/utils/compute-actions/shipping-methods.ts @@ -60,7 +60,7 @@ export function applyPromotionToShippingMethods( } const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0 - let promotionValue = parseFloat(applicationMethod!.value!) + let promotionValue = applicationMethod?.value ?? 0 const applicableTotal = method.subtotal - appliedPromoValue if (applicationMethod?.type === ApplicationMethodType.PERCENTAGE) { @@ -111,7 +111,7 @@ export function applyPromotionToShippingMethods( continue } - const promotionValue = parseFloat(applicationMethod!.value!) + const promotionValue = applicationMethod?.value ?? 0 const applicableTotal = method.subtotal const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0 diff --git a/packages/promotion/src/utils/validations/application-method.ts b/packages/promotion/src/utils/validations/application-method.ts index 2dfea0df8e..6e37d1e6c6 100644 --- a/packages/promotion/src/utils/validations/application-method.ts +++ b/packages/promotion/src/utils/validations/application-method.ts @@ -36,14 +36,14 @@ export function validateApplicationMethodAttributes( const targetType = data.target_type || applicationMethod?.target_type const type = data.type || applicationMethod?.type const applicationMethodType = data.type || applicationMethod?.type - const value = parseFloat(data.value! || applicationMethod?.value!) + const value = data.value || applicationMethod.value const maxQuantity = data.max_quantity || applicationMethod.max_quantity const allocation = data.allocation || applicationMethod.allocation const allTargetTypes: string[] = Object.values(ApplicationMethodTargetType) if ( type === ApplicationMethodType.PERCENTAGE && - (value <= 0 || value > 100) + (typeof value !== "number" || value <= 0 || value > 100) ) { throw new MedusaError( MedusaError.Types.INVALID_DATA, diff --git a/packages/types/src/promotion/common/application-method.ts b/packages/types/src/promotion/common/application-method.ts index 4a5e469853..a8c2a55529 100644 --- a/packages/types/src/promotion/common/application-method.ts +++ b/packages/types/src/promotion/common/application-method.ts @@ -14,7 +14,7 @@ export interface ApplicationMethodDTO { type?: ApplicationMethodTypeValues target_type?: ApplicationMethodTargetTypeValues allocation?: ApplicationMethodAllocationValues - value?: string | null + value?: number max_quantity?: number | null buy_rules_min_quantity?: number | null apply_to_quantity?: number | null @@ -27,7 +27,7 @@ export interface CreateApplicationMethodDTO { type: ApplicationMethodTypeValues target_type: ApplicationMethodTargetTypeValues allocation?: ApplicationMethodAllocationValues - value?: string | null + value?: number max_quantity?: number | null buy_rules_min_quantity?: number | null apply_to_quantity?: number | null @@ -41,7 +41,7 @@ export interface UpdateApplicationMethodDTO { type?: ApplicationMethodTypeValues target_type?: ApplicationMethodTargetTypeValues allocation?: ApplicationMethodAllocationValues - value?: string | null + value?: number max_quantity?: number | null buy_rules_min_quantity?: number | null apply_to_quantity?: number | null