feat: add taggedOrgs feature
This commit is contained in:
@@ -8,17 +8,10 @@ export async function createNote(
|
||||
input: CreateNoteInput,
|
||||
resourceId: string,
|
||||
resourceType: string,
|
||||
user: AuthenticatedUser
|
||||
user: AuthenticatedUser,
|
||||
) {
|
||||
const userIds = extractExpressions(input.content);
|
||||
|
||||
const taggedUsers = userIds.map((item) => {
|
||||
return {
|
||||
userId: item,
|
||||
taggedAt: new Date(),
|
||||
};
|
||||
});
|
||||
|
||||
const newNote = await noteModel.create({
|
||||
tenantId: user.tenantId,
|
||||
pid: generateId(),
|
||||
@@ -43,44 +36,86 @@ export async function createNote(
|
||||
const model = modelMap[resourceType];
|
||||
const obj = await model.findOne({ pid: resourceId });
|
||||
|
||||
if (!obj.taggedUsers) {
|
||||
await model.updateOne(
|
||||
{ pid: resourceId },
|
||||
{
|
||||
$set: { taggedUsers: taggedUsers },
|
||||
$addToSet: { assignedTo: userIds[0] },
|
||||
}
|
||||
const orgs = [];
|
||||
userIds.forEach((item) => {
|
||||
if (item == "client" && obj.client)
|
||||
orgs.push({ orgId: obj.client.toString(), taggedAt: new Date() });
|
||||
if (item == "agent")
|
||||
orgs.push({ orgId: process.env.SUNCOAST_ID, taggedAt: new Date() });
|
||||
});
|
||||
|
||||
const taggedUsers = userIds
|
||||
.filter((item) => !["client", "agent"].includes(item))
|
||||
.map((item) => {
|
||||
return {
|
||||
userId: item,
|
||||
taggedAt: new Date(),
|
||||
};
|
||||
});
|
||||
|
||||
for (const org of orgs) {
|
||||
if (!obj.taggedOrgs) obj.taggedOrgs = [];
|
||||
|
||||
const orgIndex = obj.taggedOrgs.findIndex(
|
||||
(item) => item.orgId == org.orgId,
|
||||
);
|
||||
} else {
|
||||
for (const user of taggedUsers) {
|
||||
const userIndex = obj.taggedUsers.findIndex(
|
||||
(item) => item.userId == user.userId
|
||||
);
|
||||
if (userIndex != -1) obj.taggedUsers[userIndex].taggedAt = new Date();
|
||||
else obj.taggedUsers.push(user);
|
||||
}
|
||||
|
||||
const assignee = obj.assignedTo.find(
|
||||
(item) => item.toString() == userIds[0]
|
||||
);
|
||||
|
||||
if (!assignee) obj.assignedTo.push(userIds[0]);
|
||||
|
||||
obj.markModified("taggedUsers", "assignedTo");
|
||||
await obj.save();
|
||||
if (orgIndex != -1) obj.taggedOrgs[orgIndex].taggedAt = new Date();
|
||||
else obj.taggedOrgs.push(org);
|
||||
}
|
||||
|
||||
for (const id of userIds) {
|
||||
if (id == user.userId) continue;
|
||||
for (const user of taggedUsers) {
|
||||
if (!obj.taggedUsers) obj.taggedUsers = [];
|
||||
|
||||
await createAlert(
|
||||
user.tenantId,
|
||||
"You are tagged in a note",
|
||||
"user",
|
||||
id,
|
||||
resourceId,
|
||||
resourceType
|
||||
const userIndex = obj.taggedUsers.findIndex(
|
||||
(item) => item.userId == user.userId,
|
||||
);
|
||||
if (userIndex != -1) obj.taggedUsers[userIndex].taggedAt = new Date();
|
||||
else obj.taggedUsers.push(user);
|
||||
}
|
||||
|
||||
if (taggedUsers.length > 0) {
|
||||
const assignee = obj.assignedTo.find(
|
||||
(item) => item.toString() == taggedUsers[0].userId,
|
||||
);
|
||||
|
||||
if (!assignee) obj.assignedTo.push(taggedUsers[0].userId);
|
||||
}
|
||||
|
||||
obj.markModified("taggedUsers", "assignedTo", "taggedOrgs");
|
||||
await obj.save();
|
||||
|
||||
if (taggedUsers.length > 0) {
|
||||
for (const taggedUser of taggedUsers) {
|
||||
if (taggedUser.userId == user.userId) continue;
|
||||
|
||||
await createAlert(
|
||||
user.tenantId,
|
||||
"You are tagged in a note",
|
||||
"user",
|
||||
taggedUser.userId,
|
||||
resourceId,
|
||||
resourceType,
|
||||
{
|
||||
createdBy: user.userId,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (orgs.length > 0) {
|
||||
for (const org of orgs) {
|
||||
await createAlert(
|
||||
user.tenantId,
|
||||
`Your organization is tagged in a note`,
|
||||
"team",
|
||||
org.orgId,
|
||||
resourceId,
|
||||
resourceType,
|
||||
{
|
||||
createdBy: user.userId,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +126,7 @@ export async function updateNote(
|
||||
input: CreateNoteInput,
|
||||
resourceId: string,
|
||||
noteId: string,
|
||||
tenantId: string
|
||||
tenantId: string,
|
||||
) {
|
||||
return await noteModel
|
||||
.findOneAndUpdate(
|
||||
@@ -103,7 +138,7 @@ export async function updateNote(
|
||||
],
|
||||
},
|
||||
{ ...input },
|
||||
{ new: true }
|
||||
{ new: true },
|
||||
)
|
||||
.populate({ path: "createdBy", select: "pid name avatar" });
|
||||
}
|
||||
@@ -147,7 +182,7 @@ export async function listNotes(resourceId: string, tenantId: string) {
|
||||
export async function deleteNote(
|
||||
resourceId: string,
|
||||
noteId: string,
|
||||
tenantId: string
|
||||
tenantId: string,
|
||||
) {
|
||||
return await noteModel.deleteOne({
|
||||
$and: [{ pid: noteId }, { tenantId: tenantId }, { resourceId: resourceId }],
|
||||
|
||||
Reference in New Issue
Block a user