Synced isdue/next due when user joins challenge (#9779)

This commit is contained in:
Keith Holliday
2018-01-12 10:16:51 -06:00
committed by GitHub
parent 0afe797bae
commit 4c7157807b
3 changed files with 12 additions and 8 deletions

View File

@@ -101,19 +101,21 @@ describe('POST /challenges/:challengeId/join', () => {
}); });
it('syncs challenge tasks to joining user', async () => { it('syncs challenge tasks to joining user', async () => {
let taskText = 'A challenge task text'; const taskText = 'A challenge task text';
await groupLeader.post(`/tasks/challenge/${challenge._id}`, [ await groupLeader.post(`/tasks/challenge/${challenge._id}`, [
{type: 'habit', text: taskText}, {type: 'daily', text: taskText},
]); ]);
await authorizedUser.post(`/challenges/${challenge._id}/join`); await authorizedUser.post(`/challenges/${challenge._id}/join`);
let tasks = await authorizedUser.get('/tasks/user');
let tasksTexts = tasks.map((task) => { const tasks = await authorizedUser.get('/tasks/user');
return task.text; const syncedTask = tasks.find((task) => {
return task.text === taskText;
}); });
expect(tasksTexts).to.include(taskText); expect(syncedTask.text).to.eql(taskText);
expect(syncedTask.isDue).to.exist;
expect(syncedTask.nextDue).to.exist;
}); });
it('adds challenge tag to user tags', async () => { it('adds challenge tag to user tags', async () => {

View File

@@ -45,6 +45,7 @@ export function setNextDue (task, user, dueDateOption) {
let optionsForShouldDo = user.preferences.toObject(); let optionsForShouldDo = user.preferences.toObject();
optionsForShouldDo.now = now; optionsForShouldDo.now = now;
task.isDue = shared.shouldDo(dateTaskIsDue, task, optionsForShouldDo); task.isDue = shared.shouldDo(dateTaskIsDue, task, optionsForShouldDo);
optionsForShouldDo.nextDue = true; optionsForShouldDo.nextDue = true;
let nextDue = shared.shouldDo(dateTaskIsDue, task, optionsForShouldDo); let nextDue = shared.shouldDo(dateTaskIsDue, task, optionsForShouldDo);
if (nextDue && nextDue.length > 0) { if (nextDue && nextDue.length > 0) {

View File

@@ -14,7 +14,7 @@ import shared from '../../common';
import { sendTxn as txnEmail } from '../libs/email'; import { sendTxn as txnEmail } from '../libs/email';
import { sendNotification as sendPushNotification } from '../libs/pushNotifications'; import { sendNotification as sendPushNotification } from '../libs/pushNotifications';
import cwait from 'cwait'; import cwait from 'cwait';
import { syncableAttrs } from '../libs/taskManager'; import { syncableAttrs, setNextDue } from '../libs/taskManager';
const Schema = mongoose.Schema; const Schema = mongoose.Schema;
@@ -145,6 +145,7 @@ schema.methods.syncToUser = async function syncChallengeToUser (user) {
matchingTask.challenge = {taskId: chalTask._id, id: challenge._id, shortName: challenge.shortName}; matchingTask.challenge = {taskId: chalTask._id, id: challenge._id, shortName: challenge.shortName};
matchingTask.userId = user._id; matchingTask.userId = user._id;
user.tasksOrder[`${chalTask.type}s`].push(matchingTask._id); user.tasksOrder[`${chalTask.type}s`].push(matchingTask._id);
setNextDue(matchingTask, user);
} else { } else {
_.merge(matchingTask, syncableAttrs(chalTask)); _.merge(matchingTask, syncableAttrs(chalTask));
// Make sure the task is in user.tasksOrder // Make sure the task is in user.tasksOrder