feat: add tagging alerts and sorting on tags
This commit is contained in:
@@ -1,12 +1,24 @@
|
||||
import { createAlert } from "../alert/alert.service";
|
||||
import { AuthenticatedUser } from "../auth";
|
||||
import { generateId } from "../utils/id";
|
||||
import { extractExpressions, modelMap } from "../utils/tags";
|
||||
import { CreateNoteInput, noteModel } from "./note.schema";
|
||||
|
||||
export async function createNote(
|
||||
input: CreateNoteInput,
|
||||
resourceId: string,
|
||||
resourceType: string,
|
||||
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(),
|
||||
@@ -16,6 +28,56 @@ export async function createNote(
|
||||
createdBy: user.userId,
|
||||
});
|
||||
|
||||
if (
|
||||
userIds.length > 0 &&
|
||||
[
|
||||
"permits",
|
||||
"processed",
|
||||
"rts",
|
||||
"tasks",
|
||||
"ctasks",
|
||||
"payments",
|
||||
"notifications",
|
||||
].includes(resourceType)
|
||||
) {
|
||||
const model = modelMap[resourceType];
|
||||
const obj = await model.findOne({ pid: resourceId });
|
||||
|
||||
console.log(obj.taggedUsers);
|
||||
|
||||
if (!obj.taggedUsers) {
|
||||
await model.updateOne(
|
||||
{ pid: resourceId },
|
||||
{ $set: { taggedUsers: taggedUsers } }
|
||||
);
|
||||
} else {
|
||||
for (const user of taggedUsers) {
|
||||
const userIndex = obj.taggedUsers.findIndex(
|
||||
(item) => item.userId == user.userId
|
||||
);
|
||||
console.log(userIndex);
|
||||
if (userIndex != -1) obj.taggedUsers[userIndex].taggedAt = new Date();
|
||||
else obj.taggedUsers.push(user);
|
||||
}
|
||||
|
||||
obj.markModified("taggedUsers");
|
||||
await obj.save();
|
||||
}
|
||||
|
||||
for (const id of userIds) {
|
||||
if (id == user.userId) continue;
|
||||
|
||||
await createAlert(
|
||||
user.tenantId,
|
||||
"You are tagged in a note",
|
||||
"user",
|
||||
id,
|
||||
resourceId,
|
||||
resourceType
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return newNote.populate({ path: "createdBy", select: "pid name avatar" });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user