@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.cjs CHANGED
@@ -12332,7 +12332,8 @@ var schema11 = new MongooseSchema15(
12332
12332
  required: false,
12333
12333
  type: [resourceRelationsSchema]
12334
12334
  },
12335
- tags: { required: true, type: [String] }
12335
+ tags: { required: true, type: [String] },
12336
+ unregisteredVendorIds: { default: [], required: false, type: [String] }
12336
12337
  },
12337
12338
  { timestamps: true }
12338
12339
  );
@@ -12843,83 +12844,101 @@ async function updateAdStatuses() {
12843
12844
 
12844
12845
  // src/service/associate.ts
12845
12846
  var import_mongoose26 = __toESM(require("mongoose"));
12846
- async function removeAssociateFromResource({
12847
- resourceId,
12848
- resourceOwnerId,
12847
+ function normalizeObjectId(id) {
12848
+ return typeof id === "string" ? new import_mongoose26.default.Types.ObjectId(id) : id;
12849
+ }
12850
+ async function getAssociateEmailsForResource({
12851
+ normalizedResourceId,
12849
12852
  resourceType
12850
12853
  }) {
12851
- try {
12852
- const normalizedResourceId = resourceId.toString();
12853
- const normalizedOwnerId = typeof resourceOwnerId === "string" ? new import_mongoose26.default.Types.ObjectId(resourceOwnerId) : resourceOwnerId;
12854
- const usersWithAssociates = await UserModel.find(
12855
- {
12856
- associates: {
12857
- $elemMatch: {
12858
- resourceId: normalizedResourceId,
12859
- resourceType
12860
- }
12854
+ const usersWithAssociates = await UserModel.find(
12855
+ {
12856
+ associates: {
12857
+ $elemMatch: {
12858
+ resourceId: normalizedResourceId,
12859
+ resourceType
12861
12860
  }
12862
- },
12863
- {
12864
- associates: 1
12865
- }
12866
- ).lean();
12867
- const associateEmails = [
12868
- ...new Set(
12869
- usersWithAssociates.flatMap(
12870
- (user) => (user.associates ?? []).filter(
12871
- (associate) => associate.resourceId === normalizedResourceId && associate.resourceType === resourceType
12872
- ).map((associate) => associate.email)
12873
- )
12861
+ }
12862
+ },
12863
+ {
12864
+ associates: 1
12865
+ }
12866
+ ).lean();
12867
+ return [
12868
+ ...new Set(
12869
+ usersWithAssociates.flatMap(
12870
+ (user) => (user.associates ?? []).filter(
12871
+ (associate) => associate.resourceId === normalizedResourceId && associate.resourceType === resourceType
12872
+ ).map((associate) => associate.email)
12874
12873
  )
12875
- ];
12876
- if (associateEmails.length === 0) {
12877
- return;
12878
- }
12879
- await UserModel.updateMany(
12880
- {
12874
+ )
12875
+ ];
12876
+ }
12877
+ async function pullAssociatesFromUsers({
12878
+ normalizedResourceId,
12879
+ resourceType
12880
+ }) {
12881
+ await UserModel.updateMany(
12882
+ {
12883
+ associates: {
12884
+ $elemMatch: {
12885
+ resourceId: normalizedResourceId,
12886
+ resourceType
12887
+ }
12888
+ }
12889
+ },
12890
+ {
12891
+ $pull: {
12881
12892
  associates: {
12882
- $elemMatch: {
12883
- resourceId: normalizedResourceId,
12884
- resourceType
12885
- }
12893
+ resourceId: normalizedResourceId,
12894
+ resourceType
12886
12895
  }
12887
- },
12888
- {
12889
- $pull: {
12890
- associates: {
12891
- resourceId: normalizedResourceId,
12892
- resourceType
12893
- }
12896
+ }
12897
+ }
12898
+ );
12899
+ }
12900
+ async function pullAssociateParticipantsFromChats(resourceOwnerId, associateEmails) {
12901
+ await ChatModel.updateMany(
12902
+ {
12903
+ active: true,
12904
+ chatType: EnumChatType.RELATION,
12905
+ deletedAt: null,
12906
+ participants: {
12907
+ $elemMatch: {
12908
+ userId: resourceOwnerId
12894
12909
  }
12895
12910
  }
12896
- );
12897
- await ChatModel.updateMany(
12898
- {
12899
- active: true,
12900
- chatType: EnumChatType.RELATION,
12901
- deletedAt: null,
12911
+ },
12912
+ {
12913
+ $pull: {
12902
12914
  participants: {
12903
- $elemMatch: {
12904
- userId: normalizedOwnerId
12915
+ isAssociate: true,
12916
+ userEmail: {
12917
+ $in: associateEmails
12905
12918
  }
12906
12919
  }
12907
- },
12908
- {
12909
- $set: {
12910
- "participants.$[associate].active": false
12911
- }
12912
- },
12913
- {
12914
- arrayFilters: [
12915
- {
12916
- "associate.isAssociate": true,
12917
- "associate.userEmail": {
12918
- $in: associateEmails
12919
- }
12920
- }
12921
- ]
12922
12920
  }
12921
+ }
12922
+ );
12923
+ }
12924
+ async function removeAssociateFromResource({
12925
+ resourceId,
12926
+ resourceOwnerId,
12927
+ resourceType
12928
+ }) {
12929
+ try {
12930
+ const scope = {
12931
+ normalizedResourceId: resourceId.toString(),
12932
+ resourceType
12933
+ };
12934
+ const associateEmails = await getAssociateEmailsForResource(scope);
12935
+ if (associateEmails.length === 0) {
12936
+ return;
12937
+ }
12938
+ await pullAssociatesFromUsers(scope);
12939
+ await pullAssociateParticipantsFromChats(
12940
+ normalizeObjectId(resourceOwnerId),
12941
+ associateEmails
12923
12942
  );
12924
12943
  } catch (error) {
12925
12944
  console.error(