@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 +83 -65
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +83 -65
- package/dist/index.mjs.map +1 -1
- package/dist/service/index.cjs +83 -65
- package/dist/service/index.cjs.map +1 -1
- package/dist/service/index.mjs +82 -64
- package/dist/service/index.mjs.map +1 -1
- package/package.json +1 -1
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
|
-
|
|
12788
|
-
|
|
12789
|
-
|
|
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
|
-
|
|
12793
|
-
|
|
12794
|
-
|
|
12795
|
-
|
|
12796
|
-
|
|
12797
|
-
|
|
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
|
-
|
|
12806
|
-
|
|
12807
|
-
|
|
12808
|
-
|
|
12809
|
-
|
|
12810
|
-
|
|
12811
|
-
|
|
12812
|
-
|
|
12813
|
-
|
|
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
|
-
|
|
12818
|
-
|
|
12819
|
-
|
|
12820
|
-
|
|
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
|
-
|
|
12824
|
-
|
|
12825
|
-
resourceType
|
|
12826
|
-
}
|
|
12833
|
+
resourceId: normalizedResourceId,
|
|
12834
|
+
resourceType
|
|
12827
12835
|
}
|
|
12828
|
-
}
|
|
12829
|
-
|
|
12830
|
-
|
|
12831
|
-
|
|
12832
|
-
|
|
12833
|
-
|
|
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
|
-
|
|
12839
|
-
{
|
|
12840
|
-
active: true,
|
|
12841
|
-
chatType: EnumChatType.RELATION,
|
|
12842
|
-
deletedAt: null,
|
|
12851
|
+
},
|
|
12852
|
+
{
|
|
12853
|
+
$pull: {
|
|
12843
12854
|
participants: {
|
|
12844
|
-
|
|
12845
|
-
|
|
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(
|