@medusajs/promotion 0.0.5-snapshot-20240523103337 → 0.0.5-snapshot-20240523153451
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/migrations/Migration20240227120221.d.ts.map +1 -1
- package/dist/migrations/Migration20240227120221.js +10 -2
- package/dist/models/application-method.d.ts +3 -5
- package/dist/models/application-method.d.ts.map +1 -1
- package/dist/models/application-method.js +15 -5
- package/dist/models/campaign-budget.d.ts +3 -2
- package/dist/models/campaign-budget.d.ts.map +1 -1
- package/dist/models/campaign-budget.js +8 -4
- package/dist/models/campaign.d.ts +1 -2
- package/dist/models/campaign.d.ts.map +1 -1
- package/dist/models/campaign.js +0 -5
- package/dist/services/promotion-module.d.ts +5 -1
- package/dist/services/promotion-module.d.ts.map +1 -1
- package/dist/services/promotion-module.js +80 -25
- package/dist/types/application-method.d.ts +3 -1
- package/dist/types/application-method.d.ts.map +1 -1
- package/dist/types/campaign-budget.d.ts +4 -2
- package/dist/types/campaign-budget.d.ts.map +1 -1
- package/dist/types/campaign.d.ts +0 -2
- package/dist/types/campaign.d.ts.map +1 -1
- package/dist/types/promotion.d.ts +2 -2
- package/dist/types/promotion.d.ts.map +1 -1
- package/package.json +5 -5
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Migration20240227120221.d.ts","sourceRoot":"","sources":["../../src/migrations/Migration20240227120221.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAEjD,qBAAa,uBAAwB,SAAQ,SAAS;IAC9C,EAAE,IAAI,OAAO,CAAC,IAAI,CAAC;
|
1
|
+
{"version":3,"file":"Migration20240227120221.d.ts","sourceRoot":"","sources":["../../src/migrations/Migration20240227120221.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAEjD,qBAAa,uBAAwB,SAAQ,SAAS;IAC9C,EAAE,IAAI,OAAO,CAAC,IAAI,CAAC;CAiJ1B"}
|
@@ -4,9 +4,9 @@ exports.Migration20240227120221 = void 0;
|
|
4
4
|
const migrations_1 = require("@mikro-orm/migrations");
|
5
5
|
class Migration20240227120221 extends migrations_1.Migration {
|
6
6
|
async up() {
|
7
|
-
this.addSql('create table if not exists "promotion_campaign" ("id" text not null, "name" text not null, "description" text null, "
|
7
|
+
this.addSql('create table if not exists "promotion_campaign" ("id" text not null, "name" text not null, "description" 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"));');
|
8
8
|
this.addSql('alter table if exists "promotion_campaign" add constraint "IDX_campaign_identifier_unique" unique ("campaign_identifier");');
|
9
|
-
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"));');
|
9
|
+
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 not null default 0, "raw_used" jsonb not 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"));');
|
10
10
|
this.addSql('create index if not exists "IDX_campaign_budget_type" on "promotion_campaign_budget" ("type");');
|
11
11
|
this.addSql('alter table if exists "promotion_campaign_budget" add constraint "promotion_campaign_budget_campaign_id_unique" unique ("campaign_id");');
|
12
12
|
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"));');
|
@@ -36,6 +36,14 @@ class Migration20240227120221 extends migrations_1.Migration {
|
|
36
36
|
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;');
|
37
37
|
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;');
|
38
38
|
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;');
|
39
|
+
this.addSql('alter table if exists "promotion" drop constraint if exists "promotion_campaign_id_foreign";');
|
40
|
+
this.addSql('alter table if exists "promotion" add constraint "promotion_campaign_id_foreign" foreign key ("campaign_id") references "promotion_campaign" ("id") on update cascade on delete set null;');
|
41
|
+
this.addSql('alter table if exists "promotion_application_method" add column if not exists "currency_code" text not null;');
|
42
|
+
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_promotion_application_method_currency_code" ON "promotion_application_method" (currency_code) WHERE deleted_at IS NOT NULL;');
|
43
|
+
this.addSql('alter table "promotion_application_method" alter column "value" type numeric using ("value"::numeric);');
|
44
|
+
this.addSql('alter table "promotion_application_method" alter column "raw_value" type jsonb using ("raw_value"::jsonb);');
|
45
|
+
this.addSql('alter table "promotion_application_method" alter column "raw_value" set not null;');
|
46
|
+
this.addSql('alter table if exists "promotion_campaign_budget" add column if not exists "currency_code" text null;');
|
39
47
|
}
|
40
48
|
}
|
41
49
|
exports.Migration20240227120221 = Migration20240227120221;
|
@@ -1,14 +1,13 @@
|
|
1
|
-
import { ApplicationMethodAllocationValues, ApplicationMethodTargetTypeValues, ApplicationMethodTypeValues, BigNumberRawValue
|
1
|
+
import { ApplicationMethodAllocationValues, ApplicationMethodTargetTypeValues, ApplicationMethodTypeValues, BigNumberRawValue } from "@medusajs/types";
|
2
2
|
import { BigNumber } from "@medusajs/utils";
|
3
|
-
import { Collection
|
3
|
+
import { Collection } from "@mikro-orm/core";
|
4
4
|
import Promotion from "./promotion";
|
5
5
|
import PromotionRule from "./promotion-rule";
|
6
|
-
type OptionalFields = "value" | "max_quantity" | "apply_to_quantity" | "buy_rules_min_quantity" | "allocation" | DAL.SoftDeletableEntityDateColumns;
|
7
6
|
export default class ApplicationMethod {
|
8
|
-
[OptionalProps]?: OptionalFields;
|
9
7
|
id: string;
|
10
8
|
value: BigNumber | number | null;
|
11
9
|
raw_value: BigNumberRawValue | null;
|
10
|
+
currency_code: string | null;
|
12
11
|
max_quantity?: number | null;
|
13
12
|
apply_to_quantity?: number | null;
|
14
13
|
buy_rules_min_quantity?: number | null;
|
@@ -24,5 +23,4 @@ export default class ApplicationMethod {
|
|
24
23
|
onCreate(): void;
|
25
24
|
onInit(): void;
|
26
25
|
}
|
27
|
-
export {};
|
28
26
|
//# sourceMappingURL=application-method.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"application-method.d.ts","sourceRoot":"","sources":["../../src/models/application-method.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iCAAiC,EACjC,iCAAiC,EACjC,2BAA2B,EAC3B,iBAAiB,
|
1
|
+
{"version":3,"file":"application-method.d.ts","sourceRoot":"","sources":["../../src/models/application-method.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iCAAiC,EACjC,iCAAiC,EACjC,2BAA2B,EAC3B,iBAAiB,EAClB,MAAM,iBAAiB,CAAA;AACxB,OAAO,EACL,SAAS,EAMV,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAEL,UAAU,EAUX,MAAM,iBAAiB,CAAA;AACxB,OAAO,SAAS,MAAM,aAAa,CAAA;AACnC,OAAO,aAAa,MAAM,kBAAkB,CAAA;AAW5C,MAAM,CAAC,OAAO,OAAO,iBAAiB;IAEpC,EAAE,EAAG,MAAM,CAAA;IAGX,KAAK,EAAE,SAAS,GAAG,MAAM,GAAG,IAAI,CAAA;IAGhC,SAAS,EAAE,iBAAiB,GAAG,IAAI,CAAA;IAInC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAO;IAGnC,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAO;IAGnC,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAO;IAGxC,sBAAsB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAO;IAI7C,IAAI,EAAE,2BAA2B,CAAA;IAIjC,WAAW,EAAE,iCAAiC,CAAA;IAO9C,UAAU,CAAC,EAAE,iCAAiC,CAAA;IAM9C,SAAS,EAAE,SAAS,CAAA;IAOpB,YAAY,oCAAsC;IAOlD,SAAS,oCAAsC;IAO/C,UAAU,EAAE,IAAI,CAAA;IAQhB,UAAU,EAAE,IAAI,CAAA;IAGhB,UAAU,EAAE,IAAI,GAAG,IAAI,CAAO;IAG9B,QAAQ;IAKR,MAAM;CAGP"}
|
@@ -16,10 +16,15 @@ const utils_1 = require("@medusajs/utils");
|
|
16
16
|
const core_1 = require("@mikro-orm/core");
|
17
17
|
const promotion_1 = __importDefault(require("./promotion"));
|
18
18
|
const promotion_rule_1 = __importDefault(require("./promotion-rule"));
|
19
|
+
const tableName = "promotion_application_method";
|
20
|
+
const CurrencyCodeIndex = (0, utils_1.createPsqlIndexStatementHelper)({
|
21
|
+
tableName,
|
22
|
+
columns: "currency_code",
|
23
|
+
where: "deleted_at IS NOT NULL",
|
24
|
+
});
|
19
25
|
let ApplicationMethod = class ApplicationMethod {
|
20
26
|
constructor() {
|
21
|
-
this.
|
22
|
-
this.raw_value = null;
|
27
|
+
this.currency_code = null;
|
23
28
|
this.max_quantity = null;
|
24
29
|
this.apply_to_quantity = null;
|
25
30
|
this.buy_rules_min_quantity = null;
|
@@ -39,13 +44,18 @@ __decorate([
|
|
39
44
|
__metadata("design:type", String)
|
40
45
|
], ApplicationMethod.prototype, "id", void 0);
|
41
46
|
__decorate([
|
42
|
-
(0, utils_1.MikroOrmBigNumberProperty)(
|
47
|
+
(0, utils_1.MikroOrmBigNumberProperty)(),
|
43
48
|
__metadata("design:type", Object)
|
44
49
|
], ApplicationMethod.prototype, "value", void 0);
|
45
50
|
__decorate([
|
46
|
-
(0, core_1.Property)({ columnType: "jsonb"
|
51
|
+
(0, core_1.Property)({ columnType: "jsonb" }),
|
47
52
|
__metadata("design:type", Object)
|
48
53
|
], ApplicationMethod.prototype, "raw_value", void 0);
|
54
|
+
__decorate([
|
55
|
+
(0, core_1.Property)({ columnType: "text", nullable: true }),
|
56
|
+
CurrencyCodeIndex.MikroORMIndex(),
|
57
|
+
__metadata("design:type", Object)
|
58
|
+
], ApplicationMethod.prototype, "currency_code", void 0);
|
49
59
|
__decorate([
|
50
60
|
(0, core_1.Property)({ columnType: "numeric", nullable: true, serializer: Number }),
|
51
61
|
__metadata("design:type", Object)
|
@@ -133,7 +143,7 @@ __decorate([
|
|
133
143
|
__metadata("design:returntype", void 0)
|
134
144
|
], ApplicationMethod.prototype, "onInit", null);
|
135
145
|
ApplicationMethod = __decorate([
|
136
|
-
(0, core_1.Entity)({ tableName
|
146
|
+
(0, core_1.Entity)({ tableName }),
|
137
147
|
(0, core_1.Filter)(utils_1.DALUtils.mikroOrmSoftDeletableFilterOptions)
|
138
148
|
], ApplicationMethod);
|
139
149
|
exports.default = ApplicationMethod;
|
@@ -8,10 +8,11 @@ export default class CampaignBudget {
|
|
8
8
|
id: string;
|
9
9
|
type: CampaignBudgetTypeValues;
|
10
10
|
campaign: Campaign | null;
|
11
|
+
currency_code: string | null;
|
11
12
|
limit: BigNumber | number | null;
|
12
13
|
raw_limit: BigNumberRawValue | null;
|
13
|
-
used: BigNumber | number
|
14
|
-
raw_used: BigNumberRawValue
|
14
|
+
used: BigNumber | number;
|
15
|
+
raw_used: BigNumberRawValue;
|
15
16
|
created_at: Date;
|
16
17
|
updated_at: Date;
|
17
18
|
deleted_at: Date | null;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"campaign-budget.d.ts","sourceRoot":"","sources":["../../src/models/campaign-budget.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,wBAAwB,EACxB,GAAG,EACJ,MAAM,iBAAiB,CAAA;AACxB,OAAO,EACL,SAAS,EAKV,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAQL,aAAa,EAGd,MAAM,iBAAiB,CAAA;AACxB,OAAO,QAAQ,MAAM,YAAY,CAAA;AAEjC,KAAK,cAAc,GACf,aAAa,GACb,OAAO,GACP,MAAM,GACN,GAAG,CAAC,8BAA8B,CAAA;AAItC,MAAM,CAAC,OAAO,OAAO,cAAc;IACjC,CAAC,aAAa,CAAC,CAAC,EAAE,cAAc,CAAA;IAGhC,EAAE,EAAG,MAAM,CAAA;IAIX,IAAI,EAAE,wBAAwB,CAAA;IAK9B,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAAO;IAGhC,KAAK,EAAE,SAAS,GAAG,MAAM,GAAG,IAAI,CAAO;IAGvC,SAAS,EAAE,iBAAiB,GAAG,IAAI,CAAO;IAG1C,IAAI,EAAE,SAAS,GAAG,MAAM,
|
1
|
+
{"version":3,"file":"campaign-budget.d.ts","sourceRoot":"","sources":["../../src/models/campaign-budget.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,wBAAwB,EACxB,GAAG,EACJ,MAAM,iBAAiB,CAAA;AACxB,OAAO,EACL,SAAS,EAKV,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAQL,aAAa,EAGd,MAAM,iBAAiB,CAAA;AACxB,OAAO,QAAQ,MAAM,YAAY,CAAA;AAEjC,KAAK,cAAc,GACf,aAAa,GACb,OAAO,GACP,MAAM,GACN,GAAG,CAAC,8BAA8B,CAAA;AAItC,MAAM,CAAC,OAAO,OAAO,cAAc;IACjC,CAAC,aAAa,CAAC,CAAC,EAAE,cAAc,CAAA;IAGhC,EAAE,EAAG,MAAM,CAAA;IAIX,IAAI,EAAE,wBAAwB,CAAA;IAK9B,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAAO;IAGhC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAO;IAGnC,KAAK,EAAE,SAAS,GAAG,MAAM,GAAG,IAAI,CAAO;IAGvC,SAAS,EAAE,iBAAiB,GAAG,IAAI,CAAO;IAG1C,IAAI,EAAE,SAAS,GAAG,MAAM,CAAI;IAG5B,QAAQ,EAAE,iBAAiB,CAAA;IAO3B,UAAU,EAAE,IAAI,CAAA;IAQhB,UAAU,EAAE,IAAI,CAAA;IAGhB,UAAU,EAAE,IAAI,GAAG,IAAI,CAAO;IAG9B,QAAQ;IAKR,MAAM;CAGP"}
|
@@ -18,10 +18,10 @@ const campaign_1 = __importDefault(require("./campaign"));
|
|
18
18
|
let CampaignBudget = class CampaignBudget {
|
19
19
|
constructor() {
|
20
20
|
this.campaign = null;
|
21
|
+
this.currency_code = null;
|
21
22
|
this.limit = null;
|
22
23
|
this.raw_limit = null;
|
23
|
-
this.used =
|
24
|
-
this.raw_used = null;
|
24
|
+
this.used = 0;
|
25
25
|
this.deleted_at = null;
|
26
26
|
}
|
27
27
|
onCreate() {
|
@@ -46,6 +46,10 @@ __decorate([
|
|
46
46
|
}),
|
47
47
|
__metadata("design:type", Object)
|
48
48
|
], CampaignBudget.prototype, "campaign", void 0);
|
49
|
+
__decorate([
|
50
|
+
(0, core_1.Property)({ columnType: "text", nullable: true }),
|
51
|
+
__metadata("design:type", Object)
|
52
|
+
], CampaignBudget.prototype, "currency_code", void 0);
|
49
53
|
__decorate([
|
50
54
|
(0, utils_1.MikroOrmBigNumberProperty)({ nullable: true }),
|
51
55
|
__metadata("design:type", Object)
|
@@ -55,11 +59,11 @@ __decorate([
|
|
55
59
|
__metadata("design:type", Object)
|
56
60
|
], CampaignBudget.prototype, "raw_limit", void 0);
|
57
61
|
__decorate([
|
58
|
-
(0, utils_1.MikroOrmBigNumberProperty)({
|
62
|
+
(0, utils_1.MikroOrmBigNumberProperty)({ default: 0 }),
|
59
63
|
__metadata("design:type", Object)
|
60
64
|
], CampaignBudget.prototype, "used", void 0);
|
61
65
|
__decorate([
|
62
|
-
(0, core_1.Property)({ columnType: "jsonb"
|
66
|
+
(0, core_1.Property)({ columnType: "jsonb" }),
|
63
67
|
__metadata("design:type", Object)
|
64
68
|
], CampaignBudget.prototype, "raw_used", void 0);
|
65
69
|
__decorate([
|
@@ -3,13 +3,12 @@ import { Collection, OptionalProps } from "@mikro-orm/core";
|
|
3
3
|
import CampaignBudget from "./campaign-budget";
|
4
4
|
import Promotion from "./promotion";
|
5
5
|
type OptionalRelations = "budget";
|
6
|
-
type OptionalFields = "description" | "
|
6
|
+
type OptionalFields = "description" | "starts_at" | "ends_at" | DAL.SoftDeletableEntityDateColumns;
|
7
7
|
export default class Campaign {
|
8
8
|
[OptionalProps]?: OptionalFields | OptionalRelations;
|
9
9
|
id: string;
|
10
10
|
name: string;
|
11
11
|
description: string | null;
|
12
|
-
currency: string | null;
|
13
12
|
campaign_identifier: string;
|
14
13
|
starts_at: Date | null;
|
15
14
|
ends_at: Date | null;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"campaign.d.ts","sourceRoot":"","sources":["../../src/models/campaign.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AAErC,OAAO,EAEL,UAAU,EAMV,aAAa,EAId,MAAM,iBAAiB,CAAA;AACxB,OAAO,cAAc,MAAM,mBAAmB,CAAA;AAC9C,OAAO,SAAS,MAAM,aAAa,CAAA;AAEnC,KAAK,iBAAiB,GAAG,QAAQ,CAAA;AACjC,KAAK,cAAc,GACf,aAAa,GACb,
|
1
|
+
{"version":3,"file":"campaign.d.ts","sourceRoot":"","sources":["../../src/models/campaign.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AAErC,OAAO,EAEL,UAAU,EAMV,aAAa,EAId,MAAM,iBAAiB,CAAA;AACxB,OAAO,cAAc,MAAM,mBAAmB,CAAA;AAC9C,OAAO,SAAS,MAAM,aAAa,CAAA;AAEnC,KAAK,iBAAiB,GAAG,QAAQ,CAAA;AACjC,KAAK,cAAc,GACf,aAAa,GACb,WAAW,GACX,SAAS,GACT,GAAG,CAAC,8BAA8B,CAAA;AAItC,MAAM,CAAC,OAAO,OAAO,QAAQ;IAC3B,CAAC,aAAa,CAAC,CAAC,EAAE,cAAc,GAAG,iBAAiB,CAAA;IAGpD,EAAE,EAAG,MAAM,CAAA;IAIX,IAAI,EAAE,MAAM,CAAA;IAIZ,WAAW,EAAE,MAAM,GAAG,IAAI,CAAO;IAOjC,mBAAmB,EAAE,MAAM,CAAA;IAM3B,SAAS,EAAE,IAAI,GAAG,IAAI,CAAO;IAM7B,OAAO,EAAE,IAAI,GAAG,IAAI,CAAO;IAQ3B,MAAM,EAAE,cAAc,GAAG,IAAI,CAAO;IAGpC,UAAU,gCAAkC;IAO5C,UAAU,EAAE,IAAI,CAAA;IAQhB,UAAU,EAAE,IAAI,CAAA;IAGhB,UAAU,EAAE,IAAI,GAAG,IAAI,CAAO;IAG9B,QAAQ;IAKR,MAAM;CAGP"}
|
package/dist/models/campaign.js
CHANGED
@@ -19,7 +19,6 @@ const promotion_1 = __importDefault(require("./promotion"));
|
|
19
19
|
let Campaign = class Campaign {
|
20
20
|
constructor() {
|
21
21
|
this.description = null;
|
22
|
-
this.currency = null;
|
23
22
|
this.starts_at = null;
|
24
23
|
this.ends_at = null;
|
25
24
|
this.budget = null;
|
@@ -47,10 +46,6 @@ __decorate([
|
|
47
46
|
(0, core_1.Property)({ columnType: "text", nullable: true }),
|
48
47
|
__metadata("design:type", Object)
|
49
48
|
], Campaign.prototype, "description", void 0);
|
50
|
-
__decorate([
|
51
|
-
(0, core_1.Property)({ columnType: "text", nullable: true }),
|
52
|
-
__metadata("design:type", Object)
|
53
|
-
], Campaign.prototype, "currency", void 0);
|
54
49
|
__decorate([
|
55
50
|
(0, core_1.Property)({ columnType: "text" }),
|
56
51
|
(0, core_1.Unique)({
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { Context, DAL, InternalModuleDeclaration, ModuleJoinerConfig, ModulesSdkTypes, PromotionTypes } from "@medusajs/types";
|
1
|
+
import { CampaignBudgetTypeValues, Context, DAL, InternalModuleDeclaration, ModuleJoinerConfig, ModulesSdkTypes, PromotionTypes } from "@medusajs/types";
|
2
2
|
import { ModulesSdkUtils } from "@medusajs/utils";
|
3
3
|
import { ApplicationMethod, Campaign, CampaignBudget, Promotion, PromotionRule, PromotionRuleValue } from "../models";
|
4
4
|
import { ApplicationMethodRuleTypes } from "../types";
|
@@ -61,6 +61,10 @@ export default class PromotionModuleService<TApplicationMethod extends Applicati
|
|
61
61
|
createCampaigns(data: PromotionTypes.CreateCampaignDTO, sharedContext?: Context): Promise<PromotionTypes.CampaignDTO>;
|
62
62
|
createCampaigns(data: PromotionTypes.CreateCampaignDTO[], sharedContext?: Context): Promise<PromotionTypes.CampaignDTO[]>;
|
63
63
|
protected createCampaigns_(data: PromotionTypes.CreateCampaignDTO[], sharedContext?: Context): Promise<TCampaign[]>;
|
64
|
+
protected validateCampaignBudgetData(data: {
|
65
|
+
type?: CampaignBudgetTypeValues;
|
66
|
+
currency_code?: string | null;
|
67
|
+
}): void;
|
64
68
|
updateCampaigns(data: PromotionTypes.UpdateCampaignDTO, sharedContext?: Context): Promise<PromotionTypes.CampaignDTO>;
|
65
69
|
updateCampaigns(data: PromotionTypes.UpdateCampaignDTO[], sharedContext?: Context): Promise<PromotionTypes.CampaignDTO[]>;
|
66
70
|
protected updateCampaigns_(data: PromotionTypes.UpdateCampaignDTO[], sharedContext?: Context): Promise<TCampaign[]>;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"promotion-module.d.ts","sourceRoot":"","sources":["../../src/services/promotion-module.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,GAAG,EACH,yBAAyB,EACzB,kBAAkB,EAClB,eAAe,EACf,cAAc,EACf,MAAM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C9B,aAAa,GAAE,OAAY;CAiC/C"}
|
@@ -234,21 +234,35 @@ class PromotionModuleService extends utils_1.ModulesSdkUtils.abstractModuleServi
|
|
234
234
|
const promotionsData = [];
|
235
235
|
const applicationMethodsData = [];
|
236
236
|
const campaignsData = [];
|
237
|
+
const existingCampaigns = await this.campaignService_.list({ id: data.map((d) => d.campaign_id).filter((id) => (0, utils_1.isString)(id)) }, { relations: ["budget"] }, sharedContext);
|
237
238
|
const promotionCodeApplicationMethodDataMap = new Map();
|
238
239
|
const promotionCodeRulesDataMap = new Map();
|
239
240
|
const methodTargetRulesMap = new Map();
|
240
241
|
const methodBuyRulesMap = new Map();
|
241
242
|
const promotionCodeCampaignMap = new Map();
|
242
243
|
for (const { application_method: applicationMethodData, rules: rulesData, campaign: campaignData, campaign_id: campaignId, ...promotionData } of data) {
|
243
|
-
|
244
|
-
promotionCodeApplicationMethodDataMap.set(promotionData.code, applicationMethodData);
|
245
|
-
}
|
244
|
+
promotionCodeApplicationMethodDataMap.set(promotionData.code, applicationMethodData);
|
246
245
|
if (rulesData) {
|
247
246
|
promotionCodeRulesDataMap.set(promotionData.code, rulesData);
|
248
247
|
}
|
249
248
|
if (campaignData && campaignId) {
|
250
249
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Provide either the 'campaign' or 'campaign_id' parameter; both cannot be used simultaneously.`);
|
251
250
|
}
|
251
|
+
if (!campaignData && !campaignId) {
|
252
|
+
promotionsData.push({ ...promotionData });
|
253
|
+
continue;
|
254
|
+
}
|
255
|
+
const existingCampaign = existingCampaigns.find((c) => c.id === campaignId);
|
256
|
+
if (campaignId && !existingCampaign) {
|
257
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Could not find campaign with id - ${campaignId}`);
|
258
|
+
}
|
259
|
+
const campaignCurrency = campaignData?.budget?.currency_code ||
|
260
|
+
existingCampaigns.find((c) => c.id === campaignId)?.budget
|
261
|
+
?.currency_code;
|
262
|
+
if (campaignData?.budget?.type === utils_1.CampaignBudgetType.SPEND &&
|
263
|
+
campaignCurrency !== applicationMethodData?.currency_code) {
|
264
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Currency between promotion and campaigns should match`);
|
265
|
+
}
|
252
266
|
if (campaignData) {
|
253
267
|
promotionCodeCampaignMap.set(promotionData.code, campaignData);
|
254
268
|
}
|
@@ -258,7 +272,6 @@ class PromotionModuleService extends utils_1.ModulesSdkUtils.abstractModuleServi
|
|
258
272
|
});
|
259
273
|
}
|
260
274
|
const createdPromotions = await this.promotionService_.create(promotionsData, sharedContext);
|
261
|
-
const promotionsToAdd = [];
|
262
275
|
for (const promotion of createdPromotions) {
|
263
276
|
const applMethodData = promotionCodeApplicationMethodDataMap.get(promotion.code);
|
264
277
|
const campaignData = promotionCodeCampaignMap.get(promotion.code);
|
@@ -333,22 +346,31 @@ class PromotionModuleService extends utils_1.ModulesSdkUtils.abstractModuleServi
|
|
333
346
|
async update_(data, sharedContext = {}) {
|
334
347
|
const promotionIds = data.map((d) => d.id);
|
335
348
|
const existingPromotions = await this.promotionService_.list({ id: promotionIds }, { relations: ["application_method"] });
|
349
|
+
const existingCampaigns = await this.campaignService_.list({ id: data.map((d) => d.campaign_id).filter((d) => (0, utils_1.isPresent)(d)) }, { relations: ["budget"] });
|
336
350
|
const existingPromotionsMap = new Map(existingPromotions.map((promotion) => [promotion.id, promotion]));
|
337
351
|
const promotionsData = [];
|
338
352
|
const applicationMethodsData = [];
|
339
353
|
for (const { application_method: applicationMethodData, campaign_id: campaignId, ...promotionData } of data) {
|
340
|
-
|
354
|
+
const existingCampaign = existingCampaigns.find((c) => c.id === campaignId);
|
355
|
+
const existingPromotion = existingPromotionsMap.get(promotionData.id);
|
356
|
+
const existingApplicationMethod = existingPromotion?.application_method;
|
357
|
+
const promotionCurrencyCode = existingApplicationMethod?.currency_code ||
|
358
|
+
applicationMethodData?.currency_code;
|
359
|
+
if (campaignId && !existingCampaign) {
|
360
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Could not find campaign with id ${campaignId}`);
|
361
|
+
}
|
362
|
+
if (campaignId &&
|
363
|
+
existingCampaign?.budget?.type === utils_1.CampaignBudgetType.SPEND &&
|
364
|
+
existingCampaign.budget.currency_code !== promotionCurrencyCode) {
|
365
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Currency code doesn't match for campaign (${campaignId}) and promotion (${existingPromotion.id})`);
|
366
|
+
}
|
367
|
+
if ((0, utils_1.isDefined)(campaignId)) {
|
341
368
|
promotionsData.push({ ...promotionData, campaign_id: campaignId });
|
342
369
|
}
|
343
370
|
else {
|
344
371
|
promotionsData.push(promotionData);
|
345
372
|
}
|
346
|
-
if (!applicationMethodData) {
|
347
|
-
continue;
|
348
|
-
}
|
349
|
-
const existingPromotion = existingPromotionsMap.get(promotionData.id);
|
350
|
-
const existingApplicationMethod = existingPromotion?.application_method;
|
351
|
-
if (!existingApplicationMethod) {
|
373
|
+
if (!applicationMethodData || !existingApplicationMethod) {
|
352
374
|
continue;
|
353
375
|
}
|
354
376
|
if (applicationMethodData.allocation &&
|
@@ -420,9 +442,7 @@ class PromotionModuleService extends utils_1.ModulesSdkUtils.abstractModuleServi
|
|
420
442
|
return await this.listPromotionRules({ id: createdPromotionRules.map((pr) => pr.id) }, { relations: ["values"] }, sharedContext);
|
421
443
|
}
|
422
444
|
async addPromotionBuyRules(promotionId, rulesData, sharedContext = {}) {
|
423
|
-
const promotion = await this.promotionService_.retrieve(promotionId, {
|
424
|
-
relations: ["application_method"],
|
425
|
-
});
|
445
|
+
const promotion = await this.promotionService_.retrieve(promotionId, { relations: ["application_method"] }, sharedContext);
|
426
446
|
const applicationMethod = promotion.application_method;
|
427
447
|
if (!applicationMethod) {
|
428
448
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `application_method for promotion not found`);
|
@@ -510,6 +530,7 @@ class PromotionModuleService extends utils_1.ModulesSdkUtils.abstractModuleServi
|
|
510
530
|
for (const createdCampaign of createdCampaigns) {
|
511
531
|
const campaignBudgetData = campaignIdentifierBudgetMap.get(createdCampaign.campaign_identifier);
|
512
532
|
if (campaignBudgetData) {
|
533
|
+
this.validateCampaignBudgetData(campaignBudgetData);
|
513
534
|
campaignBudgetsData.push({
|
514
535
|
...campaignBudgetData,
|
515
536
|
campaign: createdCampaign.id,
|
@@ -521,6 +542,15 @@ class PromotionModuleService extends utils_1.ModulesSdkUtils.abstractModuleServi
|
|
521
542
|
}
|
522
543
|
return createdCampaigns;
|
523
544
|
}
|
545
|
+
validateCampaignBudgetData(data) {
|
546
|
+
if (!data.type) {
|
547
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Campaign Budget type is a required field`);
|
548
|
+
}
|
549
|
+
if (data.type === utils_1.CampaignBudgetType.SPEND &&
|
550
|
+
!(0, utils_1.isPresent)(data.currency_code)) {
|
551
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Campaign Budget type is a required field`);
|
552
|
+
}
|
553
|
+
}
|
524
554
|
async updateCampaigns(data, sharedContext = {}) {
|
525
555
|
const input = Array.isArray(data) ? data : [data];
|
526
556
|
const updatedCampaigns = await this.updateCampaigns_(input, sharedContext);
|
@@ -533,24 +563,43 @@ class PromotionModuleService extends utils_1.ModulesSdkUtils.abstractModuleServi
|
|
533
563
|
async updateCampaigns_(data, sharedContext = {}) {
|
534
564
|
const campaignIds = data.map((d) => d.id);
|
535
565
|
const campaignsData = [];
|
536
|
-
const
|
566
|
+
const updateBudgetData = [];
|
567
|
+
const createBudgetData = [];
|
537
568
|
const existingCampaigns = await this.listCampaigns({ id: campaignIds }, { relations: ["budget"], take: null }, sharedContext);
|
538
569
|
const existingCampaignsMap = new Map(existingCampaigns.map((campaign) => [campaign.id, campaign]));
|
539
570
|
for (const updateCampaignData of data) {
|
540
|
-
const { budget:
|
571
|
+
const { budget: budgetData, ...campaignData } = updateCampaignData;
|
541
572
|
const existingCampaign = existingCampaignsMap.get(campaignData.id);
|
542
|
-
const existingCampaignBudget = existingCampaign?.budget;
|
543
573
|
campaignsData.push(campaignData);
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
574
|
+
// Type & currency code of the budget is immutable, we don't allow for it to be updated.
|
575
|
+
// If an existing budget is present, we remove the type and currency from being updated
|
576
|
+
if ((existingCampaign?.budget && budgetData?.type) ||
|
577
|
+
budgetData?.currency_code) {
|
578
|
+
delete budgetData?.type;
|
579
|
+
delete budgetData?.currency_code;
|
580
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Campaign budget attributes (type, currency_code) are immutable`);
|
581
|
+
}
|
582
|
+
if (budgetData) {
|
583
|
+
if (existingCampaign?.budget) {
|
584
|
+
updateBudgetData.push({
|
585
|
+
id: existingCampaign.budget.id,
|
586
|
+
...budgetData,
|
587
|
+
});
|
588
|
+
}
|
589
|
+
else {
|
590
|
+
createBudgetData.push({
|
591
|
+
...budgetData,
|
592
|
+
campaign: existingCampaign.id,
|
593
|
+
});
|
594
|
+
}
|
549
595
|
}
|
550
596
|
}
|
551
597
|
const updatedCampaigns = await this.campaignService_.update(campaignsData, sharedContext);
|
552
|
-
if (
|
553
|
-
await this.campaignBudgetService_.update(
|
598
|
+
if (updateBudgetData.length) {
|
599
|
+
await this.campaignBudgetService_.update(updateBudgetData, sharedContext);
|
600
|
+
}
|
601
|
+
if (createBudgetData.length) {
|
602
|
+
await this.campaignBudgetService_.create(createBudgetData, sharedContext);
|
554
603
|
}
|
555
604
|
return updatedCampaigns;
|
556
605
|
}
|
@@ -565,11 +614,17 @@ class PromotionModuleService extends utils_1.ModulesSdkUtils.abstractModuleServi
|
|
565
614
|
async addPromotionsToCampaign_(data, sharedContext = {}) {
|
566
615
|
const { id, promotion_ids: promotionIds = [] } = data;
|
567
616
|
const campaign = await this.campaignService_.retrieve(id, {}, sharedContext);
|
568
|
-
const promotionsToAdd = await this.promotionService_.list({ id: promotionIds, campaign_id: null }, { take: null }, sharedContext);
|
617
|
+
const promotionsToAdd = await this.promotionService_.list({ id: promotionIds, campaign_id: null }, { take: null, relations: ["application_method"] }, sharedContext);
|
569
618
|
const diff = (0, utils_1.arrayDifference)(promotionsToAdd.map((p) => p.id), promotionIds);
|
570
619
|
if (diff.length > 0) {
|
571
620
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Cannot add promotions (${diff.join(",")}) to campaign. These promotions are either already part of a campaign or not found.`);
|
572
621
|
}
|
622
|
+
const promotionsWithInvalidCurrency = promotionsToAdd.filter((promotion) => campaign.budget?.type === utils_1.CampaignBudgetType.SPEND &&
|
623
|
+
promotion.application_method?.currency_code !==
|
624
|
+
campaign?.budget?.currency_code);
|
625
|
+
if (promotionsWithInvalidCurrency.length > 0) {
|
626
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Cannot add promotions to campaign where currency_code don't match.`);
|
627
|
+
}
|
573
628
|
await this.promotionService_.update(promotionsToAdd.map((promotion) => ({
|
574
629
|
id: promotion.id,
|
575
630
|
campaign_id: campaign.id,
|
@@ -5,17 +5,19 @@ export interface CreateApplicationMethodDTO {
|
|
5
5
|
target_type: ApplicationMethodTargetTypeValues;
|
6
6
|
allocation?: ApplicationMethodAllocationValues;
|
7
7
|
value?: number;
|
8
|
+
currency_code: string;
|
8
9
|
promotion: Promotion | string | PromotionDTO;
|
9
10
|
max_quantity?: number | null;
|
10
11
|
buy_rules_min_quantity?: number | null;
|
11
12
|
apply_to_quantity?: number | null;
|
12
13
|
}
|
13
14
|
export interface UpdateApplicationMethodDTO {
|
14
|
-
id
|
15
|
+
id?: string;
|
15
16
|
type?: ApplicationMethodTypeValues;
|
16
17
|
target_type?: ApplicationMethodTargetTypeValues;
|
17
18
|
allocation?: ApplicationMethodAllocationValues;
|
18
19
|
value?: number;
|
20
|
+
currency_code?: string;
|
19
21
|
promotion?: Promotion | string | PromotionDTO;
|
20
22
|
max_quantity?: number | null;
|
21
23
|
buy_rules_min_quantity?: number | null;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"application-method.d.ts","sourceRoot":"","sources":["../../src/types/application-method.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iCAAiC,EACjC,iCAAiC,EACjC,2BAA2B,EAC3B,YAAY,EACb,MAAM,iBAAiB,CAAA;AAExB,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC,MAAM,WAAW,0BAA0B;IACzC,IAAI,EAAE,2BAA2B,CAAA;IACjC,WAAW,EAAE,iCAAiC,CAAA;IAC9C,UAAU,CAAC,EAAE,iCAAiC,CAAA;IAC9C,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,SAAS,EAAE,SAAS,GAAG,MAAM,GAAG,YAAY,CAAA;IAC5C,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,sBAAsB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACtC,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAClC;AAED,MAAM,WAAW,0BAA0B;IACzC,EAAE,EAAE,MAAM,CAAA;
|
1
|
+
{"version":3,"file":"application-method.d.ts","sourceRoot":"","sources":["../../src/types/application-method.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iCAAiC,EACjC,iCAAiC,EACjC,2BAA2B,EAC3B,YAAY,EACb,MAAM,iBAAiB,CAAA;AAExB,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC,MAAM,WAAW,0BAA0B;IACzC,IAAI,EAAE,2BAA2B,CAAA;IACjC,WAAW,EAAE,iCAAiC,CAAA;IAC9C,UAAU,CAAC,EAAE,iCAAiC,CAAA;IAC9C,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,aAAa,EAAE,MAAM,CAAA;IACrB,SAAS,EAAE,SAAS,GAAG,MAAM,GAAG,YAAY,CAAA;IAC5C,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,sBAAsB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACtC,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAClC;AAED,MAAM,WAAW,0BAA0B;IACzC,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,IAAI,CAAC,EAAE,2BAA2B,CAAA;IAClC,WAAW,CAAC,EAAE,iCAAiC,CAAA;IAC/C,UAAU,CAAC,EAAE,iCAAiC,CAAA;IAC9C,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,SAAS,CAAC,EAAE,SAAS,GAAG,MAAM,GAAG,YAAY,CAAA;IAC7C,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,sBAAsB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACtC,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAClC"}
|
@@ -2,14 +2,16 @@ import { CampaignBudgetTypeValues } from "@medusajs/types";
|
|
2
2
|
import { Campaign } from "../models";
|
3
3
|
export interface CreateCampaignBudgetDTO {
|
4
4
|
type?: CampaignBudgetTypeValues;
|
5
|
-
limit?: number;
|
5
|
+
limit?: number | null;
|
6
|
+
currency_code?: string | null;
|
6
7
|
used?: number;
|
7
8
|
campaign?: Campaign | string;
|
8
9
|
}
|
9
10
|
export interface UpdateCampaignBudgetDTO {
|
10
11
|
id: string;
|
11
12
|
type?: CampaignBudgetTypeValues;
|
12
|
-
limit?: number;
|
13
|
+
limit?: number | null;
|
14
|
+
currency_code?: string | null;
|
13
15
|
used?: number;
|
14
16
|
}
|
15
17
|
//# sourceMappingURL=campaign-budget.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"campaign-budget.d.ts","sourceRoot":"","sources":["../../src/types/campaign-budget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAA;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAElC,MAAM,WAAW,uBAAuB;IACtC,IAAI,CAAC,EAAE,wBAAwB,CAAA;IAC/B,KAAK,CAAC,EAAE,MAAM,CAAA;
|
1
|
+
{"version":3,"file":"campaign-budget.d.ts","sourceRoot":"","sources":["../../src/types/campaign-budget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAA;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAElC,MAAM,WAAW,uBAAuB;IACtC,IAAI,CAAC,EAAE,wBAAwB,CAAA;IAC/B,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAA;CAC7B;AAED,MAAM,WAAW,uBAAuB;IACtC,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,CAAC,EAAE,wBAAwB,CAAA;IAC/B,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAA;CACd"}
|
package/dist/types/campaign.d.ts
CHANGED
@@ -3,7 +3,6 @@ import { Promotion } from "../models";
|
|
3
3
|
export interface CreateCampaignDTO {
|
4
4
|
name: string;
|
5
5
|
description?: string;
|
6
|
-
currency?: string;
|
7
6
|
campaign_identifier: string;
|
8
7
|
starts_at?: Date;
|
9
8
|
ends_at?: Date;
|
@@ -13,7 +12,6 @@ export interface UpdateCampaignDTO {
|
|
13
12
|
id: string;
|
14
13
|
name?: string;
|
15
14
|
description?: string;
|
16
|
-
currency?: string;
|
17
15
|
campaign_identifier?: string;
|
18
16
|
starts_at?: Date;
|
19
17
|
ends_at?: Date;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"campaign.d.ts","sourceRoot":"","sources":["../../src/types/campaign.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,
|
1
|
+
{"version":3,"file":"campaign.d.ts","sourceRoot":"","sources":["../../src/types/campaign.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,mBAAmB,EAAE,MAAM,CAAA;IAC3B,SAAS,CAAC,EAAE,IAAI,CAAA;IAChB,OAAO,CAAC,EAAE,IAAI,CAAA;IACd,UAAU,CAAC,EAAE,CAAC,YAAY,GAAG,SAAS,CAAC,EAAE,CAAA;CAC1C;AAED,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B,SAAS,CAAC,EAAE,IAAI,CAAA;IAChB,OAAO,CAAC,EAAE,IAAI,CAAA;IACd,UAAU,CAAC,EAAE,CAAC,YAAY,GAAG,SAAS,CAAC,EAAE,CAAA;CAC1C"}
|
@@ -3,13 +3,13 @@ export interface CreatePromotionDTO {
|
|
3
3
|
code: string;
|
4
4
|
type: PromotionTypeValues;
|
5
5
|
is_automatic?: boolean;
|
6
|
-
campaign_id?: string;
|
6
|
+
campaign_id?: string | null;
|
7
7
|
}
|
8
8
|
export interface UpdatePromotionDTO {
|
9
9
|
id: string;
|
10
10
|
code?: string;
|
11
11
|
type?: PromotionTypeValues;
|
12
12
|
is_automatic?: boolean;
|
13
|
-
campaign_id?: string;
|
13
|
+
campaign_id?: string | null;
|
14
14
|
}
|
15
15
|
//# sourceMappingURL=promotion.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"promotion.d.ts","sourceRoot":"","sources":["../../src/types/promotion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAErD,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,mBAAmB,CAAA;IACzB,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,WAAW,CAAC,EAAE,MAAM,CAAA;
|
1
|
+
{"version":3,"file":"promotion.d.ts","sourceRoot":"","sources":["../../src/types/promotion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAErD,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,mBAAmB,CAAA;IACzB,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAC5B;AAED,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,mBAAmB,CAAA;IAC1B,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAC5B"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@medusajs/promotion",
|
3
|
-
"version": "0.0.5-snapshot-
|
3
|
+
"version": "0.0.5-snapshot-20240523153451",
|
4
4
|
"description": "Medusa Promotion module",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
@@ -40,7 +40,7 @@
|
|
40
40
|
"@mikro-orm/cli": "5.9.7",
|
41
41
|
"cross-env": "^5.2.1",
|
42
42
|
"jest": "^29.6.3",
|
43
|
-
"medusa-test-utils": "1.1.45-snapshot-
|
43
|
+
"medusa-test-utils": "1.1.45-snapshot-20240523153451",
|
44
44
|
"rimraf": "^3.0.2",
|
45
45
|
"ts-jest": "^29.1.1",
|
46
46
|
"ts-node": "^10.9.1",
|
@@ -48,9 +48,9 @@
|
|
48
48
|
"typescript": "^5.1.6"
|
49
49
|
},
|
50
50
|
"dependencies": {
|
51
|
-
"@medusajs/modules-sdk": "1.13.0-snapshot-
|
52
|
-
"@medusajs/types": "1.12.0-snapshot-
|
53
|
-
"@medusajs/utils": "1.12.0-snapshot-
|
51
|
+
"@medusajs/modules-sdk": "1.13.0-snapshot-20240523153451",
|
52
|
+
"@medusajs/types": "1.12.0-snapshot-20240523153451",
|
53
|
+
"@medusajs/utils": "1.12.0-snapshot-20240523153451",
|
54
54
|
"@mikro-orm/core": "5.9.7",
|
55
55
|
"@mikro-orm/migrations": "5.9.7",
|
56
56
|
"@mikro-orm/postgresql": "5.9.7",
|