@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.
@@ -10,7 +10,7 @@ import {
10
10
  PushTokenModel,
11
11
  UserModel,
12
12
  VendorModel
13
- } from "../chunk-TIGQ6F5T.mjs";
13
+ } from "../chunk-XGBC5DCX.mjs";
14
14
  import "../chunk-TMPPPLIW.mjs";
15
15
 
16
16
  // src/service/promoCode/constants.ts
@@ -611,13 +611,187 @@ function convertObjectIdsToStrings(obj) {
611
611
  return obj;
612
612
  }
613
613
 
614
+ // src/service/userPoints.ts
615
+ import dayjs2 from "dayjs";
616
+ function assertValidPointAmount(amount, label) {
617
+ if (!Number.isFinite(amount) || amount < 0) {
618
+ throw new RangeError(
619
+ `Invalid user point ${label}: ${String(amount)}. Expected a finite, non-negative number.`
620
+ );
621
+ }
622
+ }
623
+ function buildUserPointAwardEntry(pointType, points, sourceId) {
624
+ assertValidPointAmount(points, "award");
625
+ return {
626
+ createdAt: dayjs2().toDate(),
627
+ points,
628
+ pointType,
629
+ ...sourceId === void 0 ? {} : { sourceId }
630
+ };
631
+ }
632
+ function buildUserPointCreditFilter(userId, sourceId) {
633
+ return {
634
+ _id: userId,
635
+ overallPoints: { $not: { $elemMatch: { sourceId } } }
636
+ };
637
+ }
638
+ function buildUserHasEnoughPointsFilter(amount) {
639
+ assertValidPointAmount(amount, "threshold");
640
+ return {
641
+ $expr: {
642
+ $gte: [{ $sum: { $ifNull: ["$overallPoints.points", []] } }, amount]
643
+ }
644
+ };
645
+ }
646
+ function buildLedgerWriteExpr(computedLedger) {
647
+ return {
648
+ $cond: [
649
+ { $isArray: "$overallPoints" },
650
+ computedLedger,
651
+ { $ifNull: ["$overallPoints", []] }
652
+ ]
653
+ };
654
+ }
655
+ function buildUserPointDebitPipeline(amount) {
656
+ assertValidPointAmount(amount, "debit");
657
+ const debitedLedger = {
658
+ $let: {
659
+ vars: {
660
+ sorted: {
661
+ $sortArray: {
662
+ input: { $ifNull: ["$overallPoints", []] },
663
+ sortBy: { createdAt: 1 }
664
+ }
665
+ }
666
+ },
667
+ in: {
668
+ $let: {
669
+ vars: {
670
+ debited: {
671
+ $reduce: {
672
+ initialValue: { budget: amount, entries: [] },
673
+ in: {
674
+ $let: {
675
+ vars: { available: { $ifNull: ["$$this.points", 0] } },
676
+ in: {
677
+ budget: {
678
+ $subtract: [
679
+ "$$value.budget",
680
+ { $min: ["$$available", "$$value.budget"] }
681
+ ]
682
+ },
683
+ entries: {
684
+ $concatArrays: [
685
+ "$$value.entries",
686
+ [
687
+ {
688
+ $mergeObjects: [
689
+ "$$this",
690
+ {
691
+ points: {
692
+ $subtract: [
693
+ "$$available",
694
+ {
695
+ $min: ["$$available", "$$value.budget"]
696
+ }
697
+ ]
698
+ }
699
+ }
700
+ ]
701
+ }
702
+ ]
703
+ ]
704
+ }
705
+ }
706
+ }
707
+ },
708
+ input: "$$sorted"
709
+ }
710
+ }
711
+ },
712
+ in: {
713
+ $filter: {
714
+ as: "entry",
715
+ cond: { $gt: ["$$entry.points", 0] },
716
+ input: "$$debited.entries"
717
+ }
718
+ }
719
+ }
720
+ }
721
+ }
722
+ };
723
+ return [
724
+ {
725
+ $set: {
726
+ overallPoints: buildLedgerWriteExpr(debitedLedger)
727
+ }
728
+ }
729
+ ];
730
+ }
731
+ function buildUserPointEntryRemovalPipeline(pointType, sourceId) {
732
+ const indexExpression = sourceId === void 0 ? { $indexOfArray: ["$$entries.pointType", pointType] } : {
733
+ $indexOfArray: [
734
+ {
735
+ $map: {
736
+ as: "entry",
737
+ in: {
738
+ $and: [
739
+ { $eq: ["$$entry.pointType", pointType] },
740
+ { $eq: ["$$entry.sourceId", sourceId] }
741
+ ]
742
+ },
743
+ input: "$$entries"
744
+ }
745
+ },
746
+ true
747
+ ]
748
+ };
749
+ const entriesAfterRemoval = {
750
+ $let: {
751
+ vars: { entries: { $ifNull: ["$overallPoints", []] } },
752
+ in: {
753
+ $let: {
754
+ vars: {
755
+ index: indexExpression
756
+ },
757
+ in: {
758
+ $cond: [
759
+ { $lt: ["$$index", 0] },
760
+ "$$entries",
761
+ {
762
+ $concatArrays: [
763
+ { $slice: ["$$entries", "$$index"] },
764
+ {
765
+ $slice: [
766
+ "$$entries",
767
+ { $add: ["$$index", 1] },
768
+ { $size: "$$entries" }
769
+ ]
770
+ }
771
+ ]
772
+ }
773
+ ]
774
+ }
775
+ }
776
+ }
777
+ }
778
+ };
779
+ return [
780
+ {
781
+ $set: {
782
+ overallPoints: buildLedgerWriteExpr(entriesAfterRemoval)
783
+ }
784
+ }
785
+ ];
786
+ }
787
+
614
788
  // src/service/event/updateAllEventDateTimeStatuses.ts
615
789
  import {
616
790
  EnumEventDateStatus as EnumEventDateStatus2,
617
791
  filterDatesByFutureStatus,
618
792
  updateSingleDateTimeStatus
619
793
  } from "@timardex/cluemart-shared";
620
- import dayjs3 from "dayjs";
794
+ import dayjs4 from "dayjs";
621
795
 
622
796
  // src/service/event/archiveEndedEventDates.ts
623
797
  import {
@@ -625,9 +799,9 @@ import {
625
799
  dateFormat,
626
800
  timeFormat
627
801
  } from "@timardex/cluemart-shared";
628
- import dayjs2 from "dayjs";
802
+ import dayjs3 from "dayjs";
629
803
  import customParseFormat from "dayjs/plugin/customParseFormat";
630
- dayjs2.extend(customParseFormat);
804
+ dayjs3.extend(customParseFormat);
631
805
  var ARCHIVED_STATUSES = /* @__PURE__ */ new Set([
632
806
  EnumEventDateStatus.ENDED
633
807
  ]);
@@ -658,7 +832,7 @@ function isPastCanceledEventDate(slot, now) {
658
832
  if (slot.dateStatus !== EnumEventDateStatus.CANCELED) {
659
833
  return false;
660
834
  }
661
- const endDateTime = dayjs2(
835
+ const endDateTime = dayjs3(
662
836
  `${slot.endDate} ${slot.endTime}`,
663
837
  `${dateFormat} ${timeFormat}`,
664
838
  true
@@ -853,7 +1027,7 @@ async function cleanEventInfoDates(now) {
853
1027
  return removedCount;
854
1028
  }
855
1029
  async function updateAllEventDateTimeStatuses() {
856
- const now = dayjs3();
1030
+ const now = dayjs4();
857
1031
  const [eventCount, marketCount] = await Promise.all([
858
1032
  updateModelDateTimeStatuses(EventModel, now),
859
1033
  updateModelDateTimeStatuses(
@@ -894,6 +1068,7 @@ async function findEventOrImportedMarketById(resourceId) {
894
1068
 
895
1069
  // src/service/relations.ts
896
1070
  import {
1071
+ EnumEventDateStatus as EnumEventDateStatus3,
897
1072
  EnumInviteStatus
898
1073
  } from "@timardex/cluemart-shared";
899
1074
  function didRemoveAnyEventDates(previousDateTime, nextDateTime) {
@@ -906,6 +1081,16 @@ function didRemoveAnyEventDates(previousDateTime, nextDateTime) {
906
1081
  )
907
1082
  );
908
1083
  }
1084
+ function didRemoveAnyStartedDates(previousDateTime, nextDateTime) {
1085
+ if (!previousDateTime?.length) {
1086
+ return false;
1087
+ }
1088
+ return previousDateTime.some(
1089
+ (prev) => prev.dateStatus === EnumEventDateStatus3.STARTED && !nextDateTime?.some(
1090
+ (next) => next.startDate === prev.startDate && next.startTime === prev.startTime && next.dateStatus === EnumEventDateStatus3.STARTED
1091
+ )
1092
+ );
1093
+ }
909
1094
  function updateRelationDatesToUnavailable(relationDates, eventDateTime) {
910
1095
  return relationDates.map((relationDate) => {
911
1096
  const existsInEvent = eventDateTime?.some(
@@ -919,13 +1104,13 @@ function updateRelationDatesToUnavailable(relationDates, eventDateTime) {
919
1104
  }
920
1105
 
921
1106
  // src/service/license/compliance/ensureUserLicenseCompliance.ts
922
- import dayjs7 from "dayjs";
1107
+ import dayjs8 from "dayjs";
923
1108
 
924
1109
  // src/service/license/compliance/utils.ts
925
1110
  import {
926
1111
  EnumUserLicence as EnumUserLicence7
927
1112
  } from "@timardex/cluemart-shared";
928
- import dayjs6 from "dayjs";
1113
+ import dayjs7 from "dayjs";
929
1114
 
930
1115
  // src/service/license/compliance/event.ts
931
1116
  import { EnumUserLicence as EnumUserLicence3 } from "@timardex/cluemart-shared";
@@ -1163,7 +1348,7 @@ import {
1163
1348
  EnumUserLicence as EnumUserLicence6,
1164
1349
  vendorLicences
1165
1350
  } from "@timardex/cluemart-shared";
1166
- import dayjs5 from "dayjs";
1351
+ import dayjs6 from "dayjs";
1167
1352
 
1168
1353
  // src/service/license/models.ts
1169
1354
  import {
@@ -1172,7 +1357,7 @@ import {
1172
1357
  isNonVisitorResourceType,
1173
1358
  LicenceExpiryDate
1174
1359
  } from "@timardex/cluemart-shared";
1175
- import dayjs4 from "dayjs";
1360
+ import dayjs5 from "dayjs";
1176
1361
  import { GraphQLError } from "graphql";
1177
1362
  var LicenceCategory = /* @__PURE__ */ ((LicenceCategory2) => {
1178
1363
  LicenceCategory2["EVENT"] = "event";
@@ -1253,7 +1438,7 @@ function getHighestLicenceForCategory(licences, category) {
1253
1438
  return highestLicence;
1254
1439
  }
1255
1440
  function createLicenceObject(expiryDate, licenceType) {
1256
- const today = dayjs4();
1441
+ const today = dayjs5();
1257
1442
  const expiryDateMapping = {
1258
1443
  [LicenceExpiryDate.ONE_YEAR]: today.add(1, "year").endOf("day").toDate(),
1259
1444
  [LicenceExpiryDate.ONE_MONTH]: today.add(1, "month").endOf("day").toDate(),
@@ -1516,7 +1701,7 @@ function isProtectedByCurrentStripeSubscription(user, licenceType, now) {
1516
1701
  if (!stripe || stripe.currentPlan !== licenceType) {
1517
1702
  return false;
1518
1703
  }
1519
- const gracePeriodEndsAt = stripe.paymentGracePeriodEndsAt ? dayjs5(stripe.paymentGracePeriodEndsAt) : null;
1704
+ const gracePeriodEndsAt = stripe.paymentGracePeriodEndsAt ? dayjs6(stripe.paymentGracePeriodEndsAt) : null;
1520
1705
  const hasActivePaymentGracePeriod = (stripe.status === EnumSubscriptionStatus2.PAST_DUE || stripe.status === EnumSubscriptionStatus2.INACTIVE) && Boolean(stripe.paymentGraceSubscriptionId) && (!stripe.subscriptionId || stripe.subscriptionId === stripe.paymentGraceSubscriptionId) && gracePeriodEndsAt !== null && gracePeriodEndsAt.isValid() && gracePeriodEndsAt.isAfter(now);
1521
1706
  if (hasActivePaymentGracePeriod) {
1522
1707
  return true;
@@ -1530,7 +1715,7 @@ function isProtectedByCurrentStripeSubscription(user, licenceType, now) {
1530
1715
  if (!stripe.endDate) {
1531
1716
  return false;
1532
1717
  }
1533
- const paidThrough = dayjs5(stripe.endDate);
1718
+ const paidThrough = dayjs6(stripe.endDate);
1534
1719
  return paidThrough.isValid() && paidThrough.isAfter(now);
1535
1720
  }
1536
1721
  async function handleVendorLicenceCompliance(user, now) {
@@ -1547,7 +1732,7 @@ async function handleVendorLicenceCompliance(user, now) {
1547
1732
  (licence) => licence.licenceType !== EnumUserLicence6.STANDARD_VENDOR
1548
1733
  );
1549
1734
  const expiredProLicences = proLicences.filter(
1550
- (licence) => !dayjs5(licence.expiryDate).isAfter(now)
1735
+ (licence) => !dayjs6(licence.expiryDate).isAfter(now)
1551
1736
  );
1552
1737
  const stripeProtectedLicenceTypes = new Set(
1553
1738
  expiredProLicences.filter(
@@ -1559,7 +1744,7 @@ async function handleVendorLicenceCompliance(user, now) {
1559
1744
  );
1560
1745
  const hasValidProLicence = stripeProtectedLicenceTypes.size > 0 || hasValidLicence(proLicences, vendorLicences, now);
1561
1746
  const hasExpiredVendorLicence = vendorLicencesOfUser.some(
1562
- (licence) => !dayjs5(licence.expiryDate).isAfter(now) && !stripeProtectedLicenceTypes.has(licence.licenceType)
1747
+ (licence) => !dayjs6(licence.expiryDate).isAfter(now) && !stripeProtectedLicenceTypes.has(licence.licenceType)
1563
1748
  );
1564
1749
  if (!hasExpiredVendorLicence) {
1565
1750
  return;
@@ -1613,9 +1798,9 @@ async function vendorOwnerHasProLicence(vendorId) {
1613
1798
  if (!owner) {
1614
1799
  return false;
1615
1800
  }
1616
- const now = dayjs5();
1801
+ const now = dayjs6();
1617
1802
  const effectiveVendorLicences = owner.licences?.filter(
1618
- (licence) => licence?.licenceType && (dayjs5(licence.expiryDate).isAfter(now) || isProtectedByCurrentStripeSubscription(
1803
+ (licence) => licence?.licenceType && (dayjs6(licence.expiryDate).isAfter(now) || isProtectedByCurrentStripeSubscription(
1619
1804
  owner,
1620
1805
  licence.licenceType,
1621
1806
  now
@@ -1652,7 +1837,7 @@ async function syncUserLicencesAfterExpiry(userId, licenceTypes, now, mode = "do
1652
1837
  }
1653
1838
  function hasValidLicence(licences, licenceTypes, now) {
1654
1839
  return licences.some(
1655
- (licence) => licenceTypes.includes(licence.licenceType) && dayjs6(licence.expiryDate).isAfter(now)
1840
+ (licence) => licenceTypes.includes(licence.licenceType) && dayjs7(licence.expiryDate).isAfter(now)
1656
1841
  );
1657
1842
  }
1658
1843
  async function sendDeactivationNotification(payload, userId, resourceName, resourceType) {
@@ -1679,7 +1864,7 @@ async function processUserCompliance(user, now) {
1679
1864
  }
1680
1865
 
1681
1866
  // src/service/license/compliance/ensureUserLicenseCompliance.ts
1682
- async function ensureUserLicenseComplianceForUser(userId, now = dayjs7()) {
1867
+ async function ensureUserLicenseComplianceForUser(userId, now = dayjs8()) {
1683
1868
  const user = await UserModel.findById(userId);
1684
1869
  if (!user) {
1685
1870
  return;
@@ -1687,7 +1872,7 @@ async function ensureUserLicenseComplianceForUser(userId, now = dayjs7()) {
1687
1872
  await processUserCompliance(user, now);
1688
1873
  }
1689
1874
  async function ensureUserLicenseCompliance() {
1690
- const now = dayjs7();
1875
+ const now = dayjs8();
1691
1876
  console.log(`\u{1F50D} Checking license compliance at ${now.format()}`);
1692
1877
  const nowDate = now.toDate();
1693
1878
  const users = await UserModel.find({
@@ -1762,7 +1947,7 @@ async function getPartnerResourceInfo(userId, partnerId) {
1762
1947
  }
1763
1948
 
1764
1949
  // src/service/license/reminder/remindUsersAboutLicenseExpiration.ts
1765
- import dayjs9 from "dayjs";
1950
+ import dayjs10 from "dayjs";
1766
1951
 
1767
1952
  // src/service/license/reminder/reminderWindow.ts
1768
1953
  var REMINDER_WINDOW_MIN_HOURS = 6.5 * 24;
@@ -1778,7 +1963,7 @@ import {
1778
1963
  vendorLicences as vendorLicences2
1779
1964
  } from "@timardex/cluemart-shared";
1780
1965
  import { formatDate } from "@timardex/cluemart-shared/utils";
1781
- import dayjs8 from "dayjs";
1966
+ import dayjs9 from "dayjs";
1782
1967
 
1783
1968
  // src/service/license/reminder/vendor.ts
1784
1969
  async function getVendorResourceInfo(userId, vendorId) {
@@ -1837,7 +2022,7 @@ async function checkAndCreateNotification(payload) {
1837
2022
  }
1838
2023
  }
1839
2024
  function isLicenseExpiringIn7Days(expiryDate, now) {
1840
- const expiry = dayjs8(expiryDate);
2025
+ const expiry = dayjs9(expiryDate);
1841
2026
  const hoursUntilExpiry = expiry.diff(now, "hour", true);
1842
2027
  return hoursUntilExpiry >= REMINDER_WINDOW_MIN_HOURS && hoursUntilExpiry <= REMINDER_WINDOW_MAX_HOURS;
1843
2028
  }
@@ -1915,7 +2100,7 @@ function getLicenceExpiryMode(licenceType) {
1915
2100
  }
1916
2101
  async function sendLicenseReminder(user, licence) {
1917
2102
  const licenceName = getLicenseName(licence.licenceType);
1918
- const expiryDate = dayjs8(licence.expiryDate).format(dateFormat2);
2103
+ const expiryDate = dayjs9(licence.expiryDate).format(dateFormat2);
1919
2104
  const userId = String(user._id);
1920
2105
  const resourceInfo = await getLicenseResourceInfo(user, licence);
1921
2106
  if (!resourceInfo) {
@@ -1947,7 +2132,7 @@ async function sendLicenseReminder(user, licence) {
1947
2132
  console.log(`\u2705 Sent 7-day reminder to user ${userId} for ${licenceName}`);
1948
2133
  }
1949
2134
  async function processLicenseReminder(licence, now) {
1950
- if (dayjs8(licence.expiryDate).isBefore(now)) {
2135
+ if (dayjs9(licence.expiryDate).isBefore(now)) {
1951
2136
  return false;
1952
2137
  }
1953
2138
  if (!isLicenseExpiringIn7Days(licence.expiryDate, now)) {
@@ -2001,7 +2186,7 @@ async function processUserLicenseReminders(user, now) {
2001
2186
 
2002
2187
  // src/service/license/reminder/remindUsersAboutLicenseExpiration.ts
2003
2188
  async function remindUsersAboutLicenseExpiration() {
2004
- const now = dayjs9();
2189
+ const now = dayjs10();
2005
2190
  console.log(`\u{1F50D} Checking for licenses expiring in 7 days at ${now.format()}`);
2006
2191
  const minExpiryDate = now.add(REMINDER_WINDOW_MIN_HOURS, "hour").toDate();
2007
2192
  const maxExpiryDate = now.add(REMINDER_WINDOW_MAX_HOURS, "hour").toDate();
@@ -2047,12 +2232,18 @@ export {
2047
2232
  activeAffiliateCodeExists,
2048
2233
  awardAffiliateVendorSubscriptionRewards,
2049
2234
  buildLicenceExpiryPipeline,
2235
+ buildUserHasEnoughPointsFilter,
2236
+ buildUserPointAwardEntry,
2237
+ buildUserPointCreditFilter,
2238
+ buildUserPointDebitPipeline,
2239
+ buildUserPointEntryRemovalPipeline,
2050
2240
  checkOwnerHasLicence,
2051
2241
  connectToDatabase,
2052
2242
  convertObjectIdsToStrings,
2053
2243
  createLicenceObject,
2054
2244
  dedupeArchivedSlotsByStartDateTime,
2055
2245
  didRemoveAnyEventDates,
2246
+ didRemoveAnyStartedDates,
2056
2247
  downgradeVendorProFeatures,
2057
2248
  ensureUserLicenseCompliance,
2058
2249
  ensureUserLicenseComplianceForUser,