@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.
@@ -12806,83 +12806,101 @@ async function updateAdStatuses() {
12806
12806
 
12807
12807
  // src/service/associate.ts
12808
12808
  var import_mongoose26 = __toESM(require("mongoose"));
12809
- async function removeAssociateFromResource({
12810
- resourceId,
12811
- resourceOwnerId,
12809
+ function normalizeObjectId(id) {
12810
+ return typeof id === "string" ? new import_mongoose26.default.Types.ObjectId(id) : id;
12811
+ }
12812
+ async function getAssociateEmailsForResource({
12813
+ normalizedResourceId,
12812
12814
  resourceType
12813
12815
  }) {
12814
- try {
12815
- const normalizedResourceId = resourceId.toString();
12816
- const normalizedOwnerId = typeof resourceOwnerId === "string" ? new import_mongoose26.default.Types.ObjectId(resourceOwnerId) : resourceOwnerId;
12817
- const usersWithAssociates = await UserModel.find(
12818
- {
12819
- associates: {
12820
- $elemMatch: {
12821
- resourceId: normalizedResourceId,
12822
- resourceType
12823
- }
12816
+ const usersWithAssociates = await UserModel.find(
12817
+ {
12818
+ associates: {
12819
+ $elemMatch: {
12820
+ resourceId: normalizedResourceId,
12821
+ resourceType
12824
12822
  }
12825
- },
12826
- {
12827
- associates: 1
12828
- }
12829
- ).lean();
12830
- const associateEmails = [
12831
- ...new Set(
12832
- usersWithAssociates.flatMap(
12833
- (user) => (user.associates ?? []).filter(
12834
- (associate) => associate.resourceId === normalizedResourceId && associate.resourceType === resourceType
12835
- ).map((associate) => associate.email)
12836
- )
12823
+ }
12824
+ },
12825
+ {
12826
+ associates: 1
12827
+ }
12828
+ ).lean();
12829
+ return [
12830
+ ...new Set(
12831
+ usersWithAssociates.flatMap(
12832
+ (user) => (user.associates ?? []).filter(
12833
+ (associate) => associate.resourceId === normalizedResourceId && associate.resourceType === resourceType
12834
+ ).map((associate) => associate.email)
12837
12835
  )
12838
- ];
12839
- if (associateEmails.length === 0) {
12840
- return;
12841
- }
12842
- await UserModel.updateMany(
12843
- {
12836
+ )
12837
+ ];
12838
+ }
12839
+ async function pullAssociatesFromUsers({
12840
+ normalizedResourceId,
12841
+ resourceType
12842
+ }) {
12843
+ await UserModel.updateMany(
12844
+ {
12845
+ associates: {
12846
+ $elemMatch: {
12847
+ resourceId: normalizedResourceId,
12848
+ resourceType
12849
+ }
12850
+ }
12851
+ },
12852
+ {
12853
+ $pull: {
12844
12854
  associates: {
12845
- $elemMatch: {
12846
- resourceId: normalizedResourceId,
12847
- resourceType
12848
- }
12855
+ resourceId: normalizedResourceId,
12856
+ resourceType
12849
12857
  }
12850
- },
12851
- {
12852
- $pull: {
12853
- associates: {
12854
- resourceId: normalizedResourceId,
12855
- resourceType
12856
- }
12858
+ }
12859
+ }
12860
+ );
12861
+ }
12862
+ async function pullAssociateParticipantsFromChats(resourceOwnerId, associateEmails) {
12863
+ await ChatModel.updateMany(
12864
+ {
12865
+ active: true,
12866
+ chatType: EnumChatType.RELATION,
12867
+ deletedAt: null,
12868
+ participants: {
12869
+ $elemMatch: {
12870
+ userId: resourceOwnerId
12857
12871
  }
12858
12872
  }
12859
- );
12860
- await ChatModel.updateMany(
12861
- {
12862
- active: true,
12863
- chatType: EnumChatType.RELATION,
12864
- deletedAt: null,
12873
+ },
12874
+ {
12875
+ $pull: {
12865
12876
  participants: {
12866
- $elemMatch: {
12867
- userId: normalizedOwnerId
12877
+ isAssociate: true,
12878
+ userEmail: {
12879
+ $in: associateEmails
12868
12880
  }
12869
12881
  }
12870
- },
12871
- {
12872
- $set: {
12873
- "participants.$[associate].active": false
12874
- }
12875
- },
12876
- {
12877
- arrayFilters: [
12878
- {
12879
- "associate.isAssociate": true,
12880
- "associate.userEmail": {
12881
- $in: associateEmails
12882
- }
12883
- }
12884
- ]
12885
12882
  }
12883
+ }
12884
+ );
12885
+ }
12886
+ async function removeAssociateFromResource({
12887
+ resourceId,
12888
+ resourceOwnerId,
12889
+ resourceType
12890
+ }) {
12891
+ try {
12892
+ const scope = {
12893
+ normalizedResourceId: resourceId.toString(),
12894
+ resourceType
12895
+ };
12896
+ const associateEmails = await getAssociateEmailsForResource(scope);
12897
+ if (associateEmails.length === 0) {
12898
+ return;
12899
+ }
12900
+ await pullAssociatesFromUsers(scope);
12901
+ await pullAssociateParticipantsFromChats(
12902
+ normalizeObjectId(resourceOwnerId),
12903
+ associateEmails
12886
12904
  );
12887
12905
  } catch (error) {
12888
12906
  console.error(