Files
wekan/client/lib/dueDateColor.js
T
Lauri Ojansivu 3de2220964 Due date colours: when a card turns amber and red is the board's own.
Board Settings / Date has two numbers of hours: before the due date a card
is due soon (amber), WeKan's 48 until set, and after it a card is overdue
(red), 0 - the due time itself - until set. So a board can be grey above 24
hours, amber within them and red when overdue. An empty field is the default
again.

dueDateClass takes the board's thresholds (models/lib/dueDateThresholds.js
dueThresholdsOf) on the card, the minicard and the checklist items; a value
that is no number of hours from 0 to a year is no setting. A board admin
sets them with setBoardDueThresholds, which refuses anything else before it
writes; the fields are in the Boards schema and go with a board's WeKan
export and import. The two labels are in every language.

tests/dueDateThresholds6947.test.cjs checks the colours with and without
thresholds, the refused values, that no caller is left on the fixed 48 hours,
the method's guard and the translations;
tests/playwright/specs/due-thresholds-6947.e2e.js the colour following the
board and Board Settings / Date saving, refusing and resetting the hours.

Thanks to BarbossHack and xet7 !

Fixes #6947.
2026-11-26 20:59:10 +02:00

88 lines
3.6 KiB
JavaScript

/**
* Pure helper that decides the CSS status class for a card "due date" badge.
*
* This logic is shared between the card-detail badge (`cardDueDate`) and the
* minicard badge (`minicardDueDate`) so that both views always agree on the
* colour of a due date (see issues #6000 and #5965).
*
* Rules (no Blaze / no DOM dependencies, easy to unit-test):
* - If the card has an end date:
* - end before due => 'completed-early'
* - otherwise => 'completed'
* - Otherwise, compare the due date against "now", with the board's
* thresholds (#6947, models/lib/dueDateThresholds.js dueThresholdsOf):
* - more than overdueAfterHours past the due date => 'overdue' (red)
* - within soonHours of it, or past it by less => 'due-soon' (amber)
* - more than soonHours before it => 'not-due' (grey)
* Without thresholds: 48 hours before, and overdue at the due time.
*
* @param {Date|string|number} dueDate - The card due date.
* @param {Date|string|number} now - The current time.
* @param {Date|string|number} [endDate] - The card end date, if set.
* @param {{soonHours: number, overdueAfterHours: number}} [thresholds]
* @returns {string} One of 'overdue' | 'due-soon' | 'not-due' |
* 'completed-early' | 'completed'.
*/
export function dueDateClass(dueDate, now, endDate, thresholds) {
const due = new Date(dueDate);
const nowVal = new Date(now);
if (endDate) {
const end = new Date(endDate);
if (end.getTime() < due.getTime()) {
return 'completed-early';
}
return 'completed';
}
const diffMs = due.getTime() - nowVal.getTime();
const hoursDiff = diffMs / (1000 * 60 * 60);
// No imports here (tests load this file on its own): the thresholds come
// checked from dueThresholdsOf, and only a number is taken.
const hours = v => (typeof v === 'number' && Number.isFinite(v) && v >= 0 ? v : null);
const soon = hours(thresholds && thresholds.soonHours);
const after = hours(thresholds && thresholds.overdueAfterHours);
const soonHours = soon === null ? 48 : soon;
const overdueAfterHours = after === null ? 0 : after;
if (hoursDiff < -overdueAfterHours) {
return 'overdue';
} else if (hoursDiff <= soonHours) {
return 'due-soon';
}
return 'not-due';
}
/**
* Pure helper that turns a due date into a countdown - "N days left" or
* "N days overdue" - for the due-date badge (GitHub issue #2424). The badge
* used to show only the formatted date; this adds a relative day count next
* to it, using CALENDAR days (the due date's own day minus "now"'s day), so
* a card due later today still reads "Due today" rather than "0 days left".
*
* @param {Date|string|number} dueDate - The card due date.
* @param {Date|string|number} now - The current time.
* @returns {{key: 'due-today'|'due-days-left'|'due-days-overdue', days: number}}
* `days` is always a non-negative count; `key` says which direction/phrase
* to use. Look the phrase up with TAPi18n.__(key, { count: days }) (or
* plain %s substitution) at the call site - this module stays i18n-free.
*/
export function dueCountdown(dueDate, now) {
const due = new Date(dueDate);
const nowVal = new Date(now);
const startOfDay = date =>
new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
const dayMs = 24 * 60 * 60 * 1000;
const diffDays = Math.round((startOfDay(due) - startOfDay(nowVal)) / dayMs);
if (diffDays === 0) {
return { key: 'due-today', days: 0 };
} else if (diffDays > 0) {
return { key: 'due-days-left', days: diffDays };
}
return { key: 'due-days-overdue', days: -diffDays };
}