Skip to content

Commit a1b9095

Browse files
authored
Fix tag hides task (#586)
* Fix limited tags fetch * fixup! Fix limited tags fetch
1 parent 3b30b90 commit a1b9095

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

backend/routes/tasks/queries/query-builders.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { Task, Tag, Project, sequelize } = require('../../../models');
2-
const { Op } = require('sequelize');
2+
const { Op, QueryTypes } = require('sequelize');
33
const permissionsService = require('../../../services/permissionsService');
44
const {
55
getSafeTimezone,
@@ -222,10 +222,7 @@ async function filterTasksByParams(
222222
}
223223
}
224224

225-
if (params.tag) {
226-
includeClause[0].where = { name: params.tag };
227-
includeClause[0].required = true;
228-
}
225+
let tagFilteredTaskIds = null;
229226

230227
if (params.priority) {
231228
whereClause.priority = Task.getPriorityValue(params.priority);
@@ -268,6 +265,33 @@ async function filterTasksByParams(
268265
}
269266
}
270267

268+
if (params.tag) {
269+
const taggedTaskIds = await sequelize.query(
270+
`SELECT DISTINCT tasks_tags.task_id
271+
FROM tasks_tags
272+
INNER JOIN tags ON tags.id = tasks_tags.tag_id
273+
WHERE tags.name = :tagName`,
274+
{
275+
replacements: { tagName: params.tag },
276+
type: QueryTypes.SELECT,
277+
}
278+
);
279+
280+
tagFilteredTaskIds = taggedTaskIds.map((row) => row.task_id);
281+
282+
// No tasks found with this tag - return early to avoid unnecessary query
283+
if (tagFilteredTaskIds.length === 0) {
284+
return [];
285+
}
286+
}
287+
288+
if (tagFilteredTaskIds) {
289+
whereClause.id = {
290+
...(whereClause.id || {}),
291+
[Op.in]: tagFilteredTaskIds,
292+
};
293+
}
294+
271295
const finalWhereClause = {
272296
[Op.and]: [ownedOrShared, whereClause],
273297
};

backend/tests/integration/tasks.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,27 @@ describe('Tasks Routes', () => {
284284
expect(response.body.Tags.map((t) => t.name)).toContain('work');
285285
expect(response.body.Tags.map((t) => t.name)).toContain('urgent');
286286
});
287+
288+
it('should return all tags when filtering by a specific tag', async () => {
289+
const taskData = {
290+
name: 'Task with multiple tags',
291+
tags: [{ name: 'alpha' }, { name: 'beta' }],
292+
};
293+
294+
const createResponse = await agent.post('/api/task').send(taskData);
295+
expect(createResponse.status).toBe(201);
296+
297+
const response = await agent.get('/api/tasks?tag=alpha');
298+
299+
expect(response.status).toBe(200);
300+
expect(response.body.tasks.length).toBe(1);
301+
302+
const [task] = response.body.tasks;
303+
const tagNames = (task.tags || []).map((t) => t.name);
304+
305+
expect(tagNames).toEqual(expect.arrayContaining(['alpha', 'beta']));
306+
expect(tagNames.length).toBe(2);
307+
});
287308
});
288309

289310
describe('Subtasks filtering', () => {

0 commit comments

Comments
 (0)