@classytic/promo 0.2.5 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +127 -0
- package/README.md +68 -8
- package/dist/{constants-BB5O8zlN.mjs → constants-BE886vJk.mjs} +52 -34
- package/dist/events/promo-event-catalog.d.mts +233 -0
- package/dist/events/promo-event-catalog.mjs +2 -0
- package/dist/index.d.mts +260 -362
- package/dist/index.mjs +232 -363
- package/dist/promo-event-catalog-Dyh0xQ6w.mjs +250 -0
- package/dist/schemas/index.d.mts +4 -4
- package/dist/schemas/index.mjs +1 -1
- package/package.json +10 -6
- /package/dist/{constants-CrbSSQG5.d.mts → constants-hcMTDJml.d.mts} +0 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { createEvent } from "@classytic/primitives/events";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
//#region src/events/event-constants.ts
|
|
4
|
+
const PromoEvents = {
|
|
5
|
+
PROGRAM_CREATED: "promo.program.created",
|
|
6
|
+
PROGRAM_ACTIVATED: "promo.program.activated",
|
|
7
|
+
PROGRAM_PAUSED: "promo.program.paused",
|
|
8
|
+
PROGRAM_ARCHIVED: "promo.program.archived",
|
|
9
|
+
RULE_ADDED: "promo.rule.added",
|
|
10
|
+
RULE_UPDATED: "promo.rule.updated",
|
|
11
|
+
RULE_REMOVED: "promo.rule.removed",
|
|
12
|
+
REWARD_ADDED: "promo.reward.added",
|
|
13
|
+
REWARD_UPDATED: "promo.reward.updated",
|
|
14
|
+
REWARD_REMOVED: "promo.reward.removed",
|
|
15
|
+
VOUCHER_GENERATED: "promo.voucher.generated",
|
|
16
|
+
VOUCHER_REDEEMED: "promo.voucher.redeemed",
|
|
17
|
+
VOUCHER_CANCELLED: "promo.voucher.cancelled",
|
|
18
|
+
VOUCHER_EXPIRED: "promo.voucher.expired",
|
|
19
|
+
GIFT_CARD_SPENT: "promo.gift_card.spent",
|
|
20
|
+
GIFT_CARD_TOPPED_UP: "promo.gift_card.topped_up",
|
|
21
|
+
GIFT_CARD_EXHAUSTED: "promo.gift_card.exhausted",
|
|
22
|
+
EVALUATION_COMPLETED: "promo.evaluation.completed",
|
|
23
|
+
EVALUATION_COMMITTED: "promo.evaluation.committed",
|
|
24
|
+
EVALUATION_ROLLED_BACK: "promo.evaluation.rolled_back"
|
|
25
|
+
};
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/events/promo-event-catalog.ts
|
|
28
|
+
function definePromoEvent(input) {
|
|
29
|
+
const { name, version = 1, description, zodSchema } = input;
|
|
30
|
+
const def = {
|
|
31
|
+
name,
|
|
32
|
+
version,
|
|
33
|
+
schema: z.toJSONSchema(zodSchema),
|
|
34
|
+
zodSchema,
|
|
35
|
+
create(payload, meta) {
|
|
36
|
+
return createEvent(name, payload, {
|
|
37
|
+
source: "promo",
|
|
38
|
+
...meta
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
if (description !== void 0) def.description = description;
|
|
43
|
+
return def;
|
|
44
|
+
}
|
|
45
|
+
/** Mirrors `ProgramLifecyclePayload`. */
|
|
46
|
+
const programLifecycleSchema = z.object({
|
|
47
|
+
programId: z.string(),
|
|
48
|
+
programType: z.string(),
|
|
49
|
+
status: z.string(),
|
|
50
|
+
actorRef: z.string().optional()
|
|
51
|
+
});
|
|
52
|
+
/** Mirrors `RulePayload`. */
|
|
53
|
+
const ruleSchema = z.object({
|
|
54
|
+
programId: z.string(),
|
|
55
|
+
ruleId: z.string(),
|
|
56
|
+
actorRef: z.string().optional()
|
|
57
|
+
});
|
|
58
|
+
/** Mirrors `RewardPayload`. */
|
|
59
|
+
const rewardSchema = z.object({
|
|
60
|
+
programId: z.string(),
|
|
61
|
+
rewardId: z.string(),
|
|
62
|
+
actorRef: z.string().optional()
|
|
63
|
+
});
|
|
64
|
+
/** Mirrors `VoucherGeneratedPayload`. */
|
|
65
|
+
const voucherGeneratedSchema = z.object({
|
|
66
|
+
programId: z.string(),
|
|
67
|
+
voucherIds: z.array(z.string()),
|
|
68
|
+
codes: z.array(z.string()),
|
|
69
|
+
count: z.number().int().nonnegative(),
|
|
70
|
+
actorRef: z.string().optional()
|
|
71
|
+
});
|
|
72
|
+
/** Mirrors `VoucherRedeemedPayload`. */
|
|
73
|
+
const voucherRedeemedSchema = z.object({
|
|
74
|
+
voucherId: z.string(),
|
|
75
|
+
code: z.string(),
|
|
76
|
+
orderId: z.string(),
|
|
77
|
+
discountAmount: z.number(),
|
|
78
|
+
customerId: z.string().optional()
|
|
79
|
+
});
|
|
80
|
+
/**
|
|
81
|
+
* Mirrors `VoucherLifecyclePayload` — shared by VOUCHER_CANCELLED,
|
|
82
|
+
* VOUCHER_EXPIRED, and GIFT_CARD_EXHAUSTED (repo emits `status: 'cancelled'`
|
|
83
|
+
* / `'used'` / host-supplied terminal value).
|
|
84
|
+
*/
|
|
85
|
+
const voucherLifecycleSchema = z.object({
|
|
86
|
+
voucherId: z.string(),
|
|
87
|
+
code: z.string(),
|
|
88
|
+
status: z.string()
|
|
89
|
+
});
|
|
90
|
+
/** Mirrors `GiftCardSpentPayload`. */
|
|
91
|
+
const giftCardSpentSchema = z.object({
|
|
92
|
+
voucherId: z.string(),
|
|
93
|
+
code: z.string(),
|
|
94
|
+
amount: z.number(),
|
|
95
|
+
remainingBalance: z.number(),
|
|
96
|
+
orderId: z.string()
|
|
97
|
+
});
|
|
98
|
+
/** Mirrors `GiftCardToppedUpPayload`. */
|
|
99
|
+
const giftCardToppedUpSchema = z.object({
|
|
100
|
+
voucherId: z.string(),
|
|
101
|
+
code: z.string(),
|
|
102
|
+
amount: z.number(),
|
|
103
|
+
newBalance: z.number()
|
|
104
|
+
});
|
|
105
|
+
/** Mirrors `EvaluationCompletedPayload`. */
|
|
106
|
+
const evaluationCompletedSchema = z.object({
|
|
107
|
+
evaluationId: z.string(),
|
|
108
|
+
totalDiscount: z.number(),
|
|
109
|
+
programsApplied: z.number().int().nonnegative(),
|
|
110
|
+
codesUsed: z.array(z.string()),
|
|
111
|
+
isPreview: z.boolean()
|
|
112
|
+
});
|
|
113
|
+
/** Mirrors `EvaluationCommittedPayload`. */
|
|
114
|
+
const evaluationCommittedSchema = z.object({
|
|
115
|
+
evaluationId: z.string(),
|
|
116
|
+
orderId: z.string(),
|
|
117
|
+
totalDiscount: z.number()
|
|
118
|
+
});
|
|
119
|
+
/** Single-field rollback payload — emitted by `evaluation.service.ts`. */
|
|
120
|
+
const evaluationRolledBackSchema = z.object({ evaluationId: z.string() });
|
|
121
|
+
const ProgramCreated = definePromoEvent({
|
|
122
|
+
name: PromoEvents.PROGRAM_CREATED,
|
|
123
|
+
description: "A new promo program was created (starts in draft).",
|
|
124
|
+
zodSchema: programLifecycleSchema
|
|
125
|
+
});
|
|
126
|
+
const ProgramActivated = definePromoEvent({
|
|
127
|
+
name: PromoEvents.PROGRAM_ACTIVATED,
|
|
128
|
+
description: "A draft or paused program was activated.",
|
|
129
|
+
zodSchema: programLifecycleSchema
|
|
130
|
+
});
|
|
131
|
+
const ProgramPaused = definePromoEvent({
|
|
132
|
+
name: PromoEvents.PROGRAM_PAUSED,
|
|
133
|
+
description: "An active program was paused.",
|
|
134
|
+
zodSchema: programLifecycleSchema
|
|
135
|
+
});
|
|
136
|
+
const ProgramArchived = definePromoEvent({
|
|
137
|
+
name: PromoEvents.PROGRAM_ARCHIVED,
|
|
138
|
+
description: "A program was archived (terminal — no further transitions).",
|
|
139
|
+
zodSchema: programLifecycleSchema
|
|
140
|
+
});
|
|
141
|
+
const RuleAdded = definePromoEvent({
|
|
142
|
+
name: PromoEvents.RULE_ADDED,
|
|
143
|
+
description: "A new rule was added to a program.",
|
|
144
|
+
zodSchema: ruleSchema
|
|
145
|
+
});
|
|
146
|
+
const RuleUpdated = definePromoEvent({
|
|
147
|
+
name: PromoEvents.RULE_UPDATED,
|
|
148
|
+
description: "An existing rule on a program was updated.",
|
|
149
|
+
zodSchema: ruleSchema
|
|
150
|
+
});
|
|
151
|
+
const RuleRemoved = definePromoEvent({
|
|
152
|
+
name: PromoEvents.RULE_REMOVED,
|
|
153
|
+
description: "A rule was removed from a program.",
|
|
154
|
+
zodSchema: ruleSchema
|
|
155
|
+
});
|
|
156
|
+
const RewardAdded = definePromoEvent({
|
|
157
|
+
name: PromoEvents.REWARD_ADDED,
|
|
158
|
+
description: "A reward was added to a program.",
|
|
159
|
+
zodSchema: rewardSchema
|
|
160
|
+
});
|
|
161
|
+
const RewardUpdated = definePromoEvent({
|
|
162
|
+
name: PromoEvents.REWARD_UPDATED,
|
|
163
|
+
description: "A reward on a program was updated.",
|
|
164
|
+
zodSchema: rewardSchema
|
|
165
|
+
});
|
|
166
|
+
const RewardRemoved = definePromoEvent({
|
|
167
|
+
name: PromoEvents.REWARD_REMOVED,
|
|
168
|
+
description: "A reward was removed from a program.",
|
|
169
|
+
zodSchema: rewardSchema
|
|
170
|
+
});
|
|
171
|
+
const VoucherGenerated = definePromoEvent({
|
|
172
|
+
name: PromoEvents.VOUCHER_GENERATED,
|
|
173
|
+
description: "One or more vouchers were generated for a program.",
|
|
174
|
+
zodSchema: voucherGeneratedSchema
|
|
175
|
+
});
|
|
176
|
+
const VoucherRedeemed = definePromoEvent({
|
|
177
|
+
name: PromoEvents.VOUCHER_REDEEMED,
|
|
178
|
+
description: "A voucher was redeemed against an order.",
|
|
179
|
+
zodSchema: voucherRedeemedSchema
|
|
180
|
+
});
|
|
181
|
+
const VoucherCancelled = definePromoEvent({
|
|
182
|
+
name: PromoEvents.VOUCHER_CANCELLED,
|
|
183
|
+
description: "A voucher was cancelled by an operator.",
|
|
184
|
+
zodSchema: voucherLifecycleSchema
|
|
185
|
+
});
|
|
186
|
+
const VoucherExpired = definePromoEvent({
|
|
187
|
+
name: PromoEvents.VOUCHER_EXPIRED,
|
|
188
|
+
description: "A voucher reached its expiry date and was transitioned to expired.",
|
|
189
|
+
zodSchema: voucherLifecycleSchema
|
|
190
|
+
});
|
|
191
|
+
const GiftCardSpent = definePromoEvent({
|
|
192
|
+
name: PromoEvents.GIFT_CARD_SPENT,
|
|
193
|
+
description: "A gift-card voucher was debited against an order — remaining balance reported.",
|
|
194
|
+
zodSchema: giftCardSpentSchema
|
|
195
|
+
});
|
|
196
|
+
const GiftCardToppedUp = definePromoEvent({
|
|
197
|
+
name: PromoEvents.GIFT_CARD_TOPPED_UP,
|
|
198
|
+
description: "A gift-card voucher received a top-up — new balance reported.",
|
|
199
|
+
zodSchema: giftCardToppedUpSchema
|
|
200
|
+
});
|
|
201
|
+
const GiftCardExhausted = definePromoEvent({
|
|
202
|
+
name: PromoEvents.GIFT_CARD_EXHAUSTED,
|
|
203
|
+
description: "A gift-card voucher reached a zero balance and was marked used.",
|
|
204
|
+
zodSchema: voucherLifecycleSchema
|
|
205
|
+
});
|
|
206
|
+
const EvaluationCompleted = definePromoEvent({
|
|
207
|
+
name: PromoEvents.EVALUATION_COMPLETED,
|
|
208
|
+
description: "A cart evaluation finished (preview or pre-commit) — totals and applied codes reported.",
|
|
209
|
+
zodSchema: evaluationCompletedSchema
|
|
210
|
+
});
|
|
211
|
+
const EvaluationCommitted = definePromoEvent({
|
|
212
|
+
name: PromoEvents.EVALUATION_COMMITTED,
|
|
213
|
+
description: "A stored evaluation was committed against an order — usage counters moved.",
|
|
214
|
+
zodSchema: evaluationCommittedSchema
|
|
215
|
+
});
|
|
216
|
+
const EvaluationRolledBack = definePromoEvent({
|
|
217
|
+
name: PromoEvents.EVALUATION_ROLLED_BACK,
|
|
218
|
+
description: "A stored evaluation was discarded without commit.",
|
|
219
|
+
zodSchema: evaluationRolledBackSchema
|
|
220
|
+
});
|
|
221
|
+
/**
|
|
222
|
+
* Every promo event defined in the package — pass to Arc's
|
|
223
|
+
* `EventRegistry`. Hosts wire ONE array; the whole `promo.*` namespace
|
|
224
|
+
* becomes introspectable via OpenAPI and auto-validated at publish time
|
|
225
|
+
* when `eventPlugin({ validateMode: 'reject' })` is set.
|
|
226
|
+
*/
|
|
227
|
+
const promoEventDefinitions = [
|
|
228
|
+
ProgramCreated,
|
|
229
|
+
ProgramActivated,
|
|
230
|
+
ProgramPaused,
|
|
231
|
+
ProgramArchived,
|
|
232
|
+
RuleAdded,
|
|
233
|
+
RuleUpdated,
|
|
234
|
+
RuleRemoved,
|
|
235
|
+
RewardAdded,
|
|
236
|
+
RewardUpdated,
|
|
237
|
+
RewardRemoved,
|
|
238
|
+
VoucherGenerated,
|
|
239
|
+
VoucherRedeemed,
|
|
240
|
+
VoucherCancelled,
|
|
241
|
+
VoucherExpired,
|
|
242
|
+
GiftCardSpent,
|
|
243
|
+
GiftCardToppedUp,
|
|
244
|
+
GiftCardExhausted,
|
|
245
|
+
EvaluationCompleted,
|
|
246
|
+
EvaluationCommitted,
|
|
247
|
+
EvaluationRolledBack
|
|
248
|
+
];
|
|
249
|
+
//#endregion
|
|
250
|
+
export { PromoEvents as S, VoucherCancelled as _, GiftCardSpent as a, VoucherRedeemed as b, ProgramArchived as c, RewardAdded as d, RewardRemoved as f, RuleUpdated as g, RuleRemoved as h, GiftCardExhausted as i, ProgramCreated as l, RuleAdded as m, EvaluationCompleted as n, GiftCardToppedUp as o, RewardUpdated as p, EvaluationRolledBack as r, ProgramActivated as s, EvaluationCommitted as t, ProgramPaused as u, VoucherExpired as v, promoEventDefinitions as x, VoucherGenerated as y };
|
package/dist/schemas/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as PROGRAM_STATUSES, d as STACKING_MODES, h as VOUCHER_STATUSES, l as REWARD_TYPES, n as DISCOUNT_SCOPES, o as PROGRAM_TYPES, p as TRIGGER_MODES, t as DISCOUNT_MODES } from "../constants-
|
|
1
|
+
import { a as PROGRAM_STATUSES, d as STACKING_MODES, h as VOUCHER_STATUSES, l as REWARD_TYPES, n as DISCOUNT_SCOPES, o as PROGRAM_TYPES, p as TRIGGER_MODES, t as DISCOUNT_MODES } from "../constants-hcMTDJml.mjs";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
|
|
4
4
|
//#region src/schemas/index.d.ts
|
|
@@ -13,8 +13,8 @@ declare const programCreateSchema: z.ZodObject<{
|
|
|
13
13
|
gift_card: "gift_card";
|
|
14
14
|
}>;
|
|
15
15
|
triggerMode: z.ZodEnum<{
|
|
16
|
-
auto: "auto";
|
|
17
16
|
code: "code";
|
|
17
|
+
auto: "auto";
|
|
18
18
|
}>;
|
|
19
19
|
stackingMode: z.ZodOptional<z.ZodEnum<{
|
|
20
20
|
exclusive: "exclusive";
|
|
@@ -41,8 +41,8 @@ declare const programUpdateSchema: z.ZodObject<{
|
|
|
41
41
|
gift_card: "gift_card";
|
|
42
42
|
}>>;
|
|
43
43
|
triggerMode: z.ZodOptional<z.ZodEnum<{
|
|
44
|
-
auto: "auto";
|
|
45
44
|
code: "code";
|
|
45
|
+
auto: "auto";
|
|
46
46
|
}>>;
|
|
47
47
|
stackingMode: z.ZodOptional<z.ZodOptional<z.ZodEnum<{
|
|
48
48
|
exclusive: "exclusive";
|
|
@@ -229,8 +229,8 @@ declare const programListFilterSchema: z.ZodObject<{
|
|
|
229
229
|
archived: "archived";
|
|
230
230
|
}>>;
|
|
231
231
|
triggerMode: z.ZodOptional<z.ZodEnum<{
|
|
232
|
-
auto: "auto";
|
|
233
232
|
code: "code";
|
|
233
|
+
auto: "auto";
|
|
234
234
|
}>>;
|
|
235
235
|
stackingMode: z.ZodOptional<z.ZodEnum<{
|
|
236
236
|
exclusive: "exclusive";
|
package/dist/schemas/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as PROGRAM_TYPES, c as TRIGGER_MODES, i as PROGRAM_STATUSES, l as VOUCHER_STATUSES, n as DISCOUNT_SCOPES, o as REWARD_TYPES, s as STACKING_MODES, t as DISCOUNT_MODES } from "../constants-
|
|
1
|
+
import { a as PROGRAM_TYPES, c as TRIGGER_MODES, i as PROGRAM_STATUSES, l as VOUCHER_STATUSES, n as DISCOUNT_SCOPES, o as REWARD_TYPES, s as STACKING_MODES, t as DISCOUNT_MODES } from "../constants-BE886vJk.mjs";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
//#region src/schemas/index.ts
|
|
4
4
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@classytic/promo",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Production-grade promotion, coupon, and discount engine for MongoDB — programs, rules, rewards, vouchers, gift cards, buy-x-get-y",
|
|
5
5
|
"author": "Classytic",
|
|
6
6
|
"homepage": "https://www.npmjs.com/package/@classytic/promo",
|
|
@@ -27,6 +27,10 @@
|
|
|
27
27
|
"./schemas": {
|
|
28
28
|
"types": "./dist/schemas/index.d.mts",
|
|
29
29
|
"default": "./dist/schemas/index.mjs"
|
|
30
|
+
},
|
|
31
|
+
"./events": {
|
|
32
|
+
"types": "./dist/events/promo-event-catalog.d.mts",
|
|
33
|
+
"default": "./dist/events/promo-event-catalog.mjs"
|
|
30
34
|
}
|
|
31
35
|
},
|
|
32
36
|
"main": "./dist/index.mjs",
|
|
@@ -57,18 +61,18 @@
|
|
|
57
61
|
"release": "npm run push -- main && npm run release:tag && npm publish"
|
|
58
62
|
},
|
|
59
63
|
"peerDependencies": {
|
|
60
|
-
"@classytic/mongokit": ">=3.
|
|
64
|
+
"@classytic/mongokit": ">=3.16.0",
|
|
61
65
|
"@classytic/primitives": ">=0.5.0",
|
|
62
|
-
"@classytic/repo-core": ">=0.
|
|
66
|
+
"@classytic/repo-core": ">=0.6.0",
|
|
63
67
|
"mongoose": ">=9.4.1",
|
|
64
68
|
"zod": ">=4.0.0"
|
|
65
69
|
},
|
|
66
70
|
"devDependencies": {
|
|
67
71
|
"@biomejs/biome": "^2.4.9",
|
|
68
72
|
"@classytic/dev-tools": "^0.2.0",
|
|
69
|
-
"@classytic/mongokit": "^3.
|
|
70
|
-
"@classytic/primitives": "
|
|
71
|
-
"@classytic/repo-core": "^0.
|
|
73
|
+
"@classytic/mongokit": "^3.16.0",
|
|
74
|
+
"@classytic/primitives": "^0.7.2",
|
|
75
|
+
"@classytic/repo-core": "^0.6.0",
|
|
72
76
|
"@types/node": "^25.5.0",
|
|
73
77
|
"@vitest/coverage-v8": "^3.2.4",
|
|
74
78
|
"knip": "^6.3.0",
|
|
File without changes
|