@timardex/cluemart-server-shared 2.2.23 → 2.3.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/dist/index.mjs CHANGED
@@ -1378,6 +1378,7 @@ import {
1378
1378
  EnumBillingPeriod as EnumBillingPeriod2,
1379
1379
  EnumNotificationResourceType as EnumNotificationResourceType2,
1380
1380
  EnumOSPlatform as EnumOSPlatform3,
1381
+ EnumPointType,
1381
1382
  EnumResourceType as EnumResourceType4,
1382
1383
  EnumReward,
1383
1384
  EnumSubscriptionStatus,
@@ -1511,6 +1512,19 @@ var redeemedRewardSchema = new MongooseSchema12(
1511
1512
  },
1512
1513
  { _id: false }
1513
1514
  );
1515
+ var userOverallPointsSchema = new MongooseSchema12(
1516
+ {
1517
+ createdAt: { required: true, type: Date },
1518
+ points: { default: 0, required: true, type: Number },
1519
+ pointType: {
1520
+ enum: Object.values(EnumPointType),
1521
+ required: true,
1522
+ type: String
1523
+ },
1524
+ sourceId: { required: false, type: String }
1525
+ },
1526
+ { _id: false }
1527
+ );
1514
1528
  var schema12 = new MongooseSchema12(
1515
1529
  {
1516
1530
  active: { default: false, required: true, type: Boolean },
@@ -1562,7 +1576,11 @@ var schema12 = new MongooseSchema12(
1562
1576
  type: locationsSchema
1563
1577
  },
1564
1578
  nickName: { required: false, type: String },
1565
- overallPoints: { default: 0, required: false, type: Number },
1579
+ overallPoints: {
1580
+ default: [],
1581
+ required: false,
1582
+ type: [userOverallPointsSchema]
1583
+ },
1566
1584
  partner: {
1567
1585
  ref: "Partner",
1568
1586
  required: false,
@@ -2216,7 +2234,10 @@ var gameTypeSchema = new MongooseSchema24(
2216
2234
  required: true,
2217
2235
  type: String
2218
2236
  },
2219
- gameTypeId: { required: true, type: String }
2237
+ gameTypeId: { required: true, type: String },
2238
+ // See `GameType.pointsCredited`: absent means "completed before the marker
2239
+ // existed", which must not be re-credited.
2240
+ pointsCredited: { required: false, type: Boolean }
2220
2241
  },
2221
2242
  { timestamps: true }
2222
2243
  );
@@ -3186,13 +3207,187 @@ function convertObjectIdsToStrings(obj) {
3186
3207
  return obj;
3187
3208
  }
3188
3209
 
3210
+ // src/service/userPoints.ts
3211
+ import dayjs2 from "dayjs";
3212
+ function assertValidPointAmount(amount, label) {
3213
+ if (!Number.isFinite(amount) || amount < 0) {
3214
+ throw new RangeError(
3215
+ `Invalid user point ${label}: ${String(amount)}. Expected a finite, non-negative number.`
3216
+ );
3217
+ }
3218
+ }
3219
+ function buildUserPointAwardEntry(pointType, points, sourceId) {
3220
+ assertValidPointAmount(points, "award");
3221
+ return {
3222
+ createdAt: dayjs2().toDate(),
3223
+ points,
3224
+ pointType,
3225
+ ...sourceId === void 0 ? {} : { sourceId }
3226
+ };
3227
+ }
3228
+ function buildUserPointCreditFilter(userId, sourceId) {
3229
+ return {
3230
+ _id: userId,
3231
+ overallPoints: { $not: { $elemMatch: { sourceId } } }
3232
+ };
3233
+ }
3234
+ function buildUserHasEnoughPointsFilter(amount) {
3235
+ assertValidPointAmount(amount, "threshold");
3236
+ return {
3237
+ $expr: {
3238
+ $gte: [{ $sum: { $ifNull: ["$overallPoints.points", []] } }, amount]
3239
+ }
3240
+ };
3241
+ }
3242
+ function buildLedgerWriteExpr(computedLedger) {
3243
+ return {
3244
+ $cond: [
3245
+ { $isArray: "$overallPoints" },
3246
+ computedLedger,
3247
+ { $ifNull: ["$overallPoints", []] }
3248
+ ]
3249
+ };
3250
+ }
3251
+ function buildUserPointDebitPipeline(amount) {
3252
+ assertValidPointAmount(amount, "debit");
3253
+ const debitedLedger = {
3254
+ $let: {
3255
+ vars: {
3256
+ sorted: {
3257
+ $sortArray: {
3258
+ input: { $ifNull: ["$overallPoints", []] },
3259
+ sortBy: { createdAt: 1 }
3260
+ }
3261
+ }
3262
+ },
3263
+ in: {
3264
+ $let: {
3265
+ vars: {
3266
+ debited: {
3267
+ $reduce: {
3268
+ initialValue: { budget: amount, entries: [] },
3269
+ in: {
3270
+ $let: {
3271
+ vars: { available: { $ifNull: ["$$this.points", 0] } },
3272
+ in: {
3273
+ budget: {
3274
+ $subtract: [
3275
+ "$$value.budget",
3276
+ { $min: ["$$available", "$$value.budget"] }
3277
+ ]
3278
+ },
3279
+ entries: {
3280
+ $concatArrays: [
3281
+ "$$value.entries",
3282
+ [
3283
+ {
3284
+ $mergeObjects: [
3285
+ "$$this",
3286
+ {
3287
+ points: {
3288
+ $subtract: [
3289
+ "$$available",
3290
+ {
3291
+ $min: ["$$available", "$$value.budget"]
3292
+ }
3293
+ ]
3294
+ }
3295
+ }
3296
+ ]
3297
+ }
3298
+ ]
3299
+ ]
3300
+ }
3301
+ }
3302
+ }
3303
+ },
3304
+ input: "$$sorted"
3305
+ }
3306
+ }
3307
+ },
3308
+ in: {
3309
+ $filter: {
3310
+ as: "entry",
3311
+ cond: { $gt: ["$$entry.points", 0] },
3312
+ input: "$$debited.entries"
3313
+ }
3314
+ }
3315
+ }
3316
+ }
3317
+ }
3318
+ };
3319
+ return [
3320
+ {
3321
+ $set: {
3322
+ overallPoints: buildLedgerWriteExpr(debitedLedger)
3323
+ }
3324
+ }
3325
+ ];
3326
+ }
3327
+ function buildUserPointEntryRemovalPipeline(pointType, sourceId) {
3328
+ const indexExpression = sourceId === void 0 ? { $indexOfArray: ["$$entries.pointType", pointType] } : {
3329
+ $indexOfArray: [
3330
+ {
3331
+ $map: {
3332
+ as: "entry",
3333
+ in: {
3334
+ $and: [
3335
+ { $eq: ["$$entry.pointType", pointType] },
3336
+ { $eq: ["$$entry.sourceId", sourceId] }
3337
+ ]
3338
+ },
3339
+ input: "$$entries"
3340
+ }
3341
+ },
3342
+ true
3343
+ ]
3344
+ };
3345
+ const entriesAfterRemoval = {
3346
+ $let: {
3347
+ vars: { entries: { $ifNull: ["$overallPoints", []] } },
3348
+ in: {
3349
+ $let: {
3350
+ vars: {
3351
+ index: indexExpression
3352
+ },
3353
+ in: {
3354
+ $cond: [
3355
+ { $lt: ["$$index", 0] },
3356
+ "$$entries",
3357
+ {
3358
+ $concatArrays: [
3359
+ { $slice: ["$$entries", "$$index"] },
3360
+ {
3361
+ $slice: [
3362
+ "$$entries",
3363
+ { $add: ["$$index", 1] },
3364
+ { $size: "$$entries" }
3365
+ ]
3366
+ }
3367
+ ]
3368
+ }
3369
+ ]
3370
+ }
3371
+ }
3372
+ }
3373
+ }
3374
+ };
3375
+ return [
3376
+ {
3377
+ $set: {
3378
+ overallPoints: buildLedgerWriteExpr(entriesAfterRemoval)
3379
+ }
3380
+ }
3381
+ ];
3382
+ }
3383
+
3189
3384
  // src/service/event/updateAllEventDateTimeStatuses.ts
3190
3385
  import {
3191
3386
  EnumEventDateStatus as EnumEventDateStatus7,
3192
3387
  filterDatesByFutureStatus,
3193
3388
  updateSingleDateTimeStatus
3194
3389
  } from "@timardex/cluemart-shared";
3195
- import dayjs3 from "dayjs";
3390
+ import dayjs4 from "dayjs";
3196
3391
 
3197
3392
  // src/service/event/archiveEndedEventDates.ts
3198
3393
  import {
@@ -3200,9 +3395,9 @@ import {
3200
3395
  dateFormat,
3201
3396
  timeFormat
3202
3397
  } from "@timardex/cluemart-shared";
3203
- import dayjs2 from "dayjs";
3398
+ import dayjs3 from "dayjs";
3204
3399
  import customParseFormat from "dayjs/plugin/customParseFormat";
3205
- dayjs2.extend(customParseFormat);
3400
+ dayjs3.extend(customParseFormat);
3206
3401
  var ARCHIVED_STATUSES = /* @__PURE__ */ new Set([
3207
3402
  EnumEventDateStatus6.ENDED
3208
3403
  ]);
@@ -3233,7 +3428,7 @@ function isPastCanceledEventDate(slot, now) {
3233
3428
  if (slot.dateStatus !== EnumEventDateStatus6.CANCELED) {
3234
3429
  return false;
3235
3430
  }
3236
- const endDateTime = dayjs2(
3431
+ const endDateTime = dayjs3(
3237
3432
  `${slot.endDate} ${slot.endTime}`,
3238
3433
  `${dateFormat} ${timeFormat}`,
3239
3434
  true
@@ -3428,7 +3623,7 @@ async function cleanEventInfoDates(now) {
3428
3623
  return removedCount;
3429
3624
  }
3430
3625
  async function updateAllEventDateTimeStatuses() {
3431
- const now = dayjs3();
3626
+ const now = dayjs4();
3432
3627
  const [eventCount, marketCount] = await Promise.all([
3433
3628
  updateModelDateTimeStatuses(EventModel, now),
3434
3629
  updateModelDateTimeStatuses(
@@ -3505,13 +3700,13 @@ function updateRelationDatesToUnavailable(relationDates, eventDateTime) {
3505
3700
  }
3506
3701
 
3507
3702
  // src/service/license/compliance/ensureUserLicenseCompliance.ts
3508
- import dayjs7 from "dayjs";
3703
+ import dayjs8 from "dayjs";
3509
3704
 
3510
3705
  // src/service/license/compliance/utils.ts
3511
3706
  import {
3512
3707
  EnumUserLicence as EnumUserLicence10
3513
3708
  } from "@timardex/cluemart-shared";
3514
- import dayjs6 from "dayjs";
3709
+ import dayjs7 from "dayjs";
3515
3710
 
3516
3711
  // src/service/license/compliance/event.ts
3517
3712
  import { EnumUserLicence as EnumUserLicence6 } from "@timardex/cluemart-shared";
@@ -3749,7 +3944,7 @@ import {
3749
3944
  EnumUserLicence as EnumUserLicence9,
3750
3945
  vendorLicences
3751
3946
  } from "@timardex/cluemart-shared";
3752
- import dayjs5 from "dayjs";
3947
+ import dayjs6 from "dayjs";
3753
3948
 
3754
3949
  // src/service/license/models.ts
3755
3950
  import {
@@ -3758,7 +3953,7 @@ import {
3758
3953
  isNonVisitorResourceType,
3759
3954
  LicenceExpiryDate
3760
3955
  } from "@timardex/cluemart-shared";
3761
- import dayjs4 from "dayjs";
3956
+ import dayjs5 from "dayjs";
3762
3957
  import { GraphQLError } from "graphql";
3763
3958
  var LicenceCategory = /* @__PURE__ */ ((LicenceCategory2) => {
3764
3959
  LicenceCategory2["EVENT"] = "event";
@@ -3839,7 +4034,7 @@ function getHighestLicenceForCategory(licences, category) {
3839
4034
  return highestLicence;
3840
4035
  }
3841
4036
  function createLicenceObject(expiryDate, licenceType) {
3842
- const today = dayjs4();
4037
+ const today = dayjs5();
3843
4038
  const expiryDateMapping = {
3844
4039
  [LicenceExpiryDate.ONE_YEAR]: today.add(1, "year").endOf("day").toDate(),
3845
4040
  [LicenceExpiryDate.ONE_MONTH]: today.add(1, "month").endOf("day").toDate(),
@@ -4102,7 +4297,7 @@ function isProtectedByCurrentStripeSubscription(user, licenceType, now) {
4102
4297
  if (!stripe || stripe.currentPlan !== licenceType) {
4103
4298
  return false;
4104
4299
  }
4105
- const gracePeriodEndsAt = stripe.paymentGracePeriodEndsAt ? dayjs5(stripe.paymentGracePeriodEndsAt) : null;
4300
+ const gracePeriodEndsAt = stripe.paymentGracePeriodEndsAt ? dayjs6(stripe.paymentGracePeriodEndsAt) : null;
4106
4301
  const hasActivePaymentGracePeriod = (stripe.status === EnumSubscriptionStatus3.PAST_DUE || stripe.status === EnumSubscriptionStatus3.INACTIVE) && Boolean(stripe.paymentGraceSubscriptionId) && (!stripe.subscriptionId || stripe.subscriptionId === stripe.paymentGraceSubscriptionId) && gracePeriodEndsAt !== null && gracePeriodEndsAt.isValid() && gracePeriodEndsAt.isAfter(now);
4107
4302
  if (hasActivePaymentGracePeriod) {
4108
4303
  return true;
@@ -4116,7 +4311,7 @@ function isProtectedByCurrentStripeSubscription(user, licenceType, now) {
4116
4311
  if (!stripe.endDate) {
4117
4312
  return false;
4118
4313
  }
4119
- const paidThrough = dayjs5(stripe.endDate);
4314
+ const paidThrough = dayjs6(stripe.endDate);
4120
4315
  return paidThrough.isValid() && paidThrough.isAfter(now);
4121
4316
  }
4122
4317
  async function handleVendorLicenceCompliance(user, now) {
@@ -4133,7 +4328,7 @@ async function handleVendorLicenceCompliance(user, now) {
4133
4328
  (licence) => licence.licenceType !== EnumUserLicence9.STANDARD_VENDOR
4134
4329
  );
4135
4330
  const expiredProLicences = proLicences.filter(
4136
- (licence) => !dayjs5(licence.expiryDate).isAfter(now)
4331
+ (licence) => !dayjs6(licence.expiryDate).isAfter(now)
4137
4332
  );
4138
4333
  const stripeProtectedLicenceTypes = new Set(
4139
4334
  expiredProLicences.filter(
@@ -4145,7 +4340,7 @@ async function handleVendorLicenceCompliance(user, now) {
4145
4340
  );
4146
4341
  const hasValidProLicence = stripeProtectedLicenceTypes.size > 0 || hasValidLicence(proLicences, vendorLicences, now);
4147
4342
  const hasExpiredVendorLicence = vendorLicencesOfUser.some(
4148
- (licence) => !dayjs5(licence.expiryDate).isAfter(now) && !stripeProtectedLicenceTypes.has(licence.licenceType)
4343
+ (licence) => !dayjs6(licence.expiryDate).isAfter(now) && !stripeProtectedLicenceTypes.has(licence.licenceType)
4149
4344
  );
4150
4345
  if (!hasExpiredVendorLicence) {
4151
4346
  return;
@@ -4199,9 +4394,9 @@ async function vendorOwnerHasProLicence(vendorId) {
4199
4394
  if (!owner) {
4200
4395
  return false;
4201
4396
  }
4202
- const now = dayjs5();
4397
+ const now = dayjs6();
4203
4398
  const effectiveVendorLicences = owner.licences?.filter(
4204
- (licence) => licence?.licenceType && (dayjs5(licence.expiryDate).isAfter(now) || isProtectedByCurrentStripeSubscription(
4399
+ (licence) => licence?.licenceType && (dayjs6(licence.expiryDate).isAfter(now) || isProtectedByCurrentStripeSubscription(
4205
4400
  owner,
4206
4401
  licence.licenceType,
4207
4402
  now
@@ -4238,7 +4433,7 @@ async function syncUserLicencesAfterExpiry(userId, licenceTypes, now, mode = "do
4238
4433
  }
4239
4434
  function hasValidLicence(licences, licenceTypes, now) {
4240
4435
  return licences.some(
4241
- (licence) => licenceTypes.includes(licence.licenceType) && dayjs6(licence.expiryDate).isAfter(now)
4436
+ (licence) => licenceTypes.includes(licence.licenceType) && dayjs7(licence.expiryDate).isAfter(now)
4242
4437
  );
4243
4438
  }
4244
4439
  async function sendDeactivationNotification(payload, userId, resourceName, resourceType) {
@@ -4265,7 +4460,7 @@ async function processUserCompliance(user, now) {
4265
4460
  }
4266
4461
 
4267
4462
  // src/service/license/compliance/ensureUserLicenseCompliance.ts
4268
- async function ensureUserLicenseComplianceForUser(userId, now = dayjs7()) {
4463
+ async function ensureUserLicenseComplianceForUser(userId, now = dayjs8()) {
4269
4464
  const user = await UserModel.findById(userId);
4270
4465
  if (!user) {
4271
4466
  return;
@@ -4273,7 +4468,7 @@ async function ensureUserLicenseComplianceForUser(userId, now = dayjs7()) {
4273
4468
  await processUserCompliance(user, now);
4274
4469
  }
4275
4470
  async function ensureUserLicenseCompliance() {
4276
- const now = dayjs7();
4471
+ const now = dayjs8();
4277
4472
  console.log(`\u{1F50D} Checking license compliance at ${now.format()}`);
4278
4473
  const nowDate = now.toDate();
4279
4474
  const users = await UserModel.find({
@@ -4348,7 +4543,7 @@ async function getPartnerResourceInfo(userId, partnerId) {
4348
4543
  }
4349
4544
 
4350
4545
  // src/service/license/reminder/remindUsersAboutLicenseExpiration.ts
4351
- import dayjs9 from "dayjs";
4546
+ import dayjs10 from "dayjs";
4352
4547
 
4353
4548
  // src/service/license/reminder/reminderWindow.ts
4354
4549
  var REMINDER_WINDOW_MIN_HOURS = 6.5 * 24;
@@ -4364,7 +4559,7 @@ import {
4364
4559
  vendorLicences as vendorLicences2
4365
4560
  } from "@timardex/cluemart-shared";
4366
4561
  import { formatDate } from "@timardex/cluemart-shared/utils";
4367
- import dayjs8 from "dayjs";
4562
+ import dayjs9 from "dayjs";
4368
4563
 
4369
4564
  // src/service/license/reminder/vendor.ts
4370
4565
  async function getVendorResourceInfo(userId, vendorId) {
@@ -4423,7 +4618,7 @@ async function checkAndCreateNotification(payload) {
4423
4618
  }
4424
4619
  }
4425
4620
  function isLicenseExpiringIn7Days(expiryDate, now) {
4426
- const expiry = dayjs8(expiryDate);
4621
+ const expiry = dayjs9(expiryDate);
4427
4622
  const hoursUntilExpiry = expiry.diff(now, "hour", true);
4428
4623
  return hoursUntilExpiry >= REMINDER_WINDOW_MIN_HOURS && hoursUntilExpiry <= REMINDER_WINDOW_MAX_HOURS;
4429
4624
  }
@@ -4501,7 +4696,7 @@ function getLicenceExpiryMode(licenceType) {
4501
4696
  }
4502
4697
  async function sendLicenseReminder(user, licence) {
4503
4698
  const licenceName = getLicenseName(licence.licenceType);
4504
- const expiryDate = dayjs8(licence.expiryDate).format(dateFormat2);
4699
+ const expiryDate = dayjs9(licence.expiryDate).format(dateFormat2);
4505
4700
  const userId = String(user._id);
4506
4701
  const resourceInfo = await getLicenseResourceInfo(user, licence);
4507
4702
  if (!resourceInfo) {
@@ -4533,7 +4728,7 @@ async function sendLicenseReminder(user, licence) {
4533
4728
  console.log(`\u2705 Sent 7-day reminder to user ${userId} for ${licenceName}`);
4534
4729
  }
4535
4730
  async function processLicenseReminder(licence, now) {
4536
- if (dayjs8(licence.expiryDate).isBefore(now)) {
4731
+ if (dayjs9(licence.expiryDate).isBefore(now)) {
4537
4732
  return false;
4538
4733
  }
4539
4734
  if (!isLicenseExpiringIn7Days(licence.expiryDate, now)) {
@@ -4587,7 +4782,7 @@ async function processUserLicenseReminders(user, now) {
4587
4782
 
4588
4783
  // src/service/license/reminder/remindUsersAboutLicenseExpiration.ts
4589
4784
  async function remindUsersAboutLicenseExpiration() {
4590
- const now = dayjs9();
4785
+ const now = dayjs10();
4591
4786
  console.log(`\u{1F50D} Checking for licenses expiring in 7 days at ${now.format()}`);
4592
4787
  const minExpiryDate = now.add(REMINDER_WINDOW_MIN_HOURS, "hour").toDate();
4593
4788
  const maxExpiryDate = now.add(REMINDER_WINDOW_MAX_HOURS, "hour").toDate();
@@ -4677,6 +4872,11 @@ export {
4677
4872
  baseResourceFields,
4678
4873
  buildInitialBillingCatalogEntries,
4679
4874
  buildLicenceExpiryPipeline,
4875
+ buildUserHasEnoughPointsFilter,
4876
+ buildUserPointAwardEntry,
4877
+ buildUserPointCreditFilter,
4878
+ buildUserPointDebitPipeline,
4879
+ buildUserPointEntryRemovalPipeline,
4680
4880
  checkOwnerHasLicence,
4681
4881
  connectToDatabase,
4682
4882
  convertObjectIdsToStrings,