Skip to content
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
283 changes: 271 additions & 12 deletions client/src/components/ChoreWidget.jsx

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions client/src/i18n/locales/en/admin.json
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@
"items": {
"addChore": "Add a chore",
"transferChore": "Transfer a chore to someone else",
"splitChore": "Split a bonus chore's Stars among several people",
"snoozeChore": "Snooze a chore",
"prizeApproval": "Approve or decline prize requests",
"quickSpend": "Spend {{points}} from a profile",
Expand Down
10 changes: 9 additions & 1 deletion client/src/i18n/locales/en/chores.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,15 @@
"noneAvailable": "No bonus chores available",
"alreadyHasBonus": "User already has an uncompleted bonus chore. Complete it first!",
"assignFailed": "Failed to assign bonus chore",
"notOffered": "This chore isn't offered to this person."
"notOffered": "This chore isn't offered to this person.",
"splitTitle": "Split \"{{title}}\"",
"splitHint": "Pick who shares this chore. Each person gets their share of the {{points}} — the leftover {{points}} go one each to the first people picked.",
"splitNeedTwo": "Pick at least two people to split the {{points}}.",
"splitPreview": "Splitting {{total}} {{pointsIcon}} between {{count}} people.",
"splitConfirm": "Split",
"splitFailed": "Failed to split bonus chore",
"splitDone": "Split {{total}} {{pointsIcon}}",
"splitUndoHint": "To undo a split, transfer the chore back to the bonus pool — the whole split comes back, and any share already ticked stays earned."
},
"transfer": {
"menuItem": "Transfer chore",
Expand Down
1 change: 1 addition & 0 deletions client/src/i18n/locales/es/admin.json
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@
"items": {
"addChore": "Añadir una tarea",
"transferChore": "Transferir una tarea a otra persona",
"splitChore": "Repartir las Estrellas de una tarea extra entre varias personas",
"snoozeChore": "Posponer una tarea",
"prizeApproval": "Aprobar o rechazar solicitudes de premios",
"quickSpend": "Gastar {{points}} desde un perfil",
Expand Down
10 changes: 9 additions & 1 deletion client/src/i18n/locales/es/chores.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,15 @@
"noneAvailable": "No hay tareas extra disponibles",
"alreadyHasBonus": "Esta persona ya tiene una tarea extra sin terminar. ¡Complétala primero!",
"assignFailed": "No se pudo asignar la tarea extra",
"notOffered": "Esta tarea no se ofrece a esta persona."
"notOffered": "Esta tarea no se ofrece a esta persona.",
"splitTitle": "Dividir \"{{title}}\"",
"splitHint": "Elige quién comparte esta tarea. Cada persona recibe su parte de los {{points}}; los {{points}} sobrantes van uno a uno a las primeras personas elegidas.",
"splitNeedTwo": "Elige al menos dos personas para dividir los {{points}}.",
"splitPreview": "Dividiendo {{total}} {{pointsIcon}} entre {{count}} personas.",
"splitConfirm": "Dividir",
"splitFailed": "No se pudo dividir la tarea extra",
"splitDone": "Dividido: {{total}} {{pointsIcon}}",
"splitUndoHint": "Para deshacer una división, transfiere la tarea de vuelta al panel de tareas extra: toda la división vuelve, y las partes ya marcadas quedan ganadas."
},
"transfer": {
"menuItem": "Transferir tarea",
Expand Down
120 changes: 114 additions & 6 deletions client/src/utils/choreHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,105 @@ export function ringsToday(schedule, today = getTodayDateString()) {

export function hasOutstandingBonusChore(schedules, history, userId, today) {
return schedules
.filter((schedule) => schedule.user_id === userId
&& (schedule.clam_value || 0) > 0
&& shouldShowChoreToday(schedule, today))
.some((schedule) => !history.some((entry) => entry.chore_schedule_id === schedule.id
&& entry.user_id === userId
&& entry.date === today));
.filter((schedule) => {
// A split share open today counts the same as an assigned bonus
// chore: the person is holding it until they tick their own share.
const inSplit = isSplitSchedule(schedule) && parseSplitUserIds(schedule.split_user_ids)?.includes(Number(userId));
return (schedule.user_id === userId || inSplit)
&& (schedule.clam_value || 0) > 0
&& shouldShowChoreToday(schedule, today);
})
.some((schedule) => {
// A split share is done once the member ticks it — on or after the
// split's claim date, which the server attaches as
// split_done_user_ids, so a share ticked yesterday does not block
// a claim tomorrow (issue #407).
const inSplit = isSplitSchedule(schedule) && parseSplitUserIds(schedule.split_user_ids)?.includes(Number(userId));
if (inSplit) return !splitShareDoneFor(schedule, userId, history, today);
return !history.some((entry) => entry.chore_schedule_id === schedule.id
&& entry.user_id === userId
&& entry.date === today);
});
}

// A split bonus chore (issue #407) rides on the one pool schedule as two
// parallel lists, modelled on prize_offers.split_user_ids: `split_user_ids`
// who shares it and `split_shares` how many Stars each gets, in the same
// order. NULL is not split. Both parse leniently: a stored value that is
// not a list of ids / shares is treated as no split.
export function parseSplitUserIds(stored) {
if (stored === null || stored === undefined || stored === '') return null;
let list = stored;
if (typeof stored === 'string') {
try { list = JSON.parse(stored); } catch { return null; }
}
if (!Array.isArray(list)) return null;
const ids = [...new Set(list.filter((id) => Number.isInteger(id) && id > 0))];
return ids.length > 0 ? ids : null;
}

export function parseSplitShares(stored) {
if (stored === null || stored === undefined || stored === '') return null;
let list = stored;
if (typeof stored === 'string') {
try { list = JSON.parse(stored); } catch { return null; }
}
if (!Array.isArray(list)) return null;
const shares = list.filter((share) => Number.isInteger(share) && share >= 0);
return shares.length === list.length ? shares : null;
}

export function isSplitSchedule(schedule) {
return parseSplitUserIds(schedule?.split_user_ids) !== null;
}

// The Stars a person earns for ticking a schedule: their share of a split,
// or null when it is not split (or they are not in it).
export function splitShareFor(schedule, userId) {
const ids = parseSplitUserIds(schedule?.split_user_ids);
if (ids === null) return null;
const index = ids.indexOf(Number(userId));
if (index === -1) return null;
const shares = parseSplitShares(schedule.split_shares);
if (shares === null || shares.length !== ids.length) return null;
return shares[index];
}

// Whether a split member's share is done (issue #407): the server attaches
// the members whose share is done (`split_done_user_ids`, a completion on
// or after the split's claim date), so a share ticked yesterday still reads
// as done tomorrow. Without it — an older server — fall back to today's
// history, which is all the dashboard fetches.
export function splitShareDoneFor(schedule, userId, history, today = getTodayDateString()) {
if (!isSplitSchedule(schedule)) return false;
if (Array.isArray(schedule.split_done_user_ids)) return schedule.split_done_user_ids.includes(Number(userId));
return history.some((entry) => entry.chore_schedule_id === schedule.id
&& entry.user_id === userId
&& entry.kind === 'completion'
&& entry.date === today);
}

// Whether any share of a split has been ticked (issue #407): the split is
// locked once a share is ticked, so the dashboard hides Transfer for it.
// Reads the server's `split_done_user_ids` when present, so a share ticked
// yesterday still locks the split tomorrow; without it, falls back to
// today's history.
export function splitHasTickedShare(schedule, history, today = getTodayDateString()) {
if (!isSplitSchedule(schedule)) return false;
if (Array.isArray(schedule.split_done_user_ids)) return schedule.split_done_user_ids.length > 0;
return splitHasTickedShareToday(schedule, history, today);
}

// Whether any share of a split has been ticked today (issue #407): the
// split is locked once a share is ticked, so the dashboard hides Transfer
// for it. `history` is today's history rows; a tick from an earlier day is
// invisible here, which is what `splitHasTickedShare` (above, reading the
// server's `split_done_user_ids`) is for.
export function splitHasTickedShareToday(schedule, history, today = getTodayDateString()) {
if (!isSplitSchedule(schedule)) return false;
return history.some((entry) => entry.chore_schedule_id === schedule.id
&& entry.kind === 'completion'
&& entry.date === today);
}

/**
Expand Down Expand Up @@ -98,6 +191,21 @@ export function claimUserIdsToSave(picked, people, { bonus, nobody }) {
return picked;
}

/**
* Splitting a bonus chore's Stars among `count` people: the base share is
* the floor of the total over the people, and the leftover Stars go one
* each to the first people in the order given — 10 over 2 is [5, 5], 7
* over 2 is [4, 3], 10 over 3 is [4, 3, 3]. The server runs the same
* rule (index.js splitStars), so the picker shows exactly what the
* split will assign. Returns one amount per person, in selection order.
*/
export function splitStars(total, count) {
if (!Number.isInteger(count) || count < 1) return [];
const base = Math.floor(total / count);
const remainder = total % count;
return Array.from({ length: count }, (_, i) => base + (i < remainder ? 1 : 0));
}

export function getTodayDateString() {
const tz = getServerTimezoneSync();
const today = new Date();
Expand Down
154 changes: 152 additions & 2 deletions client/src/utils/choreHelpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ vi.mock('./timezone.js', () => ({
getServerTimezoneSync: () => zones.server,
}));

import { shouldShowChoreToday, convertDaysToCrontab, getDueDateStatus, formatDueDate, hasOutstandingBonusChore, canClaimBonusChore, claimUserIdsToSave, compareByKey, countsTowardDailyBonus, ringsToday, getTransferTargets } from './choreHelpers.js';
import { shouldShowChoreToday, convertDaysToCrontab, getDueDateStatus, formatDueDate, hasOutstandingBonusChore, canClaimBonusChore, claimUserIdsToSave, compareByKey, countsTowardDailyBonus, ringsToday, getTransferTargets, splitStars, parseSplitUserIds, parseSplitShares, isSplitSchedule, splitShareFor, splitShareDoneFor, splitHasTickedShare, splitHasTickedShareToday } from './choreHelpers.js';

// Issue #241: a follow-up counts toward the daily bonus from the day after it
// was handed out, as the server's getTodaysRegularChoresForUser does.
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('hasOutstandingBonusChore', () => {

const TODAY = '2026-09-06';
const bonus = (id, crontab) => ({ id, user_id: 4, visible: 1, clam_value: 1, crontab });
const done = (id) => ({ chore_schedule_id: id, user_id: 4, date: TODAY });
const done = (id) => ({ chore_schedule_id: id, user_id: 4, date: TODAY, kind: 'completion' });

it('does not block on a bonus chore that is not due today', () => {
// Thursday-only chore, uncompleted, on a Sunday: nothing is outstanding.
Expand Down Expand Up @@ -265,6 +265,126 @@ describe('hasOutstandingBonusChore', () => {
it('treats a one-off schedule with no crontab as due today', () => {
expect(hasOutstandingBonusChore([bonus(41, null)], [], 4, TODAY)).toBe(true);
});

it('blocks on a split share open today', () => {
// A split schedule is unassigned, but the person in it is holding
// a share until they tick it.
const split = { id: 50, user_id: null, visible: 1, clam_value: 6, crontab: null, split_user_ids: [4, 3], split_shares: [3, 3] };
expect(hasOutstandingBonusChore([split], [], 4, TODAY)).toBe(true);
});

it('does not block once the person ticked their split share', () => {
const split = { id: 50, user_id: null, visible: 1, clam_value: 6, crontab: null, split_user_ids: [4, 3], split_shares: [3, 3] };
expect(hasOutstandingBonusChore([split], [done(50)], 4, TODAY)).toBe(false);
});

it('does not block on a split share the server says is done, even with no history row', () => {
// A share ticked yesterday: today's history does not see it, but the
// server attaches split_done_user_ids, so the person is free again.
const split = { id: 50, user_id: null, visible: 1, clam_value: 6, crontab: null, split_user_ids: [4, 3], split_shares: [3, 3], split_done_user_ids: [4] };
expect(hasOutstandingBonusChore([split], [], 4, TODAY)).toBe(false);
});

it('still blocks when only the other person ticked their split share', () => {
const split = { id: 50, user_id: null, visible: 1, clam_value: 6, crontab: null, split_user_ids: [4, 3], split_shares: [3, 3], split_done_user_ids: [3] };
expect(hasOutstandingBonusChore([split], [], 4, TODAY)).toBe(true);
});

it('does not block a person who is not in the split', () => {
const split = { id: 50, user_id: null, visible: 1, clam_value: 6, crontab: null, split_user_ids: [3, 2], split_shares: [3, 3] };
expect(hasOutstandingBonusChore([split], [], 4, TODAY)).toBe(false);
});
});

describe('split helpers', () => {
const split = (userIds, shares) => ({ id: 50, user_id: null, visible: 1, clam_value: 6, crontab: null, split_user_ids: userIds, split_shares: shares });

it('parses split user ids from an array or a JSON string', () => {
expect(parseSplitUserIds([4, 3])).toEqual([4, 3]);
expect(parseSplitUserIds('[4, 3]')).toEqual([4, 3]);
expect(parseSplitUserIds(null)).toBeNull();
expect(parseSplitUserIds(undefined)).toBeNull();
expect(parseSplitUserIds('')).toBeNull();
expect(parseSplitUserIds('not json')).toBeNull();
expect(parseSplitUserIds(4)).toBeNull();
expect(parseSplitUserIds([])).toBeNull();
});

it('drops bad ids and dedupes, and gives up when none are left', () => {
expect(parseSplitUserIds([4, 'x', 0, -1, 4, 3])).toEqual([4, 3]);
expect(parseSplitUserIds(['x', 0])).toBeNull();
});

it('parses split shares from an array or a JSON string', () => {
expect(parseSplitShares([4, 3])).toEqual([4, 3]);
expect(parseSplitShares('[4, 3]')).toEqual([4, 3]);
expect(parseSplitShares(null)).toBeNull();
expect(parseSplitShares('not json')).toBeNull();
});

it('refuses shares with a bad entry rather than misaligning them', () => {
expect(parseSplitShares([4, 'x'])).toBeNull();
expect(parseSplitShares([4, -1])).toBeNull();
});

it('recognises a split schedule and nothing else', () => {
expect(isSplitSchedule(split([4, 3], [3, 3]))).toBe(true);
expect(isSplitSchedule(split('[4, 3]', '[3, 3]'))).toBe(true);
expect(isSplitSchedule({ ...split([4, 3], [3, 3]), split_user_ids: null })).toBe(false);
expect(isSplitSchedule({ id: 1, user_id: 4 })).toBe(false);
expect(isSplitSchedule(null)).toBe(false);
});

it('hands each person their own share', () => {
const schedule = split([4, 3], [4, 3]);
expect(splitShareFor(schedule, 4)).toBe(4);
expect(splitShareFor(schedule, 3)).toBe(3);
expect(splitShareFor(schedule, 2)).toBeNull();
expect(splitShareFor({ ...schedule, split_user_ids: null }, 4)).toBeNull();
});

it('refuses a share when the lists do not line up', () => {
expect(splitShareFor(split([4, 3], [4]), 4)).toBeNull();
expect(splitShareFor(split([4, 3], [4, 'x']), 4)).toBeNull();
});

it('says a split is locked once any share is ticked today', () => {
const schedule = split([4, 3], [4, 3]);
const today = '2026-09-06';
const ticked = (userId, date = today, kind = 'completion') => ({ chore_schedule_id: 50, user_id: userId, date, kind });
expect(splitHasTickedShareToday(schedule, [], today)).toBe(false);
expect(splitHasTickedShareToday(schedule, [ticked(4)], today)).toBe(true);
expect(splitHasTickedShareToday(schedule, [ticked(3)], today)).toBe(true);
// Another day's tick, or a row that is not a completion, locks nothing.
expect(splitHasTickedShareToday(schedule, [ticked(4, '2026-09-05')], today)).toBe(false);
expect(splitHasTickedShareToday(schedule, [ticked(4, today, 'missed')], today)).toBe(false);
// A tick on another chore, or a schedule that is not split, is not a lock.
expect(splitHasTickedShareToday(schedule, [{ ...ticked(4), chore_schedule_id: 51 }], today)).toBe(false);
expect(splitHasTickedShareToday({ ...schedule, split_user_ids: null }, [ticked(4)], today)).toBe(false);
expect(splitHasTickedShareToday(null, [ticked(4)], today)).toBe(false);
});

it('says a share is done per person, from the server list when present', () => {
const schedule = split([4, 3], [4, 3]);
const today = '2026-09-06';
const ticked = (userId, date = today, kind = 'completion') => ({ chore_schedule_id: 50, user_id: userId, date, kind });
// The server's list is the whole answer: a share ticked yesterday
// still reads as done, and the other person's share as open.
const done = { ...schedule, split_done_user_ids: [4] };
expect(splitShareDoneFor(done, 4, [], today)).toBe(true);
expect(splitShareDoneFor(done, 3, [], today)).toBe(false);
expect(splitHasTickedShare(done, [], today)).toBe(true);
expect(splitHasTickedShare({ ...schedule, split_done_user_ids: [] }, [], today)).toBe(false);
// Without the list — an older server — today's history decides.
expect(splitShareDoneFor(schedule, 4, [ticked(4)], today)).toBe(true);
expect(splitShareDoneFor(schedule, 4, [ticked(4, '2026-09-05')], today)).toBe(false);
expect(splitShareDoneFor(schedule, 4, [], today)).toBe(false);
expect(splitHasTickedShare(schedule, [ticked(3)], today)).toBe(true);
expect(splitHasTickedShare(schedule, [], today)).toBe(false);
// Not a split, or nobody in it, is never done or locked.
expect(splitShareDoneFor({ ...schedule, split_user_ids: null }, 4, [ticked(4)], today)).toBe(false);
expect(splitShareDoneFor(schedule, 2, [ticked(4)], today)).toBe(false);
});
});

describe('compareByKey', () => {
Expand Down Expand Up @@ -460,3 +580,33 @@ describe('getTransferTargets', () => {
expect(targets.filter((t) => t.id === 0)).toEqual([{ id: 0, username: BONUS, isBonus: true }]);
});
});

describe('splitStars', () => {
it('splits evenly when the total divides', () => {
expect(splitStars(10, 2)).toEqual([5, 5]);
expect(splitStars(9, 3)).toEqual([3, 3, 3]);
expect(splitStars(4, 4)).toEqual([1, 1, 1, 1]);
});

it('gives the leftover Stars one each to the first people', () => {
expect(splitStars(7, 2)).toEqual([4, 3]);
expect(splitStars(10, 3)).toEqual([4, 3, 3]);
expect(splitStars(11, 3)).toEqual([4, 4, 3]);
expect(splitStars(1, 2)).toEqual([1, 0]);
});

it('always hands out exactly the total', () => {
for (const total of [0, 1, 2, 5, 7, 10, 13]) {
for (const count of [1, 2, 3, 4, 5]) {
const shares = splitStars(total, count);
expect(shares).toHaveLength(count);
expect(shares.reduce((sum, share) => sum + share, 0)).toBe(total);
}
}
});

it('returns no shares without people', () => {
expect(splitStars(10, 0)).toEqual([]);
expect(splitStars(10, -1)).toEqual([]);
});
});
1 change: 1 addition & 0 deletions client/src/utils/displayControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const CONTROL_LIMITS_DEFAULT_KEY = 'DISPLAY_CONTROL_LIMITS_DEFAULT';
export const CORE_CONTROLS = [
{ id: 'core:addChore', labelKey: 'admin:controls.items.addChore' },
{ id: 'core:transferChore', labelKey: 'admin:controls.items.transferChore' },
{ id: 'core:splitChore', labelKey: 'admin:controls.items.splitChore' },
{ id: 'core:snoozeChore', labelKey: 'admin:controls.items.snoozeChore' },
{ id: 'core:prizeApproval', labelKey: 'admin:controls.items.prizeApproval' },
{ id: 'core:quickSpend', labelKey: 'admin:controls.items.quickSpend' },
Expand Down
1 change: 1 addition & 0 deletions client/src/utils/displayControls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('constants', () => {
expect(CORE_IDS).toEqual([
'core:addChore',
'core:transferChore',
'core:splitChore',
'core:snoozeChore',
'core:prizeApproval',
'core:quickSpend',
Expand Down
Loading