@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.
@@ -45,7 +45,7 @@ import {
45
45
  termsAgreementSchema,
46
46
  userLicenseSchema,
47
47
  vendorProductTypeSchema
48
- } from "../chunk-IICIHY26.mjs";
48
+ } from "../chunk-BEUTBR4H.mjs";
49
49
  export {
50
50
  APP_SETTINGS_ID,
51
51
  AdModel,
@@ -44,6 +44,7 @@ __export(service_exports, {
44
44
  convertObjectIdsToStrings: () => convertObjectIdsToStrings,
45
45
  createLicenceObject: () => createLicenceObject,
46
46
  didRemoveAnyEventDates: () => didRemoveAnyEventDates,
47
+ downgradeVendorProFeatures: () => downgradeVendorProFeatures,
47
48
  ensureUserLicenseCompliance: () => ensureUserLicenseCompliance,
48
49
  ensureUserLicenseComplianceForUser: () => ensureUserLicenseComplianceForUser,
49
50
  eventLicences: () => eventLicences,
@@ -78,6 +79,7 @@ __export(service_exports, {
78
79
  publishNotificationEvents: () => publishNotificationEvents,
79
80
  remindUsersAboutLicenseExpiration: () => remindUsersAboutLicenseExpiration,
80
81
  replaceUserLicenceWithObject: () => replaceUserLicenceWithObject,
82
+ restoreVendorProFeatures: () => restoreVendorProFeatures,
81
83
  saveNotificationsInDb: () => saveNotificationsInDb,
82
84
  sendDeactivationNotification: () => sendDeactivationNotification,
83
85
  sendLicenseReminder: () => sendLicenseReminder,
@@ -88,7 +90,6 @@ __export(service_exports, {
88
90
  updateAllEventDateTimeStatuses: () => updateAllEventDateTimeStatuses,
89
91
  updateRelationDatesToUnavailable: () => updateRelationDatesToUnavailable,
90
92
  updateSingleDateTimeStatus: () => updateSingleDateTimeStatus,
91
- updateVendorBasedOnUserLicense: () => updateVendorBasedOnUserLicense,
92
93
  upgradeUserToLicence: () => upgradeUserToLicence,
93
94
  vendorLicences: () => vendorLicences,
94
95
  vendorOwnerHasProLicence: () => vendorOwnerHasProLicence
@@ -1442,15 +1443,27 @@ var calendarWrapperSchema = new MongooseSchema14(
1442
1443
  },
1443
1444
  { _id: false }
1444
1445
  );
1446
+ var vendorAvailabilitySchema = new MongooseSchema14(
1447
+ {
1448
+ corporate: { default: false, required: false, type: Boolean },
1449
+ private: { default: false, required: false, type: Boolean },
1450
+ school: { default: false, required: false, type: Boolean }
1451
+ },
1452
+ { _id: false }
1453
+ );
1454
+ var downgradeSnapshotSchema = new MongooseSchema14(
1455
+ {
1456
+ availability: { required: false, type: vendorAvailabilitySchema },
1457
+ calendarActive: { required: false, type: Boolean },
1458
+ productsActive: { required: false, type: Boolean }
1459
+ },
1460
+ { _id: false }
1461
+ );
1445
1462
  var schema10 = new MongooseSchema14(
1446
1463
  {
1447
1464
  ...baseResourceFields,
1448
1465
  // Importing base resource fields from global.ts
1449
- availability: {
1450
- corporate: { default: false, required: false, type: Boolean },
1451
- private: { default: false, required: false, type: Boolean },
1452
- school: { default: false, required: false, type: Boolean }
1453
- },
1466
+ availability: { required: false, type: vendorAvailabilitySchema },
1454
1467
  calendar: {
1455
1468
  required: false,
1456
1469
  type: calendarWrapperSchema
@@ -1458,6 +1471,7 @@ var schema10 = new MongooseSchema14(
1458
1471
  categories: { required: true, type: [CategorySchema] },
1459
1472
  categoriesNames: { default: [], required: false, type: [String] },
1460
1473
  claimed: { default: true, required: false, type: Boolean },
1474
+ downgradeSnapshot: { required: false, type: downgradeSnapshotSchema },
1461
1475
  foodTruck: { required: true, type: Boolean },
1462
1476
  products: { required: false, type: productWrapperSchema },
1463
1477
  relations: {
@@ -1490,11 +1504,11 @@ schema10.index({ region: 1 });
1490
1504
  schema10.index({ "categories.name": 1 });
1491
1505
  schema10.index({ slug: 1 });
1492
1506
  schema10.index({
1493
- name: "text",
1507
+ categoriesNames: "text",
1494
1508
  description: "text",
1495
- categoriesNames: "text"
1509
+ name: "text"
1496
1510
  });
1497
- schema10.index({ "owner.userId": 1, deletedAt: 1 });
1511
+ schema10.index({ deletedAt: 1, "owner.userId": 1 });
1498
1512
  schema10.plugin(searchNamePlugin);
1499
1513
  var VendorModel = import_mongoose14.default.models.Vendor || import_mongoose14.default.model("Vendor", schema10);
1500
1514
 
@@ -3398,42 +3412,118 @@ async function checkOwnerHasLicence({
3398
3412
  }
3399
3413
 
3400
3414
  // src/service/license/compliance/vendor.ts
3401
- async function updateVendorBasedOnUserLicense(userId, licenceType) {
3415
+ async function findUserVendor(userId, logLabel) {
3416
+ const user = await UserModel.findById(userId).select("vendor").lean().exec();
3417
+ if (!user?.vendor) {
3418
+ console.warn(`[${logLabel}] No vendor found for userId=${userId}`);
3419
+ return null;
3420
+ }
3421
+ const vendor = await VendorModel.findOne({
3422
+ _id: user.vendor,
3423
+ active: true,
3424
+ deletedAt: null,
3425
+ "owner.userId": userId
3426
+ }).lean().exec();
3427
+ if (!vendor) {
3428
+ console.warn(`[${logLabel}] Vendor not found for id=${user.vendor}`);
3429
+ return null;
3430
+ }
3431
+ return vendor;
3432
+ }
3433
+ async function applyVendorDowngrade(vendor, ownerUserId) {
3434
+ const updateData = {};
3435
+ updateData.availability = {
3436
+ corporate: false,
3437
+ private: false,
3438
+ school: false
3439
+ };
3440
+ updateData.products = {
3441
+ active: false,
3442
+ productsList: vendor.products?.productsList ?? []
3443
+ };
3444
+ updateData.calendar = {
3445
+ active: false,
3446
+ calendarData: vendor.calendar?.calendarData ?? []
3447
+ };
3448
+ updateData.downgradeSnapshot = {
3449
+ availability: vendor.availability,
3450
+ calendarActive: vendor.calendar?.active,
3451
+ productsActive: vendor.products?.active
3452
+ };
3453
+ updateData.images = (vendor.images ?? []).map((image, index) => ({
3454
+ ...image,
3455
+ active: index < 6
3456
+ }));
3457
+ const result = await VendorModel.updateOne(
3458
+ {
3459
+ _id: vendor._id,
3460
+ active: true,
3461
+ deletedAt: null,
3462
+ "owner.userId": ownerUserId
3463
+ },
3464
+ { $set: updateData }
3465
+ );
3466
+ return (result?.matchedCount ?? 0) > 0;
3467
+ }
3468
+ async function downgradeVendorProFeatures(userId, licenceType) {
3469
+ const isStandardVendor = licenceType === import_cluemart_shared28.EnumUserLicence.STANDARD_VENDOR;
3470
+ if (!isStandardVendor) {
3471
+ return;
3472
+ }
3402
3473
  try {
3403
- const user = await UserModel.findById(userId).select("vendor").lean().exec();
3404
- if (!user?.vendor) {
3405
- console.warn(`[updateVendor] No vendor found for userId=${userId}`);
3474
+ const vendor = await findUserVendor(userId, "downgradeVendorProFeatures");
3475
+ if (!vendor) {
3406
3476
  return;
3407
3477
  }
3408
- const vendor = await VendorModel.findById(user.vendor).lean().exec();
3478
+ await applyVendorDowngrade(vendor, userId);
3479
+ } catch (error) {
3480
+ console.error("[downgradeVendorProFeatures] Failed:", error);
3481
+ }
3482
+ }
3483
+ async function applyVendorRestore(vendor, ownerUserId) {
3484
+ const updateData = {};
3485
+ const snapshot = vendor.downgradeSnapshot;
3486
+ updateData.availability = {
3487
+ corporate: snapshot?.availability?.corporate ?? true,
3488
+ private: snapshot?.availability?.private ?? true,
3489
+ school: snapshot?.availability?.school ?? true
3490
+ };
3491
+ updateData.products = {
3492
+ active: snapshot?.productsActive ?? true,
3493
+ productsList: vendor.products?.productsList ?? []
3494
+ };
3495
+ updateData.calendar = {
3496
+ active: snapshot?.calendarActive ?? true,
3497
+ calendarData: vendor.calendar?.calendarData ?? []
3498
+ };
3499
+ updateData.images = (vendor.images ?? []).map((image) => ({
3500
+ ...image,
3501
+ active: true
3502
+ }));
3503
+ const result = await VendorModel.updateOne(
3504
+ {
3505
+ _id: vendor._id,
3506
+ active: true,
3507
+ deletedAt: null,
3508
+ "owner.userId": ownerUserId
3509
+ },
3510
+ { $set: updateData }
3511
+ );
3512
+ return (result?.matchedCount ?? 0) > 0;
3513
+ }
3514
+ async function restoreVendorProFeatures(userId, licenceType) {
3515
+ const isProVendor = licenceType === import_cluemart_shared28.EnumUserLicence.PRO_VENDOR || licenceType === import_cluemart_shared28.EnumUserLicence.PRO_PLUS_VENDOR;
3516
+ if (!isProVendor) {
3517
+ return;
3518
+ }
3519
+ try {
3520
+ const vendor = await findUserVendor(userId, "restoreVendorProFeatures");
3409
3521
  if (!vendor) {
3410
- console.warn(`[updateVendor] Vendor not found for id=${user.vendor}`);
3411
3522
  return;
3412
3523
  }
3413
- const updateData = {};
3414
- const isStandardVendor = licenceType === import_cluemart_shared28.EnumUserLicence.STANDARD_VENDOR;
3415
- if (isStandardVendor) {
3416
- updateData.availability = {
3417
- corporate: false,
3418
- private: false,
3419
- school: false
3420
- };
3421
- updateData.products = {
3422
- active: false,
3423
- productsList: vendor.products?.productsList ?? []
3424
- };
3425
- updateData.calendar = {
3426
- active: false,
3427
- calendarData: vendor.calendar?.calendarData ?? []
3428
- };
3429
- }
3430
- updateData.images = (vendor.images ?? []).map((image, index) => ({
3431
- ...image,
3432
- active: isStandardVendor ? index < 6 : true
3433
- }));
3434
- await VendorModel.updateOne({ _id: vendor._id }, { $set: updateData });
3524
+ await applyVendorRestore(vendor, userId);
3435
3525
  } catch (error) {
3436
- console.error("[updateVendorBasedOnUserLicense] Failed:", error);
3526
+ console.error("[restoreVendorProFeatures] Failed:", error);
3437
3527
  }
3438
3528
  }
3439
3529
  var vendorLicences = [
@@ -3499,10 +3589,10 @@ async function handleVendorLicenceCompliance(user, now) {
3499
3589
  if (!expiredProLicence || !vendor || hasValidProLicence) {
3500
3590
  return;
3501
3591
  }
3502
- await updateVendorBasedOnUserLicense(
3503
- user._id,
3504
- import_cluemart_shared28.EnumUserLicence.STANDARD_VENDOR
3505
- );
3592
+ const downgraded = await applyVendorDowngrade(vendor, user._id);
3593
+ if (!downgraded) {
3594
+ return;
3595
+ }
3506
3596
  const payload = {
3507
3597
  data: {
3508
3598
  resourceId: String(vendor._id),
@@ -3952,6 +4042,7 @@ async function remindUsersAboutLicenseExpiration() {
3952
4042
  convertObjectIdsToStrings,
3953
4043
  createLicenceObject,
3954
4044
  didRemoveAnyEventDates,
4045
+ downgradeVendorProFeatures,
3955
4046
  ensureUserLicenseCompliance,
3956
4047
  ensureUserLicenseComplianceForUser,
3957
4048
  eventLicences,
@@ -3986,6 +4077,7 @@ async function remindUsersAboutLicenseExpiration() {
3986
4077
  publishNotificationEvents,
3987
4078
  remindUsersAboutLicenseExpiration,
3988
4079
  replaceUserLicenceWithObject,
4080
+ restoreVendorProFeatures,
3989
4081
  saveNotificationsInDb,
3990
4082
  sendDeactivationNotification,
3991
4083
  sendLicenseReminder,
@@ -3996,7 +4088,6 @@ async function remindUsersAboutLicenseExpiration() {
3996
4088
  updateAllEventDateTimeStatuses,
3997
4089
  updateRelationDatesToUnavailable,
3998
4090
  updateSingleDateTimeStatus,
3999
- updateVendorBasedOnUserLicense,
4000
4091
  upgradeUserToLicence,
4001
4092
  vendorLicences,
4002
4093
  vendorOwnerHasProLicence