@timardex/cluemart-server-shared 1.0.148 → 1.0.150

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.
@@ -1,8 +1,8 @@
1
1
  import * as _timardex_cluemart_shared from '@timardex/cluemart-shared';
2
2
  import { AdType, ReportChatUser, OwnerType, SocialMediaType, ResourceImageType, Category, UserLicenceType, AssociateType, ResourceContactDetailsType, TermsAgreement, RelationDate, DateTimeType, LocationGeoType, LocationType, PosterUsageType, GoogleImportedMarket, RelationType, ResourceActivityType, UserActivityEvent, UserType, VendorType, VendorInfoType, EventType, StallType, RefundPolicy, EventInfoType, PartnerType, PostType, PostContentType, AppSettingsType, GameType, GameDocType, SchoolType } from '@timardex/cluemart-shared';
3
3
  import mongoose from 'mongoose';
4
- import { O as ObjectId } from '../Chat-CM_mGOB0.mjs';
5
- export { C as ChatModel, N as NotificationModel, P as ParticipantSchema, a as SchemaChatMessageReactionType, S as SchemaChatMessageReplyPreviewType, b as SchemaChatMessageSeenType, c as SchemaChatMessageType, e as SchemaChatType, f as SchemaCreateBulkNotificationInput, g as SchemaNotificationType, d as SchemaParticipantType } from '../Chat-CM_mGOB0.mjs';
4
+ import { O as ObjectId } from '../Chat-Ctmtatmi.mjs';
5
+ export { C as ChatModel, N as NotificationModel, a as SchemaChatMessageReactionType, S as SchemaChatMessageReplyPreviewType, b as SchemaChatMessageSeenType, c as SchemaChatMessageType, e as SchemaChatType, f as SchemaCreateBulkNotificationInput, g as SchemaNotificationType, d as SchemaParticipantType } from '../Chat-Ctmtatmi.mjs';
6
6
  import { EnumOSPlatform } from '@timardex/cluemart-shared/enums';
7
7
  import '@timardex/cluemart-shared/types';
8
8
  import 'express';
@@ -449,7 +449,7 @@ declare const UserModel: mongoose.Model<SchemaUserType, {}, {}, {}, mongoose.Doc
449
449
  };
450
450
  };
451
451
  } & Required<{
452
- _id: mongoose.Schema.Types.ObjectId;
452
+ _id: mongoose.Types.ObjectId;
453
453
  }> & {
454
454
  __v: number;
455
455
  }, any>;
@@ -1,8 +1,8 @@
1
1
  import * as _timardex_cluemart_shared from '@timardex/cluemart-shared';
2
2
  import { AdType, ReportChatUser, OwnerType, SocialMediaType, ResourceImageType, Category, UserLicenceType, AssociateType, ResourceContactDetailsType, TermsAgreement, RelationDate, DateTimeType, LocationGeoType, LocationType, PosterUsageType, GoogleImportedMarket, RelationType, ResourceActivityType, UserActivityEvent, UserType, VendorType, VendorInfoType, EventType, StallType, RefundPolicy, EventInfoType, PartnerType, PostType, PostContentType, AppSettingsType, GameType, GameDocType, SchoolType } from '@timardex/cluemart-shared';
3
3
  import mongoose from 'mongoose';
4
- import { O as ObjectId } from '../Chat-CM_mGOB0.js';
5
- export { C as ChatModel, N as NotificationModel, P as ParticipantSchema, a as SchemaChatMessageReactionType, S as SchemaChatMessageReplyPreviewType, b as SchemaChatMessageSeenType, c as SchemaChatMessageType, e as SchemaChatType, f as SchemaCreateBulkNotificationInput, g as SchemaNotificationType, d as SchemaParticipantType } from '../Chat-CM_mGOB0.js';
4
+ import { O as ObjectId } from '../Chat-Ctmtatmi.js';
5
+ export { C as ChatModel, N as NotificationModel, a as SchemaChatMessageReactionType, S as SchemaChatMessageReplyPreviewType, b as SchemaChatMessageSeenType, c as SchemaChatMessageType, e as SchemaChatType, f as SchemaCreateBulkNotificationInput, g as SchemaNotificationType, d as SchemaParticipantType } from '../Chat-Ctmtatmi.js';
6
6
  import { EnumOSPlatform } from '@timardex/cluemart-shared/enums';
7
7
  import '@timardex/cluemart-shared/types';
8
8
  import 'express';
@@ -449,7 +449,7 @@ declare const UserModel: mongoose.Model<SchemaUserType, {}, {}, {}, mongoose.Doc
449
449
  };
450
450
  };
451
451
  } & Required<{
452
- _id: mongoose.Schema.Types.ObjectId;
452
+ _id: mongoose.Types.ObjectId;
453
453
  }> & {
454
454
  __v: number;
455
455
  }, any>;
@@ -12,7 +12,6 @@ import {
12
12
  GoogleImportedMarketModel,
13
13
  NotificationModel,
14
14
  OwnerTypeSchema,
15
- ParticipantSchema,
16
15
  PartnerModel,
17
16
  PostModel,
18
17
  PushTokenModel,
@@ -37,7 +36,7 @@ import {
37
36
  resourceRelationsSchema,
38
37
  termsAgreementSchema,
39
38
  userLicenseSchema
40
- } from "../chunk-XT6SQI4D.mjs";
39
+ } from "../chunk-YNM5IJGO.mjs";
41
40
  import "../chunk-3QS3WKRC.mjs";
42
41
  export {
43
42
  APP_SETTINGS_ID,
@@ -53,7 +52,6 @@ export {
53
52
  GoogleImportedMarketModel,
54
53
  NotificationModel,
55
54
  OwnerTypeSchema,
56
- ParticipantSchema,
57
55
  PartnerModel,
58
56
  PostModel,
59
57
  PushTokenModel,
@@ -9033,6 +9033,7 @@ var CHAT_MESSAGE_FIELDS_FRAGMENT = gql`
9033
9033
  var CHAT_PARTICIPANT = gql`
9034
9034
  fragment ChatParticipantFields on ChatParticipantType {
9035
9035
  active
9036
+ isAssociate
9036
9037
  userAvatar
9037
9038
  userEmail
9038
9039
  userId
@@ -11421,6 +11422,7 @@ var MessageSchema = new MongooseSchema4(
11421
11422
  var ParticipantSchema = new MongooseSchema4(
11422
11423
  {
11423
11424
  active: { default: true, required: true, type: Boolean },
11425
+ isAssociate: { default: null, required: false, type: Boolean },
11424
11426
  userAvatar: { required: false, type: String },
11425
11427
  userEmail: { required: true, type: String },
11426
11428
  userId: {
@@ -12642,10 +12644,44 @@ async function updateAdStatuses() {
12642
12644
  );
12643
12645
  }
12644
12646
 
12647
+ // src/service/vendor.ts
12648
+ var import_mongoose26 = __toESM(require("mongoose"));
12649
+
12645
12650
  // src/service/associate.ts
12646
- async function removeAssociateFromResource(resourceId, resourceType) {
12651
+ var import_mongoose24 = __toESM(require("mongoose"));
12652
+ async function removeAssociateFromResource({
12653
+ resourceId,
12654
+ resourceOwnerId,
12655
+ resourceType
12656
+ }) {
12647
12657
  const normalizedResourceId = resourceId.toString();
12658
+ const normalizedOwnerId = typeof resourceOwnerId === "string" ? new import_mongoose24.default.Types.ObjectId(resourceOwnerId) : resourceOwnerId;
12659
+ const session = await import_mongoose24.default.startSession();
12648
12660
  try {
12661
+ session.startTransaction();
12662
+ const usersWithAssociates = await UserModel.find(
12663
+ {
12664
+ associates: {
12665
+ $elemMatch: {
12666
+ resourceId: normalizedResourceId,
12667
+ resourceType
12668
+ }
12669
+ }
12670
+ },
12671
+ {
12672
+ associates: 1
12673
+ // Only fetch associates field for efficiency
12674
+ }
12675
+ ).lean().session(session);
12676
+ const associateEmails = usersWithAssociates.flatMap(
12677
+ (user) => (user.associates ?? []).filter(
12678
+ (assoc) => assoc.resourceId === normalizedResourceId && assoc.resourceType === resourceType
12679
+ ).map((assoc) => assoc.email)
12680
+ );
12681
+ if (associateEmails.length === 0) {
12682
+ await session.commitTransaction();
12683
+ return;
12684
+ }
12649
12685
  await UserModel.updateMany(
12650
12686
  {
12651
12687
  associates: {
@@ -12662,34 +12698,67 @@ async function removeAssociateFromResource(resourceId, resourceType) {
12662
12698
  resourceType
12663
12699
  }
12664
12700
  }
12701
+ },
12702
+ { session }
12703
+ );
12704
+ await ChatModel.updateMany(
12705
+ {
12706
+ active: true,
12707
+ chatType: EnumChatType.RELATION,
12708
+ deletedAt: null,
12709
+ participants: {
12710
+ $elemMatch: {
12711
+ userId: normalizedOwnerId
12712
+ }
12713
+ }
12714
+ },
12715
+ {
12716
+ $set: {
12717
+ "participants.$[assoc].active": false
12718
+ }
12719
+ },
12720
+ {
12721
+ arrayFilters: [
12722
+ {
12723
+ "assoc.isAssociate": true,
12724
+ "assoc.userEmail": { $in: associateEmails }
12725
+ }
12726
+ ],
12727
+ session
12665
12728
  }
12666
- ).exec();
12729
+ );
12730
+ await session.commitTransaction();
12667
12731
  } catch (error) {
12732
+ await session.abortTransaction();
12668
12733
  console.error(
12669
- `[removeAssociateFromResource] Failed to remove associates for resourceId=${normalizedResourceId}, resourceType=${resourceType}`,
12734
+ `[removeAssociateFromResource] Failed for resourceId=${normalizedResourceId}, resourceType=${resourceType}`,
12670
12735
  error
12671
12736
  );
12672
12737
  throw error;
12738
+ } finally {
12739
+ session.endSession();
12673
12740
  }
12674
12741
  }
12675
12742
 
12676
12743
  // src/service/vendor.ts
12677
12744
  async function updateVendorBasedOnUserLicense(userId, licenceType) {
12745
+ const session = await import_mongoose26.default.startSession();
12678
12746
  try {
12679
- const user = await UserModel.findById(userId).exec();
12680
- if (!user) {
12747
+ session.startTransaction();
12748
+ const user = await UserModel.findById(userId).select("vendor").lean().session(session);
12749
+ if (!user?.vendor) {
12750
+ console.warn(`[updateVendor] No vendor for userId=${userId}`);
12751
+ await session.abortTransaction();
12681
12752
  return;
12682
12753
  }
12683
- const userVendor = await VendorModel.findById(user.vendor).exec();
12684
- if (!userVendor) {
12685
- return;
12686
- }
12687
- const selectedLicence = licenceType;
12688
- if (selectedLicence === void 0) {
12754
+ const vendor = await VendorModel.findById(user.vendor).lean().session(session);
12755
+ if (!vendor) {
12756
+ console.warn(`[updateVendor] Vendor not found for id=${user.vendor}`);
12757
+ await session.abortTransaction();
12689
12758
  return;
12690
12759
  }
12691
12760
  const vendorUpdateData = {};
12692
- if (selectedLicence === EnumUserLicence.STANDARD_VENDOR) {
12761
+ if (licenceType === EnumUserLicence.STANDARD_VENDOR) {
12693
12762
  vendorUpdateData.associates = [];
12694
12763
  vendorUpdateData.availability = {
12695
12764
  corporate: false,
@@ -12698,46 +12767,50 @@ async function updateVendorBasedOnUserLicense(userId, licenceType) {
12698
12767
  };
12699
12768
  vendorUpdateData.products = {
12700
12769
  active: false,
12701
- productsList: userVendor.products?.productsList || []
12770
+ productsList: vendor.products?.productsList ?? []
12702
12771
  };
12703
12772
  vendorUpdateData.calendar = {
12704
12773
  active: false,
12705
- calendarData: userVendor.calendar?.calendarData || []
12774
+ calendarData: vendor.calendar?.calendarData ?? []
12706
12775
  };
12707
- vendorUpdateData.images = (userVendor.images || []).map((img, index) => ({
12708
- active: index < 6,
12709
- // first 6 will be true, the rest false
12710
- source: img.source,
12711
- title: img.title
12776
+ vendorUpdateData.images = (vendor.images ?? []).map((img, index) => ({
12777
+ ...img,
12778
+ active: index < 6
12712
12779
  }));
12713
12780
  } else {
12714
- vendorUpdateData.images = (userVendor.images || []).map((img) => ({
12715
- active: true,
12716
- // all images will be true
12717
- source: img.source,
12718
- title: img.title
12781
+ vendorUpdateData.images = (vendor.images ?? []).map((img) => ({
12782
+ ...img,
12783
+ active: true
12719
12784
  }));
12720
12785
  }
12721
- await VendorModel.findByIdAndUpdate(
12722
- userVendor._id,
12723
- {
12724
- $set: vendorUpdateData
12725
- },
12726
- { new: true }
12786
+ await VendorModel.updateOne(
12787
+ { _id: vendor._id },
12788
+ { $set: vendorUpdateData },
12789
+ { session }
12727
12790
  );
12728
- await removeAssociateFromResource(userVendor._id, EnumResourceType.VENDOR);
12791
+ if (licenceType === EnumUserLicence.STANDARD_VENDOR) {
12792
+ await removeAssociateFromResource({
12793
+ resourceId: vendor._id,
12794
+ resourceOwnerId: vendor.owner.userId,
12795
+ resourceType: EnumResourceType.VENDOR
12796
+ });
12797
+ }
12798
+ await session.commitTransaction();
12729
12799
  } catch (error) {
12730
- console.error("Error updating vendor based on user license:", error);
12800
+ await session.abortTransaction();
12801
+ console.error("[updateVendorBasedOnUserLicense] Failed:", error);
12802
+ } finally {
12803
+ session.endSession();
12731
12804
  }
12732
12805
  }
12733
12806
 
12734
12807
  // src/service/objectIdToString.ts
12735
- var import_mongoose26 = __toESM(require("mongoose"));
12808
+ var import_mongoose28 = __toESM(require("mongoose"));
12736
12809
  function convertObjectIdsToStrings(obj) {
12737
12810
  if (obj === null || obj === void 0) {
12738
12811
  return obj;
12739
12812
  }
12740
- if (obj instanceof import_mongoose26.default.Types.ObjectId) {
12813
+ if (obj instanceof import_mongoose28.default.Types.ObjectId) {
12741
12814
  return obj.toString();
12742
12815
  }
12743
12816
  if (Array.isArray(obj)) {