@timardex/cluemart-server-shared 1.0.162 → 1.0.164

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.mjs CHANGED
@@ -12272,7 +12272,8 @@ var schema11 = new MongooseSchema15(
12272
12272
  required: false,
12273
12273
  type: [resourceRelationsSchema]
12274
12274
  },
12275
- tags: { required: true, type: [String] }
12275
+ tags: { required: true, type: [String] },
12276
+ unregisteredVendorIds: { default: [], required: false, type: [String] }
12276
12277
  },
12277
12278
  { timestamps: true }
12278
12279
  );
@@ -12783,83 +12784,101 @@ async function updateAdStatuses() {
12783
12784
 
12784
12785
  // src/service/associate.ts
12785
12786
  import mongoose25 from "mongoose";
12786
- async function removeAssociateFromResource({
12787
- resourceId,
12788
- resourceOwnerId,
12787
+ function normalizeObjectId(id) {
12788
+ return typeof id === "string" ? new mongoose25.Types.ObjectId(id) : id;
12789
+ }
12790
+ async function getAssociateEmailsForResource({
12791
+ normalizedResourceId,
12789
12792
  resourceType
12790
12793
  }) {
12791
- try {
12792
- const normalizedResourceId = resourceId.toString();
12793
- const normalizedOwnerId = typeof resourceOwnerId === "string" ? new mongoose25.Types.ObjectId(resourceOwnerId) : resourceOwnerId;
12794
- const usersWithAssociates = await UserModel.find(
12795
- {
12796
- associates: {
12797
- $elemMatch: {
12798
- resourceId: normalizedResourceId,
12799
- resourceType
12800
- }
12794
+ const usersWithAssociates = await UserModel.find(
12795
+ {
12796
+ associates: {
12797
+ $elemMatch: {
12798
+ resourceId: normalizedResourceId,
12799
+ resourceType
12801
12800
  }
12802
- },
12803
- {
12804
- associates: 1
12805
- }
12806
- ).lean();
12807
- const associateEmails = [
12808
- ...new Set(
12809
- usersWithAssociates.flatMap(
12810
- (user) => (user.associates ?? []).filter(
12811
- (associate) => associate.resourceId === normalizedResourceId && associate.resourceType === resourceType
12812
- ).map((associate) => associate.email)
12813
- )
12801
+ }
12802
+ },
12803
+ {
12804
+ associates: 1
12805
+ }
12806
+ ).lean();
12807
+ return [
12808
+ ...new Set(
12809
+ usersWithAssociates.flatMap(
12810
+ (user) => (user.associates ?? []).filter(
12811
+ (associate) => associate.resourceId === normalizedResourceId && associate.resourceType === resourceType
12812
+ ).map((associate) => associate.email)
12814
12813
  )
12815
- ];
12816
- if (associateEmails.length === 0) {
12817
- return;
12818
- }
12819
- await UserModel.updateMany(
12820
- {
12814
+ )
12815
+ ];
12816
+ }
12817
+ async function pullAssociatesFromUsers({
12818
+ normalizedResourceId,
12819
+ resourceType
12820
+ }) {
12821
+ await UserModel.updateMany(
12822
+ {
12823
+ associates: {
12824
+ $elemMatch: {
12825
+ resourceId: normalizedResourceId,
12826
+ resourceType
12827
+ }
12828
+ }
12829
+ },
12830
+ {
12831
+ $pull: {
12821
12832
  associates: {
12822
- $elemMatch: {
12823
- resourceId: normalizedResourceId,
12824
- resourceType
12825
- }
12833
+ resourceId: normalizedResourceId,
12834
+ resourceType
12826
12835
  }
12827
- },
12828
- {
12829
- $pull: {
12830
- associates: {
12831
- resourceId: normalizedResourceId,
12832
- resourceType
12833
- }
12836
+ }
12837
+ }
12838
+ );
12839
+ }
12840
+ async function pullAssociateParticipantsFromChats(resourceOwnerId, associateEmails) {
12841
+ await ChatModel.updateMany(
12842
+ {
12843
+ active: true,
12844
+ chatType: EnumChatType.RELATION,
12845
+ deletedAt: null,
12846
+ participants: {
12847
+ $elemMatch: {
12848
+ userId: resourceOwnerId
12834
12849
  }
12835
12850
  }
12836
- );
12837
- await ChatModel.updateMany(
12838
- {
12839
- active: true,
12840
- chatType: EnumChatType.RELATION,
12841
- deletedAt: null,
12851
+ },
12852
+ {
12853
+ $pull: {
12842
12854
  participants: {
12843
- $elemMatch: {
12844
- userId: normalizedOwnerId
12855
+ isAssociate: true,
12856
+ userEmail: {
12857
+ $in: associateEmails
12845
12858
  }
12846
12859
  }
12847
- },
12848
- {
12849
- $set: {
12850
- "participants.$[associate].active": false
12851
- }
12852
- },
12853
- {
12854
- arrayFilters: [
12855
- {
12856
- "associate.isAssociate": true,
12857
- "associate.userEmail": {
12858
- $in: associateEmails
12859
- }
12860
- }
12861
- ]
12862
12860
  }
12861
+ }
12862
+ );
12863
+ }
12864
+ async function removeAssociateFromResource({
12865
+ resourceId,
12866
+ resourceOwnerId,
12867
+ resourceType
12868
+ }) {
12869
+ try {
12870
+ const scope = {
12871
+ normalizedResourceId: resourceId.toString(),
12872
+ resourceType
12873
+ };
12874
+ const associateEmails = await getAssociateEmailsForResource(scope);
12875
+ if (associateEmails.length === 0) {
12876
+ return;
12877
+ }
12878
+ await pullAssociatesFromUsers(scope);
12879
+ await pullAssociateParticipantsFromChats(
12880
+ normalizeObjectId(resourceOwnerId),
12881
+ associateEmails
12863
12882
  );
12864
12883
  } catch (error) {
12865
12884
  console.error(