fix: stock correction no longer neutralized by phantom consumption (#109)

After correcting medication stock, the coverage calculation immediately
counted 1 dose as consumed (due to +1 in occurrences formula), which
neutralized small corrections like +1 pill.

Fix: start consumption counting from stockCorrectionCutoff + period
(the next scheduled dose) instead of from the correction time itself.

Added 3 frontend tests for stock correction scenarios and 6 backend
e2e tests for the PATCH /medications/:id/stock-adjustment endpoint.
This commit is contained in:
Daniel Volz
2026-02-07 13:30:44 +01:00
committed by GitHub
parent 06943f5831
commit f73c79c6cf
3 changed files with 283 additions and 2 deletions
+117
View File
@@ -366,6 +366,123 @@ describe("calculateCoverage", () => {
expect(result.all).toHaveLength(1);
// Daily rate should be doubled for 2 people
});
it("stock correction immediately reflects new stock without phantom consumption", () => {
// BUG: After a stock correction of +1 pill, the coverage calculation
// immediately consumed 1 dose (due to the +1 in occurrences formula),
// making the correction appear to have no effect.
//
// Scenario: User has 112 pills, corrects to 113 (+1 pill).
// Expected: medsLeft = 113 immediately after correction.
// Bug: medsLeft stayed at 112 because 1 dose was counted as consumed.
const correctionTime = new Date("2024-03-15T12:00:00Z");
const meds: Medication[] = [
{
id: 1,
name: "TestMed",
packCount: 1,
blistersPerPack: 14,
pillsPerBlister: 14,
looseTablets: 0,
stockAdjustment: -83, // 196 - 83 = 113 pills
lastStockCorrectionAt: correctionTime.toISOString(),
takenBy: [],
blisters: [
{
usage: 1,
every: 1,
start: "2024-01-01T08:00:00",
},
],
updatedAt: correctionTime.toISOString(),
},
];
// Calculate coverage immediately after correction (same second)
const result = calculateCoverage(meds, [], "en", 7, "automatic", new Set());
expect(result.all).toHaveLength(1);
// getMedTotal = 1*14*14 + 0 + (-83) = 113
// Consumed since correction should be 0 (not 1!)
expect(result.all[0].medsLeft).toBe(113);
});
it("stock correction with dose tracking data also reflects correctly", () => {
// When the user has dose tracking data, the actualConsumed path is used.
// Verify that no phantom dose is generated right after a stock correction.
const correctionTime = new Date("2024-03-15T12:00:00Z");
const march14 = new Date("2024-03-14T00:00:00").getTime();
const meds: Medication[] = [
{
id: 1,
name: "TestMed",
packCount: 1,
blistersPerPack: 1,
pillsPerBlister: 30,
looseTablets: 0,
stockAdjustment: -7, // 30 - 7 = 23 pills
lastStockCorrectionAt: correctionTime.toISOString(),
takenBy: [],
blisters: [
{
usage: 1,
every: 1,
start: "2024-03-01T08:00:00",
},
],
updatedAt: correctionTime.toISOString(),
},
];
// User has tracked a dose yesterday (before the correction)
const takenDoses = new Set([`1-0-${march14}`]);
const result = calculateCoverage(meds, [], "en", 7, "automatic", takenDoses);
expect(result.all).toHaveLength(1);
// getMedTotal = 30 - 7 = 23.
// The taken dose from yesterday should NOT be counted (it's before the correction).
// No new doses should exist since the correction just happened.
expect(result.all[0].medsLeft).toBe(23);
});
it("stock correction consumption resumes after one full period", () => {
// After 1 day (for daily medication), the next dose should be consumed.
// Set system time to 1 day + 1 hour after correction.
const correctionTime = new Date("2024-03-14T12:00:00Z");
vi.setSystemTime(new Date("2024-03-15T13:00:00Z")); // 25 hours after correction
const meds: Medication[] = [
{
id: 1,
name: "TestMed",
packCount: 1,
blistersPerPack: 1,
pillsPerBlister: 30,
looseTablets: 0,
stockAdjustment: -7, // 30 - 7 = 23 pills
lastStockCorrectionAt: correctionTime.toISOString(),
takenBy: [],
blisters: [
{
usage: 1,
every: 1,
start: "2024-03-01T08:00:00",
},
],
updatedAt: correctionTime.toISOString(),
},
];
const result = calculateCoverage(meds, [], "en", 7, "automatic", new Set());
expect(result.all).toHaveLength(1);
// After 1 full period since correction, 1 dose should be consumed.
// medsLeft = 23 - 1 = 22
expect(result.all[0].medsLeft).toBe(22);
});
});
describe("getStockStatus", () => {
+11 -2
View File
@@ -126,9 +126,18 @@ export function calculateCoverage(
// but also account for manual corrections (doses marked as not taken)
blisters.forEach((s, blisterIdx) => {
const blisterStart = new Date(s.start).getTime();
const effectiveStart = Math.max(blisterStart, stockCorrectionCutoff);
if (Number.isNaN(effectiveStart) || effectiveStart > now) return;
const period = Math.max(1, s.every) * MS_PER_DAY;
// After a stock correction, start counting consumption from the NEXT
// scheduled dose, because the user's pill count already reflects all
// consumption up to the correction time.
let effectiveStart: number;
if (stockCorrectionCutoff > 0 && stockCorrectionCutoff >= blisterStart) {
effectiveStart = stockCorrectionCutoff + period;
} else {
effectiveStart = blisterStart;
}
if (Number.isNaN(effectiveStart) || effectiveStart > now) return;
const occurrences = Math.floor((now - effectiveStart) / period) + 1;
const intake = intakes[blisterIdx];
const intakePerson = intake?.takenBy;