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