@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/service/index.cjs
CHANGED
|
@@ -12806,83 +12806,101 @@ async function updateAdStatuses() {
|
|
|
12806
12806
|
|
|
12807
12807
|
// src/service/associate.ts
|
|
12808
12808
|
var import_mongoose26 = __toESM(require("mongoose"));
|
|
12809
|
-
|
|
12810
|
-
|
|
12811
|
-
|
|
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
|
-
|
|
12815
|
-
|
|
12816
|
-
|
|
12817
|
-
|
|
12818
|
-
|
|
12819
|
-
|
|
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
|
-
|
|
12828
|
-
|
|
12829
|
-
|
|
12830
|
-
|
|
12831
|
-
|
|
12832
|
-
|
|
12833
|
-
|
|
12834
|
-
|
|
12835
|
-
|
|
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
|
-
|
|
12840
|
-
|
|
12841
|
-
|
|
12842
|
-
|
|
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
|
-
|
|
12846
|
-
|
|
12847
|
-
resourceType
|
|
12848
|
-
}
|
|
12855
|
+
resourceId: normalizedResourceId,
|
|
12856
|
+
resourceType
|
|
12849
12857
|
}
|
|
12850
|
-
}
|
|
12851
|
-
|
|
12852
|
-
|
|
12853
|
-
|
|
12854
|
-
|
|
12855
|
-
|
|
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
|
-
|
|
12861
|
-
{
|
|
12862
|
-
active: true,
|
|
12863
|
-
chatType: EnumChatType.RELATION,
|
|
12864
|
-
deletedAt: null,
|
|
12873
|
+
},
|
|
12874
|
+
{
|
|
12875
|
+
$pull: {
|
|
12865
12876
|
participants: {
|
|
12866
|
-
|
|
12867
|
-
|
|
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(
|