@timardex/cluemart-server-shared 2.2.22 → 2.2.30

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(
@@ -3469,6 +3664,7 @@ async function findEventOrImportedMarketById(resourceId) {
3469
3664
 
3470
3665
  // src/service/relations.ts
3471
3666
  import {
3667
+ EnumEventDateStatus as EnumEventDateStatus8,
3472
3668
  EnumInviteStatus as EnumInviteStatus2
3473
3669
  } from "@timardex/cluemart-shared";
3474
3670
  function didRemoveAnyEventDates(previousDateTime, nextDateTime) {
@@ -3481,6 +3677,16 @@ function didRemoveAnyEventDates(previousDateTime, nextDateTime) {
3481
3677
  )
3482
3678
  );
3483
3679
  }
3680
+ function didRemoveAnyStartedDates(previousDateTime, nextDateTime) {
3681
+ if (!previousDateTime?.length) {
3682
+ return false;
3683
+ }
3684
+ return previousDateTime.some(
3685
+ (prev) => prev.dateStatus === EnumEventDateStatus8.STARTED && !nextDateTime?.some(
3686
+ (next) => next.startDate === prev.startDate && next.startTime === prev.startTime && next.dateStatus === EnumEventDateStatus8.STARTED
3687
+ )
3688
+ );
3689
+ }
3484
3690
  function updateRelationDatesToUnavailable(relationDates, eventDateTime) {
3485
3691
  return relationDates.map((relationDate) => {
3486
3692
  const existsInEvent = eventDateTime?.some(
@@ -3494,13 +3700,13 @@ function updateRelationDatesToUnavailable(relationDates, eventDateTime) {
3494
3700
  }
3495
3701
 
3496
3702
  // src/service/license/compliance/ensureUserLicenseCompliance.ts
3497
- import dayjs7 from "dayjs";
3703
+ import dayjs8 from "dayjs";
3498
3704
 
3499
3705
  // src/service/license/compliance/utils.ts
3500
3706
  import {
3501
3707
  EnumUserLicence as EnumUserLicence10
3502
3708
  } from "@timardex/cluemart-shared";
3503
- import dayjs6 from "dayjs";
3709
+ import dayjs7 from "dayjs";
3504
3710
 
3505
3711
  // src/service/license/compliance/event.ts
3506
3712
  import { EnumUserLicence as EnumUserLicence6 } from "@timardex/cluemart-shared";
@@ -3738,7 +3944,7 @@ import {
3738
3944
  EnumUserLicence as EnumUserLicence9,
3739
3945
  vendorLicences
3740
3946
  } from "@timardex/cluemart-shared";
3741
- import dayjs5 from "dayjs";
3947
+ import dayjs6 from "dayjs";
3742
3948
 
3743
3949
  // src/service/license/models.ts
3744
3950
  import {
@@ -3747,7 +3953,7 @@ import {
3747
3953
  isNonVisitorResourceType,
3748
3954
  LicenceExpiryDate
3749
3955
  } from "@timardex/cluemart-shared";
3750
- import dayjs4 from "dayjs";
3956
+ import dayjs5 from "dayjs";
3751
3957
  import { GraphQLError } from "graphql";
3752
3958
  var LicenceCategory = /* @__PURE__ */ ((LicenceCategory2) => {
3753
3959
  LicenceCategory2["EVENT"] = "event";
@@ -3828,7 +4034,7 @@ function getHighestLicenceForCategory(licences, category) {
3828
4034
  return highestLicence;
3829
4035
  }
3830
4036
  function createLicenceObject(expiryDate, licenceType) {
3831
- const today = dayjs4();
4037
+ const today = dayjs5();
3832
4038
  const expiryDateMapping = {
3833
4039
  [LicenceExpiryDate.ONE_YEAR]: today.add(1, "year").endOf("day").toDate(),
3834
4040
  [LicenceExpiryDate.ONE_MONTH]: today.add(1, "month").endOf("day").toDate(),
@@ -4091,7 +4297,7 @@ function isProtectedByCurrentStripeSubscription(user, licenceType, now) {
4091
4297
  if (!stripe || stripe.currentPlan !== licenceType) {
4092
4298
  return false;
4093
4299
  }
4094
- const gracePeriodEndsAt = stripe.paymentGracePeriodEndsAt ? dayjs5(stripe.paymentGracePeriodEndsAt) : null;
4300
+ const gracePeriodEndsAt = stripe.paymentGracePeriodEndsAt ? dayjs6(stripe.paymentGracePeriodEndsAt) : null;
4095
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);
4096
4302
  if (hasActivePaymentGracePeriod) {
4097
4303
  return true;
@@ -4105,7 +4311,7 @@ function isProtectedByCurrentStripeSubscription(user, licenceType, now) {
4105
4311
  if (!stripe.endDate) {
4106
4312
  return false;
4107
4313
  }
4108
- const paidThrough = dayjs5(stripe.endDate);
4314
+ const paidThrough = dayjs6(stripe.endDate);
4109
4315
  return paidThrough.isValid() && paidThrough.isAfter(now);
4110
4316
  }
4111
4317
  async function handleVendorLicenceCompliance(user, now) {
@@ -4122,7 +4328,7 @@ async function handleVendorLicenceCompliance(user, now) {
4122
4328
  (licence) => licence.licenceType !== EnumUserLicence9.STANDARD_VENDOR
4123
4329
  );
4124
4330
  const expiredProLicences = proLicences.filter(
4125
- (licence) => !dayjs5(licence.expiryDate).isAfter(now)
4331
+ (licence) => !dayjs6(licence.expiryDate).isAfter(now)
4126
4332
  );
4127
4333
  const stripeProtectedLicenceTypes = new Set(
4128
4334
  expiredProLicences.filter(
@@ -4134,7 +4340,7 @@ async function handleVendorLicenceCompliance(user, now) {
4134
4340
  );
4135
4341
  const hasValidProLicence = stripeProtectedLicenceTypes.size > 0 || hasValidLicence(proLicences, vendorLicences, now);
4136
4342
  const hasExpiredVendorLicence = vendorLicencesOfUser.some(
4137
- (licence) => !dayjs5(licence.expiryDate).isAfter(now) && !stripeProtectedLicenceTypes.has(licence.licenceType)
4343
+ (licence) => !dayjs6(licence.expiryDate).isAfter(now) && !stripeProtectedLicenceTypes.has(licence.licenceType)
4138
4344
  );
4139
4345
  if (!hasExpiredVendorLicence) {
4140
4346
  return;
@@ -4188,9 +4394,9 @@ async function vendorOwnerHasProLicence(vendorId) {
4188
4394
  if (!owner) {
4189
4395
  return false;
4190
4396
  }
4191
- const now = dayjs5();
4397
+ const now = dayjs6();
4192
4398
  const effectiveVendorLicences = owner.licences?.filter(
4193
- (licence) => licence?.licenceType && (dayjs5(licence.expiryDate).isAfter(now) || isProtectedByCurrentStripeSubscription(
4399
+ (licence) => licence?.licenceType && (dayjs6(licence.expiryDate).isAfter(now) || isProtectedByCurrentStripeSubscription(
4194
4400
  owner,
4195
4401
  licence.licenceType,
4196
4402
  now
@@ -4227,7 +4433,7 @@ async function syncUserLicencesAfterExpiry(userId, licenceTypes, now, mode = "do
4227
4433
  }
4228
4434
  function hasValidLicence(licences, licenceTypes, now) {
4229
4435
  return licences.some(
4230
- (licence) => licenceTypes.includes(licence.licenceType) && dayjs6(licence.expiryDate).isAfter(now)
4436
+ (licence) => licenceTypes.includes(licence.licenceType) && dayjs7(licence.expiryDate).isAfter(now)
4231
4437
  );
4232
4438
  }
4233
4439
  async function sendDeactivationNotification(payload, userId, resourceName, resourceType) {
@@ -4254,7 +4460,7 @@ async function processUserCompliance(user, now) {
4254
4460
  }
4255
4461
 
4256
4462
  // src/service/license/compliance/ensureUserLicenseCompliance.ts
4257
- async function ensureUserLicenseComplianceForUser(userId, now = dayjs7()) {
4463
+ async function ensureUserLicenseComplianceForUser(userId, now = dayjs8()) {
4258
4464
  const user = await UserModel.findById(userId);
4259
4465
  if (!user) {
4260
4466
  return;
@@ -4262,7 +4468,7 @@ async function ensureUserLicenseComplianceForUser(userId, now = dayjs7()) {
4262
4468
  await processUserCompliance(user, now);
4263
4469
  }
4264
4470
  async function ensureUserLicenseCompliance() {
4265
- const now = dayjs7();
4471
+ const now = dayjs8();
4266
4472
  console.log(`\u{1F50D} Checking license compliance at ${now.format()}`);
4267
4473
  const nowDate = now.toDate();
4268
4474
  const users = await UserModel.find({
@@ -4337,7 +4543,7 @@ async function getPartnerResourceInfo(userId, partnerId) {
4337
4543
  }
4338
4544
 
4339
4545
  // src/service/license/reminder/remindUsersAboutLicenseExpiration.ts
4340
- import dayjs9 from "dayjs";
4546
+ import dayjs10 from "dayjs";
4341
4547
 
4342
4548
  // src/service/license/reminder/reminderWindow.ts
4343
4549
  var REMINDER_WINDOW_MIN_HOURS = 6.5 * 24;
@@ -4353,7 +4559,7 @@ import {
4353
4559
  vendorLicences as vendorLicences2
4354
4560
  } from "@timardex/cluemart-shared";
4355
4561
  import { formatDate } from "@timardex/cluemart-shared/utils";
4356
- import dayjs8 from "dayjs";
4562
+ import dayjs9 from "dayjs";
4357
4563
 
4358
4564
  // src/service/license/reminder/vendor.ts
4359
4565
  async function getVendorResourceInfo(userId, vendorId) {
@@ -4412,7 +4618,7 @@ async function checkAndCreateNotification(payload) {
4412
4618
  }
4413
4619
  }
4414
4620
  function isLicenseExpiringIn7Days(expiryDate, now) {
4415
- const expiry = dayjs8(expiryDate);
4621
+ const expiry = dayjs9(expiryDate);
4416
4622
  const hoursUntilExpiry = expiry.diff(now, "hour", true);
4417
4623
  return hoursUntilExpiry >= REMINDER_WINDOW_MIN_HOURS && hoursUntilExpiry <= REMINDER_WINDOW_MAX_HOURS;
4418
4624
  }
@@ -4490,7 +4696,7 @@ function getLicenceExpiryMode(licenceType) {
4490
4696
  }
4491
4697
  async function sendLicenseReminder(user, licence) {
4492
4698
  const licenceName = getLicenseName(licence.licenceType);
4493
- const expiryDate = dayjs8(licence.expiryDate).format(dateFormat2);
4699
+ const expiryDate = dayjs9(licence.expiryDate).format(dateFormat2);
4494
4700
  const userId = String(user._id);
4495
4701
  const resourceInfo = await getLicenseResourceInfo(user, licence);
4496
4702
  if (!resourceInfo) {
@@ -4522,7 +4728,7 @@ async function sendLicenseReminder(user, licence) {
4522
4728
  console.log(`\u2705 Sent 7-day reminder to user ${userId} for ${licenceName}`);
4523
4729
  }
4524
4730
  async function processLicenseReminder(licence, now) {
4525
- if (dayjs8(licence.expiryDate).isBefore(now)) {
4731
+ if (dayjs9(licence.expiryDate).isBefore(now)) {
4526
4732
  return false;
4527
4733
  }
4528
4734
  if (!isLicenseExpiringIn7Days(licence.expiryDate, now)) {
@@ -4576,7 +4782,7 @@ async function processUserLicenseReminders(user, now) {
4576
4782
 
4577
4783
  // src/service/license/reminder/remindUsersAboutLicenseExpiration.ts
4578
4784
  async function remindUsersAboutLicenseExpiration() {
4579
- const now = dayjs9();
4785
+ const now = dayjs10();
4580
4786
  console.log(`\u{1F50D} Checking for licenses expiring in 7 days at ${now.format()}`);
4581
4787
  const minExpiryDate = now.add(REMINDER_WINDOW_MIN_HOURS, "hour").toDate();
4582
4788
  const maxExpiryDate = now.add(REMINDER_WINDOW_MAX_HOURS, "hour").toDate();
@@ -4666,6 +4872,11 @@ export {
4666
4872
  baseResourceFields,
4667
4873
  buildInitialBillingCatalogEntries,
4668
4874
  buildLicenceExpiryPipeline,
4875
+ buildUserHasEnoughPointsFilter,
4876
+ buildUserPointAwardEntry,
4877
+ buildUserPointCreditFilter,
4878
+ buildUserPointDebitPipeline,
4879
+ buildUserPointEntryRemovalPipeline,
4669
4880
  checkOwnerHasLicence,
4670
4881
  connectToDatabase,
4671
4882
  convertObjectIdsToStrings,
@@ -4673,6 +4884,7 @@ export {
4673
4884
  dateTimeSchema,
4674
4885
  dedupeArchivedSlotsByStartDateTime,
4675
4886
  didRemoveAnyEventDates,
4887
+ didRemoveAnyStartedDates,
4676
4888
  downgradeVendorProFeatures,
4677
4889
  ensureUserLicenseCompliance,
4678
4890
  ensureUserLicenseComplianceForUser,