@timardex/cluemart-server-shared 2.3.0 → 2.5.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
@@ -7,7 +7,8 @@ import {
7
7
  EnumAdStatus,
8
8
  EnumAdStyle,
9
9
  EnumAdType,
10
- EnumResourceType
10
+ EnumResourceType,
11
+ isUtcMidnight
11
12
  } from "@timardex/cluemart-shared";
12
13
  import mongoose from "mongoose";
13
14
  var MongooseSchema = mongoose.Schema;
@@ -40,7 +41,6 @@ var schemaAdresource = new MongooseSchema({
40
41
  var schema = new MongooseSchema(
41
42
  {
42
43
  active: { default: true, type: Boolean },
43
- adminManaged: { default: false, type: Boolean },
44
44
  approvalStatus: {
45
45
  enum: Object.values(EnumAdApprovalStatus),
46
46
  required: false,
@@ -56,9 +56,14 @@ var schema = new MongooseSchema(
56
56
  required: false,
57
57
  type: String
58
58
  },
59
- clicks: { default: 0, required: true, type: Number },
60
- end: { required: true, type: Date },
61
- impressions: { default: 0, required: true, type: Number },
59
+ end: {
60
+ required: true,
61
+ type: Date,
62
+ validate: {
63
+ message: "end must be a UTC-midnight day key.",
64
+ validator: isUtcMidnight
65
+ }
66
+ },
62
67
  paymentStatus: {
63
68
  enum: Object.values(EnumAdPaymentStatus),
64
69
  required: false,
@@ -71,7 +76,14 @@ var schema = new MongooseSchema(
71
76
  required: true,
72
77
  type: [String]
73
78
  },
74
- start: { required: true, type: Date },
79
+ start: {
80
+ required: true,
81
+ type: Date,
82
+ validate: {
83
+ message: "start must be a UTC-midnight day key.",
84
+ validator: isUtcMidnight
85
+ }
86
+ },
75
87
  status: {
76
88
  default: EnumAdStatus.ACTIVE,
77
89
  enum: Object.values(EnumAdStatus),
@@ -105,6 +117,7 @@ schema.index(
105
117
  var AdModel = mongoose.models.Ad || mongoose.model("Ad", schema);
106
118
 
107
119
  // src/mongoose/ad/AdBillingCatalog.ts
120
+ import { EnumAdShowOn as EnumAdShowOn2 } from "@timardex/cluemart-shared";
108
121
  import mongoose2 from "mongoose";
109
122
  var AD_BILLING_CATALOG_COLLECTION_NAME = "adBillingCatalog";
110
123
  var schema2 = new mongoose2.Schema(
@@ -118,7 +131,7 @@ var schema2 = new mongoose2.Schema(
118
131
  type: String
119
132
  },
120
133
  key: {
121
- enum: ["front_page_day", "market_search_day"],
134
+ enum: Object.values(EnumAdShowOn2),
122
135
  immutable: true,
123
136
  required: true,
124
137
  type: String,
@@ -148,8 +161,8 @@ import {
148
161
  EnumAdApprovalStatus as EnumAdApprovalStatus2,
149
162
  EnumAdBookingSlotKind as EnumAdBookingSlotKind2,
150
163
  EnumAdBookingState,
151
- EnumAdEntitlementSource,
152
- EnumAdShowOn as EnumAdShowOn2
164
+ EnumAdShowOn as EnumAdShowOn3,
165
+ isUtcMidnight as isUtcMidnight2
153
166
  } from "@timardex/cluemart-shared";
154
167
  import mongoose3 from "mongoose";
155
168
  var liveStates = [
@@ -157,7 +170,6 @@ var liveStates = [
157
170
  EnumAdBookingState.PENDING_APPROVAL,
158
171
  EnumAdBookingState.APPROVED
159
172
  ];
160
- var placementValues = [EnumAdShowOn2.FRONT_PAGE, EnumAdShowOn2.MARKET_SEARCH];
161
173
  var schema3 = new mongoose3.Schema(
162
174
  {
163
175
  adId: { default: null, ref: "Ad", type: String },
@@ -171,19 +183,16 @@ var schema3 = new mongoose3.Schema(
171
183
  currency: { default: null, lowercase: true, type: String },
172
184
  dayKey: {
173
185
  immutable: true,
174
- match: /^\d{4}-\d{2}-\d{2}$/,
175
186
  required: true,
176
- type: String
177
- },
178
- entitlementClaimKey: { default: null, type: String },
179
- entitlementSource: {
180
- default: null,
181
- enum: Object.values(EnumAdEntitlementSource),
182
- type: String
187
+ type: Date,
188
+ validate: {
189
+ message: "dayKey must be a UTC-midnight day key.",
190
+ validator: isUtcMidnight2
191
+ }
183
192
  },
184
193
  expiresAt: { default: null, type: Date },
185
194
  placement: {
186
- enum: placementValues,
195
+ enum: Object.values(EnumAdShowOn3),
187
196
  immutable: true,
188
197
  required: true,
189
198
  type: String
@@ -198,6 +207,7 @@ var schema3 = new mongoose3.Schema(
198
207
  },
199
208
  resource: { required: true, type: schemaAdresource },
200
209
  slotKind: {
210
+ default: EnumAdBookingSlotKind2.PAID,
201
211
  enum: Object.values(EnumAdBookingSlotKind2),
202
212
  immutable: true,
203
213
  required: true,
@@ -224,7 +234,7 @@ schema3.pre(
224
234
  this.invalidate("slotNumber", "Ad booking slot must be an integer.");
225
235
  return;
226
236
  }
227
- const maximum = this.slotKind === EnumAdBookingSlotKind2.PAID ? 30 : 7;
237
+ const maximum = 30;
228
238
  if (this.slotNumber < 1 || this.slotNumber > maximum) {
229
239
  this.invalidate(
230
240
  "slotNumber",
@@ -261,26 +271,6 @@ schema3.index(
261
271
  unique: true
262
272
  }
263
273
  );
264
- schema3.index(
265
- { entitlementClaimKey: 1 },
266
- {
267
- partialFilterExpression: {
268
- entitlementClaimKey: { $type: "string" },
269
- state: { $in: liveStates }
270
- },
271
- unique: true
272
- }
273
- );
274
- schema3.index(
275
- { adId: 1, region: 1, placement: 1, dayKey: 1 },
276
- {
277
- partialFilterExpression: {
278
- entitlementSource: EnumAdEntitlementSource.ADMIN,
279
- state: { $in: liveStates }
280
- },
281
- unique: true
282
- }
283
- );
284
274
  schema3.index(
285
275
  { stripeCheckoutSessionId: 1 },
286
276
  {
@@ -290,79 +280,67 @@ schema3.index(
290
280
  );
291
281
  var AdBookingModel = mongoose3.models.AdBooking || mongoose3.model("AdBooking", schema3);
292
282
 
293
- // src/mongoose/ad/AdImpression.ts
294
- import { EnumAdShowOn as EnumAdShowOn3 } from "@timardex/cluemart-shared";
283
+ // src/mongoose/ad/AdMetrics.ts
284
+ import {
285
+ EnumAdPlatform,
286
+ EnumAdShowOn as EnumAdShowOn4,
287
+ isUtcMidnight as isUtcMidnight3
288
+ } from "@timardex/cluemart-shared";
295
289
  import mongoose4 from "mongoose";
296
290
  var schema4 = new mongoose4.Schema(
297
291
  {
298
- _id: { required: true, type: String },
299
- adId: { required: true, type: String },
300
- dayKey: { match: /^\d{4}-\d{2}-\d{2}$/, required: true, type: String },
292
+ adId: { ref: "Ad", required: true, type: String },
293
+ clicks: { default: 0, required: true, type: Number },
294
+ date: {
295
+ immutable: true,
296
+ required: true,
297
+ type: Date,
298
+ validate: {
299
+ message: "date must be a UTC-midnight day key.",
300
+ validator: isUtcMidnight3
301
+ }
302
+ },
303
+ impressions: { default: 0, required: true, type: Number },
301
304
  placement: {
302
- enum: [EnumAdShowOn3.FRONT_PAGE, EnumAdShowOn3.MARKET_SEARCH],
305
+ enum: Object.values(EnumAdShowOn4),
306
+ immutable: true,
303
307
  required: true,
304
308
  type: String
305
309
  },
306
- recordedAt: { default: Date.now, required: true, type: Date },
307
- viewerSlotKey: { required: true, type: String }
310
+ platform: {
311
+ enum: Object.values(EnumAdPlatform),
312
+ immutable: true,
313
+ required: true,
314
+ type: String
315
+ }
308
316
  },
309
- { collection: "adImpressions" }
317
+ { timestamps: true }
310
318
  );
311
- schema4.index({ adId: 1, recordedAt: 1 });
312
- schema4.index({ adId: 1, placement: 1, viewerSlotKey: 1 }, { unique: true });
313
- schema4.index({ recordedAt: 1 }, { expireAfterSeconds: 7 * 24 * 60 * 60 });
314
- schema4.index({ dayKey: 1, placement: 1, adId: 1 });
315
- var AdImpressionModel = mongoose4.models.AdImpression || mongoose4.model("AdImpression", schema4, "adImpressions");
319
+ schema4.index({ adId: 1, date: 1, placement: 1, platform: 1 }, { unique: true });
320
+ var AdMetricsModel = mongoose4.models.AdMetrics || mongoose4.model("AdMetrics", schema4);
316
321
 
317
322
  // src/mongoose/ad/AdSettings.ts
318
323
  import mongoose5 from "mongoose";
319
324
  var DEFAULT_AD_SETTINGS = {
320
325
  _id: "default",
321
- approvalRequired: true,
322
- minAppearancesPerHour: 12,
323
- paidSlotsPerPlacement: 30,
324
- reservedSlotsPerPlacement: 7,
325
- rotationSeconds: 8
326
+ approvalRequired: true
326
327
  };
327
328
  var schema5 = new mongoose5.Schema(
328
329
  {
329
330
  _id: { default: "default", enum: ["default"], type: String },
330
331
  approvalRequired: { default: true, required: true, type: Boolean },
331
332
  minAppearancesPerHour: {
332
- default: 12,
333
+ max: 12,
333
334
  min: 1,
334
- required: true,
335
335
  type: Number,
336
- validate: {
337
- message: "Minimum appearances per hour must be a whole number.",
338
- validator: Number.isInteger
339
- }
336
+ validate: Number.isInteger
340
337
  },
341
- paidSlotsPerPlacement: {
342
- default: 30,
343
- enum: [30],
344
- immutable: true,
345
- type: Number
346
- },
347
- reservedSlotsPerPlacement: {
348
- default: 7,
349
- enum: [7],
350
- immutable: true,
351
- type: Number
352
- },
353
- rotationSeconds: { default: 8, enum: [8], immutable: true, type: Number }
338
+ paidSlotsPerPlacement: { enum: [30], immutable: true, type: Number },
339
+ reservedSlotsPerPlacement: { enum: [7], immutable: true, type: Number },
340
+ rotationSeconds: { enum: [8], immutable: true, type: Number }
354
341
  },
355
342
  { collection: "adSettings", timestamps: true }
356
343
  );
357
- schema5.pre("validate", function validateRotationCapacity() {
358
- const availableRotationsPerHour = 3600 / this.rotationSeconds;
359
- if (this.minAppearancesPerHour * (this.paidSlotsPerPlacement + this.reservedSlotsPerPlacement) > availableRotationsPerHour) {
360
- this.invalidate(
361
- "minAppearancesPerHour",
362
- "The configured minimum exceeds the available rotation slots."
363
- );
364
- }
365
- });
366
344
  var AdSettingsModel = mongoose5.models.AdSettings || mongoose5.model("AdSettings", schema5, "adSettings");
367
345
 
368
346
  // src/mongoose/BillingCatalog.ts
@@ -2755,7 +2733,8 @@ async function sendPushNotifications({
2755
2733
  // src/service/affiliate/vendorSubscriptionRewards.ts
2756
2734
  import {
2757
2735
  EnumAffiliateRewardType as EnumAffiliateRewardType2,
2758
- EnumUserLicence as EnumUserLicence5
2736
+ EnumUserLicence as EnumUserLicence5,
2737
+ NZ_TZ
2759
2738
  } from "@timardex/cluemart-shared";
2760
2739
  import dayjs from "dayjs";
2761
2740
  import timezone from "dayjs/plugin/timezone";
@@ -2778,7 +2757,7 @@ var PRO_PLANS = /* @__PURE__ */ new Set([
2778
2757
  ...PRO_EVENT_PLANS,
2779
2758
  ...PRO_VENDOR_PLANS
2780
2759
  ]);
2781
- async function getPayingStripePlanFor(userId, now, payingPlans) {
2760
+ async function getStripePlanFor(userId, now, allowedPlans, requirePaidAccess = true) {
2782
2761
  const user = await UserModel.findById(userId).select("licences stripe").lean().exec();
2783
2762
  if (!user) {
2784
2763
  return { hasSubscription: false, plan: null, validTypes: /* @__PURE__ */ new Set() };
@@ -2793,30 +2772,33 @@ async function getPayingStripePlanFor(userId, now, payingPlans) {
2793
2772
  if (stripe == null || stripe.subscriptionId == null) {
2794
2773
  return { hasSubscription: false, plan: null, validTypes };
2795
2774
  }
2796
- const hasActiveStatus = stripe.status === EnumSubscriptionStatus2.ACTIVE;
2775
+ const hasActiveStatus = stripe.status === EnumSubscriptionStatus2.ACTIVE || !requirePaidAccess && stripe.status === EnumSubscriptionStatus2.TRIALING;
2797
2776
  const paidThrough = stripe.endDate ? new Date(stripe.endDate).getTime() : Number.NaN;
2798
- if (!hasActiveStatus || !Number.isFinite(paidThrough) || paidThrough <= now.getTime() || stripe.currentPlan == null) {
2777
+ if (!hasActiveStatus || requirePaidAccess && (!Number.isFinite(paidThrough) || paidThrough <= now.getTime()) || stripe.currentPlan == null) {
2799
2778
  return { hasSubscription: false, plan: null, validTypes };
2800
2779
  }
2801
- const plan = payingPlans.has(stripe.currentPlan) ? stripe.currentPlan : null;
2780
+ const plan = allowedPlans.has(stripe.currentPlan) ? stripe.currentPlan : null;
2802
2781
  return { hasSubscription: plan != null, plan, validTypes };
2803
2782
  }
2804
2783
  async function getPayingStripeSubscriptionPlan(userId, now = /* @__PURE__ */ new Date()) {
2805
- return getPayingStripePlanFor(userId, now, PRO_PLANS);
2784
+ return getStripePlanFor(userId, now, PRO_PLANS);
2806
2785
  }
2807
2786
  async function getPayingVendorStripeSubscriptionPlan(userId, now = /* @__PURE__ */ new Date()) {
2808
- return getPayingStripePlanFor(userId, now, PRO_VENDOR_PLANS);
2787
+ return getStripePlanFor(userId, now, PRO_VENDOR_PLANS);
2788
+ }
2789
+ async function getVendorStripeSubscriptionPlanForRewards(userId, now = /* @__PURE__ */ new Date()) {
2790
+ return getStripePlanFor(userId, now, PRO_VENDOR_PLANS, false);
2809
2791
  }
2810
2792
  async function getPayingEventStripeSubscriptionPlan(userId, now = /* @__PURE__ */ new Date()) {
2811
- return getPayingStripePlanFor(userId, now, PRO_EVENT_PLANS);
2793
+ return getStripePlanFor(userId, now, PRO_EVENT_PLANS);
2812
2794
  }
2813
2795
 
2814
2796
  // src/service/affiliate/vendorSubscriptionRewards.ts
2815
2797
  dayjs.extend(utc);
2816
2798
  dayjs.extend(timezone);
2817
- var SUBSCRIPTION_REWARD_TIMEZONE = "Pacific/Auckland";
2799
+ var SUBSCRIPTION_REWARD_TIMEZONE = NZ_TZ;
2818
2800
  async function mapVendorLicenceToSubscriptionRewardType(userId, now = /* @__PURE__ */ new Date()) {
2819
- const { hasSubscription, validTypes } = await getPayingVendorStripeSubscriptionPlan(userId, now);
2801
+ const { hasSubscription, validTypes } = await getVendorStripeSubscriptionPlanForRewards(userId, now);
2820
2802
  if (validTypes.has(EnumUserLicence5.PRO_VENDOR) || validTypes.has(EnumUserLicence5.PRO_PLUS_VENDOR)) {
2821
2803
  if (hasSubscription) {
2822
2804
  return EnumAffiliateRewardType2.ACTIVE_VENDOR_PRO_SUBSCRIPTION;
@@ -3118,10 +3100,11 @@ import {
3118
3100
  EnumAdApprovalStatus as EnumAdApprovalStatus3,
3119
3101
  EnumAdBookingSlotKind as EnumAdBookingSlotKind3,
3120
3102
  EnumAdPaymentStatus as EnumAdPaymentStatus2,
3121
- EnumAdStatus as EnumAdStatus2
3103
+ EnumAdStatus as EnumAdStatus2,
3104
+ nzDayKey
3122
3105
  } from "@timardex/cluemart-shared";
3123
3106
  async function updateAdStatuses() {
3124
- const now = /* @__PURE__ */ new Date();
3107
+ const now = nzDayKey();
3125
3108
  const invalidResult = await AdModel.updateMany(
3126
3109
  {
3127
3110
  $or: [
@@ -3687,14 +3670,14 @@ function didRemoveAnyStartedDates(previousDateTime, nextDateTime) {
3687
3670
  )
3688
3671
  );
3689
3672
  }
3690
- function updateRelationDatesToUnavailable(relationDates, eventDateTime) {
3673
+ function updateRelationDatesToCanceled(relationDates, eventDateTime) {
3691
3674
  return relationDates.map((relationDate) => {
3692
3675
  const existsInEvent = eventDateTime?.some(
3693
3676
  (dt) => dt.startDate === relationDate.dateTime.startDate && dt.startTime === relationDate.dateTime.startTime
3694
3677
  ) ?? false;
3695
3678
  return {
3696
3679
  ...relationDate,
3697
- status: existsInEvent ? relationDate.status : EnumInviteStatus2.UNAVAILABLE
3680
+ status: existsInEvent ? relationDate.status : EnumInviteStatus2.CANCELED
3698
3681
  };
3699
3682
  });
3700
3683
  }
@@ -4825,7 +4808,7 @@ export {
4825
4808
  APP_SETTINGS_ID,
4826
4809
  AdBillingCatalogModel,
4827
4810
  AdBookingModel,
4828
- AdImpressionModel,
4811
+ AdMetricsModel,
4829
4812
  AdModel,
4830
4813
  AdSettingsModel,
4831
4814
  AffiliateModel,
@@ -4942,7 +4925,7 @@ export {
4942
4925
  termsAgreementSchema,
4943
4926
  updateAdStatuses,
4944
4927
  updateAllEventDateTimeStatuses,
4945
- updateRelationDatesToUnavailable,
4928
+ updateRelationDatesToCanceled,
4946
4929
  updateSingleDateTimeStatus2 as updateSingleDateTimeStatus,
4947
4930
  upgradeUserToLicence,
4948
4931
  userLicenseSchema,