@timardex/cluemart-server-shared 2.0.1 → 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.
package/dist/index.cjs CHANGED
@@ -80,6 +80,7 @@ __export(index_exports, {
80
80
  createLicenceObject: () => createLicenceObject,
81
81
  dateTimeSchema: () => dateTimeSchema2,
82
82
  didRemoveAnyEventDates: () => didRemoveAnyEventDates,
83
+ downgradeVendorProFeatures: () => downgradeVendorProFeatures,
83
84
  ensureUserLicenseCompliance: () => ensureUserLicenseCompliance,
84
85
  ensureUserLicenseComplianceForUser: () => ensureUserLicenseComplianceForUser,
85
86
  eventLicences: () => eventLicences,
@@ -123,6 +124,7 @@ __export(index_exports, {
123
124
  remindUsersAboutLicenseExpiration: () => remindUsersAboutLicenseExpiration,
124
125
  replaceUserLicenceWithObject: () => replaceUserLicenceWithObject,
125
126
  resourceRelationsSchema: () => resourceRelationsSchema,
127
+ restoreVendorProFeatures: () => restoreVendorProFeatures,
126
128
  saveNotificationsInDb: () => saveNotificationsInDb,
127
129
  sendDeactivationNotification: () => sendDeactivationNotification,
128
130
  sendLicenseReminder: () => sendLicenseReminder,
@@ -135,7 +137,6 @@ __export(index_exports, {
135
137
  updateAllEventDateTimeStatuses: () => updateAllEventDateTimeStatuses,
136
138
  updateRelationDatesToUnavailable: () => updateRelationDatesToUnavailable,
137
139
  updateSingleDateTimeStatus: () => updateSingleDateTimeStatus,
138
- updateVendorBasedOnUserLicense: () => updateVendorBasedOnUserLicense,
139
140
  upgradeUserToLicence: () => upgradeUserToLicence,
140
141
  userLicenseSchema: () => userLicenseSchema,
141
142
  vendorLicences: () => vendorLicences,
@@ -1511,15 +1512,27 @@ var calendarWrapperSchema = new MongooseSchema14(
1511
1512
  },
1512
1513
  { _id: false }
1513
1514
  );
1515
+ var vendorAvailabilitySchema = new MongooseSchema14(
1516
+ {
1517
+ corporate: { default: false, required: false, type: Boolean },
1518
+ private: { default: false, required: false, type: Boolean },
1519
+ school: { default: false, required: false, type: Boolean }
1520
+ },
1521
+ { _id: false }
1522
+ );
1523
+ var downgradeSnapshotSchema = new MongooseSchema14(
1524
+ {
1525
+ availability: { required: false, type: vendorAvailabilitySchema },
1526
+ calendarActive: { required: false, type: Boolean },
1527
+ productsActive: { required: false, type: Boolean }
1528
+ },
1529
+ { _id: false }
1530
+ );
1514
1531
  var schema10 = new MongooseSchema14(
1515
1532
  {
1516
1533
  ...baseResourceFields,
1517
1534
  // Importing base resource fields from global.ts
1518
- availability: {
1519
- corporate: { default: false, required: false, type: Boolean },
1520
- private: { default: false, required: false, type: Boolean },
1521
- school: { default: false, required: false, type: Boolean }
1522
- },
1535
+ availability: { required: false, type: vendorAvailabilitySchema },
1523
1536
  calendar: {
1524
1537
  required: false,
1525
1538
  type: calendarWrapperSchema
@@ -1527,6 +1540,7 @@ var schema10 = new MongooseSchema14(
1527
1540
  categories: { required: true, type: [CategorySchema] },
1528
1541
  categoriesNames: { default: [], required: false, type: [String] },
1529
1542
  claimed: { default: true, required: false, type: Boolean },
1543
+ downgradeSnapshot: { required: false, type: downgradeSnapshotSchema },
1530
1544
  foodTruck: { required: true, type: Boolean },
1531
1545
  products: { required: false, type: productWrapperSchema },
1532
1546
  relations: {
@@ -1559,11 +1573,11 @@ schema10.index({ region: 1 });
1559
1573
  schema10.index({ "categories.name": 1 });
1560
1574
  schema10.index({ slug: 1 });
1561
1575
  schema10.index({
1562
- name: "text",
1576
+ categoriesNames: "text",
1563
1577
  description: "text",
1564
- categoriesNames: "text"
1578
+ name: "text"
1565
1579
  });
1566
- schema10.index({ "owner.userId": 1, deletedAt: 1 });
1580
+ schema10.index({ deletedAt: 1, "owner.userId": 1 });
1567
1581
  schema10.plugin(searchNamePlugin);
1568
1582
  var VendorModel = import_mongoose14.default.models.Vendor || import_mongoose14.default.model("Vendor", schema10);
1569
1583
 
@@ -2978,7 +2992,7 @@ async function findEventOrImportedMarketById(resourceId) {
2978
2992
  }
2979
2993
  const normalizedId = convertObjectIdsToStrings(resourceId);
2980
2994
  const [eventDoc, googleImportedDoc] = await Promise.all([
2981
- EventModel.findById(normalizedId).select("_id name dateTime").lean().exec(),
2995
+ EventModel.findById(normalizedId).select("_id name dateTime owner").lean().exec(),
2982
2996
  GoogleImportedMarketModel.findById(normalizedId).select("_id name dateTime").lean().exec()
2983
2997
  ]);
2984
2998
  return eventDoc ?? googleImportedDoc;
@@ -3474,42 +3488,118 @@ async function checkOwnerHasLicence({
3474
3488
  }
3475
3489
 
3476
3490
  // src/service/license/compliance/vendor.ts
3477
- async function updateVendorBasedOnUserLicense(userId, licenceType) {
3491
+ async function findUserVendor(userId, logLabel) {
3492
+ const user = await UserModel.findById(userId).select("vendor").lean().exec();
3493
+ if (!user?.vendor) {
3494
+ console.warn(`[${logLabel}] No vendor found for userId=${userId}`);
3495
+ return null;
3496
+ }
3497
+ const vendor = await VendorModel.findOne({
3498
+ _id: user.vendor,
3499
+ active: true,
3500
+ deletedAt: null,
3501
+ "owner.userId": userId
3502
+ }).lean().exec();
3503
+ if (!vendor) {
3504
+ console.warn(`[${logLabel}] Vendor not found for id=${user.vendor}`);
3505
+ return null;
3506
+ }
3507
+ return vendor;
3508
+ }
3509
+ async function applyVendorDowngrade(vendor, ownerUserId) {
3510
+ const updateData = {};
3511
+ updateData.availability = {
3512
+ corporate: false,
3513
+ private: false,
3514
+ school: false
3515
+ };
3516
+ updateData.products = {
3517
+ active: false,
3518
+ productsList: vendor.products?.productsList ?? []
3519
+ };
3520
+ updateData.calendar = {
3521
+ active: false,
3522
+ calendarData: vendor.calendar?.calendarData ?? []
3523
+ };
3524
+ updateData.downgradeSnapshot = {
3525
+ availability: vendor.availability,
3526
+ calendarActive: vendor.calendar?.active,
3527
+ productsActive: vendor.products?.active
3528
+ };
3529
+ updateData.images = (vendor.images ?? []).map((image, index) => ({
3530
+ ...image,
3531
+ active: index < 6
3532
+ }));
3533
+ const result = await VendorModel.updateOne(
3534
+ {
3535
+ _id: vendor._id,
3536
+ active: true,
3537
+ deletedAt: null,
3538
+ "owner.userId": ownerUserId
3539
+ },
3540
+ { $set: updateData }
3541
+ );
3542
+ return (result?.matchedCount ?? 0) > 0;
3543
+ }
3544
+ async function downgradeVendorProFeatures(userId, licenceType) {
3545
+ const isStandardVendor = licenceType === import_cluemart_shared28.EnumUserLicence.STANDARD_VENDOR;
3546
+ if (!isStandardVendor) {
3547
+ return;
3548
+ }
3478
3549
  try {
3479
- const user = await UserModel.findById(userId).select("vendor").lean().exec();
3480
- if (!user?.vendor) {
3481
- console.warn(`[updateVendor] No vendor found for userId=${userId}`);
3550
+ const vendor = await findUserVendor(userId, "downgradeVendorProFeatures");
3551
+ if (!vendor) {
3482
3552
  return;
3483
3553
  }
3484
- const vendor = await VendorModel.findById(user.vendor).lean().exec();
3554
+ await applyVendorDowngrade(vendor, userId);
3555
+ } catch (error) {
3556
+ console.error("[downgradeVendorProFeatures] Failed:", error);
3557
+ }
3558
+ }
3559
+ async function applyVendorRestore(vendor, ownerUserId) {
3560
+ const updateData = {};
3561
+ const snapshot = vendor.downgradeSnapshot;
3562
+ updateData.availability = {
3563
+ corporate: snapshot?.availability?.corporate ?? true,
3564
+ private: snapshot?.availability?.private ?? true,
3565
+ school: snapshot?.availability?.school ?? true
3566
+ };
3567
+ updateData.products = {
3568
+ active: snapshot?.productsActive ?? true,
3569
+ productsList: vendor.products?.productsList ?? []
3570
+ };
3571
+ updateData.calendar = {
3572
+ active: snapshot?.calendarActive ?? true,
3573
+ calendarData: vendor.calendar?.calendarData ?? []
3574
+ };
3575
+ updateData.images = (vendor.images ?? []).map((image) => ({
3576
+ ...image,
3577
+ active: true
3578
+ }));
3579
+ const result = await VendorModel.updateOne(
3580
+ {
3581
+ _id: vendor._id,
3582
+ active: true,
3583
+ deletedAt: null,
3584
+ "owner.userId": ownerUserId
3585
+ },
3586
+ { $set: updateData }
3587
+ );
3588
+ return (result?.matchedCount ?? 0) > 0;
3589
+ }
3590
+ async function restoreVendorProFeatures(userId, licenceType) {
3591
+ const isProVendor = licenceType === import_cluemart_shared28.EnumUserLicence.PRO_VENDOR || licenceType === import_cluemart_shared28.EnumUserLicence.PRO_PLUS_VENDOR;
3592
+ if (!isProVendor) {
3593
+ return;
3594
+ }
3595
+ try {
3596
+ const vendor = await findUserVendor(userId, "restoreVendorProFeatures");
3485
3597
  if (!vendor) {
3486
- console.warn(`[updateVendor] Vendor not found for id=${user.vendor}`);
3487
3598
  return;
3488
3599
  }
3489
- const updateData = {};
3490
- const isStandardVendor = licenceType === import_cluemart_shared28.EnumUserLicence.STANDARD_VENDOR;
3491
- if (isStandardVendor) {
3492
- updateData.availability = {
3493
- corporate: false,
3494
- private: false,
3495
- school: false
3496
- };
3497
- updateData.products = {
3498
- active: false,
3499
- productsList: vendor.products?.productsList ?? []
3500
- };
3501
- updateData.calendar = {
3502
- active: false,
3503
- calendarData: vendor.calendar?.calendarData ?? []
3504
- };
3505
- }
3506
- updateData.images = (vendor.images ?? []).map((image, index) => ({
3507
- ...image,
3508
- active: isStandardVendor ? index < 6 : true
3509
- }));
3510
- await VendorModel.updateOne({ _id: vendor._id }, { $set: updateData });
3600
+ await applyVendorRestore(vendor, userId);
3511
3601
  } catch (error) {
3512
- console.error("[updateVendorBasedOnUserLicense] Failed:", error);
3602
+ console.error("[restoreVendorProFeatures] Failed:", error);
3513
3603
  }
3514
3604
  }
3515
3605
  var vendorLicences = [
@@ -3575,10 +3665,10 @@ async function handleVendorLicenceCompliance(user, now) {
3575
3665
  if (!expiredProLicence || !vendor || hasValidProLicence) {
3576
3666
  return;
3577
3667
  }
3578
- await updateVendorBasedOnUserLicense(
3579
- user._id,
3580
- import_cluemart_shared28.EnumUserLicence.STANDARD_VENDOR
3581
- );
3668
+ const downgraded = await applyVendorDowngrade(vendor, user._id);
3669
+ if (!downgraded) {
3670
+ return;
3671
+ }
3582
3672
  const payload = {
3583
3673
  data: {
3584
3674
  resourceId: String(vendor._id),
@@ -4064,6 +4154,7 @@ async function remindUsersAboutLicenseExpiration() {
4064
4154
  createLicenceObject,
4065
4155
  dateTimeSchema,
4066
4156
  didRemoveAnyEventDates,
4157
+ downgradeVendorProFeatures,
4067
4158
  ensureUserLicenseCompliance,
4068
4159
  ensureUserLicenseComplianceForUser,
4069
4160
  eventLicences,
@@ -4107,6 +4198,7 @@ async function remindUsersAboutLicenseExpiration() {
4107
4198
  remindUsersAboutLicenseExpiration,
4108
4199
  replaceUserLicenceWithObject,
4109
4200
  resourceRelationsSchema,
4201
+ restoreVendorProFeatures,
4110
4202
  saveNotificationsInDb,
4111
4203
  sendDeactivationNotification,
4112
4204
  sendLicenseReminder,
@@ -4119,7 +4211,6 @@ async function remindUsersAboutLicenseExpiration() {
4119
4211
  updateAllEventDateTimeStatuses,
4120
4212
  updateRelationDatesToUnavailable,
4121
4213
  updateSingleDateTimeStatus,
4122
- updateVendorBasedOnUserLicense,
4123
4214
  upgradeUserToLicence,
4124
4215
  userLicenseSchema,
4125
4216
  vendorLicences,