@timardex/cluemart-server-shared 2.0.2 → 2.1.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.
@@ -310,7 +310,15 @@ declare function sendDeactivationNotification(payload: {
310
310
  */
311
311
  declare function processUserCompliance(user: SchemaUserType, now: dayjs.Dayjs): Promise<void>;
312
312
 
313
- declare function updateVendorBasedOnUserLicense(userId: ObjectId, licenceType: EnumUserLicence$1): Promise<void>;
313
+ declare function downgradeVendorProFeatures(userId: ObjectId, licenceType: EnumUserLicence$1): Promise<void>;
314
+ /**
315
+ * Restores the PRO vendor features that are removed by
316
+ * downgradeVendorProFeatures when a vendor is downgraded to STANDARD_VENDOR.
317
+ * This is the opposite operation: it re-enables availability, products and
318
+ * calendar from the pre-downgrade snapshot (falling back to fully enabled when
319
+ * no snapshot exists), and keeps all images active.
320
+ */
321
+ declare function restoreVendorProFeatures(userId: ObjectId, licenceType: EnumUserLicence$1): Promise<void>;
314
322
  declare const vendorLicences: EnumUserLicence$1[];
315
323
  /**
316
324
  * Handles vendor licence compliance for a single user
@@ -467,4 +475,4 @@ declare function checkOwnerHasLicence({ userId, resourceType, }: {
467
475
  resourceType: EnumResourceType;
468
476
  }): Promise<void>;
469
477
 
470
- export { ACTIVE_NOT_DELETED_FILTER, type AwardAffiliateVendorSubscriptionRewardsResult, LicenceCategory, LicenceExpiryDate, type NotificationPubSub, REMINDER_WINDOW_MAX_HOURS, REMINDER_WINDOW_MIN_HOURS, SUBSCRIPTION_REWARD_TIMEZONE, activeAffiliateCodeExists, awardAffiliateVendorSubscriptionRewards, buildLicenceExpiryPipeline, checkOwnerHasLicence, connectToDatabase, convertObjectIdsToStrings, createLicenceObject, didRemoveAnyEventDates, ensureUserLicenseCompliance, ensureUserLicenseComplianceForUser, eventLicences, findAffiliateByPromoCode, findEventOrImportedMarketById, getEventResourceInfo, getHighestLicenceForCategory, getLicenceCategory, getLicenceExpiryMode, getPartnerResourceInfo, getPayingEventStripeSubscriptionPlan, getPayingStripeSubscriptionPlan, getPayingVendorStripeSubscriptionPlan, getSubscriptionRewardPeriodBounds, getVendorResourceInfo, handleEventLicenceCompliance, handlePartnerLicenceCompliance, handleVendorLicenceCompliance, hasValidLicence, isStandardLicence, isValidObjectId, licenceCategoryMapping, licencePriority, mapVendorLicenceToSubscriptionRewardType, normalizeAffiliatePromoCodes, normalizePromoCode, notifyUsers, partnerLicences, processLicenseReminder, processUserCompliance, processUserLicenseReminders, publishNotificationEvents, remindUsersAboutLicenseExpiration, replaceUserLicenceWithObject, saveNotificationsInDb, sendDeactivationNotification, sendLicenseReminder, sendPushNotifications, sendReminderSafely, syncUserLicencesAfterExpiry, updateAdStatuses, updateAllEventDateTimeStatuses, updateRelationDatesToUnavailable, updateSingleDateTimeStatus, updateVendorBasedOnUserLicense, upgradeUserToLicence, vendorLicences, vendorOwnerHasProLicence };
478
+ export { ACTIVE_NOT_DELETED_FILTER, type AwardAffiliateVendorSubscriptionRewardsResult, LicenceCategory, LicenceExpiryDate, type NotificationPubSub, REMINDER_WINDOW_MAX_HOURS, REMINDER_WINDOW_MIN_HOURS, SUBSCRIPTION_REWARD_TIMEZONE, activeAffiliateCodeExists, awardAffiliateVendorSubscriptionRewards, buildLicenceExpiryPipeline, checkOwnerHasLicence, connectToDatabase, convertObjectIdsToStrings, createLicenceObject, didRemoveAnyEventDates, downgradeVendorProFeatures, ensureUserLicenseCompliance, ensureUserLicenseComplianceForUser, eventLicences, findAffiliateByPromoCode, findEventOrImportedMarketById, getEventResourceInfo, getHighestLicenceForCategory, getLicenceCategory, getLicenceExpiryMode, getPartnerResourceInfo, getPayingEventStripeSubscriptionPlan, getPayingStripeSubscriptionPlan, getPayingVendorStripeSubscriptionPlan, getSubscriptionRewardPeriodBounds, getVendorResourceInfo, handleEventLicenceCompliance, handlePartnerLicenceCompliance, handleVendorLicenceCompliance, hasValidLicence, isStandardLicence, isValidObjectId, licenceCategoryMapping, licencePriority, mapVendorLicenceToSubscriptionRewardType, normalizeAffiliatePromoCodes, normalizePromoCode, notifyUsers, partnerLicences, processLicenseReminder, processUserCompliance, processUserLicenseReminders, publishNotificationEvents, remindUsersAboutLicenseExpiration, replaceUserLicenceWithObject, restoreVendorProFeatures, saveNotificationsInDb, sendDeactivationNotification, sendLicenseReminder, sendPushNotifications, sendReminderSafely, syncUserLicencesAfterExpiry, updateAdStatuses, updateAllEventDateTimeStatuses, updateRelationDatesToUnavailable, updateSingleDateTimeStatus, upgradeUserToLicence, vendorLicences, vendorOwnerHasProLicence };
@@ -310,7 +310,15 @@ declare function sendDeactivationNotification(payload: {
310
310
  */
311
311
  declare function processUserCompliance(user: SchemaUserType, now: dayjs.Dayjs): Promise<void>;
312
312
 
313
- declare function updateVendorBasedOnUserLicense(userId: ObjectId, licenceType: EnumUserLicence$1): Promise<void>;
313
+ declare function downgradeVendorProFeatures(userId: ObjectId, licenceType: EnumUserLicence$1): Promise<void>;
314
+ /**
315
+ * Restores the PRO vendor features that are removed by
316
+ * downgradeVendorProFeatures when a vendor is downgraded to STANDARD_VENDOR.
317
+ * This is the opposite operation: it re-enables availability, products and
318
+ * calendar from the pre-downgrade snapshot (falling back to fully enabled when
319
+ * no snapshot exists), and keeps all images active.
320
+ */
321
+ declare function restoreVendorProFeatures(userId: ObjectId, licenceType: EnumUserLicence$1): Promise<void>;
314
322
  declare const vendorLicences: EnumUserLicence$1[];
315
323
  /**
316
324
  * Handles vendor licence compliance for a single user
@@ -467,4 +475,4 @@ declare function checkOwnerHasLicence({ userId, resourceType, }: {
467
475
  resourceType: EnumResourceType;
468
476
  }): Promise<void>;
469
477
 
470
- export { ACTIVE_NOT_DELETED_FILTER, type AwardAffiliateVendorSubscriptionRewardsResult, LicenceCategory, LicenceExpiryDate, type NotificationPubSub, REMINDER_WINDOW_MAX_HOURS, REMINDER_WINDOW_MIN_HOURS, SUBSCRIPTION_REWARD_TIMEZONE, activeAffiliateCodeExists, awardAffiliateVendorSubscriptionRewards, buildLicenceExpiryPipeline, checkOwnerHasLicence, connectToDatabase, convertObjectIdsToStrings, createLicenceObject, didRemoveAnyEventDates, ensureUserLicenseCompliance, ensureUserLicenseComplianceForUser, eventLicences, findAffiliateByPromoCode, findEventOrImportedMarketById, getEventResourceInfo, getHighestLicenceForCategory, getLicenceCategory, getLicenceExpiryMode, getPartnerResourceInfo, getPayingEventStripeSubscriptionPlan, getPayingStripeSubscriptionPlan, getPayingVendorStripeSubscriptionPlan, getSubscriptionRewardPeriodBounds, getVendorResourceInfo, handleEventLicenceCompliance, handlePartnerLicenceCompliance, handleVendorLicenceCompliance, hasValidLicence, isStandardLicence, isValidObjectId, licenceCategoryMapping, licencePriority, mapVendorLicenceToSubscriptionRewardType, normalizeAffiliatePromoCodes, normalizePromoCode, notifyUsers, partnerLicences, processLicenseReminder, processUserCompliance, processUserLicenseReminders, publishNotificationEvents, remindUsersAboutLicenseExpiration, replaceUserLicenceWithObject, saveNotificationsInDb, sendDeactivationNotification, sendLicenseReminder, sendPushNotifications, sendReminderSafely, syncUserLicencesAfterExpiry, updateAdStatuses, updateAllEventDateTimeStatuses, updateRelationDatesToUnavailable, updateSingleDateTimeStatus, updateVendorBasedOnUserLicense, upgradeUserToLicence, vendorLicences, vendorOwnerHasProLicence };
478
+ export { ACTIVE_NOT_DELETED_FILTER, type AwardAffiliateVendorSubscriptionRewardsResult, LicenceCategory, LicenceExpiryDate, type NotificationPubSub, REMINDER_WINDOW_MAX_HOURS, REMINDER_WINDOW_MIN_HOURS, SUBSCRIPTION_REWARD_TIMEZONE, activeAffiliateCodeExists, awardAffiliateVendorSubscriptionRewards, buildLicenceExpiryPipeline, checkOwnerHasLicence, connectToDatabase, convertObjectIdsToStrings, createLicenceObject, didRemoveAnyEventDates, downgradeVendorProFeatures, ensureUserLicenseCompliance, ensureUserLicenseComplianceForUser, eventLicences, findAffiliateByPromoCode, findEventOrImportedMarketById, getEventResourceInfo, getHighestLicenceForCategory, getLicenceCategory, getLicenceExpiryMode, getPartnerResourceInfo, getPayingEventStripeSubscriptionPlan, getPayingStripeSubscriptionPlan, getPayingVendorStripeSubscriptionPlan, getSubscriptionRewardPeriodBounds, getVendorResourceInfo, handleEventLicenceCompliance, handlePartnerLicenceCompliance, handleVendorLicenceCompliance, hasValidLicence, isStandardLicence, isValidObjectId, licenceCategoryMapping, licencePriority, mapVendorLicenceToSubscriptionRewardType, normalizeAffiliatePromoCodes, normalizePromoCode, notifyUsers, partnerLicences, processLicenseReminder, processUserCompliance, processUserLicenseReminders, publishNotificationEvents, remindUsersAboutLicenseExpiration, replaceUserLicenceWithObject, restoreVendorProFeatures, saveNotificationsInDb, sendDeactivationNotification, sendLicenseReminder, sendPushNotifications, sendReminderSafely, syncUserLicencesAfterExpiry, updateAdStatuses, updateAllEventDateTimeStatuses, updateRelationDatesToUnavailable, updateSingleDateTimeStatus, upgradeUserToLicence, vendorLicences, vendorOwnerHasProLicence };
@@ -8,7 +8,7 @@ import {
8
8
  PushTokenModel,
9
9
  UserModel,
10
10
  VendorModel
11
- } from "../chunk-IICIHY26.mjs";
11
+ } from "../chunk-BEUTBR4H.mjs";
12
12
  import "../chunk-6PNG5BI5.mjs";
13
13
 
14
14
  // src/service/promoCode/constants.ts
@@ -1216,42 +1216,118 @@ async function checkOwnerHasLicence({
1216
1216
  }
1217
1217
 
1218
1218
  // src/service/license/compliance/vendor.ts
1219
- async function updateVendorBasedOnUserLicense(userId, licenceType) {
1219
+ async function findUserVendor(userId, logLabel) {
1220
+ const user = await UserModel.findById(userId).select("vendor").lean().exec();
1221
+ if (!user?.vendor) {
1222
+ console.warn(`[${logLabel}] No vendor found for userId=${userId}`);
1223
+ return null;
1224
+ }
1225
+ const vendor = await VendorModel.findOne({
1226
+ _id: user.vendor,
1227
+ active: true,
1228
+ deletedAt: null,
1229
+ "owner.userId": userId
1230
+ }).lean().exec();
1231
+ if (!vendor) {
1232
+ console.warn(`[${logLabel}] Vendor not found for id=${user.vendor}`);
1233
+ return null;
1234
+ }
1235
+ return vendor;
1236
+ }
1237
+ async function applyVendorDowngrade(vendor, ownerUserId) {
1238
+ const updateData = {};
1239
+ updateData.availability = {
1240
+ corporate: false,
1241
+ private: false,
1242
+ school: false
1243
+ };
1244
+ updateData.products = {
1245
+ active: false,
1246
+ productsList: vendor.products?.productsList ?? []
1247
+ };
1248
+ updateData.calendar = {
1249
+ active: false,
1250
+ calendarData: vendor.calendar?.calendarData ?? []
1251
+ };
1252
+ updateData.downgradeSnapshot = {
1253
+ availability: vendor.availability,
1254
+ calendarActive: vendor.calendar?.active,
1255
+ productsActive: vendor.products?.active
1256
+ };
1257
+ updateData.images = (vendor.images ?? []).map((image, index) => ({
1258
+ ...image,
1259
+ active: index < 6
1260
+ }));
1261
+ const result = await VendorModel.updateOne(
1262
+ {
1263
+ _id: vendor._id,
1264
+ active: true,
1265
+ deletedAt: null,
1266
+ "owner.userId": ownerUserId
1267
+ },
1268
+ { $set: updateData }
1269
+ );
1270
+ return (result?.matchedCount ?? 0) > 0;
1271
+ }
1272
+ async function downgradeVendorProFeatures(userId, licenceType) {
1273
+ const isStandardVendor = licenceType === EnumUserLicence6.STANDARD_VENDOR;
1274
+ if (!isStandardVendor) {
1275
+ return;
1276
+ }
1220
1277
  try {
1221
- const user = await UserModel.findById(userId).select("vendor").lean().exec();
1222
- if (!user?.vendor) {
1223
- console.warn(`[updateVendor] No vendor found for userId=${userId}`);
1278
+ const vendor = await findUserVendor(userId, "downgradeVendorProFeatures");
1279
+ if (!vendor) {
1224
1280
  return;
1225
1281
  }
1226
- const vendor = await VendorModel.findById(user.vendor).lean().exec();
1282
+ await applyVendorDowngrade(vendor, userId);
1283
+ } catch (error) {
1284
+ console.error("[downgradeVendorProFeatures] Failed:", error);
1285
+ }
1286
+ }
1287
+ async function applyVendorRestore(vendor, ownerUserId) {
1288
+ const updateData = {};
1289
+ const snapshot = vendor.downgradeSnapshot;
1290
+ updateData.availability = {
1291
+ corporate: snapshot?.availability?.corporate ?? true,
1292
+ private: snapshot?.availability?.private ?? true,
1293
+ school: snapshot?.availability?.school ?? true
1294
+ };
1295
+ updateData.products = {
1296
+ active: snapshot?.productsActive ?? true,
1297
+ productsList: vendor.products?.productsList ?? []
1298
+ };
1299
+ updateData.calendar = {
1300
+ active: snapshot?.calendarActive ?? true,
1301
+ calendarData: vendor.calendar?.calendarData ?? []
1302
+ };
1303
+ updateData.images = (vendor.images ?? []).map((image) => ({
1304
+ ...image,
1305
+ active: true
1306
+ }));
1307
+ const result = await VendorModel.updateOne(
1308
+ {
1309
+ _id: vendor._id,
1310
+ active: true,
1311
+ deletedAt: null,
1312
+ "owner.userId": ownerUserId
1313
+ },
1314
+ { $set: updateData }
1315
+ );
1316
+ return (result?.matchedCount ?? 0) > 0;
1317
+ }
1318
+ async function restoreVendorProFeatures(userId, licenceType) {
1319
+ const isProVendor = licenceType === EnumUserLicence6.PRO_VENDOR || licenceType === EnumUserLicence6.PRO_PLUS_VENDOR;
1320
+ if (!isProVendor) {
1321
+ return;
1322
+ }
1323
+ try {
1324
+ const vendor = await findUserVendor(userId, "restoreVendorProFeatures");
1227
1325
  if (!vendor) {
1228
- console.warn(`[updateVendor] Vendor not found for id=${user.vendor}`);
1229
1326
  return;
1230
1327
  }
1231
- const updateData = {};
1232
- const isStandardVendor = licenceType === EnumUserLicence6.STANDARD_VENDOR;
1233
- if (isStandardVendor) {
1234
- updateData.availability = {
1235
- corporate: false,
1236
- private: false,
1237
- school: false
1238
- };
1239
- updateData.products = {
1240
- active: false,
1241
- productsList: vendor.products?.productsList ?? []
1242
- };
1243
- updateData.calendar = {
1244
- active: false,
1245
- calendarData: vendor.calendar?.calendarData ?? []
1246
- };
1247
- }
1248
- updateData.images = (vendor.images ?? []).map((image, index) => ({
1249
- ...image,
1250
- active: isStandardVendor ? index < 6 : true
1251
- }));
1252
- await VendorModel.updateOne({ _id: vendor._id }, { $set: updateData });
1328
+ await applyVendorRestore(vendor, userId);
1253
1329
  } catch (error) {
1254
- console.error("[updateVendorBasedOnUserLicense] Failed:", error);
1330
+ console.error("[restoreVendorProFeatures] Failed:", error);
1255
1331
  }
1256
1332
  }
1257
1333
  var vendorLicences = [
@@ -1317,10 +1393,10 @@ async function handleVendorLicenceCompliance(user, now) {
1317
1393
  if (!expiredProLicence || !vendor || hasValidProLicence) {
1318
1394
  return;
1319
1395
  }
1320
- await updateVendorBasedOnUserLicense(
1321
- user._id,
1322
- EnumUserLicence6.STANDARD_VENDOR
1323
- );
1396
+ const downgraded = await applyVendorDowngrade(vendor, user._id);
1397
+ if (!downgraded) {
1398
+ return;
1399
+ }
1324
1400
  const payload = {
1325
1401
  data: {
1326
1402
  resourceId: String(vendor._id),
@@ -1775,6 +1851,7 @@ export {
1775
1851
  convertObjectIdsToStrings,
1776
1852
  createLicenceObject,
1777
1853
  didRemoveAnyEventDates,
1854
+ downgradeVendorProFeatures,
1778
1855
  ensureUserLicenseCompliance,
1779
1856
  ensureUserLicenseComplianceForUser,
1780
1857
  eventLicences,
@@ -1809,6 +1886,7 @@ export {
1809
1886
  publishNotificationEvents,
1810
1887
  remindUsersAboutLicenseExpiration,
1811
1888
  replaceUserLicenceWithObject,
1889
+ restoreVendorProFeatures,
1812
1890
  saveNotificationsInDb,
1813
1891
  sendDeactivationNotification,
1814
1892
  sendLicenseReminder,
@@ -1819,7 +1897,6 @@ export {
1819
1897
  updateAllEventDateTimeStatuses,
1820
1898
  updateRelationDatesToUnavailable,
1821
1899
  updateSingleDateTimeStatus,
1822
- updateVendorBasedOnUserLicense,
1823
1900
  upgradeUserToLicence,
1824
1901
  vendorLicences,
1825
1902
  vendorOwnerHasProLicence