@timardex/cluemart-server-shared 5.1.0 → 5.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/{Affiliate-DXLwbX9Z.d.ts → Affiliate-BRwzOt3b.d.ts} +2 -2
- package/dist/{Affiliate-C1bp5oR3.d.mts → Affiliate-DmLBN3lW.d.mts} +2 -2
- package/dist/{chunk-MYZL22GL.mjs → chunk-JYBS6IOE.mjs} +4 -1
- package/dist/chunk-JYBS6IOE.mjs.map +1 -0
- package/dist/index.cjs +273 -104
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +121 -11
- package/dist/index.d.ts +121 -11
- package/dist/index.mjs +273 -104
- package/dist/index.mjs.map +1 -1
- package/dist/mongoose/index.cjs +3 -0
- package/dist/mongoose/index.cjs.map +1 -1
- package/dist/mongoose/index.d.mts +28 -2
- package/dist/mongoose/index.d.ts +28 -2
- package/dist/mongoose/index.mjs +1 -1
- package/dist/service/index.cjs +273 -104
- package/dist/service/index.cjs.map +1 -1
- package/dist/service/index.d.mts +96 -12
- package/dist/service/index.d.ts +96 -12
- package/dist/service/index.mjs +271 -105
- package/dist/service/index.mjs.map +1 -1
- package/package.json +2 -2
- package/dist/chunk-MYZL22GL.mjs.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -89,6 +89,7 @@ __export(index_exports, {
|
|
|
89
89
|
activeAffiliateCodeExists: () => activeAffiliateCodeExists,
|
|
90
90
|
awardAffiliateVendorSubscriptionRewards: () => awardAffiliateVendorSubscriptionRewards,
|
|
91
91
|
baseResourceFields: () => baseResourceFields,
|
|
92
|
+
buildAssociatesWriteGuard: () => buildAssociatesWriteGuard,
|
|
92
93
|
buildInitialBillingCatalogEntries: () => buildInitialBillingCatalogEntries,
|
|
93
94
|
buildLicenceExpiryPipeline: () => buildLicenceExpiryPipeline,
|
|
94
95
|
buildUserHasEnoughPointsFilter: () => buildUserHasEnoughPointsFilter,
|
|
@@ -153,6 +154,7 @@ __export(index_exports, {
|
|
|
153
154
|
notifyUsers: () => notifyUsers,
|
|
154
155
|
partnerLicences: () => partnerLicences,
|
|
155
156
|
persistArchivedEventDates: () => persistArchivedEventDates,
|
|
157
|
+
pinAssociateTimestamp: () => pinAssociateTimestamp,
|
|
156
158
|
processLicenseReminder: () => processLicenseReminder,
|
|
157
159
|
processUserCompliance: () => processUserCompliance,
|
|
158
160
|
processUserLicenseReminders: () => processUserLicenseReminders,
|
|
@@ -2214,8 +2216,11 @@ var schema17 = new MongooseSchema18(
|
|
|
2214
2216
|
categories: { required: true, type: [CategorySchema] },
|
|
2215
2217
|
categoriesNames: { default: [], required: false, type: [String] },
|
|
2216
2218
|
claimed: { default: true, required: false, type: Boolean },
|
|
2219
|
+
downgradeNotifiedAt: { default: null, required: false, type: Date },
|
|
2217
2220
|
downgradeSnapshot: { required: false, type: downgradeSnapshotSchema },
|
|
2221
|
+
featuresDowngraded: { default: false, required: false, type: Boolean },
|
|
2218
2222
|
foodTruck: { required: true, type: Boolean },
|
|
2223
|
+
licenceEpoch: { default: 0, required: false, type: Number },
|
|
2219
2224
|
products: { required: false, type: productWrapperSchema },
|
|
2220
2225
|
shareRelation: { required: false, type: socialShareResourceSchema },
|
|
2221
2226
|
unregisteredVendorId: {
|
|
@@ -3544,6 +3549,38 @@ var ASSOCIABLE_MODELS = {
|
|
|
3544
3549
|
[import_cluemart_shared28.EnumResourceType.PARTNER]: PartnerModel,
|
|
3545
3550
|
[import_cluemart_shared28.EnumResourceType.VENDOR]: VendorModel
|
|
3546
3551
|
};
|
|
3552
|
+
function pinAssociateTimestamp(value) {
|
|
3553
|
+
return value == null ? null : { $in: [value, null] };
|
|
3554
|
+
}
|
|
3555
|
+
function buildAssociatesWriteGuard({
|
|
3556
|
+
canManage,
|
|
3557
|
+
nextAssociates,
|
|
3558
|
+
snapshot
|
|
3559
|
+
}) {
|
|
3560
|
+
if (!canManage) {
|
|
3561
|
+
return null;
|
|
3562
|
+
}
|
|
3563
|
+
const previous = snapshot ?? [];
|
|
3564
|
+
const emptyListFilter = {
|
|
3565
|
+
$or: [{ associates: { $size: 0 } }, { associates: null }]
|
|
3566
|
+
};
|
|
3567
|
+
return {
|
|
3568
|
+
filter: previous.length > 0 ? {
|
|
3569
|
+
$and: previous.map((associate) => ({
|
|
3570
|
+
associates: {
|
|
3571
|
+
$elemMatch: {
|
|
3572
|
+
addedAt: pinAssociateTimestamp(associate.addedAt),
|
|
3573
|
+
respondedAt: pinAssociateTimestamp(associate.respondedAt),
|
|
3574
|
+
status: associate.status,
|
|
3575
|
+
userId: associate.userId
|
|
3576
|
+
}
|
|
3577
|
+
}
|
|
3578
|
+
})),
|
|
3579
|
+
associates: { $size: previous.length }
|
|
3580
|
+
} : emptyListFilter,
|
|
3581
|
+
update: { associates: nextAssociates }
|
|
3582
|
+
};
|
|
3583
|
+
}
|
|
3547
3584
|
function acceptedAssociateUserIds(associates) {
|
|
3548
3585
|
return (associates ?? []).filter((associate) => associate.status === import_cluemart_shared28.EnumAssociateStatus.ACCEPTED).map((associate) => associate.userId);
|
|
3549
3586
|
}
|
|
@@ -3583,7 +3620,10 @@ async function acceptedUserIdsForResource({
|
|
|
3583
3620
|
resourceType
|
|
3584
3621
|
}) {
|
|
3585
3622
|
const model = ASSOCIABLE_MODELS[resourceType];
|
|
3586
|
-
const resource = await model.findOne({ _id: resourceId, deletedAt: null }).select("associates").lean().exec();
|
|
3623
|
+
const resource = await model.findOne({ _id: resourceId, deletedAt: null }).select("associates featuresDowngraded").lean().exec();
|
|
3624
|
+
if (resource?.featuresDowngraded) {
|
|
3625
|
+
return /* @__PURE__ */ new Set();
|
|
3626
|
+
}
|
|
3587
3627
|
return new Set(
|
|
3588
3628
|
acceptedAssociateUserIds(resource?.associates).map(
|
|
3589
3629
|
(userId) => String(userId)
|
|
@@ -3733,12 +3773,12 @@ async function reconcileUserBatch({
|
|
|
3733
3773
|
_id: {
|
|
3734
3774
|
$in: [...heldByResource.values()].map((entry) => entry.resourceId)
|
|
3735
3775
|
}
|
|
3736
|
-
}).select("associates deletedAt").lean().exec();
|
|
3776
|
+
}).select("associates deletedAt featuresDowngraded").lean().exec();
|
|
3737
3777
|
const acceptedByResource = /* @__PURE__ */ new Map();
|
|
3738
3778
|
for (const resource of resources) {
|
|
3739
3779
|
acceptedByResource.set(
|
|
3740
3780
|
String(resource._id),
|
|
3741
|
-
resource.deletedAt ? /* @__PURE__ */ new Set() : new Set(
|
|
3781
|
+
resource.deletedAt || resource.featuresDowngraded ? /* @__PURE__ */ new Set() : new Set(
|
|
3742
3782
|
acceptedAssociateUserIds(resource.associates).map(
|
|
3743
3783
|
(userId) => String(userId)
|
|
3744
3784
|
)
|
|
@@ -4804,13 +4844,13 @@ var import_dayjs6 = __toESM(require("dayjs"));
|
|
|
4804
4844
|
var import_cluemart_shared36 = require("@timardex/cluemart-shared");
|
|
4805
4845
|
var import_dayjs5 = __toESM(require("dayjs"));
|
|
4806
4846
|
var import_graphql = require("graphql");
|
|
4807
|
-
var LicenceCategory = /* @__PURE__ */ ((
|
|
4808
|
-
|
|
4809
|
-
|
|
4810
|
-
|
|
4811
|
-
|
|
4812
|
-
|
|
4813
|
-
return
|
|
4847
|
+
var LicenceCategory = /* @__PURE__ */ ((LicenceCategory2) => {
|
|
4848
|
+
LicenceCategory2["EVENT"] = "event";
|
|
4849
|
+
LicenceCategory2["PARTNER"] = "partner";
|
|
4850
|
+
LicenceCategory2["VENDOR"] = "vendor";
|
|
4851
|
+
LicenceCategory2["AFFILIATE"] = "affiliate";
|
|
4852
|
+
LicenceCategory2["SCHOOL"] = "school";
|
|
4853
|
+
return LicenceCategory2;
|
|
4814
4854
|
})(LicenceCategory || {});
|
|
4815
4855
|
var licenceCategoryMapping = {
|
|
4816
4856
|
[import_cluemart_shared36.EnumUserLicence.PRO_EVENT]: "event" /* EVENT */,
|
|
@@ -4834,25 +4874,8 @@ var licencePriority = {
|
|
|
4834
4874
|
[import_cluemart_shared36.EnumUserLicence.STANDARD_AFFILIATE]: 1,
|
|
4835
4875
|
[import_cluemart_shared36.EnumUserLicence.STANDARD_SCHOOL]: 1
|
|
4836
4876
|
};
|
|
4837
|
-
var
|
|
4838
|
-
|
|
4839
|
-
import_cluemart_shared36.EnumUserLicence.STANDARD_EVENT,
|
|
4840
|
-
import_cluemart_shared36.EnumUserLicence.STANDARD_PARTNER
|
|
4841
|
-
];
|
|
4842
|
-
var standardLicenceSet = new Set(standardLicences);
|
|
4843
|
-
function isStandardLicence(licence) {
|
|
4844
|
-
return standardLicenceSet.has(licence);
|
|
4845
|
-
}
|
|
4846
|
-
var proLicences = [
|
|
4847
|
-
import_cluemart_shared36.EnumUserLicence.PRO_EVENT,
|
|
4848
|
-
import_cluemart_shared36.EnumUserLicence.PRO_PLUS_EVENT,
|
|
4849
|
-
import_cluemart_shared36.EnumUserLicence.PRO_VENDOR,
|
|
4850
|
-
import_cluemart_shared36.EnumUserLicence.PRO_PLUS_VENDOR
|
|
4851
|
-
];
|
|
4852
|
-
var proLicenceSet = new Set(proLicences);
|
|
4853
|
-
function isProLicence(licence) {
|
|
4854
|
-
return proLicenceSet.has(licence);
|
|
4855
|
-
}
|
|
4877
|
+
var isStandardLicence = import_cluemart_shared36.isStandardLicenceType;
|
|
4878
|
+
var isProLicence = import_cluemart_shared36.isProLicenceType;
|
|
4856
4879
|
var categoryRegexMapping = {
|
|
4857
4880
|
["event" /* EVENT */]: /_event$/,
|
|
4858
4881
|
["partner" /* PARTNER */]: /_partner$/,
|
|
@@ -5175,30 +5198,134 @@ async function findUserVendor(userId, logLabel) {
|
|
|
5175
5198
|
}
|
|
5176
5199
|
return vendor;
|
|
5177
5200
|
}
|
|
5201
|
+
async function downgradeStillApplies(ownerUserId) {
|
|
5202
|
+
const hasActiveProLicence = await hasActiveProLicenceForResource({
|
|
5203
|
+
includeStripeProtected: true,
|
|
5204
|
+
resourceType: import_cluemart_shared37.EnumResourceType.VENDOR,
|
|
5205
|
+
userId: ownerUserId
|
|
5206
|
+
});
|
|
5207
|
+
return !hasActiveProLicence;
|
|
5208
|
+
}
|
|
5178
5209
|
async function applyVendorDowngrade(vendor, ownerUserId) {
|
|
5179
|
-
|
|
5180
|
-
|
|
5181
|
-
|
|
5182
|
-
|
|
5183
|
-
|
|
5184
|
-
|
|
5185
|
-
|
|
5186
|
-
|
|
5187
|
-
|
|
5188
|
-
|
|
5189
|
-
|
|
5190
|
-
|
|
5191
|
-
|
|
5192
|
-
|
|
5193
|
-
|
|
5194
|
-
|
|
5195
|
-
|
|
5196
|
-
|
|
5210
|
+
let current = vendor;
|
|
5211
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
5212
|
+
if (!await downgradeStillApplies(ownerUserId)) {
|
|
5213
|
+
console.info(
|
|
5214
|
+
"[applyVendorDowngrade] owner holds an active PRO licence again; skipping the downgrade",
|
|
5215
|
+
{ vendorId: String(current._id) }
|
|
5216
|
+
);
|
|
5217
|
+
return "skipped";
|
|
5218
|
+
}
|
|
5219
|
+
const associatesGuard = buildAssociatesWriteGuard({
|
|
5220
|
+
canManage: true,
|
|
5221
|
+
nextAssociates: [],
|
|
5222
|
+
snapshot: current.associates
|
|
5223
|
+
});
|
|
5224
|
+
if (!associatesGuard) {
|
|
5225
|
+
return "skipped";
|
|
5226
|
+
}
|
|
5227
|
+
const result = await VendorModel.updateOne(
|
|
5228
|
+
{
|
|
5229
|
+
_id: current._id,
|
|
5230
|
+
active: true,
|
|
5231
|
+
deletedAt: null,
|
|
5232
|
+
// Already downgraded: another run got there first and its snapshot is
|
|
5233
|
+
// the original state this one must not overwrite.
|
|
5234
|
+
featuresDowngraded: { $ne: true },
|
|
5235
|
+
// Upgrades bump this in their own write; `null` covers vendors that
|
|
5236
|
+
// predate the field.
|
|
5237
|
+
licenceEpoch: { $in: [current.licenceEpoch ?? 0, null] },
|
|
5238
|
+
"owner.userId": ownerUserId,
|
|
5239
|
+
...associatesGuard.filter
|
|
5240
|
+
},
|
|
5241
|
+
{
|
|
5242
|
+
$inc: { licenceEpoch: 1 },
|
|
5243
|
+
$set: {
|
|
5244
|
+
...associatesGuard.update,
|
|
5245
|
+
availability: {
|
|
5246
|
+
corporate: false,
|
|
5247
|
+
private: false,
|
|
5248
|
+
school: false
|
|
5249
|
+
},
|
|
5250
|
+
calendar: {
|
|
5251
|
+
active: false,
|
|
5252
|
+
calendarData: current.calendar?.calendarData ?? []
|
|
5253
|
+
},
|
|
5254
|
+
downgradeSnapshot: {
|
|
5255
|
+
availability: current.availability,
|
|
5256
|
+
calendarActive: current.calendar?.active,
|
|
5257
|
+
productsActive: current.products?.active
|
|
5258
|
+
},
|
|
5259
|
+
featuresDowngraded: true,
|
|
5260
|
+
// STANDARD_VENDOR keeps only the first 6 images active.
|
|
5261
|
+
images: (current.images ?? []).map((image, index) => ({
|
|
5262
|
+
...image,
|
|
5263
|
+
active: index < 6
|
|
5264
|
+
})),
|
|
5265
|
+
products: {
|
|
5266
|
+
active: false,
|
|
5267
|
+
productsList: current.products?.productsList ?? []
|
|
5268
|
+
}
|
|
5269
|
+
}
|
|
5270
|
+
}
|
|
5271
|
+
);
|
|
5272
|
+
if ((result?.matchedCount ?? 0) > 0) {
|
|
5273
|
+
break;
|
|
5274
|
+
}
|
|
5275
|
+
const fresh = await VendorModel.findById(current._id).lean().exec();
|
|
5276
|
+
if (!fresh) {
|
|
5277
|
+
return "skipped";
|
|
5278
|
+
}
|
|
5279
|
+
if (fresh.featuresDowngraded === true) {
|
|
5280
|
+
console.info(
|
|
5281
|
+
"[applyVendorDowngrade] vendor is already downgraded; licence sync can proceed",
|
|
5282
|
+
{ vendorId: String(fresh._id) }
|
|
5283
|
+
);
|
|
5284
|
+
return "already-downgraded";
|
|
5285
|
+
}
|
|
5286
|
+
current = fresh;
|
|
5287
|
+
if (attempt === 1) {
|
|
5288
|
+
console.warn(
|
|
5289
|
+
"[applyVendorDowngrade] vendor changed under the downgrade; left to the next compliance run",
|
|
5290
|
+
{ vendorId: String(current._id) }
|
|
5291
|
+
);
|
|
5292
|
+
return "skipped";
|
|
5293
|
+
}
|
|
5294
|
+
}
|
|
5295
|
+
try {
|
|
5296
|
+
await detachStaleAssociateGrants({
|
|
5297
|
+
acceptedUserIds: [],
|
|
5298
|
+
resourceId: current._id,
|
|
5299
|
+
resourceType: import_cluemart_shared37.EnumResourceType.VENDOR
|
|
5300
|
+
});
|
|
5301
|
+
} catch (error) {
|
|
5302
|
+
console.error(
|
|
5303
|
+
"[applyVendorDowngrade] grant detach failed; left to reconciliation",
|
|
5304
|
+
error
|
|
5305
|
+
);
|
|
5306
|
+
}
|
|
5307
|
+
return "applied";
|
|
5308
|
+
}
|
|
5309
|
+
async function applyVendorRestore(vendor, ownerUserId) {
|
|
5310
|
+
const snapshot = vendor.downgradeSnapshot;
|
|
5311
|
+
const updateData = {
|
|
5312
|
+
availability: {
|
|
5313
|
+
corporate: snapshot?.availability?.corporate ?? true,
|
|
5314
|
+
private: snapshot?.availability?.private ?? true,
|
|
5315
|
+
school: snapshot?.availability?.school ?? true
|
|
5316
|
+
},
|
|
5317
|
+
calendar: {
|
|
5318
|
+
active: snapshot?.calendarActive ?? true,
|
|
5319
|
+
calendarData: vendor.calendar?.calendarData ?? []
|
|
5320
|
+
},
|
|
5321
|
+
downgradeNotifiedAt: null,
|
|
5322
|
+
featuresDowngraded: false,
|
|
5323
|
+
images: (vendor.images ?? []).map((image) => ({ ...image, active: true })),
|
|
5324
|
+
products: {
|
|
5325
|
+
active: snapshot?.productsActive ?? true,
|
|
5326
|
+
productsList: vendor.products?.productsList ?? []
|
|
5327
|
+
}
|
|
5197
5328
|
};
|
|
5198
|
-
updateData.images = (vendor.images ?? []).map((image, index) => ({
|
|
5199
|
-
...image,
|
|
5200
|
-
active: index < 6
|
|
5201
|
-
}));
|
|
5202
5329
|
const result = await VendorModel.updateOne(
|
|
5203
5330
|
{
|
|
5204
5331
|
_id: vendor._id,
|
|
@@ -5206,7 +5333,7 @@ async function applyVendorDowngrade(vendor, ownerUserId) {
|
|
|
5206
5333
|
deletedAt: null,
|
|
5207
5334
|
"owner.userId": ownerUserId
|
|
5208
5335
|
},
|
|
5209
|
-
{ $set: updateData }
|
|
5336
|
+
{ $inc: { licenceEpoch: 1 }, $set: updateData }
|
|
5210
5337
|
);
|
|
5211
5338
|
return (result?.matchedCount ?? 0) > 0;
|
|
5212
5339
|
}
|
|
@@ -5226,37 +5353,6 @@ async function downgradeVendorProFeatures(userId, licenceType) {
|
|
|
5226
5353
|
throw error;
|
|
5227
5354
|
}
|
|
5228
5355
|
}
|
|
5229
|
-
async function applyVendorRestore(vendor, ownerUserId) {
|
|
5230
|
-
const updateData = {};
|
|
5231
|
-
const snapshot = vendor.downgradeSnapshot;
|
|
5232
|
-
updateData.availability = {
|
|
5233
|
-
corporate: snapshot?.availability?.corporate ?? true,
|
|
5234
|
-
private: snapshot?.availability?.private ?? true,
|
|
5235
|
-
school: snapshot?.availability?.school ?? true
|
|
5236
|
-
};
|
|
5237
|
-
updateData.products = {
|
|
5238
|
-
active: snapshot?.productsActive ?? true,
|
|
5239
|
-
productsList: vendor.products?.productsList ?? []
|
|
5240
|
-
};
|
|
5241
|
-
updateData.calendar = {
|
|
5242
|
-
active: snapshot?.calendarActive ?? true,
|
|
5243
|
-
calendarData: vendor.calendar?.calendarData ?? []
|
|
5244
|
-
};
|
|
5245
|
-
updateData.images = (vendor.images ?? []).map((image) => ({
|
|
5246
|
-
...image,
|
|
5247
|
-
active: true
|
|
5248
|
-
}));
|
|
5249
|
-
const result = await VendorModel.updateOne(
|
|
5250
|
-
{
|
|
5251
|
-
_id: vendor._id,
|
|
5252
|
-
active: true,
|
|
5253
|
-
deletedAt: null,
|
|
5254
|
-
"owner.userId": ownerUserId
|
|
5255
|
-
},
|
|
5256
|
-
{ $set: updateData }
|
|
5257
|
-
);
|
|
5258
|
-
return (result?.matchedCount ?? 0) > 0;
|
|
5259
|
-
}
|
|
5260
5356
|
async function restoreVendorProFeatures(userId, licenceType) {
|
|
5261
5357
|
const isProVendor = licenceType === import_cluemart_shared37.EnumUserLicence.PRO_VENDOR || licenceType === import_cluemart_shared37.EnumUserLicence.PRO_PLUS_VENDOR;
|
|
5262
5358
|
if (!isProVendor) {
|
|
@@ -5273,6 +5369,61 @@ async function restoreVendorProFeatures(userId, licenceType) {
|
|
|
5273
5369
|
throw error;
|
|
5274
5370
|
}
|
|
5275
5371
|
}
|
|
5372
|
+
var NOTIFICATION_CLAIM_LEASE_MS = 60 * 60 * 1e3;
|
|
5373
|
+
async function notifyVendorDowngrade(vendorId, ownerUserId) {
|
|
5374
|
+
if (!await downgradeStillApplies(ownerUserId)) {
|
|
5375
|
+
return false;
|
|
5376
|
+
}
|
|
5377
|
+
const claimedAt = /* @__PURE__ */ new Date();
|
|
5378
|
+
const claimed = await VendorModel.findOneAndUpdate(
|
|
5379
|
+
{
|
|
5380
|
+
$or: [
|
|
5381
|
+
{ downgradeNotifiedAt: null },
|
|
5382
|
+
{
|
|
5383
|
+
downgradeNotifiedAt: {
|
|
5384
|
+
$lt: new Date(claimedAt.getTime() - NOTIFICATION_CLAIM_LEASE_MS)
|
|
5385
|
+
}
|
|
5386
|
+
}
|
|
5387
|
+
],
|
|
5388
|
+
_id: vendorId,
|
|
5389
|
+
active: true,
|
|
5390
|
+
deletedAt: null,
|
|
5391
|
+
// A re-upgrade that restored the vendor has flipped this back, so its
|
|
5392
|
+
// owner is not told about a downgrade that no longer holds.
|
|
5393
|
+
featuresDowngraded: true
|
|
5394
|
+
},
|
|
5395
|
+
{ $set: { downgradeNotifiedAt: claimedAt } },
|
|
5396
|
+
{ new: false }
|
|
5397
|
+
).lean().exec();
|
|
5398
|
+
if (!claimed) {
|
|
5399
|
+
return false;
|
|
5400
|
+
}
|
|
5401
|
+
const sent = await sendDeactivationNotification(
|
|
5402
|
+
{
|
|
5403
|
+
data: {
|
|
5404
|
+
resourceId: String(claimed._id),
|
|
5405
|
+
resourceName: claimed.name,
|
|
5406
|
+
resourceType: import_cluemart_shared37.EnumNotificationResourceType.DOWNGRADED_VENDOR
|
|
5407
|
+
},
|
|
5408
|
+
message: `Your Vendor licence has been downgraded due to licence expiration.`,
|
|
5409
|
+
title: `Vendor Licence Downgraded`,
|
|
5410
|
+
type: import_cluemart_shared37.EnumNotificationType.SYSTEM,
|
|
5411
|
+
userIds: [ownerUserId]
|
|
5412
|
+
},
|
|
5413
|
+
ownerUserId,
|
|
5414
|
+
claimed.name,
|
|
5415
|
+
"vendor",
|
|
5416
|
+
claimed.downgradeNotifiedAt ?? null
|
|
5417
|
+
);
|
|
5418
|
+
if (sent) {
|
|
5419
|
+
return true;
|
|
5420
|
+
}
|
|
5421
|
+
await VendorModel.updateOne(
|
|
5422
|
+
{ _id: vendorId, downgradeNotifiedAt: claimedAt },
|
|
5423
|
+
{ $set: { downgradeNotifiedAt: null } }
|
|
5424
|
+
);
|
|
5425
|
+
return false;
|
|
5426
|
+
}
|
|
5276
5427
|
async function handleVendorLicenceCompliance(user, now) {
|
|
5277
5428
|
if (!user.licences || !user.vendor) {
|
|
5278
5429
|
return;
|
|
@@ -5283,10 +5434,10 @@ async function handleVendorLicenceCompliance(user, now) {
|
|
|
5283
5434
|
if (vendorLicencesOfUser.length === 0) {
|
|
5284
5435
|
return;
|
|
5285
5436
|
}
|
|
5286
|
-
const
|
|
5437
|
+
const proLicences = vendorLicencesOfUser.filter(
|
|
5287
5438
|
(licence) => licence.licenceType !== import_cluemart_shared37.EnumUserLicence.STANDARD_VENDOR
|
|
5288
5439
|
);
|
|
5289
|
-
const expiredProLicences =
|
|
5440
|
+
const expiredProLicences = proLicences.filter(
|
|
5290
5441
|
(licence) => !(0, import_dayjs6.default)(licence.expiryDate).isAfter(now)
|
|
5291
5442
|
);
|
|
5292
5443
|
const stripeProtectedLicenceTypes = new Set(
|
|
@@ -5297,7 +5448,7 @@ async function handleVendorLicenceCompliance(user, now) {
|
|
|
5297
5448
|
const expiredProLicence = expiredProLicences.some(
|
|
5298
5449
|
(licence) => !stripeProtectedLicenceTypes.has(licence.licenceType)
|
|
5299
5450
|
);
|
|
5300
|
-
const hasValidProLicence = stripeProtectedLicenceTypes.size > 0 || hasValidLicence(
|
|
5451
|
+
const hasValidProLicence = stripeProtectedLicenceTypes.size > 0 || hasValidLicence(proLicences, import_cluemart_shared37.vendorLicences, now);
|
|
5301
5452
|
const hasExpiredVendorLicence = vendorLicencesOfUser.some(
|
|
5302
5453
|
(licence) => !(0, import_dayjs6.default)(licence.expiryDate).isAfter(now) && !stripeProtectedLicenceTypes.has(licence.licenceType)
|
|
5303
5454
|
);
|
|
@@ -5313,29 +5464,27 @@ async function handleVendorLicenceCompliance(user, now) {
|
|
|
5313
5464
|
"owner.userId": user._id
|
|
5314
5465
|
}).lean().exec();
|
|
5315
5466
|
}
|
|
5467
|
+
const needsDowngrade = expiredProLicence && Boolean(vendor) && !hasValidProLicence;
|
|
5468
|
+
const outcome = needsDowngrade && vendor ? await applyVendorDowngrade(vendor, user._id) : "skipped";
|
|
5316
5469
|
const licencesToSync = import_cluemart_shared37.vendorLicences.filter(
|
|
5317
5470
|
(licenceType) => !stripeProtectedLicenceTypes.has(licenceType)
|
|
5318
5471
|
);
|
|
5319
|
-
|
|
5320
|
-
|
|
5472
|
+
if (!needsDowngrade) {
|
|
5473
|
+
await syncUserLicencesAfterExpiry(
|
|
5474
|
+
user._id,
|
|
5475
|
+
licencesToSync,
|
|
5476
|
+
now,
|
|
5477
|
+
"downgrade"
|
|
5478
|
+
);
|
|
5321
5479
|
return;
|
|
5322
5480
|
}
|
|
5323
|
-
|
|
5324
|
-
if (!downgraded) {
|
|
5481
|
+
if (outcome === "skipped" || !vendor) {
|
|
5325
5482
|
return;
|
|
5326
5483
|
}
|
|
5327
|
-
|
|
5328
|
-
|
|
5329
|
-
|
|
5330
|
-
|
|
5331
|
-
resourceType: import_cluemart_shared37.EnumNotificationResourceType.DOWNGRADED_VENDOR
|
|
5332
|
-
},
|
|
5333
|
-
message: `Your Vendor licence has been downgraded due to licence expiration.`,
|
|
5334
|
-
title: `Vendor Licence Downgraded`,
|
|
5335
|
-
type: import_cluemart_shared37.EnumNotificationType.SYSTEM,
|
|
5336
|
-
userIds: [user._id]
|
|
5337
|
-
};
|
|
5338
|
-
await sendDeactivationNotification(payload, user._id, vendor.name, "vendor");
|
|
5484
|
+
if (!await notifyVendorDowngrade(vendor._id, user._id)) {
|
|
5485
|
+
return;
|
|
5486
|
+
}
|
|
5487
|
+
await syncUserLicencesAfterExpiry(user._id, licencesToSync, now, "downgrade");
|
|
5339
5488
|
}
|
|
5340
5489
|
async function vendorOwnerHasProLicence(vendorId) {
|
|
5341
5490
|
const vendor = await VendorModel.findOne({
|
|
@@ -5380,17 +5529,35 @@ function hasValidLicence(licences, licenceTypes, now) {
|
|
|
5380
5529
|
(licence) => licenceTypes.includes(licence.licenceType) && (0, import_dayjs7.default)(licence.expiryDate).isAfter(now)
|
|
5381
5530
|
);
|
|
5382
5531
|
}
|
|
5383
|
-
async function
|
|
5532
|
+
async function deactivationNoticeStored(payload, userId, since) {
|
|
5533
|
+
const stored = await NotificationModel.exists({
|
|
5534
|
+
createdAt: { $gte: since },
|
|
5535
|
+
"data.resourceId": payload.data.resourceId,
|
|
5536
|
+
"data.resourceType": payload.data.resourceType,
|
|
5537
|
+
title: payload.title,
|
|
5538
|
+
type: payload.type,
|
|
5539
|
+
userId
|
|
5540
|
+
});
|
|
5541
|
+
return stored !== null;
|
|
5542
|
+
}
|
|
5543
|
+
async function sendDeactivationNotification(payload, userId, resourceName, resourceType, retryOf = null) {
|
|
5544
|
+
if (retryOf && await deactivationNoticeStored(payload, userId, retryOf)) {
|
|
5545
|
+
console.log(
|
|
5546
|
+
`\u{1F501} ${resourceType} notification already stored for user ${String(userId)} for "${resourceName}"`
|
|
5547
|
+
);
|
|
5548
|
+
return true;
|
|
5549
|
+
}
|
|
5384
5550
|
const sent = await notifyUsers({ payload });
|
|
5385
5551
|
if (!sent) {
|
|
5386
5552
|
console.error(
|
|
5387
5553
|
`\u274C ${resourceType} notification not confirmed for user ${String(userId)} for "${resourceName}"`
|
|
5388
5554
|
);
|
|
5389
|
-
return;
|
|
5555
|
+
return false;
|
|
5390
5556
|
}
|
|
5391
5557
|
console.log(
|
|
5392
5558
|
`\u2705 Sent ${resourceType} notification to user ${String(userId)} for "${resourceName}"`
|
|
5393
5559
|
);
|
|
5560
|
+
return true;
|
|
5394
5561
|
}
|
|
5395
5562
|
async function processUserCompliance(user, now) {
|
|
5396
5563
|
if (!user.licences) {
|
|
@@ -5817,6 +5984,7 @@ async function remindUsersAboutLicenseExpiration() {
|
|
|
5817
5984
|
activeAffiliateCodeExists,
|
|
5818
5985
|
awardAffiliateVendorSubscriptionRewards,
|
|
5819
5986
|
baseResourceFields,
|
|
5987
|
+
buildAssociatesWriteGuard,
|
|
5820
5988
|
buildInitialBillingCatalogEntries,
|
|
5821
5989
|
buildLicenceExpiryPipeline,
|
|
5822
5990
|
buildUserHasEnoughPointsFilter,
|
|
@@ -5881,6 +6049,7 @@ async function remindUsersAboutLicenseExpiration() {
|
|
|
5881
6049
|
notifyUsers,
|
|
5882
6050
|
partnerLicences,
|
|
5883
6051
|
persistArchivedEventDates,
|
|
6052
|
+
pinAssociateTimestamp,
|
|
5884
6053
|
processLicenseReminder,
|
|
5885
6054
|
processUserCompliance,
|
|
5886
6055
|
processUserLicenseReminders,
|