@saasicat/nest 0.2.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/LICENSE +202 -0
- package/README.md +53 -0
- package/dist/admin/index.cjs +1283 -0
- package/dist/admin/index.d.cts +225 -0
- package/dist/admin/index.d.ts +225 -0
- package/dist/admin/index.js +58 -0
- package/dist/admin-audit.service-9IqXMlZm.d.cts +30 -0
- package/dist/admin-audit.service-9IqXMlZm.d.ts +30 -0
- package/dist/admin-manifest.config-DyrQNT7M.d.cts +10 -0
- package/dist/admin-manifest.config-DyrQNT7M.d.ts +10 -0
- package/dist/aggregation-CJ3qQf92.d.cts +288 -0
- package/dist/aggregation-Dvz9e8X2.d.ts +288 -0
- package/dist/billing/index.cjs +5205 -0
- package/dist/billing/index.d.cts +1326 -0
- package/dist/billing/index.d.ts +1326 -0
- package/dist/billing/index.js +197 -0
- package/dist/catalog/index.cjs +6449 -0
- package/dist/catalog/index.d.cts +1073 -0
- package/dist/catalog/index.d.ts +1073 -0
- package/dist/catalog/index.js +158 -0
- package/dist/checkout-offer/index.cjs +807 -0
- package/dist/checkout-offer/index.d.cts +120 -0
- package/dist/checkout-offer/index.d.ts +120 -0
- package/dist/checkout-offer/index.js +20 -0
- package/dist/chunk-2FR6ZL7R.js +0 -0
- package/dist/chunk-2PNX2QL2.js +347 -0
- package/dist/chunk-2VNZYRDV.js +326 -0
- package/dist/chunk-7MLGHWKO.js +12 -0
- package/dist/chunk-ABB2CRAS.js +31 -0
- package/dist/chunk-AJ3EPELS.js +157 -0
- package/dist/chunk-E56W4U2P.js +90 -0
- package/dist/chunk-HRU3CP57.js +71 -0
- package/dist/chunk-L4SUWH5B.js +14 -0
- package/dist/chunk-M6CZQYPY.js +314 -0
- package/dist/chunk-MDIZUVIK.js +87 -0
- package/dist/chunk-MV6LW5UB.js +217 -0
- package/dist/chunk-NYLON2VC.js +6 -0
- package/dist/chunk-P6MYZMXQ.js +951 -0
- package/dist/chunk-Q53N43LQ.js +13 -0
- package/dist/chunk-QJVPRD3R.js +573 -0
- package/dist/chunk-RIGLQ4BV.js +3367 -0
- package/dist/chunk-SABTXESR.js +24 -0
- package/dist/chunk-SHUYVCID.js +6 -0
- package/dist/chunk-SYEVGPFC.js +686 -0
- package/dist/chunk-V7HUZ6UQ.js +5923 -0
- package/dist/chunk-WPUVJRIN.js +29 -0
- package/dist/chunk-XCZ3TTFK.js +22 -0
- package/dist/chunk-XSGU2Y2O.js +1611 -0
- package/dist/chunk-YNEW3L63.js +190 -0
- package/dist/chunk-YXGHPILG.js +614 -0
- package/dist/chunk-ZL4LLE2Z.js +465 -0
- package/dist/di-CcNeq9v-.d.cts +16 -0
- package/dist/di-CcNeq9v-.d.ts +16 -0
- package/dist/discovery/index.cjs +588 -0
- package/dist/discovery/index.d.cts +299 -0
- package/dist/discovery/index.d.ts +299 -0
- package/dist/discovery/index.js +49 -0
- package/dist/discovery.scanner-CUYLKlYT.d.cts +59 -0
- package/dist/discovery.scanner-CUYLKlYT.d.ts +59 -0
- package/dist/entitlement/index.cjs +805 -0
- package/dist/entitlement/index.d.cts +60 -0
- package/dist/entitlement/index.d.ts +60 -0
- package/dist/entitlement/index.js +57 -0
- package/dist/index.cjs +15918 -0
- package/dist/index.d.cts +85 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.js +956 -0
- package/dist/plan-resolution-CFCoUkrE.d.cts +41 -0
- package/dist/plan-resolution-CFCoUkrE.d.ts +41 -0
- package/dist/platform/index.cjs +2772 -0
- package/dist/platform/index.d.cts +118 -0
- package/dist/platform/index.d.ts +118 -0
- package/dist/platform/index.js +37 -0
- package/dist/promo/index.cjs +995 -0
- package/dist/promo/index.d.cts +121 -0
- package/dist/promo/index.d.ts +121 -0
- package/dist/promo/index.js +58 -0
- package/dist/registration/index.cjs +1687 -0
- package/dist/registration/index.d.cts +444 -0
- package/dist/registration/index.d.ts +444 -0
- package/dist/registration/index.js +82 -0
- package/dist/saas-platform.module-CgZ2omgg.d.cts +180 -0
- package/dist/saas-platform.module-DT9TnCzk.d.ts +180 -0
- package/dist/service-DX8KbGXl.d.cts +109 -0
- package/dist/service-DX8KbGXl.d.ts +109 -0
- package/dist/subscription-contract/index.cjs +471 -0
- package/dist/subscription-contract/index.d.cts +17 -0
- package/dist/subscription-contract/index.d.ts +17 -0
- package/dist/subscription-contract/index.js +21 -0
- package/dist/subscription-contract.service--cm47ZJJ.d.cts +39 -0
- package/dist/subscription-contract.service--cm47ZJJ.d.ts +39 -0
- package/dist/testing/index.cjs +3942 -0
- package/dist/testing/index.d.cts +279 -0
- package/dist/testing/index.d.ts +279 -0
- package/dist/testing/index.js +1195 -0
- package/package.json +185 -0
|
@@ -0,0 +1,995 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/promo/index.ts
|
|
22
|
+
var promo_exports = {};
|
|
23
|
+
__export(promo_exports, {
|
|
24
|
+
PROMO_CODE_REDEMPTION_REPOSITORY_TOKEN: () => PROMO_CODE_REDEMPTION_REPOSITORY_TOKEN,
|
|
25
|
+
PROMO_CODE_REPOSITORY_TOKEN: () => PROMO_CODE_REPOSITORY_TOKEN,
|
|
26
|
+
PROMO_CODE_VALIDATION_LOG_REPOSITORY_TOKEN: () => PROMO_CODE_VALIDATION_LOG_REPOSITORY_TOKEN,
|
|
27
|
+
PROMO_FIRST_TIME_CUSTOMER_CHECK_TOKEN: () => PROMO_FIRST_TIME_CUSTOMER_CHECK_TOKEN,
|
|
28
|
+
PROMO_REVENUE_DEDUCTION_AGGREGATOR_TOKEN: () => PROMO_REVENUE_DEDUCTION_AGGREGATOR_TOKEN,
|
|
29
|
+
PROMO_SERVICE_CONFIG_TOKEN: () => PROMO_SERVICE_CONFIG_TOKEN,
|
|
30
|
+
PROMO_SUBSCRIPTION_LOOKUP_TOKEN: () => PROMO_SUBSCRIPTION_LOOKUP_TOKEN,
|
|
31
|
+
PROMO_TRANSACTION_RUNNER_TOKEN: () => PROMO_TRANSACTION_RUNNER_TOKEN,
|
|
32
|
+
PreviewPromoCodeDto: () => PreviewPromoCodeDto,
|
|
33
|
+
PromoCodeExpirer: () => PromoCodeExpirer,
|
|
34
|
+
PromoCodePublicController: () => PromoCodePublicController,
|
|
35
|
+
PromoCodeRateLimitGuard: () => PromoCodeRateLimitGuard,
|
|
36
|
+
PromoCodesModule: () => PromoCodesModule,
|
|
37
|
+
PromoCodesService: () => PromoCodesService,
|
|
38
|
+
addCycles: () => addCycles,
|
|
39
|
+
buildLabel: () => buildLabel,
|
|
40
|
+
computeDiscountGross: () => computeDiscountGross,
|
|
41
|
+
computeDiscountedGross: () => computeDiscountedGross,
|
|
42
|
+
computeIncludedVat: () => computeIncludedVat,
|
|
43
|
+
computeRegularStartsAt: () => computeRegularStartsAt,
|
|
44
|
+
grossFromNet: () => grossFromNet,
|
|
45
|
+
hashIp: () => hashIp,
|
|
46
|
+
ipFingerprint: () => ipFingerprint,
|
|
47
|
+
round2: () => round2
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(promo_exports);
|
|
50
|
+
|
|
51
|
+
// src/promo/math.ts
|
|
52
|
+
function round2(n) {
|
|
53
|
+
return Math.round(n * 100) / 100;
|
|
54
|
+
}
|
|
55
|
+
__name(round2, "round2");
|
|
56
|
+
function grossFromNet(net, vatRate) {
|
|
57
|
+
return round2(net * (1 + vatRate / 100));
|
|
58
|
+
}
|
|
59
|
+
__name(grossFromNet, "grossFromNet");
|
|
60
|
+
function computeIncludedVat(grossAmount, vatRate) {
|
|
61
|
+
return round2(grossAmount * vatRate / (100 + vatRate));
|
|
62
|
+
}
|
|
63
|
+
__name(computeIncludedVat, "computeIncludedVat");
|
|
64
|
+
|
|
65
|
+
// src/promo/calculator.ts
|
|
66
|
+
function computeDiscountGross(plan, code) {
|
|
67
|
+
const v = Number(code.value);
|
|
68
|
+
if (code.valueType === "PERCENT") {
|
|
69
|
+
return round2(plan.gross * v / 100);
|
|
70
|
+
}
|
|
71
|
+
return round2(v);
|
|
72
|
+
}
|
|
73
|
+
__name(computeDiscountGross, "computeDiscountGross");
|
|
74
|
+
function computeDiscountedGross(planGross, discountGross) {
|
|
75
|
+
return round2(planGross - discountGross);
|
|
76
|
+
}
|
|
77
|
+
__name(computeDiscountedGross, "computeDiscountedGross");
|
|
78
|
+
function computeRegularStartsAt(startsAt, cycle, durationType, durationValue) {
|
|
79
|
+
const result = new Date(startsAt);
|
|
80
|
+
if (durationType === "ONCE") {
|
|
81
|
+
return addCycles(result, cycle, 1);
|
|
82
|
+
}
|
|
83
|
+
if (durationType === "MONTHS") {
|
|
84
|
+
result.setMonth(result.getMonth() + (durationValue ?? 0));
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
return addCycles(result, cycle, durationValue ?? 0);
|
|
88
|
+
}
|
|
89
|
+
__name(computeRegularStartsAt, "computeRegularStartsAt");
|
|
90
|
+
function addCycles(date, cycle, n) {
|
|
91
|
+
const r = new Date(date);
|
|
92
|
+
if (cycle === "YEARLY") {
|
|
93
|
+
r.setFullYear(r.getFullYear() + n);
|
|
94
|
+
} else {
|
|
95
|
+
r.setMonth(r.getMonth() + n);
|
|
96
|
+
}
|
|
97
|
+
return r;
|
|
98
|
+
}
|
|
99
|
+
__name(addCycles, "addCycles");
|
|
100
|
+
function buildLabel(code, cycle) {
|
|
101
|
+
const value = Number(code.value);
|
|
102
|
+
const valueStr = code.valueType === "PERCENT" ? `${value.toLocaleString("de-DE")} %` : `${value.toLocaleString("de-DE", {
|
|
103
|
+
minimumFractionDigits: 2,
|
|
104
|
+
maximumFractionDigits: 2
|
|
105
|
+
})} \u20AC`;
|
|
106
|
+
if (code.durationType === "ONCE") {
|
|
107
|
+
return `${valueStr} einmalig`;
|
|
108
|
+
}
|
|
109
|
+
if (code.durationType === "MONTHS") {
|
|
110
|
+
const m = code.durationValue ?? 0;
|
|
111
|
+
return m === 1 ? `${valueStr} im ersten Monat` : `${valueStr} f\xFCr ${m} Monate`;
|
|
112
|
+
}
|
|
113
|
+
const n = code.durationValue ?? 0;
|
|
114
|
+
if (n === 1) return cycle === "YEARLY" ? `${valueStr} im ersten Jahr` : `${valueStr} im ersten Monat`;
|
|
115
|
+
return cycle === "YEARLY" ? `${valueStr} in den ersten ${n} Jahren` : `${valueStr} in den ersten ${n} Monaten`;
|
|
116
|
+
}
|
|
117
|
+
__name(buildLabel, "buildLabel");
|
|
118
|
+
|
|
119
|
+
// src/promo/service.ts
|
|
120
|
+
var import_common2 = require("@nestjs/common");
|
|
121
|
+
|
|
122
|
+
// src/billing/plan-catalog.module.ts
|
|
123
|
+
var import_common = require("@nestjs/common");
|
|
124
|
+
|
|
125
|
+
// src/core/di.ts
|
|
126
|
+
function asProvider(token, impl) {
|
|
127
|
+
if (typeof impl === "object" && impl !== null && "useFactory" in impl && typeof impl.useFactory === "function") {
|
|
128
|
+
const factory = impl;
|
|
129
|
+
return {
|
|
130
|
+
provide: token,
|
|
131
|
+
useFactory: factory.useFactory,
|
|
132
|
+
inject: factory.inject
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
provide: token,
|
|
137
|
+
useValue: impl
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
__name(asProvider, "asProvider");
|
|
141
|
+
|
|
142
|
+
// src/billing/plan-catalog-from-snapshot.ts
|
|
143
|
+
function buildPlanCatalogFromSnapshot(settings, snapshot) {
|
|
144
|
+
const liveByPlanKey = new Map(snapshot.livePlanVersions.map((v) => [
|
|
145
|
+
v.planId,
|
|
146
|
+
v
|
|
147
|
+
]));
|
|
148
|
+
const plans = snapshot.plans.filter((p) => p.deletedAt === null).sort((a, b) => a.sortOrder - b.sortOrder).map((stem) => {
|
|
149
|
+
const live = liveByPlanKey.get(stem.planKey);
|
|
150
|
+
if (!live) {
|
|
151
|
+
return {
|
|
152
|
+
id: stem.planKey,
|
|
153
|
+
name: stem.label,
|
|
154
|
+
tagline: stem.description ?? void 0,
|
|
155
|
+
marketed: false,
|
|
156
|
+
monthlyNet: null,
|
|
157
|
+
yearlyNet: null,
|
|
158
|
+
features: [],
|
|
159
|
+
quotas: {}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
id: stem.planKey,
|
|
164
|
+
name: stem.label,
|
|
165
|
+
tagline: stem.description ?? void 0,
|
|
166
|
+
marketed: live.marketed,
|
|
167
|
+
monthlyNet: parseFloat(live.monthlyNet),
|
|
168
|
+
yearlyNet: parseFloat(live.yearlyNet),
|
|
169
|
+
features: live.features,
|
|
170
|
+
quotas: live.quotas ?? {}
|
|
171
|
+
};
|
|
172
|
+
});
|
|
173
|
+
const features = snapshot.featureEntries.filter((f) => f.deletedAt === null).sort((a, b) => a.sortOrder - b.sortOrder).map((row) => ({
|
|
174
|
+
key: row.featureKey,
|
|
175
|
+
label: row.label,
|
|
176
|
+
icon: row.icon ?? void 0,
|
|
177
|
+
tier: row.tier ?? void 0,
|
|
178
|
+
plannedOnly: row.plannedOnly
|
|
179
|
+
}));
|
|
180
|
+
return {
|
|
181
|
+
schemaVersion: 1,
|
|
182
|
+
projectKey: settings.projectKey,
|
|
183
|
+
...settings.app ? {
|
|
184
|
+
app: settings.app
|
|
185
|
+
} : {},
|
|
186
|
+
currency: settings.currency,
|
|
187
|
+
vatRate: settings.vatRate,
|
|
188
|
+
...settings.marketing ? {
|
|
189
|
+
marketing: settings.marketing
|
|
190
|
+
} : {},
|
|
191
|
+
features,
|
|
192
|
+
plans
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
__name(buildPlanCatalogFromSnapshot, "buildPlanCatalogFromSnapshot");
|
|
196
|
+
|
|
197
|
+
// src/billing/plan-catalog.module.ts
|
|
198
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
199
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
200
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
201
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
202
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
203
|
+
}
|
|
204
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
205
|
+
var PLAN_CATALOG_TOKEN = /* @__PURE__ */ Symbol("PLAN_CATALOG");
|
|
206
|
+
var PLAN_CATALOG_READ_SINK_TOKEN = /* @__PURE__ */ Symbol("PLAN_CATALOG_READ_SINK");
|
|
207
|
+
var PlanCatalogModule = class _PlanCatalogModule {
|
|
208
|
+
static {
|
|
209
|
+
__name(this, "PlanCatalogModule");
|
|
210
|
+
}
|
|
211
|
+
static forRoot(options) {
|
|
212
|
+
return {
|
|
213
|
+
module: _PlanCatalogModule,
|
|
214
|
+
global: options.global ?? true,
|
|
215
|
+
imports: options.imports ?? [],
|
|
216
|
+
providers: [
|
|
217
|
+
...options.extraProviders ?? [],
|
|
218
|
+
asProvider(PLAN_CATALOG_READ_SINK_TOKEN, options.sink),
|
|
219
|
+
{
|
|
220
|
+
provide: PLAN_CATALOG_TOKEN,
|
|
221
|
+
useFactory: /* @__PURE__ */ __name(async (sink) => {
|
|
222
|
+
const snapshot = await sink.loadSnapshot(options.projectKey);
|
|
223
|
+
return buildPlanCatalogFromSnapshot({
|
|
224
|
+
projectKey: options.projectKey,
|
|
225
|
+
app: options.app,
|
|
226
|
+
currency: options.currency,
|
|
227
|
+
vatRate: options.vatRate,
|
|
228
|
+
marketing: options.marketing
|
|
229
|
+
}, snapshot);
|
|
230
|
+
}, "useFactory"),
|
|
231
|
+
inject: [
|
|
232
|
+
PLAN_CATALOG_READ_SINK_TOKEN
|
|
233
|
+
]
|
|
234
|
+
}
|
|
235
|
+
],
|
|
236
|
+
exports: [
|
|
237
|
+
PLAN_CATALOG_TOKEN,
|
|
238
|
+
PLAN_CATALOG_READ_SINK_TOKEN
|
|
239
|
+
]
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Tests / in-memory setup: takes the catalog object directly, without a
|
|
244
|
+
* sink/DB lookup. For unit tests + test bootstraps.
|
|
245
|
+
*/
|
|
246
|
+
static forRootWithCatalog(catalog, opts = {}) {
|
|
247
|
+
return {
|
|
248
|
+
module: _PlanCatalogModule,
|
|
249
|
+
providers: [
|
|
250
|
+
{
|
|
251
|
+
provide: PLAN_CATALOG_TOKEN,
|
|
252
|
+
useValue: catalog
|
|
253
|
+
}
|
|
254
|
+
],
|
|
255
|
+
exports: [
|
|
256
|
+
PLAN_CATALOG_TOKEN
|
|
257
|
+
],
|
|
258
|
+
global: opts.global ?? true
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
PlanCatalogModule = _ts_decorate([
|
|
263
|
+
(0, import_common.Module)({})
|
|
264
|
+
], PlanCatalogModule);
|
|
265
|
+
|
|
266
|
+
// src/billing/plan-helpers.ts
|
|
267
|
+
function findPlan(catalog, planId) {
|
|
268
|
+
return (catalog.plans ?? []).find((p) => p.id === planId);
|
|
269
|
+
}
|
|
270
|
+
__name(findPlan, "findPlan");
|
|
271
|
+
function getPlanPriceNet(catalog, planId, cycle) {
|
|
272
|
+
const plan = findPlan(catalog, planId);
|
|
273
|
+
if (!plan) return null;
|
|
274
|
+
if (plan.marketed === false) return null;
|
|
275
|
+
const net = cycle === "YEARLY" ? plan.yearlyNet : plan.monthlyNet;
|
|
276
|
+
return net ?? null;
|
|
277
|
+
}
|
|
278
|
+
__name(getPlanPriceNet, "getPlanPriceNet");
|
|
279
|
+
function getPlanPriceGross(catalog, planId, cycle, vatRate) {
|
|
280
|
+
const net = getPlanPriceNet(catalog, planId, cycle);
|
|
281
|
+
if (net === null) return null;
|
|
282
|
+
const rate = vatRate ?? catalog.vatRate;
|
|
283
|
+
return round2(net * (1 + rate / 100));
|
|
284
|
+
}
|
|
285
|
+
__name(getPlanPriceGross, "getPlanPriceGross");
|
|
286
|
+
|
|
287
|
+
// src/promo/tokens.ts
|
|
288
|
+
var PROMO_CODE_REPOSITORY_TOKEN = /* @__PURE__ */ Symbol("PROMO_CODE_REPOSITORY");
|
|
289
|
+
var PROMO_CODE_REDEMPTION_REPOSITORY_TOKEN = /* @__PURE__ */ Symbol("PROMO_CODE_REDEMPTION_REPOSITORY");
|
|
290
|
+
var PROMO_CODE_VALIDATION_LOG_REPOSITORY_TOKEN = /* @__PURE__ */ Symbol("PROMO_CODE_VALIDATION_LOG_REPOSITORY");
|
|
291
|
+
var PROMO_FIRST_TIME_CUSTOMER_CHECK_TOKEN = /* @__PURE__ */ Symbol("PROMO_FIRST_TIME_CUSTOMER_CHECK");
|
|
292
|
+
var PROMO_SUBSCRIPTION_LOOKUP_TOKEN = /* @__PURE__ */ Symbol("PROMO_SUBSCRIPTION_LOOKUP");
|
|
293
|
+
var PROMO_REVENUE_DEDUCTION_AGGREGATOR_TOKEN = /* @__PURE__ */ Symbol("PROMO_REVENUE_DEDUCTION_AGGREGATOR");
|
|
294
|
+
var PROMO_TRANSACTION_RUNNER_TOKEN = /* @__PURE__ */ Symbol("PROMO_TRANSACTION_RUNNER");
|
|
295
|
+
var PROMO_SERVICE_CONFIG_TOKEN = /* @__PURE__ */ Symbol("PROMO_SERVICE_CONFIG");
|
|
296
|
+
|
|
297
|
+
// src/promo/service.ts
|
|
298
|
+
function _ts_decorate2(decorators, target, key, desc) {
|
|
299
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
300
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
301
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
302
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
303
|
+
}
|
|
304
|
+
__name(_ts_decorate2, "_ts_decorate");
|
|
305
|
+
function _ts_metadata(k, v) {
|
|
306
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
307
|
+
}
|
|
308
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
309
|
+
function _ts_param(paramIndex, decorator) {
|
|
310
|
+
return function(target, key) {
|
|
311
|
+
decorator(target, key, paramIndex);
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
__name(_ts_param, "_ts_param");
|
|
315
|
+
var CODE_PATTERN = /^[A-Z0-9_-]{4,32}$/;
|
|
316
|
+
var PromoCodesService = class {
|
|
317
|
+
static {
|
|
318
|
+
__name(this, "PromoCodesService");
|
|
319
|
+
}
|
|
320
|
+
promoRepo;
|
|
321
|
+
redemptionRepo;
|
|
322
|
+
validationLogRepo;
|
|
323
|
+
firstTimeCheck;
|
|
324
|
+
subscriptionLookup;
|
|
325
|
+
revenueAggregator;
|
|
326
|
+
transactionRunner;
|
|
327
|
+
planCatalog;
|
|
328
|
+
config;
|
|
329
|
+
constructor(promoRepo, redemptionRepo, validationLogRepo, firstTimeCheck, subscriptionLookup, revenueAggregator, transactionRunner, planCatalog, config) {
|
|
330
|
+
this.promoRepo = promoRepo;
|
|
331
|
+
this.redemptionRepo = redemptionRepo;
|
|
332
|
+
this.validationLogRepo = validationLogRepo;
|
|
333
|
+
this.firstTimeCheck = firstTimeCheck;
|
|
334
|
+
this.subscriptionLookup = subscriptionLookup;
|
|
335
|
+
this.revenueAggregator = revenueAggregator;
|
|
336
|
+
this.transactionRunner = transactionRunner;
|
|
337
|
+
this.planCatalog = planCatalog;
|
|
338
|
+
this.config = config;
|
|
339
|
+
}
|
|
340
|
+
// ─── ADMIN: Creation / Editing ─────────────────────────────────────────
|
|
341
|
+
async create(input) {
|
|
342
|
+
const code = input.code.trim().toUpperCase();
|
|
343
|
+
if (!CODE_PATTERN.test(code)) {
|
|
344
|
+
throw new import_common2.BadRequestException('Code darf nur Gro\xDFbuchstaben, Ziffern, \u201E-" und \u201E_" enthalten (4\u201332 Zeichen).');
|
|
345
|
+
}
|
|
346
|
+
if (input.valueType === "PERCENT") {
|
|
347
|
+
if (!(input.value > 0 && input.value <= 100)) {
|
|
348
|
+
throw new import_common2.BadRequestException("Prozentsatz muss zwischen 0 und 100 liegen.");
|
|
349
|
+
}
|
|
350
|
+
} else if (!(input.value > 0)) {
|
|
351
|
+
throw new import_common2.BadRequestException("Betrag muss positiv sein.");
|
|
352
|
+
}
|
|
353
|
+
if (input.durationType === "ONCE") {
|
|
354
|
+
if (input.durationValue != null) {
|
|
355
|
+
throw new import_common2.BadRequestException("Bei einmaligem Rabatt darf keine Laufzeit gesetzt sein.");
|
|
356
|
+
}
|
|
357
|
+
} else {
|
|
358
|
+
const v = input.durationValue;
|
|
359
|
+
if (v == null || v < 1 || v > 24) {
|
|
360
|
+
throw new import_common2.BadRequestException("Laufzeit ung\xFCltig (max. 24 Monate bzw. Abrechnungsperioden).");
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
if (input.validFrom && input.validUntil && input.validFrom >= input.validUntil) {
|
|
364
|
+
throw new import_common2.BadRequestException("G\xFCltigkeits-Fenster ung\xFCltig.");
|
|
365
|
+
}
|
|
366
|
+
const plans = input.appliesToPlans ?? [];
|
|
367
|
+
const blocked = this.config.nonRedeemablePlans ?? [];
|
|
368
|
+
for (const p of plans) {
|
|
369
|
+
if (blocked.includes(p)) {
|
|
370
|
+
throw new import_common2.BadRequestException(`${p}-Plan kann nicht rabattiert werden.`);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
if (input.minimumPlanAmountGross != null && input.minimumPlanAmountGross <= 0) {
|
|
374
|
+
throw new import_common2.BadRequestException("Mindest-Plan-Brutto muss positiv sein.");
|
|
375
|
+
}
|
|
376
|
+
if (input.valueType === "ABSOLUTE" && !input.allowZeroInvoice) {
|
|
377
|
+
const lowestApplicableGross = this.lowestApplicablePlanGross(plans);
|
|
378
|
+
if (lowestApplicableGross != null && input.value >= lowestApplicableGross) {
|
|
379
|
+
throw new import_common2.BadRequestException("Bei Absolutbetr\xE4gen muss der Rabatt unter dem niedrigsten anwendbaren Plan-Preis liegen oder allowZeroInvoice aktiviert sein.");
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
const exists = await this.promoRepo.findByCode(code);
|
|
383
|
+
if (exists) throw new import_common2.BadRequestException("Code existiert bereits.");
|
|
384
|
+
return this.promoRepo.create({
|
|
385
|
+
...input,
|
|
386
|
+
code
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
async update(id, input) {
|
|
390
|
+
const existing = await this.promoRepo.findById(id);
|
|
391
|
+
if (!existing) throw new import_common2.NotFoundException("Code nicht gefunden");
|
|
392
|
+
if (input.maxRedemptions != null && existing.maxRedemptions != null) {
|
|
393
|
+
if (input.maxRedemptions < existing.maxRedemptions) {
|
|
394
|
+
throw new import_common2.BadRequestException("maxRedemptions kann nicht gesenkt werden.");
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return this.promoRepo.update(id, input);
|
|
398
|
+
}
|
|
399
|
+
async softDelete(id) {
|
|
400
|
+
const redemptions = await this.redemptionRepo.countByPromoCode(id);
|
|
401
|
+
if (redemptions > 0) {
|
|
402
|
+
throw new import_common2.BadRequestException("Code hat bereits Einl\xF6sungen \u2014 Soft-Delete nicht m\xF6glich. Stattdessen pausieren.");
|
|
403
|
+
}
|
|
404
|
+
await this.promoRepo.softDelete(id);
|
|
405
|
+
}
|
|
406
|
+
async findAll(filter = {}) {
|
|
407
|
+
await this.lazyExpire();
|
|
408
|
+
return this.promoRepo.findMany(filter);
|
|
409
|
+
}
|
|
410
|
+
async findOne(id) {
|
|
411
|
+
await this.lazyExpire();
|
|
412
|
+
const code = await this.promoRepo.findById(id);
|
|
413
|
+
if (!code || code.deletedAt) throw new import_common2.NotFoundException("Code nicht gefunden");
|
|
414
|
+
return code;
|
|
415
|
+
}
|
|
416
|
+
async stats(id) {
|
|
417
|
+
const code = await this.findOne(id);
|
|
418
|
+
const [active, total, reversed, expired, validations, revenueDeductionGross] = await Promise.all([
|
|
419
|
+
this.redemptionRepo.countByPromoCode(id, "ACTIVE"),
|
|
420
|
+
this.redemptionRepo.countByPromoCode(id),
|
|
421
|
+
this.redemptionRepo.countByPromoCode(id, "REVERSED"),
|
|
422
|
+
this.redemptionRepo.countByPromoCode(id, "EXPIRED"),
|
|
423
|
+
this.validationLogRepo.countValid(id),
|
|
424
|
+
this.revenueAggregator.sumGrossForPromoCode(id)
|
|
425
|
+
]);
|
|
426
|
+
return {
|
|
427
|
+
code,
|
|
428
|
+
validations,
|
|
429
|
+
redemptions: {
|
|
430
|
+
active,
|
|
431
|
+
total,
|
|
432
|
+
reversed,
|
|
433
|
+
expired
|
|
434
|
+
},
|
|
435
|
+
revenueDeductionGross
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
async listRedemptions(promoCodeId) {
|
|
439
|
+
return this.redemptionRepo.listByPromoCode(promoCodeId);
|
|
440
|
+
}
|
|
441
|
+
// ─── PUBLIC: Preview / Validation ──────────────────────────────────────
|
|
442
|
+
async preview(input) {
|
|
443
|
+
const code = input.code.trim().toUpperCase();
|
|
444
|
+
if (!CODE_PATTERN.test(code)) {
|
|
445
|
+
await this.validationLogRepo.log({
|
|
446
|
+
promoCodeId: null,
|
|
447
|
+
codeAttempt: code,
|
|
448
|
+
result: "NOT_FOUND",
|
|
449
|
+
ipHash: input.ipHash,
|
|
450
|
+
sessionId: input.sessionId
|
|
451
|
+
});
|
|
452
|
+
return {
|
|
453
|
+
valid: false,
|
|
454
|
+
reason: "NOT_FOUND"
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
await this.lazyExpire();
|
|
458
|
+
const promo = await this.promoRepo.findByCode(code);
|
|
459
|
+
const reason = this.checkEligibility(promo, input) ?? await this.checkFirstTimeCustomer(promo, input.email);
|
|
460
|
+
if (reason) {
|
|
461
|
+
await this.validationLogRepo.log({
|
|
462
|
+
promoCodeId: promo?.id ?? null,
|
|
463
|
+
codeAttempt: code,
|
|
464
|
+
result: reason,
|
|
465
|
+
ipHash: input.ipHash,
|
|
466
|
+
sessionId: input.sessionId
|
|
467
|
+
});
|
|
468
|
+
return {
|
|
469
|
+
valid: false,
|
|
470
|
+
reason
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
const planGross = getPlanPriceGross(this.planCatalog, input.planId, input.billingCycle);
|
|
474
|
+
const discountGross = Math.min(computeDiscountGross({
|
|
475
|
+
gross: planGross
|
|
476
|
+
}, promo), planGross);
|
|
477
|
+
const discountedGross = computeDiscountedGross(planGross, discountGross);
|
|
478
|
+
const includedVat = computeIncludedVat(discountedGross, this.planCatalog.vatRate);
|
|
479
|
+
if (!promo.allowZeroInvoice && discountedGross <= 0) {
|
|
480
|
+
await this.validationLogRepo.log({
|
|
481
|
+
promoCodeId: promo.id,
|
|
482
|
+
codeAttempt: code,
|
|
483
|
+
result: "WOULD_PRODUCE_ZERO_INVOICE",
|
|
484
|
+
ipHash: input.ipHash,
|
|
485
|
+
sessionId: input.sessionId
|
|
486
|
+
});
|
|
487
|
+
return {
|
|
488
|
+
valid: false,
|
|
489
|
+
reason: "WOULD_PRODUCE_ZERO_INVOICE"
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
const startsAt = /* @__PURE__ */ new Date();
|
|
493
|
+
const regularStartsAt = computeRegularStartsAt(startsAt, input.billingCycle, promo.durationType, promo.durationValue);
|
|
494
|
+
await this.validationLogRepo.log({
|
|
495
|
+
promoCodeId: promo.id,
|
|
496
|
+
codeAttempt: code,
|
|
497
|
+
result: "VALID",
|
|
498
|
+
ipHash: input.ipHash,
|
|
499
|
+
sessionId: input.sessionId
|
|
500
|
+
});
|
|
501
|
+
return {
|
|
502
|
+
valid: true,
|
|
503
|
+
code: promo.code,
|
|
504
|
+
label: buildLabel(promo, input.billingCycle),
|
|
505
|
+
discount: {
|
|
506
|
+
valueType: promo.valueType,
|
|
507
|
+
value: Number(promo.value).toFixed(2),
|
|
508
|
+
durationType: promo.durationType,
|
|
509
|
+
durationValue: promo.durationValue
|
|
510
|
+
},
|
|
511
|
+
price: {
|
|
512
|
+
originalGross: planGross.toFixed(2),
|
|
513
|
+
discountGross: discountGross.toFixed(2),
|
|
514
|
+
discountedGross: discountedGross.toFixed(2),
|
|
515
|
+
includedVat: includedVat.toFixed(2),
|
|
516
|
+
nextRegularAmountGross: planGross.toFixed(2),
|
|
517
|
+
regularStartsAt: regularStartsAt ? regularStartsAt.toISOString() : null
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
// ─── Onboarding path: atomic redemption ────────────────────────────────
|
|
522
|
+
/**
|
|
523
|
+
* Wraps `redeemInTransaction` in its own transaction-runner call —
|
|
524
|
+
* the default path when the caller has no external tx context
|
|
525
|
+
* (e.g. POST /billing/promo/redeem as a stand-alone endpoint).
|
|
526
|
+
*/
|
|
527
|
+
async redeem(input) {
|
|
528
|
+
return this.transactionRunner.run((tx) => this.redeemInTransaction(input, tx));
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Redeems a promo code within an EXTERNAL transaction — the caller
|
|
532
|
+
* (typically `TenantSubscriptionWritePort.applyOnboardingSelection`)
|
|
533
|
+
* already has `prisma.$transaction(...)` open and passes the `tx`
|
|
534
|
+
* context through. This lands plan change, add-on insert, and redemption
|
|
535
|
+
* insert in a single DB transaction (P10.1.1).
|
|
536
|
+
*/
|
|
537
|
+
async redeemInTransaction(input, tx) {
|
|
538
|
+
const code = input.code.trim().toUpperCase();
|
|
539
|
+
const promo = await this.promoRepo.findByCode(code, tx);
|
|
540
|
+
if (!promo || promo.deletedAt) {
|
|
541
|
+
throw new import_common2.BadRequestException("Code nicht gefunden");
|
|
542
|
+
}
|
|
543
|
+
const sub = await this.subscriptionLookup.findById(input.subscriptionId, tx);
|
|
544
|
+
if (!sub) throw new import_common2.NotFoundException("Subscription nicht gefunden");
|
|
545
|
+
if (sub.tenantId !== input.tenantId) {
|
|
546
|
+
throw new import_common2.BadRequestException("Subscription geh\xF6rt nicht zum Tenant");
|
|
547
|
+
}
|
|
548
|
+
const reason = this.checkEligibility(promo, {
|
|
549
|
+
code,
|
|
550
|
+
planId: sub.plan,
|
|
551
|
+
billingCycle: sub.billingCycle
|
|
552
|
+
});
|
|
553
|
+
if (reason) throw new import_common2.BadRequestException(`Code nicht einl\xF6sbar: ${reason}`);
|
|
554
|
+
const firstTimeReason = await this.checkFirstTimeCustomer(promo, input.email, {
|
|
555
|
+
requireEmail: true
|
|
556
|
+
});
|
|
557
|
+
if (firstTimeReason) {
|
|
558
|
+
throw new import_common2.BadRequestException(`Code nicht einl\xF6sbar: ${firstTimeReason}`);
|
|
559
|
+
}
|
|
560
|
+
const claimed = await this.promoRepo.claimSlot(promo.id, tx);
|
|
561
|
+
if (!claimed) {
|
|
562
|
+
throw new import_common2.BadRequestException("Code nicht einl\xF6sbar: EXHAUSTED");
|
|
563
|
+
}
|
|
564
|
+
await this.promoRepo.markExhaustedIfFull(promo.id, tx);
|
|
565
|
+
const startsAt = sub.startedAt ?? /* @__PURE__ */ new Date();
|
|
566
|
+
const endsAt = promo.durationType === "ONCE" ? null : computeRegularStartsAt(startsAt, sub.billingCycle, promo.durationType, promo.durationValue);
|
|
567
|
+
return this.redemptionRepo.create({
|
|
568
|
+
promoCodeId: promo.id,
|
|
569
|
+
subscriptionId: sub.id,
|
|
570
|
+
tenantId: sub.tenantId,
|
|
571
|
+
appliedValueType: promo.valueType,
|
|
572
|
+
appliedValue: promo.value,
|
|
573
|
+
appliedDurationType: promo.durationType,
|
|
574
|
+
appliedDurationValue: promo.durationValue,
|
|
575
|
+
startsAt,
|
|
576
|
+
endsAt
|
|
577
|
+
}, tx);
|
|
578
|
+
}
|
|
579
|
+
async reverse(subscriptionId) {
|
|
580
|
+
return this.transactionRunner.run(async (tx) => {
|
|
581
|
+
const redemption = await this.redemptionRepo.findBySubscription(subscriptionId, tx);
|
|
582
|
+
if (!redemption || redemption.status !== "ACTIVE") return redemption;
|
|
583
|
+
const updated = await this.redemptionRepo.setReversed(redemption.id, tx);
|
|
584
|
+
await this.promoRepo.releaseSlot(redemption.promoCodeId, tx);
|
|
585
|
+
return updated;
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
// ─── Helpers ───────────────────────────────────────────────────────────
|
|
589
|
+
checkEligibility(promo, input) {
|
|
590
|
+
if (!promo || promo.deletedAt) return "NOT_FOUND";
|
|
591
|
+
if (promo.status === "EXPIRED") return "EXPIRED";
|
|
592
|
+
if (promo.status === "EXHAUSTED") return "EXHAUSTED";
|
|
593
|
+
if (promo.status === "PAUSED") return "PAUSED";
|
|
594
|
+
const now = /* @__PURE__ */ new Date();
|
|
595
|
+
if (promo.validFrom && promo.validFrom > now) return "EXPIRED";
|
|
596
|
+
if (promo.validUntil && promo.validUntil < now) return "EXPIRED";
|
|
597
|
+
const blocked = this.config.nonRedeemablePlans ?? [];
|
|
598
|
+
if (blocked.includes(input.planId)) {
|
|
599
|
+
return "PLAN_MISMATCH";
|
|
600
|
+
}
|
|
601
|
+
if (promo.appliesToPlans.length > 0 && !promo.appliesToPlans.includes(input.planId)) {
|
|
602
|
+
return "PLAN_MISMATCH";
|
|
603
|
+
}
|
|
604
|
+
if (promo.appliesToBilling && promo.appliesToBilling !== input.billingCycle) {
|
|
605
|
+
return "BILLING_MISMATCH";
|
|
606
|
+
}
|
|
607
|
+
const planGross = getPlanPriceGross(this.planCatalog, input.planId, input.billingCycle);
|
|
608
|
+
if (planGross == null) return "PLAN_MISMATCH";
|
|
609
|
+
if (promo.minimumPlanAmountGross && planGross < Number(promo.minimumPlanAmountGross)) {
|
|
610
|
+
return "BELOW_MINIMUM_AMOUNT";
|
|
611
|
+
}
|
|
612
|
+
return null;
|
|
613
|
+
}
|
|
614
|
+
async checkFirstTimeCustomer(promo, email, options = {}) {
|
|
615
|
+
if (!promo || !promo.firstTimeCustomersOnly) return null;
|
|
616
|
+
if (!email) return options.requireEmail ? "NOT_FIRST_TIME_CUSTOMER" : null;
|
|
617
|
+
const existing = await this.firstTimeCheck.hasExistingCustomerForEmail(email);
|
|
618
|
+
return existing ? "NOT_FIRST_TIME_CUSTOMER" : null;
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Lazy expiry: sets codes whose validUntil has passed to EXPIRED.
|
|
622
|
+
* Called before every find/preview — defense-in-depth alongside the cron.
|
|
623
|
+
*/
|
|
624
|
+
async lazyExpire() {
|
|
625
|
+
const now = /* @__PURE__ */ new Date();
|
|
626
|
+
await this.promoRepo.expireDueCodes(now);
|
|
627
|
+
await this.redemptionRepo.expireDueRedemptions(now);
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Lowest applicable plan price. With a whitelist it takes the minimum
|
|
631
|
+
* from the whitelist, otherwise across all marketed plans of the catalog
|
|
632
|
+
* (except non-redeemable).
|
|
633
|
+
*/
|
|
634
|
+
lowestApplicablePlanGross(plans) {
|
|
635
|
+
const blocked = new Set(this.config.nonRedeemablePlans ?? []);
|
|
636
|
+
const candidates = plans.length > 0 ? plans : (this.planCatalog.plans ?? []).filter((p) => p.marketed !== false && !blocked.has(p.id)).map((p) => p.id);
|
|
637
|
+
let min = null;
|
|
638
|
+
for (const p of candidates) {
|
|
639
|
+
const g = getPlanPriceGross(this.planCatalog, p, "MONTHLY");
|
|
640
|
+
if (g == null) continue;
|
|
641
|
+
if (min == null || g < min) min = g;
|
|
642
|
+
}
|
|
643
|
+
return min;
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
PromoCodesService = _ts_decorate2([
|
|
647
|
+
(0, import_common2.Injectable)(),
|
|
648
|
+
_ts_param(0, (0, import_common2.Inject)(PROMO_CODE_REPOSITORY_TOKEN)),
|
|
649
|
+
_ts_param(1, (0, import_common2.Inject)(PROMO_CODE_REDEMPTION_REPOSITORY_TOKEN)),
|
|
650
|
+
_ts_param(2, (0, import_common2.Inject)(PROMO_CODE_VALIDATION_LOG_REPOSITORY_TOKEN)),
|
|
651
|
+
_ts_param(3, (0, import_common2.Inject)(PROMO_FIRST_TIME_CUSTOMER_CHECK_TOKEN)),
|
|
652
|
+
_ts_param(4, (0, import_common2.Inject)(PROMO_SUBSCRIPTION_LOOKUP_TOKEN)),
|
|
653
|
+
_ts_param(5, (0, import_common2.Inject)(PROMO_REVENUE_DEDUCTION_AGGREGATOR_TOKEN)),
|
|
654
|
+
_ts_param(6, (0, import_common2.Inject)(PROMO_TRANSACTION_RUNNER_TOKEN)),
|
|
655
|
+
_ts_param(7, (0, import_common2.Inject)(PLAN_CATALOG_TOKEN)),
|
|
656
|
+
_ts_param(8, (0, import_common2.Inject)(PROMO_SERVICE_CONFIG_TOKEN)),
|
|
657
|
+
_ts_metadata("design:type", Function),
|
|
658
|
+
_ts_metadata("design:paramtypes", [
|
|
659
|
+
typeof PromoCodeRepository === "undefined" ? Object : PromoCodeRepository,
|
|
660
|
+
typeof PromoCodeRedemptionRepository === "undefined" ? Object : PromoCodeRedemptionRepository,
|
|
661
|
+
typeof PromoCodeValidationLogRepository === "undefined" ? Object : PromoCodeValidationLogRepository,
|
|
662
|
+
typeof FirstTimeCustomerCheck === "undefined" ? Object : FirstTimeCustomerCheck,
|
|
663
|
+
typeof PromoSubscriptionLookup === "undefined" ? Object : PromoSubscriptionLookup,
|
|
664
|
+
typeof PromoRevenueDeductionAggregator === "undefined" ? Object : PromoRevenueDeductionAggregator,
|
|
665
|
+
typeof TransactionRunner === "undefined" ? Object : TransactionRunner,
|
|
666
|
+
typeof PlanCatalog === "undefined" ? Object : PlanCatalog,
|
|
667
|
+
typeof PromoServiceConfig === "undefined" ? Object : PromoServiceConfig
|
|
668
|
+
])
|
|
669
|
+
], PromoCodesService);
|
|
670
|
+
|
|
671
|
+
// src/promo/expirer.ts
|
|
672
|
+
var import_common3 = require("@nestjs/common");
|
|
673
|
+
var import_schedule = require("@nestjs/schedule");
|
|
674
|
+
function _ts_decorate3(decorators, target, key, desc) {
|
|
675
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
676
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
677
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
678
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
679
|
+
}
|
|
680
|
+
__name(_ts_decorate3, "_ts_decorate");
|
|
681
|
+
function _ts_metadata2(k, v) {
|
|
682
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
683
|
+
}
|
|
684
|
+
__name(_ts_metadata2, "_ts_metadata");
|
|
685
|
+
function _ts_param2(paramIndex, decorator) {
|
|
686
|
+
return function(target, key) {
|
|
687
|
+
decorator(target, key, paramIndex);
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
__name(_ts_param2, "_ts_param");
|
|
691
|
+
var PromoCodeExpirer = class _PromoCodeExpirer {
|
|
692
|
+
static {
|
|
693
|
+
__name(this, "PromoCodeExpirer");
|
|
694
|
+
}
|
|
695
|
+
promoRepo;
|
|
696
|
+
redemptionRepo;
|
|
697
|
+
logger = new import_common3.Logger(_PromoCodeExpirer.name);
|
|
698
|
+
constructor(promoRepo, redemptionRepo) {
|
|
699
|
+
this.promoRepo = promoRepo;
|
|
700
|
+
this.redemptionRepo = redemptionRepo;
|
|
701
|
+
}
|
|
702
|
+
async expirePromoCodes() {
|
|
703
|
+
const now = /* @__PURE__ */ new Date();
|
|
704
|
+
const codes = await this.promoRepo.expireDueCodes(now);
|
|
705
|
+
const redemptions = await this.redemptionRepo.expireDueRedemptions(now);
|
|
706
|
+
if (codes > 0 || redemptions > 0) {
|
|
707
|
+
this.logger.log(`PromoCodeExpirer: ${codes} Codes, ${redemptions} Redemptions abgelaufen.`);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
};
|
|
711
|
+
_ts_decorate3([
|
|
712
|
+
(0, import_schedule.Cron)(import_schedule.CronExpression.EVERY_DAY_AT_3AM, {
|
|
713
|
+
name: "promoCodeExpirer",
|
|
714
|
+
timeZone: "Europe/Berlin"
|
|
715
|
+
}),
|
|
716
|
+
_ts_metadata2("design:type", Function),
|
|
717
|
+
_ts_metadata2("design:paramtypes", []),
|
|
718
|
+
_ts_metadata2("design:returntype", Promise)
|
|
719
|
+
], PromoCodeExpirer.prototype, "expirePromoCodes", null);
|
|
720
|
+
PromoCodeExpirer = _ts_decorate3([
|
|
721
|
+
(0, import_common3.Injectable)(),
|
|
722
|
+
_ts_param2(0, (0, import_common3.Inject)(PROMO_CODE_REPOSITORY_TOKEN)),
|
|
723
|
+
_ts_param2(1, (0, import_common3.Inject)(PROMO_CODE_REDEMPTION_REPOSITORY_TOKEN)),
|
|
724
|
+
_ts_metadata2("design:type", Function),
|
|
725
|
+
_ts_metadata2("design:paramtypes", [
|
|
726
|
+
typeof PromoCodeRepository === "undefined" ? Object : PromoCodeRepository,
|
|
727
|
+
typeof PromoCodeRedemptionRepository === "undefined" ? Object : PromoCodeRedemptionRepository
|
|
728
|
+
])
|
|
729
|
+
], PromoCodeExpirer);
|
|
730
|
+
|
|
731
|
+
// src/promo/rate-limit.guard.ts
|
|
732
|
+
var import_common4 = require("@nestjs/common");
|
|
733
|
+
function _ts_decorate4(decorators, target, key, desc) {
|
|
734
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
735
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
736
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
737
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
738
|
+
}
|
|
739
|
+
__name(_ts_decorate4, "_ts_decorate");
|
|
740
|
+
var IP_WINDOW_MS = 6e4;
|
|
741
|
+
var IP_LIMIT = 20;
|
|
742
|
+
var SESSION_WINDOW_MS = 60 * 6e4;
|
|
743
|
+
var SESSION_LIMIT = 50;
|
|
744
|
+
var PromoCodeRateLimitGuard = class {
|
|
745
|
+
static {
|
|
746
|
+
__name(this, "PromoCodeRateLimitGuard");
|
|
747
|
+
}
|
|
748
|
+
ipBuckets = /* @__PURE__ */ new Map();
|
|
749
|
+
sessionBuckets = /* @__PURE__ */ new Map();
|
|
750
|
+
canActivate(context) {
|
|
751
|
+
const req = context.switchToHttp().getRequest();
|
|
752
|
+
const ipKey = ipFingerprint(req);
|
|
753
|
+
const sessionKey = req.user?.id ?? null;
|
|
754
|
+
if (this.exceeded(this.ipBuckets, ipKey, IP_WINDOW_MS, IP_LIMIT)) {
|
|
755
|
+
throw new import_common4.HttpException({
|
|
756
|
+
valid: false,
|
|
757
|
+
reason: "RATE_LIMITED"
|
|
758
|
+
}, import_common4.HttpStatus.TOO_MANY_REQUESTS);
|
|
759
|
+
}
|
|
760
|
+
if (sessionKey && this.exceeded(this.sessionBuckets, sessionKey, SESSION_WINDOW_MS, SESSION_LIMIT)) {
|
|
761
|
+
throw new import_common4.HttpException({
|
|
762
|
+
valid: false,
|
|
763
|
+
reason: "RATE_LIMITED"
|
|
764
|
+
}, import_common4.HttpStatus.TOO_MANY_REQUESTS);
|
|
765
|
+
}
|
|
766
|
+
return true;
|
|
767
|
+
}
|
|
768
|
+
exceeded(map, key, windowMs, limit) {
|
|
769
|
+
const now = Date.now();
|
|
770
|
+
const b = map.get(key);
|
|
771
|
+
if (!b || now - b.windowStart > windowMs) {
|
|
772
|
+
map.set(key, {
|
|
773
|
+
count: 1,
|
|
774
|
+
windowStart: now
|
|
775
|
+
});
|
|
776
|
+
return false;
|
|
777
|
+
}
|
|
778
|
+
b.count += 1;
|
|
779
|
+
return b.count > limit;
|
|
780
|
+
}
|
|
781
|
+
};
|
|
782
|
+
PromoCodeRateLimitGuard = _ts_decorate4([
|
|
783
|
+
(0, import_common4.Injectable)()
|
|
784
|
+
], PromoCodeRateLimitGuard);
|
|
785
|
+
function ipFingerprint(req) {
|
|
786
|
+
const fwd = req.headers["x-forwarded-for"];
|
|
787
|
+
const first = Array.isArray(fwd) ? fwd[0] : fwd?.split(",")[0]?.trim();
|
|
788
|
+
return first || req.ip || "unknown";
|
|
789
|
+
}
|
|
790
|
+
__name(ipFingerprint, "ipFingerprint");
|
|
791
|
+
function hashIp(req) {
|
|
792
|
+
const fp = ipFingerprint(req);
|
|
793
|
+
let h = 0;
|
|
794
|
+
for (let i = 0; i < fp.length; i++) {
|
|
795
|
+
h = h * 31 + fp.charCodeAt(i) | 0;
|
|
796
|
+
}
|
|
797
|
+
return `ip${(h >>> 0).toString(16)}`;
|
|
798
|
+
}
|
|
799
|
+
__name(hashIp, "hashIp");
|
|
800
|
+
|
|
801
|
+
// src/promo/controller.ts
|
|
802
|
+
var import_common5 = require("@nestjs/common");
|
|
803
|
+
|
|
804
|
+
// src/promo/dto/promo-public.dto.ts
|
|
805
|
+
var import_class_validator = require("class-validator");
|
|
806
|
+
function _ts_decorate5(decorators, target, key, desc) {
|
|
807
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
808
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
809
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
810
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
811
|
+
}
|
|
812
|
+
__name(_ts_decorate5, "_ts_decorate");
|
|
813
|
+
function _ts_metadata3(k, v) {
|
|
814
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
815
|
+
}
|
|
816
|
+
__name(_ts_metadata3, "_ts_metadata");
|
|
817
|
+
var PLAN_OR_CYCLE_PATTERN = /^[A-Z][A-Z0-9_]*$/;
|
|
818
|
+
var CODE_PATTERN2 = /^[A-Z0-9_-]{4,32}$/i;
|
|
819
|
+
var PreviewPromoCodeDto = class {
|
|
820
|
+
static {
|
|
821
|
+
__name(this, "PreviewPromoCodeDto");
|
|
822
|
+
}
|
|
823
|
+
code;
|
|
824
|
+
plan;
|
|
825
|
+
billingCycle;
|
|
826
|
+
email;
|
|
827
|
+
};
|
|
828
|
+
_ts_decorate5([
|
|
829
|
+
(0, import_class_validator.IsString)(),
|
|
830
|
+
(0, import_class_validator.Matches)(CODE_PATTERN2, {
|
|
831
|
+
message: 'code muss aus A\u2013Z, 0\u20139, "-" und "_" bestehen (4\u201332 Zeichen)'
|
|
832
|
+
}),
|
|
833
|
+
_ts_metadata3("design:type", String)
|
|
834
|
+
], PreviewPromoCodeDto.prototype, "code", void 0);
|
|
835
|
+
_ts_decorate5([
|
|
836
|
+
(0, import_class_validator.IsString)(),
|
|
837
|
+
(0, import_class_validator.Matches)(PLAN_OR_CYCLE_PATTERN, {
|
|
838
|
+
message: "plan muss SCREAMING_SNAKE_CASE sein"
|
|
839
|
+
}),
|
|
840
|
+
_ts_metadata3("design:type", String)
|
|
841
|
+
], PreviewPromoCodeDto.prototype, "plan", void 0);
|
|
842
|
+
_ts_decorate5([
|
|
843
|
+
(0, import_class_validator.IsString)(),
|
|
844
|
+
(0, import_class_validator.Matches)(PLAN_OR_CYCLE_PATTERN, {
|
|
845
|
+
message: "billingCycle muss SCREAMING_SNAKE_CASE sein (z. B. MONTHLY, YEARLY)"
|
|
846
|
+
}),
|
|
847
|
+
_ts_metadata3("design:type", String)
|
|
848
|
+
], PreviewPromoCodeDto.prototype, "billingCycle", void 0);
|
|
849
|
+
_ts_decorate5([
|
|
850
|
+
(0, import_class_validator.IsOptional)(),
|
|
851
|
+
(0, import_class_validator.IsEmail)(),
|
|
852
|
+
(0, import_class_validator.MaxLength)(254),
|
|
853
|
+
_ts_metadata3("design:type", String)
|
|
854
|
+
], PreviewPromoCodeDto.prototype, "email", void 0);
|
|
855
|
+
|
|
856
|
+
// src/promo/controller.ts
|
|
857
|
+
function _ts_decorate6(decorators, target, key, desc) {
|
|
858
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
859
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
860
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
861
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
862
|
+
}
|
|
863
|
+
__name(_ts_decorate6, "_ts_decorate");
|
|
864
|
+
function _ts_metadata4(k, v) {
|
|
865
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
866
|
+
}
|
|
867
|
+
__name(_ts_metadata4, "_ts_metadata");
|
|
868
|
+
function _ts_param3(paramIndex, decorator) {
|
|
869
|
+
return function(target, key) {
|
|
870
|
+
decorator(target, key, paramIndex);
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
__name(_ts_param3, "_ts_param");
|
|
874
|
+
var PromoCodePublicController = class {
|
|
875
|
+
static {
|
|
876
|
+
__name(this, "PromoCodePublicController");
|
|
877
|
+
}
|
|
878
|
+
promo;
|
|
879
|
+
constructor(promo) {
|
|
880
|
+
this.promo = promo;
|
|
881
|
+
}
|
|
882
|
+
async preview(dto, req) {
|
|
883
|
+
return this.promo.preview({
|
|
884
|
+
code: dto.code,
|
|
885
|
+
planId: dto.plan,
|
|
886
|
+
billingCycle: dto.billingCycle,
|
|
887
|
+
email: dto.email,
|
|
888
|
+
ipHash: hashIp(req),
|
|
889
|
+
sessionId: req.user?.id ?? req.user?.sub
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
_ts_decorate6([
|
|
894
|
+
(0, import_common5.Post)("preview"),
|
|
895
|
+
(0, import_common5.HttpCode)(import_common5.HttpStatus.OK),
|
|
896
|
+
(0, import_common5.UseGuards)(PromoCodeRateLimitGuard),
|
|
897
|
+
_ts_param3(0, (0, import_common5.Body)()),
|
|
898
|
+
_ts_param3(1, (0, import_common5.Req)()),
|
|
899
|
+
_ts_metadata4("design:type", Function),
|
|
900
|
+
_ts_metadata4("design:paramtypes", [
|
|
901
|
+
typeof PreviewPromoCodeDto === "undefined" ? Object : PreviewPromoCodeDto,
|
|
902
|
+
typeof RequestLike === "undefined" ? Object : RequestLike
|
|
903
|
+
]),
|
|
904
|
+
_ts_metadata4("design:returntype", Promise)
|
|
905
|
+
], PromoCodePublicController.prototype, "preview", null);
|
|
906
|
+
PromoCodePublicController = _ts_decorate6([
|
|
907
|
+
(0, import_common5.Controller)("billing/promo"),
|
|
908
|
+
_ts_param3(0, (0, import_common5.Inject)(PromoCodesService)),
|
|
909
|
+
_ts_metadata4("design:type", Function),
|
|
910
|
+
_ts_metadata4("design:paramtypes", [
|
|
911
|
+
typeof PromoCodesService === "undefined" ? Object : PromoCodesService
|
|
912
|
+
])
|
|
913
|
+
], PromoCodePublicController);
|
|
914
|
+
|
|
915
|
+
// src/promo/module.ts
|
|
916
|
+
var import_common6 = require("@nestjs/common");
|
|
917
|
+
function _ts_decorate7(decorators, target, key, desc) {
|
|
918
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
919
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
920
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
921
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
922
|
+
}
|
|
923
|
+
__name(_ts_decorate7, "_ts_decorate");
|
|
924
|
+
var PromoCodesModule = class _PromoCodesModule {
|
|
925
|
+
static {
|
|
926
|
+
__name(this, "PromoCodesModule");
|
|
927
|
+
}
|
|
928
|
+
static forRoot(options) {
|
|
929
|
+
const includeCron = options.includeExpirerCron ?? true;
|
|
930
|
+
const includePublic = options.includePublicController ?? true;
|
|
931
|
+
const providers = [
|
|
932
|
+
asProvider(PROMO_CODE_REPOSITORY_TOKEN, options.promoCodeRepository),
|
|
933
|
+
asProvider(PROMO_CODE_REDEMPTION_REPOSITORY_TOKEN, options.redemptionRepository),
|
|
934
|
+
asProvider(PROMO_CODE_VALIDATION_LOG_REPOSITORY_TOKEN, options.validationLogRepository),
|
|
935
|
+
asProvider(PROMO_FIRST_TIME_CUSTOMER_CHECK_TOKEN, options.firstTimeCustomerCheck),
|
|
936
|
+
asProvider(PROMO_SUBSCRIPTION_LOOKUP_TOKEN, options.subscriptionLookup),
|
|
937
|
+
asProvider(PROMO_REVENUE_DEDUCTION_AGGREGATOR_TOKEN, options.revenueAggregator),
|
|
938
|
+
asProvider(PROMO_TRANSACTION_RUNNER_TOKEN, options.transactionRunner),
|
|
939
|
+
{
|
|
940
|
+
provide: PROMO_SERVICE_CONFIG_TOKEN,
|
|
941
|
+
useValue: options.config ?? {}
|
|
942
|
+
},
|
|
943
|
+
PromoCodesService,
|
|
944
|
+
PromoCodeRateLimitGuard
|
|
945
|
+
];
|
|
946
|
+
if (includeCron) {
|
|
947
|
+
providers.push(PromoCodeExpirer);
|
|
948
|
+
}
|
|
949
|
+
return {
|
|
950
|
+
module: _PromoCodesModule,
|
|
951
|
+
global: options.global ?? false,
|
|
952
|
+
controllers: includePublic ? [
|
|
953
|
+
PromoCodePublicController
|
|
954
|
+
] : [],
|
|
955
|
+
providers,
|
|
956
|
+
exports: [
|
|
957
|
+
PromoCodesService,
|
|
958
|
+
PromoCodeRateLimitGuard,
|
|
959
|
+
PROMO_CODE_REPOSITORY_TOKEN,
|
|
960
|
+
PROMO_CODE_REDEMPTION_REPOSITORY_TOKEN,
|
|
961
|
+
PROMO_CODE_VALIDATION_LOG_REPOSITORY_TOKEN
|
|
962
|
+
]
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
};
|
|
966
|
+
PromoCodesModule = _ts_decorate7([
|
|
967
|
+
(0, import_common6.Module)({})
|
|
968
|
+
], PromoCodesModule);
|
|
969
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
970
|
+
0 && (module.exports = {
|
|
971
|
+
PROMO_CODE_REDEMPTION_REPOSITORY_TOKEN,
|
|
972
|
+
PROMO_CODE_REPOSITORY_TOKEN,
|
|
973
|
+
PROMO_CODE_VALIDATION_LOG_REPOSITORY_TOKEN,
|
|
974
|
+
PROMO_FIRST_TIME_CUSTOMER_CHECK_TOKEN,
|
|
975
|
+
PROMO_REVENUE_DEDUCTION_AGGREGATOR_TOKEN,
|
|
976
|
+
PROMO_SERVICE_CONFIG_TOKEN,
|
|
977
|
+
PROMO_SUBSCRIPTION_LOOKUP_TOKEN,
|
|
978
|
+
PROMO_TRANSACTION_RUNNER_TOKEN,
|
|
979
|
+
PreviewPromoCodeDto,
|
|
980
|
+
PromoCodeExpirer,
|
|
981
|
+
PromoCodePublicController,
|
|
982
|
+
PromoCodeRateLimitGuard,
|
|
983
|
+
PromoCodesModule,
|
|
984
|
+
PromoCodesService,
|
|
985
|
+
addCycles,
|
|
986
|
+
buildLabel,
|
|
987
|
+
computeDiscountGross,
|
|
988
|
+
computeDiscountedGross,
|
|
989
|
+
computeIncludedVat,
|
|
990
|
+
computeRegularStartsAt,
|
|
991
|
+
grossFromNet,
|
|
992
|
+
hashIp,
|
|
993
|
+
ipFingerprint,
|
|
994
|
+
round2
|
|
995
|
+
});
|