Compare commits

...

1 Commits

Author SHA1 Message Date
Hafiz d08ea6677d Refactor(tasks): Centralize daily task start date normalization 2026-05-05 12:21:49 -05:00
3 changed files with 19 additions and 11 deletions
+2 -4
View File
@@ -27,6 +27,7 @@ import {
moveTask,
setNextDue,
requiredGroupFields,
normalizeDailyStartDate,
} from '../../libs/tasks/utils';
import common from '../../../common';
import { apiError } from '../../libs/apiError';
@@ -648,13 +649,10 @@ api.updateTask = {
task.group.managerNotes = sanitizedObj.managerNotes;
}
// For daily tasks, update start date based on timezone to maintain consistency
if (task.type === 'daily'
&& task.startDate
) {
task.startDate = moment(task.startDate).utcOffset(
-user.preferences.timezoneOffset,
).startOf('day').toDate();
task.startDate = normalizeDailyStartDate(task.startDate, user);
// If the daily task was set to repeat monthly on a day of the month, and the start date was
// updated, the task will then need to be updated to repeat on the same day of the month as
+2 -7
View File
@@ -1,4 +1,3 @@
import moment from 'moment';
import cloneDeep from 'lodash/cloneDeep';
import compact from 'lodash/compact';
import forEach from 'lodash/forEach';
@@ -9,6 +8,7 @@ import {
setNextDue,
validateTaskAlias,
requiredGroupFields,
normalizeDailyStartDate,
} from './utils';
import { model as Challenge } from '../../models/challenge';
import { model as Group } from '../../models/group';
@@ -80,13 +80,8 @@ async function createTasks (req, res, options = {}) {
}
}
// set startDate to midnight in the user's timezone
if (taskType === 'daily') {
const awareStartDate = moment(newTask.startDate).utcOffset(-user.preferences.timezoneOffset);
if (awareStartDate.format('HMsS') !== '0000') {
awareStartDate.startOf('day');
newTask.startDate = awareStartDate.toDate();
}
newTask.startDate = normalizeDailyStartDate(newTask.startDate, user);
}
setNextDue(newTask, user);
+15
View File
@@ -59,6 +59,21 @@ export function moveTask (order, taskId, to) {
}
}
export function normalizeDailyStartDate (date, user) {
if (!date) return date;
const utcView = moment.utc(date);
const looksLikeMidnightLocal = utcView.second() === 0
&& utcView.millisecond() === 0
&& [0, 15, 30, 45].includes(utcView.minute());
if (looksLikeMidnightLocal) {
return new Date(date);
}
return moment(date)
.utcOffset(-(user.preferences.timezoneOffset || 0))
.startOf('day')
.toDate();
}
export function setNextDue (task, user, dueDateOption) {
if (task.type !== 'daily') return;