Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(datatrak): RN-1343: Task dashboard filter settings #5757

Merged
merged 26 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/central-server/src/apiV2/GETHandler/GETHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class GETHandler extends CRUDHandler {
}

async getDbQueryOptions() {
const { sort: sortString, distinct = false } = this.req.query;
const { sort: sortString, rawSort, distinct = false } = this.req.query;

// set up db query options
const columns = await this.getProcessedColumns();
Expand All @@ -73,7 +73,7 @@ export class GETHandler extends CRUDHandler {
const { limit, page } = this.getPaginationParameters();
const offset = limit * page;

const dbQueryOptions = { multiJoin, columns, sort, distinct, limit, offset };
const dbQueryOptions = { multiJoin, columns, sort, rawSort, distinct, limit, offset };

// add any user requested sorting to the start of the sort clause
if (sortString) {
Expand Down
10 changes: 7 additions & 3 deletions packages/central-server/src/apiV2/tasks/GETTasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ export class GETTasks extends GETHandler {
return {
...restOfOptions,
// Strip table prefix from `task_status` and `assignee_name` as these are customColumns
sort: sort.map(s =>
s.replace('task.task_status', 'task_status').replace('task.assignee_name', 'assignee_name'),
),
sort:
!restOfOptions.rawSort &&
sort?.map(s =>
s
.replace('task.task_status', 'task_status')
.replace('task.assignee_name', 'assignee_name'),
),
// Appending the multi-join from the Record class so that we can fetch the `task_status` and `assignee_name`
multiJoin: mergeMultiJoin(multiJoin, this.models.task.DatabaseRecordClass.joins),
};
Expand Down
1 change: 1 addition & 0 deletions packages/datatrak-web-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@tupaia/types": "workspace:*",
"@tupaia/utils": "workspace:*",
"camelcase-keys": "^6.2.2",
"cookie": "^0.6.0",
"express": "^4.19.2",
"lodash.groupby": "^4.6.0",
"lodash.keyby": "^4.6.0",
Expand Down
178 changes: 178 additions & 0 deletions packages/datatrak-web-server/src/__tests__/TasksRoute.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* Tupaia
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
*/
import { NextFunction, Request } from 'express';
import { TasksRoute } from '../routes';

const mockFunc = jest.fn(() => []);

const makeMockRequest = (overwrites: any) => {
return {
headers: {
// defaults to make the tests simpler
cookie: 'show_completed_tasks=true;show_cancelled_tasks=true;all_assignees_tasks=true',
},
query: {},
models: {
database: {
count: () => null,
},
task: {
customColumnSelectors: {},
DatabaseRecordClass: { joins: null },
},
},
ctx: {
services: {
central: {
fetchResources: mockFunc,
getUser: () => ({ id: 'test' }),
},
},
},
...overwrites,
};
};

const mockResponse: any = {
json: jest.fn(),
status: jest.fn(),
};

const mockNext: NextFunction = jest.fn();

class TestableTaskRoute extends TasksRoute {
public constructor(params: any) {
const req = makeMockRequest(params);
// @ts-ignore
super(req, mockResponse, mockNext);
}
}

describe('TaskRoute', () => {
describe('should format filters correctly', () => {
const testData = [
[
'Default filter settings',
{
headers: {
cookie: 'show_completed_tasks=true;show_cancelled_tasks=true;all_assignees_tasks=true',
},
},
{
filter: {},
},
],
[
'Partial text filter',
{
query: {
filters: [
{
id: 'survey.name',
value: 'a',
},
],
},
},
{
filter: { 'survey.name': { comparator: 'ilike', comparisonValue: 'a%' } },
},
],
[
'Status filter',
{
query: {
filters: [
{
id: 'task_status',
value: 'to_do',
},
],
},
},
{
filter: {
task_status: 'to_do',
},
},
],
[
'All completed tasks setting false',
{
headers: {
cookie: 'show_completed_tasks=false;show_cancelled_tasks=true;all_assignees_tasks=true',
},
},
{
filter: {
task_status: { comparator: 'NOT IN', comparisonValue: ['completed'] },
},
},
],
[
'All assignee filter setting false',
{
headers: {
cookie: 'show_completed_tasks=true;show_cancelled_tasks=true;all_assignees_tasks=false',
},
},
{
filter: {
assignee_id: 'test',
},
},
],
[
'All completed tasks setting false and completed status filter',
{
headers: {
cookie: 'show_completed_tasks=false;show_cancelled_tasks=true;all_assignees_tasks=true',
},
query: {
filters: [
{
id: 'task_status',
value: 'completed',
},
],
},
},
{
filter: {
task_status: 'completed',
},
},
],
[
'All completed tasks setting false and to_do status filter',
{
headers: {
cookie: 'show_completed_tasks=false;show_cancelled_tasks=true;all_assignees_tasks=true',
},
query: {
filters: [
{
id: 'task_status',
value: 'to_do',
},
],
},
},
{
filter: {
task_status: 'to_do',
},
},
],
];

// @ts-ignore
it.each(testData)('%s', async (_, filters, result) => {
const route = new TestableTaskRoute(filters);
await route.buildResponse();
expect(mockFunc).toHaveBeenCalledWith('tasks', expect.objectContaining(result));
});
});
});
Loading