@classytic/loyalty 0.1.0 → 0.2.1

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.
Files changed (43) hide show
  1. package/CHANGELOG.md +68 -0
  2. package/dist/config-DoZPGBfW.d.mts +136 -0
  3. package/dist/{constants-C75GvizO.mjs → constants-DkQYkOK1.mjs} +3 -2
  4. package/dist/domain/enums/index.d.mts +1 -1
  5. package/dist/domain/enums/index.mjs +1 -1
  6. package/dist/events/index.d.mts +2 -2
  7. package/dist/events/index.mjs +3 -3
  8. package/dist/events-BtZOdiPw.mjs +311 -0
  9. package/dist/index-BOWqBSWx.d.mts +281 -0
  10. package/dist/index-C4jFHS7m.d.mts +400 -0
  11. package/dist/index-ClPKmtxh.d.mts +361 -0
  12. package/dist/{index-CaH4GhSF.d.mts → index-DMK8a98J.d.mts} +1 -1
  13. package/dist/index.d.mts +28 -12
  14. package/dist/index.mjs +30 -26
  15. package/dist/loyalty-events-Cyt9TwX6.mjs +50 -0
  16. package/dist/models/index.d.mts +1225 -2
  17. package/dist/models/index.mjs +2 -2
  18. package/dist/{models-CrZPmVxt.mjs → models-uhh9c-7k.mjs} +104 -41
  19. package/dist/notifications/index.d.mts +1 -1
  20. package/dist/repositories/index.d.mts +2 -2
  21. package/dist/repositories/index.mjs +1 -1
  22. package/dist/repositories-DBSxe9st.mjs +1919 -0
  23. package/dist/types/index.d.mts +2 -2
  24. package/package.json +22 -17
  25. package/dist/config-aNigy4ec.d.mts +0 -90
  26. package/dist/domain/index.d.mts +0 -90
  27. package/dist/domain/index.mjs +0 -4
  28. package/dist/domain-errors-D7q-2LYD.mjs +0 -120
  29. package/dist/event-emitter.port-CLYPoMni.d.mts +0 -8
  30. package/dist/event-types-xQBtYBbX.mjs +0 -24
  31. package/dist/events-CprEWlN7.mjs +0 -25
  32. package/dist/index-B6ZstxSP.d.mts +0 -2357
  33. package/dist/index-BcoGeDco.d.mts +0 -127
  34. package/dist/index-DqzTjeEB.d.mts +0 -148
  35. package/dist/index-O7TL4R2P.d.mts +0 -86
  36. package/dist/index-_pg4J11t.d.mts +0 -191
  37. package/dist/repositories-qMX7Xh_y.mjs +0 -440
  38. package/dist/services/index.d.mts +0 -2
  39. package/dist/services/index.mjs +0 -2
  40. package/dist/services-CoTqC4E6.mjs +0 -1098
  41. package/dist/tier-definition-Djf72UPa.d.mts +0 -120
  42. package/dist/tier-definition.port-BzrHYBRo.d.mts +0 -143
  43. /package/dist/{notification.port-BzUsH0It.d.mts → notification.port-CJ9nXj0k.d.mts} +0 -0
@@ -1,440 +0,0 @@
1
- import { n as DEFAULT_TENANT_FIELD } from "./constants-C75GvizO.mjs";
2
- import { createRepository } from "@classytic/mongokit";
3
- //#region src/utils/mongoose.ts
4
- /**
5
- * Converts a Mongoose document (or lean object) into a plain object.
6
- * Returns null pass-through so callers can keep `T | null` semantics.
7
- */
8
- function toDoc(doc) {
9
- if (doc == null) return doc;
10
- const d = doc;
11
- return typeof d.toObject === "function" ? d.toObject({ virtuals: true }) : doc;
12
- }
13
- /** Batch version of `toDoc`. */
14
- function toDocs(docs) {
15
- return docs.map((d) => toDoc(d));
16
- }
17
- //#endregion
18
- //#region src/repositories/earning-rule.repository.ts
19
- var EarningRuleRepository = class {
20
- repo;
21
- tenantField;
22
- constructor(model, plugins = [], tenantField = "organizationId") {
23
- this.repo = createRepository(model, plugins);
24
- this.tenantField = tenantField;
25
- }
26
- async create(data, session) {
27
- return toDoc(await this.repo.create(data, { session }));
28
- }
29
- async getById(id, tenantId) {
30
- if (tenantId) return toDoc(await this.repo.getByQuery({
31
- _id: id,
32
- [this.tenantField]: tenantId
33
- }, { throwOnNotFound: false }));
34
- return toDoc(await this.repo.getById(id, { throwOnNotFound: false }));
35
- }
36
- async update(id, data, tenantId, session) {
37
- const opts = {
38
- session,
39
- lean: true
40
- };
41
- if (tenantId) opts.query = { [this.tenantField]: tenantId };
42
- try {
43
- return toDoc(await this.repo.update(id, data, opts));
44
- } catch (err) {
45
- if (err.status === 404 && tenantId) return null;
46
- throw err;
47
- }
48
- }
49
- async findActive(tenantId, programId) {
50
- const query = { status: "active" };
51
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
52
- if (programId !== void 0) query.programId = programId;
53
- return toDocs((await this.repo.getAll({
54
- filters: query,
55
- limit: 1e3,
56
- page: 1
57
- })).docs);
58
- }
59
- async paginate(query, options) {
60
- const result = await this.repo.getAll({
61
- filters: query,
62
- page: options.page ?? 1,
63
- limit: options.limit ?? 20,
64
- sort: options.sort
65
- });
66
- return {
67
- docs: toDocs(result.docs),
68
- total: result.total,
69
- page: result.page,
70
- pages: result.pages
71
- };
72
- }
73
- };
74
- //#endregion
75
- //#region src/repositories/member.repository.ts
76
- var MemberRepository = class {
77
- repo;
78
- tenantField;
79
- constructor(model, plugins = [], tenantField = "organizationId") {
80
- this.repo = createRepository(model, plugins);
81
- this.tenantField = tenantField;
82
- }
83
- async create(data, session) {
84
- return toDoc(await this.repo.create(data, { session }));
85
- }
86
- async getById(id, tenantId) {
87
- if (tenantId) return toDoc(await this.repo.getByQuery({
88
- _id: id,
89
- [this.tenantField]: tenantId
90
- }, { throwOnNotFound: false }));
91
- return toDoc(await this.repo.getById(id, { throwOnNotFound: false }));
92
- }
93
- async getByExternalId(externalId, externalType, tenantId) {
94
- const query = {
95
- externalId,
96
- externalType
97
- };
98
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
99
- return toDoc(await this.repo.getByQuery(query, { throwOnNotFound: false }));
100
- }
101
- async getByCardId(cardId, tenantId) {
102
- const query = { cardId };
103
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
104
- return toDoc(await this.repo.getByQuery(query, { throwOnNotFound: false }));
105
- }
106
- async getByReferralCode(code, tenantId) {
107
- const query = { referralCode: code };
108
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
109
- return toDoc(await this.repo.getByQuery(query, { throwOnNotFound: false }));
110
- }
111
- async update(id, data, tenantId, session) {
112
- const opts = {
113
- session,
114
- lean: true
115
- };
116
- if (tenantId) opts.query = { [this.tenantField]: tenantId };
117
- try {
118
- return toDoc(await this.repo.update(id, data, opts));
119
- } catch (err) {
120
- if (err.status === 404 && tenantId) return null;
121
- throw err;
122
- }
123
- }
124
- async updateBalance(id, balanceUpdate, tenantId, session) {
125
- const inc = {};
126
- for (const [key, val] of Object.entries(balanceUpdate)) inc[`balance.${key}`] = val;
127
- const opts = {
128
- session,
129
- lean: true
130
- };
131
- if (tenantId) opts.query = { [this.tenantField]: tenantId };
132
- try {
133
- return toDoc(await this.repo.update(id, { $inc: inc }, opts));
134
- } catch (err) {
135
- if (err.status === 404 && tenantId) return null;
136
- throw err;
137
- }
138
- }
139
- async findMany(query, options) {
140
- return toDocs((await this.repo.getAll({
141
- filters: query,
142
- limit: options?.limit ?? 100,
143
- page: options?.page ?? 1
144
- })).docs);
145
- }
146
- };
147
- //#endregion
148
- //#region src/repositories/point-transaction.repository.ts
149
- var PointTransactionRepository = class {
150
- repo;
151
- tenantField;
152
- constructor(model, plugins = [], tenantField = "organizationId") {
153
- this.repo = createRepository(model, plugins);
154
- this.tenantField = tenantField;
155
- }
156
- async create(data, session) {
157
- return toDoc(await this.repo.create(data, { session }));
158
- }
159
- async getById(id, tenantId) {
160
- if (tenantId) return toDoc(await this.repo.getByQuery({
161
- _id: id,
162
- [this.tenantField]: tenantId
163
- }, { throwOnNotFound: false }));
164
- return toDoc(await this.repo.getById(id, { throwOnNotFound: false }));
165
- }
166
- async getByIdempotencyKey(key, tenantId) {
167
- const query = { idempotencyKey: key };
168
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
169
- return toDoc(await this.repo.getByQuery(query, { throwOnNotFound: false }));
170
- }
171
- async update(id, data, session) {
172
- return toDoc(await this.repo.update(id, data, { session }));
173
- }
174
- async findMany(query, options) {
175
- return toDocs((await this.repo.getAll({
176
- filters: query,
177
- limit: options?.limit ?? 100,
178
- page: options?.page ?? 1,
179
- sort: options?.sort
180
- })).docs);
181
- }
182
- async paginate(query, options) {
183
- const result = await this.repo.getAll({
184
- filters: query,
185
- page: options.page ?? 1,
186
- limit: options.limit ?? 20,
187
- sort: options.sort
188
- });
189
- return {
190
- docs: toDocs(result.docs),
191
- total: result.total,
192
- page: result.page,
193
- pages: result.pages
194
- };
195
- }
196
- async findExpired(now, tenantId, limit) {
197
- const query = {
198
- expiresAt: { $lte: now },
199
- expiredAt: { $exists: false }
200
- };
201
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
202
- return toDocs((await this.repo.getAll({
203
- filters: query,
204
- limit: limit ?? 100,
205
- page: 1
206
- })).docs);
207
- }
208
- };
209
- //#endregion
210
- //#region src/repositories/redemption.repository.ts
211
- var RedemptionRepository = class {
212
- repo;
213
- tenantField;
214
- constructor(model, plugins = [], tenantField = "organizationId") {
215
- this.repo = createRepository(model, plugins);
216
- this.tenantField = tenantField;
217
- }
218
- async create(data, session) {
219
- return toDoc(await this.repo.create(data, { session }));
220
- }
221
- async getById(id, tenantId) {
222
- if (tenantId) return toDoc(await this.repo.getByQuery({
223
- _id: id,
224
- [this.tenantField]: tenantId
225
- }, { throwOnNotFound: false }));
226
- return toDoc(await this.repo.getById(id, { throwOnNotFound: false }));
227
- }
228
- async getByOwner(ownerType, ownerId, tenantId) {
229
- const query = {
230
- ownerType,
231
- ownerId
232
- };
233
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
234
- return toDoc(await this.repo.getByQuery(query, { throwOnNotFound: false }));
235
- }
236
- async update(id, data, tenantId, session) {
237
- const opts = {
238
- session,
239
- lean: true
240
- };
241
- if (tenantId) opts.query = { [this.tenantField]: tenantId };
242
- try {
243
- return toDoc(await this.repo.update(id, data, opts));
244
- } catch (err) {
245
- if (err.status === 404 && tenantId) return null;
246
- throw err;
247
- }
248
- }
249
- async atomicStatusTransition(id, expectedStatus, data, tenantId, session) {
250
- const query = { status: expectedStatus };
251
- if (tenantId) query[this.tenantField] = tenantId;
252
- try {
253
- return toDoc(await this.repo.update(id, data, {
254
- session,
255
- lean: true,
256
- query,
257
- throwOnNotFound: false
258
- }));
259
- } catch {
260
- return null;
261
- }
262
- }
263
- async findExpired(now, tenantId) {
264
- const query = {
265
- status: "reserved",
266
- expiresAt: { $lte: now }
267
- };
268
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
269
- return toDocs((await this.repo.getAll({
270
- filters: query,
271
- limit: 1e3,
272
- page: 1
273
- })).docs);
274
- }
275
- };
276
- //#endregion
277
- //#region src/repositories/referral.repository.ts
278
- var ReferralRepository = class {
279
- repo;
280
- tenantField;
281
- constructor(model, plugins = [], tenantField = "organizationId") {
282
- this.repo = createRepository(model, plugins);
283
- this.tenantField = tenantField;
284
- }
285
- async create(data, session) {
286
- return toDoc(await this.repo.create(data, { session }));
287
- }
288
- async getById(id, tenantId) {
289
- if (tenantId) return toDoc(await this.repo.getByQuery({
290
- _id: id,
291
- [this.tenantField]: tenantId
292
- }, { throwOnNotFound: false }));
293
- return toDoc(await this.repo.getById(id, { throwOnNotFound: false }));
294
- }
295
- async getByReferee(refereeId, tenantId) {
296
- const query = { refereeId };
297
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
298
- return toDoc(await this.repo.getByQuery(query, { throwOnNotFound: false }));
299
- }
300
- async update(id, data, tenantId, session) {
301
- const opts = {
302
- session,
303
- lean: true
304
- };
305
- if (tenantId) opts.query = { [this.tenantField]: tenantId };
306
- try {
307
- return toDoc(await this.repo.update(id, data, opts));
308
- } catch (err) {
309
- if (err.status === 404 && tenantId) return null;
310
- throw err;
311
- }
312
- }
313
- async atomicStatusTransition(id, expectedStatus, data, tenantId, session) {
314
- const query = { status: expectedStatus };
315
- if (tenantId) query[this.tenantField] = tenantId;
316
- try {
317
- return toDoc(await this.repo.update(id, data, {
318
- session,
319
- lean: true,
320
- query,
321
- throwOnNotFound: false
322
- }));
323
- } catch {
324
- return null;
325
- }
326
- }
327
- async countByReferrerInPeriod(referrerId, since, tenantId) {
328
- const query = {
329
- referrerId,
330
- createdAt: { $gte: since }
331
- };
332
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
333
- return this.repo.count(query);
334
- }
335
- async findByReferrer(referrerId, options, tenantId) {
336
- const query = { referrerId };
337
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
338
- const result = await this.repo.getAll({
339
- filters: query,
340
- page: options.page ?? 1,
341
- limit: options.limit ?? 20,
342
- sort: { createdAt: -1 }
343
- });
344
- return {
345
- docs: toDocs(result.docs),
346
- total: result.total,
347
- page: result.page,
348
- pages: result.pages
349
- };
350
- }
351
- async findExpired(now, tenantId) {
352
- const query = {
353
- status: "pending",
354
- expiresAt: { $lte: now }
355
- };
356
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
357
- return toDocs((await this.repo.getAll({
358
- filters: query,
359
- limit: 1e3,
360
- page: 1
361
- })).docs);
362
- }
363
- };
364
- //#endregion
365
- //#region src/repositories/tier-definition.repository.ts
366
- var TierDefinitionRepository = class {
367
- repo;
368
- tenantField;
369
- constructor(model, plugins = [], tenantField = "organizationId") {
370
- this.repo = createRepository(model, plugins);
371
- this.tenantField = tenantField;
372
- }
373
- async create(data, session) {
374
- return toDoc(await this.repo.create(data, { session }));
375
- }
376
- async getById(id, tenantId) {
377
- if (tenantId) return toDoc(await this.repo.getByQuery({
378
- _id: id,
379
- [this.tenantField]: tenantId
380
- }, { throwOnNotFound: false }));
381
- return toDoc(await this.repo.getById(id, { throwOnNotFound: false }));
382
- }
383
- async getByName(name, tenantId, programId) {
384
- const query = { name };
385
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
386
- if (programId !== void 0) query.programId = programId;
387
- return toDoc(await this.repo.getByQuery(query, { throwOnNotFound: false }));
388
- }
389
- async update(id, data, tenantId, session) {
390
- const opts = {
391
- session,
392
- lean: true
393
- };
394
- if (tenantId) opts.query = { [this.tenantField]: tenantId };
395
- try {
396
- return toDoc(await this.repo.update(id, data, opts));
397
- } catch (err) {
398
- if (err.status === 404 && tenantId) return null;
399
- throw err;
400
- }
401
- }
402
- async delete(id, tenantId) {
403
- if (tenantId) {
404
- await this.repo.Model.deleteOne({
405
- _id: id,
406
- [this.tenantField]: tenantId
407
- });
408
- return;
409
- }
410
- await this.repo.delete(id);
411
- }
412
- async findAll(tenantId, programId) {
413
- const query = {};
414
- if (tenantId !== void 0) query[this.tenantField] = tenantId;
415
- if (programId !== void 0) query.programId = programId;
416
- return toDocs((await this.repo.getAll({
417
- filters: query,
418
- limit: 1e3,
419
- page: 1,
420
- sort: { rank: 1 }
421
- })).docs);
422
- }
423
- };
424
- //#endregion
425
- //#region src/repositories/index.ts
426
- function asPlugins(p) {
427
- return p ?? [];
428
- }
429
- function createRepositories(models, plugins, tenantField = DEFAULT_TENANT_FIELD) {
430
- return {
431
- member: new MemberRepository(models.Member, asPlugins(plugins?.member), tenantField),
432
- pointTransaction: new PointTransactionRepository(models.PointTransaction, asPlugins(plugins?.pointTransaction), tenantField),
433
- earningRule: new EarningRuleRepository(models.EarningRule, asPlugins(plugins?.earningRule), tenantField),
434
- tierDefinition: new TierDefinitionRepository(models.TierDefinition, asPlugins(plugins?.tierDefinition), tenantField),
435
- redemption: new RedemptionRepository(models.Redemption, asPlugins(plugins?.redemption), tenantField),
436
- referral: new ReferralRepository(models.Referral, asPlugins(plugins?.referral), tenantField)
437
- };
438
- }
439
- //#endregion
440
- export { PointTransactionRepository as a, RedemptionRepository as i, TierDefinitionRepository as n, MemberRepository as o, ReferralRepository as r, EarningRuleRepository as s, createRepositories as t };
@@ -1,2 +0,0 @@
1
- import { a as ReferralService, c as EarningService, i as TierService, l as LedgerService, n as LoyaltyServices, o as RedemptionService, r as createServices, s as MemberService, t as CreateServicesInput } from "../index-_pg4J11t.mjs";
2
- export { CreateServicesInput, EarningService, LedgerService, LoyaltyServices, MemberService, RedemptionService, ReferralService, TierService, createServices };
@@ -1,2 +0,0 @@
1
- import { a as MemberService, i as RedemptionService, n as TierService, o as LedgerService, r as ReferralService, s as EarningService, t as createServices } from "../services-CoTqC4E6.mjs";
2
- export { EarningService, LedgerService, MemberService, RedemptionService, ReferralService, TierService, createServices };