@timardex/cluemart-server-shared 1.0.163 → 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
@@ -12844,83 +12844,101 @@ async function updateAdStatuses() {
12844
12844
 
12845
12845
  // src/service/associate.ts
12846
12846
  var import_mongoose26 = __toESM(require("mongoose"));
12847
- async function removeAssociateFromResource({
12848
- resourceId,
12849
- 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,
12850
12852
  resourceType
12851
12853
  }) {
12852
- try {
12853
- const normalizedResourceId = resourceId.toString();
12854
- const normalizedOwnerId = typeof resourceOwnerId === "string" ? new import_mongoose26.default.Types.ObjectId(resourceOwnerId) : resourceOwnerId;
12855
- const usersWithAssociates = await UserModel.find(
12856
- {
12857
- associates: {
12858
- $elemMatch: {
12859
- resourceId: normalizedResourceId,
12860
- resourceType
12861
- }
12854
+ const usersWithAssociates = await UserModel.find(
12855
+ {
12856
+ associates: {
12857
+ $elemMatch: {
12858
+ resourceId: normalizedResourceId,
12859
+ resourceType
12862
12860
  }
12863
- },
12864
- {
12865
- associates: 1
12866
- }
12867
- ).lean();
12868
- const associateEmails = [
12869
- ...new Set(
12870
- usersWithAssociates.flatMap(
12871
- (user) => (user.associates ?? []).filter(
12872
- (associate) => associate.resourceId === normalizedResourceId && associate.resourceType === resourceType
12873
- ).map((associate) => associate.email)
12874
- )
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)
12875
12873
  )
12876
- ];
12877
- if (associateEmails.length === 0) {
12878
- return;
12879
- }
12880
- await UserModel.updateMany(
12881
- {
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: {
12882
12892
  associates: {
12883
- $elemMatch: {
12884
- resourceId: normalizedResourceId,
12885
- resourceType
12886
- }
12893
+ resourceId: normalizedResourceId,
12894
+ resourceType
12887
12895
  }
12888
- },
12889
- {
12890
- $pull: {
12891
- associates: {
12892
- resourceId: normalizedResourceId,
12893
- resourceType
12894
- }
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
12895
12909
  }
12896
12910
  }
12897
- );
12898
- await ChatModel.updateMany(
12899
- {
12900
- active: true,
12901
- chatType: EnumChatType.RELATION,
12902
- deletedAt: null,
12911
+ },
12912
+ {
12913
+ $pull: {
12903
12914
  participants: {
12904
- $elemMatch: {
12905
- userId: normalizedOwnerId
12915
+ isAssociate: true,
12916
+ userEmail: {
12917
+ $in: associateEmails
12906
12918
  }
12907
12919
  }
12908
- },
12909
- {
12910
- $set: {
12911
- "participants.$[associate].active": false
12912
- }
12913
- },
12914
- {
12915
- arrayFilters: [
12916
- {
12917
- "associate.isAssociate": true,
12918
- "associate.userEmail": {
12919
- $in: associateEmails
12920
- }
12921
- }
12922
- ]
12923
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
12924
12942
  );
12925
12943
  } catch (error) {
12926
12944
  console.error(