feat: add tagging alerts and sorting on tags

This commit is contained in:
2025-08-19 08:03:44 +05:30
parent 17a63f67a5
commit caea0e60e6
20 changed files with 260 additions and 49 deletions

View File

@@ -47,7 +47,7 @@ export async function listTaskHandler(req: FastifyRequest, res: FastifyReply) {
const queryParams = req.query as PageQueryParams;
try {
const taskList = await listTasks(queryParams, req.user.tenantId);
const taskList = await listTasks(queryParams, req.user);
return res.code(200).send(taskList);
} catch (err) {
return err;

View File

@@ -128,7 +128,7 @@ export async function taskRoutes(fastify: FastifyInstance) {
const { field } = req.params as { field: string };
try {
const uniqueValues = await getUniqueFields(field, "task", req.user);
const uniqueValues = await getUniqueFields(field, "tasks", req.user);
return res.code(200).send(uniqueValues);
} catch (err) {
return err;

View File

@@ -40,6 +40,7 @@ const taskSchema = new mongoose.Schema({
type: mongoose.Types.ObjectId,
ref: "user",
},
taggedUsers: Array,
});
export const taskFields = Object.keys(taskSchema.paths).filter(

View File

@@ -37,6 +37,7 @@ export async function createTask(
content: input.note,
},
task.pid,
"tasks",
user
);
}
@@ -80,16 +81,39 @@ export async function getTask(taskId: string, tenantId: string) {
.populate({ path: "assignedTo", select: "pid name avatar" });
}
export async function listTasks(params: PageQueryParams, tenantId: string) {
export async function listTasks(
params: PageQueryParams,
user: AuthenticatedUser
) {
const page = params.page || 1;
const pageSize = params.pageSize || 10;
const sortObj = getSortObject(params, taskFields);
const filterObj = getFilterObject(params) || [];
let taggedFilter = [];
if (sortObj.taggedUsers) {
taggedFilter = [
{
$addFields: {
taggedUsers: {
$filter: {
input: "$taggedUsers",
as: "user",
cond: { $eq: ["$$user.userId", user.userId] },
},
},
},
},
{ $match: { "taggedUsers.0": { $exists: true } } },
{ $sort: { "taggedUsers.taggedAt": sortObj.taggedUsers } },
];
}
const taskList = await taskModel.aggregate([
{
$match: { $and: [{ tenantId: tenantId }, ...filterObj] },
$match: { $and: [{ tenantId: user.tenantId }, ...filterObj] },
},
...taggedFilter,
{
$lookup: {
from: "users",