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-1364: Completing of repeat tasks #5802

Merged
merged 128 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
346f3f9
Create button
alexd-bes Jun 20, 2024
beddfbe
Move modal to ui-components
alexd-bes Jun 20, 2024
6426a76
Move country selector to features folder
alexd-bes Jun 20, 2024
48d071b
Update country selector exports/imports
alexd-bes Jun 20, 2024
5d69b66
Update tupaia-pin.svg
alexd-bes Jun 20, 2024
446f2f9
Country selector on modal
alexd-bes Jun 20, 2024
b82596d
Move survey selector to features
alexd-bes Jun 20, 2024
c0cfc84
Move types
alexd-bes Jun 20, 2024
e19a287
Update survey list component to take care of fetching
alexd-bes Jun 20, 2024
694bf9a
Survey selector
alexd-bes Jun 20, 2024
8e54867
Move entity selector to features
alexd-bes Jun 20, 2024
57b534e
Fix types
alexd-bes Jun 20, 2024
3e564db
Entity selector
alexd-bes Jun 21, 2024
459ad7a
Styling entity selector
alexd-bes Jun 21, 2024
a103486
Merge branch 'rn-1335-tasks-dashboard' into rn-1336-create-task
alexd-bes Jun 21, 2024
0ab2903
Merge branch 'rn-1335-tasks-dashboard' into rn-1336-create-task
alexd-bes Jun 21, 2024
9b9a8af
Merge branch 'rn-1335-tasks-dashboard' into rn-1336-create-task
alexd-bes Jun 23, 2024
ec8f0fc
Due date
alexd-bes Jun 24, 2024
554d8fb
Merge branch 'rn-1335-tasks-dashboard' into rn-1336-create-task
alexd-bes Jun 24, 2024
1d68c15
Merge branch 'rn-1335-tasks-dashboard' into rn-1336-create-task
alexd-bes Jun 24, 2024
1a23d18
WIP
alexd-bes Jun 25, 2024
90a7f38
WIP
alexd-bes Jun 25, 2024
6d22957
assignee input
alexd-bes Jun 25, 2024
42bee49
Add loading state and save user id
alexd-bes Jun 25, 2024
8e7c924
Styling repeat scheduler
alexd-bes Jun 25, 2024
aed01e9
Comments placholder
alexd-bes Jun 25, 2024
c6e7e98
Styling
alexd-bes Jun 25, 2024
931a8cd
WIP
alexd-bes Jun 25, 2024
926805d
Create task route
alexd-bes Jul 5, 2024
4eaf58e
Create task workflow
alexd-bes Jul 5, 2024
a1459b4
Clear form when modal is reopened
alexd-bes Jul 5, 2024
7eb52c8
Merge branch 'rn-1335-tasks-dashboard' into rn-1336-create-task
alexd-bes Jul 5, 2024
b8a42d6
Update schemas.ts
alexd-bes Jul 5, 2024
9b89ff9
remove unused import
alexd-bes Jul 5, 2024
d8b9d64
Handle reset
alexd-bes Jul 5, 2024
176ac66
Fix datatrak tests
alexd-bes Jul 7, 2024
6ad35f9
Fix central server tests
alexd-bes Jul 7, 2024
e186d8f
Move modal to ui-components
alexd-bes Jul 7, 2024
62197cc
Merge branch 'rn-1336-modal-component' into rn-1336-create-task
alexd-bes Jul 7, 2024
8553b62
Merge branch 'rn-1335-tasks-dashboard' into rn-1336-modal-component
alexd-bes Jul 7, 2024
91d2cf1
Merge branch 'rn-1336-modal-component' into rn-1336-create-task
alexd-bes Jul 7, 2024
a9edba9
Remove unused import
alexd-bes Jul 7, 2024
ff82d5c
Remove duplicate file
alexd-bes Jul 7, 2024
18f6db8
Fix build
alexd-bes Jul 7, 2024
8a07448
Merge branch 'rn-1336-modal-component' into rn-1336-create-task
alexd-bes Jul 7, 2024
bc85374
Fix tests
alexd-bes Jul 7, 2024
f3ad9bd
Update packages/central-server/src/apiV2/utilities/constructNewRecord…
alexd-bes Jul 8, 2024
416339f
Fix error messages
alexd-bes Jul 8, 2024
cce3119
Handle search term in the BE
alexd-bes Jul 9, 2024
914fc10
Merge branch 'rn-1335-tasks-dashboard' into rn-1336-create-task
alexd-bes Jul 9, 2024
c1ce1ae
WIP
alexd-bes Jul 9, 2024
d2fa47b
WIP
alexd-bes Jul 9, 2024
394280d
Assignee Id modal
alexd-bes Jul 9, 2024
5709176
Working assignee
alexd-bes Jul 9, 2024
5d78a18
remove unused property
alexd-bes Jul 9, 2024
365a3ce
Fix timezone issue
alexd-bes Jul 9, 2024
986d7bc
Merge branch 'rn-1336-create-task' into rn-1358-assign-tasks
alexd-bes Jul 9, 2024
2b6bdb2
Fix date formatting of filter
alexd-bes Jul 9, 2024
5f12b3b
Merge branch 'rn-1336-create-task' into rn-1358-assign-tasks
alexd-bes Jul 9, 2024
c711bef
remove unused variable
alexd-bes Jul 9, 2024
24d24ea
Merge branch 'rn-1336-create-task' into rn-1358-assign-tasks
alexd-bes Jul 9, 2024
8c397d0
Remove unused variable
alexd-bes Jul 9, 2024
d9c6948
Fix casing
alexd-bes Jul 10, 2024
6671628
Default to showing countries if no primary entity question
alexd-bes Jul 10, 2024
e3a0212
Update AssigneeInput.tsx
alexd-bes Jul 10, 2024
6705438
Show loader when loading project and countries
alexd-bes Jul 10, 2024
a5cf0b1
Fix copy
alexd-bes Jul 10, 2024
f1662ba
Exclude internal users
alexd-bes Jul 10, 2024
16bbad6
Fix types
alexd-bes Jul 10, 2024
c2271d3
Change colour of icon in entity list
alexd-bes Jul 10, 2024
6c59547
Merge branch 'rn-1336-create-task' into rn-1358-assign-tasks
alexd-bes Jul 11, 2024
9776683
Merge branch 'rn-1335-tasks-dashboard' into rn-1336-create-task
alexd-bes Jul 11, 2024
5f41b7b
Merge branch 'rn-1336-create-task' into rn-1358-assign-tasks
alexd-bes Jul 11, 2024
4829bd3
Link to details page
alexd-bes Jul 11, 2024
915def1
Fix modal button types
alexd-bes Jul 11, 2024
6321cb3
Fix types
alexd-bes Jul 11, 2024
34527a8
Update handling of columns
alexd-bes Jul 11, 2024
cecd49e
get task route
alexd-bes Jul 11, 2024
948df10
WIP
alexd-bes Jul 11, 2024
54a09d7
Link to survey for incomplete tasks
alexd-bes Jul 11, 2024
d254d8c
Handle unloaded task
alexd-bes Jul 11, 2024
4c30c44
Task metadata section
alexd-bes Jul 11, 2024
e4b873f
Merge branch 'rn-1336-create-task' into rn-1329-tasks-details
alexd-bes Jul 11, 2024
c422d2f
Merge branch 'rn-1358-assign-tasks' into rn-1329-tasks-details
alexd-bes Jul 11, 2024
38a2fc5
Fix merge issue
alexd-bes Jul 11, 2024
a87f862
WIP
alexd-bes Jul 11, 2024
8fae25c
Update schemas.ts
alexd-bes Jul 11, 2024
33c47f6
feat(datatrakWeb): RN-1358: Assign tasks from dashboard (#5770)
alexd-bes Jul 11, 2024
a86b925
Merge branch 'epic-tasks' into rn-1329-tasks-details
alexd-bes Jul 11, 2024
2dfa0e3
Merge branch 'epic-tasks' into rn-1329-tasks-details
alexd-bes Jul 11, 2024
1221df2
Fix merge error
alexd-bes Jul 11, 2024
ad55ba3
Fix styles and types
alexd-bes Jul 11, 2024
9ce4c6c
Styling
alexd-bes Jul 11, 2024
b2a98e7
Fix multi re-render
alexd-bes Jul 11, 2024
cb50d6a
Comments placeholder
alexd-bes Jul 11, 2024
ca6dd2a
Add buttons
alexd-bes Jul 11, 2024
a78456b
Submit changes
alexd-bes Jul 11, 2024
e616dd2
Clear changes
alexd-bes Jul 11, 2024
eb9d3e7
Merge branch 'epic-tasks' into rn-1329-tasks-details
alexd-bes Jul 12, 2024
478c16e
Fix merge issues
alexd-bes Jul 12, 2024
d22a765
Merge branch 'epic-tasks' into rn-1329-tasks-details
alexd-bes Jul 12, 2024
9b43a4e
Disable inputs when task is completed or cancelled
alexd-bes Jul 12, 2024
9ed7d5c
responsive styling
alexd-bes Jul 12, 2024
0b1a5c9
Add cancel menu
alexd-bes Jul 12, 2024
d595ced
Merge branch 'epic-tasks' into rn-1329-tasks-details
alexd-bes Jul 14, 2024
29fad72
Generate types
alexd-bes Jul 14, 2024
6e0777f
Fix loading styling
alexd-bes Jul 14, 2024
9559ee6
Remove unused var
alexd-bes Jul 14, 2024
b77f752
Handle onSuccess of edit task
alexd-bes Jul 14, 2024
e191fc0
Display modal
alexd-bes Jul 14, 2024
2de2a3d
Survey response modal
alexd-bes Jul 15, 2024
0ec3c2c
Handle isResponseScreen flag
alexd-bes Jul 15, 2024
903833f
Error handling
alexd-bes Jul 15, 2024
fd384cb
Error handling
alexd-bes Jul 15, 2024
4022c23
Fix tests
alexd-bes Jul 15, 2024
5ff8e66
Remove country and survey code from url
alexd-bes Jul 16, 2024
79fc7bb
Fix imports
alexd-bes Jul 16, 2024
2b551ba
feat(datatrakWeb): RN-1339: Link survey responses to tasks (#5788)
alexd-bes Jul 16, 2024
d6ce23a
Merge branch 'epic-tasks' into rn-1339-survey-response-to-modal
alexd-bes Jul 16, 2024
5bc63ed
Fix type
alexd-bes Jul 16, 2024
212dfcc
feat(datatrakWeb): RN-1339: View completed survey response from task …
alexd-bes Jul 18, 2024
329fe05
Update TaskCompletionHandler.test.js
tcaiger Jul 19, 2024
f424234
handle repeatSchedule tasks
tcaiger Jul 19, 2024
595f0c3
set up duplicate test
tcaiger Jul 19, 2024
bc4d02b
Merge branch 'epic-tasks' into rn-1364-completing-repeat-tasks
tcaiger Jul 22, 2024
a5c3dc5
clean up PR
tcaiger Jul 22, 2024
e669db3
Merge branch 'epic-tasks' into rn-1364-completing-repeat-tasks
tcaiger Jul 24, 2024
c3ae583
allow null repeating tasks
tcaiger Jul 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,51 @@ describe('TaskCompletionHandler', () => {
await assertTaskStatus(task.id, 'to_do', null);
});
});

describe('Repeating tasks', () => {
it('updating a survey response for a repeating task creates a new completed task', async () => {
const samoa = await findOrCreateDummyRecord(models.entity, { code: 'WS' });
const repeatTask = await findOrCreateDummyRecord(models.task, {
entity_id: samoa.id,
survey_id: SURVEY.id,
created_at: '2024-07-08',
status: null,
repeat_schedule: {
frequency: 'daily',
},
});

const responses = await createResponses([{ entity_id: samoa.id, date: '2024-07-20' }]);
// Check that the repeat task has stayed as is
await assertTaskStatus(repeatTask.id, null, null);

const newTask = await models.task.findOne({
survey_response_id: responses[0],
entity_id: samoa.id,
});
await assertTaskStatus(newTask.id, 'completed', responses[0]);
});

it('updating a survey response for a repeating task with status do_do creates a new completed task', async () => {
const fiji = await findOrCreateDummyRecord(models.entity, { code: 'FJ' });
const repeatTask = await findOrCreateDummyRecord(models.task, {
entity_id: fiji.id,
survey_id: SURVEY.id,
created_at: '2024-07-08',
status: 'to_do',
repeat_schedule: {
frequency: 'daily',
},
});

const responses = await createResponses([{ entity_id: fiji.id, date: '2024-07-20' }]);
// Check that the repeat task has stayed as is
await assertTaskStatus(repeatTask.id, 'to_do', null);
const newTask = await models.task.findOne({
survey_response_id: responses[0],
entity_id: fiji.id,
});
await assertTaskStatus(newTask.id, 'completed', responses[0]);
});
});
});
41 changes: 35 additions & 6 deletions packages/database/src/changeHandlers/TaskCompletionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { getUniqueEntries } from '@tupaia/utils';
import { ChangeHandler } from './ChangeHandler';
import { QUERY_CONJUNCTIONS } from '../TupaiaDatabase';

function hasValidRepeatSchedule(repeatSchedule) {
return typeof repeatSchedule === 'object' && Object.keys(repeatSchedule).length > 0;
}

export class TaskCompletionHandler extends ChangeHandler {
constructor(models) {
super(models, 'task-completion-handler');
Expand Down Expand Up @@ -42,9 +46,14 @@ export class TaskCompletionHandler extends ChangeHandler {
})),
);

const tasks = await this.models.task.find({
// only fetch tasks that have a status of 'to_do'
return this.models.task.find({
status: 'to_do',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realised this status will need to be updated to also fetch when status is null, since repeating tasks will have a null status

[QUERY_CONJUNCTIONS.OR]: {
status: {
comparator: 'IS',
comparisonValue: null,
},
},
[QUERY_CONJUNCTIONS.RAW]: {
sql: `${surveyIdAndEntityIdPairs
.map(() => `(survey_id = ? AND entity_id = ? AND created_at <= ?)`)
Expand All @@ -56,11 +65,9 @@ export class TaskCompletionHandler extends ChangeHandler {
]),
},
});

return tasks;
}

async handleChanges(transactingModels, changedResponses) {
async handleChanges(_transactingModels, changedResponses) {
// if there are no changed responses, we don't need to do anything
if (changedResponses.length === 0) return;
const tasksToUpdate = await this.fetchTasksForSurveyResponses(changedResponses);
Expand All @@ -69,7 +76,14 @@ export class TaskCompletionHandler extends ChangeHandler {
if (tasksToUpdate.length === 0) return;

for (const task of tasksToUpdate) {
const { survey_id: surveyId, entity_id: entityId, created_at: createdAt, id } = task;
const {
survey_id: surveyId,
entity_id: entityId,
created_at: createdAt,
repeat_schedule: repeatSchedule,
assignee_id: assigneeId,
id,
} = task;
const matchingSurveyResponse = changedResponses.find(
surveyResponse =>
surveyResponse.survey_id === surveyId &&
Expand All @@ -79,6 +93,21 @@ export class TaskCompletionHandler extends ChangeHandler {

if (!matchingSurveyResponse) continue;

if (hasValidRepeatSchedule(repeatSchedule)) {
// Create a new task with the same details as the current task and mark as completed
// It is theoretically possible that more than one task could be created for a repeating
// task in a reporting period which is ok from a business point of view
await this.models.task.create({
assignee_id: assigneeId,
survey_id: surveyId,
entity_id: entityId,
repeat_schedule: repeatSchedule,
status: 'completed',
survey_response_id: matchingSurveyResponse.id,
});
continue;
}

await this.models.task.updateById(id, {
status: 'completed',
survey_response_id: matchingSurveyResponse.id,
Expand Down
Loading