diff --git a/src/components/VacationOverview.vue b/src/components/VacationOverview.vue new file mode 100644 index 0000000..25837e8 --- /dev/null +++ b/src/components/VacationOverview.vue @@ -0,0 +1,308 @@ + + + + + + diff --git a/src/utils/dates.spec.ts b/src/utils/dates.spec.ts index 5a3a1db..ea794f7 100644 --- a/src/utils/dates.spec.ts +++ b/src/utils/dates.spec.ts @@ -8,6 +8,7 @@ import { countWorkingDays, formatCountdown, formatRange, + formatShortRange, parseWeekdays, seasonOf, toIso, @@ -83,6 +84,18 @@ describe('formatRange', () => { }) }) +describe('formatShortRange', () => { + it('leaves the current year out', () => { + expect(formatShortRange('2026-11-30', '2026-12-04', 2026)).not.toContain('2026') + expect(formatShortRange('2026-11-30', '2026-11-30', 2026)).not.toContain('2026') + }) + + it('shows the year of a range that leaves the current year', () => { + expect(formatShortRange('2026-12-22', '2027-01-05', 2026)).toContain('2027') + expect(formatShortRange('2027-03-01', '2027-03-05', 2026)).toContain('2027') + }) +}) + describe('formatCountdown', () => { it('pads minutes and seconds but not hours', () => { expect(formatCountdown(((7 * 60) + 5) * 60000 + 3000)).toBe('7:05:03') diff --git a/src/utils/dates.ts b/src/utils/dates.ts index 468b073..3d2e575 100644 --- a/src/utils/dates.ts +++ b/src/utils/dates.ts @@ -86,6 +86,22 @@ export function formatRange(startIso: string | null | undefined, endIso: string return `${formatDate(startIso)} – ${formatDate(endIso)}` } +/** + * A short range like "Nov 30 – Dec 4", with the year only when it is not the + * current one: "Dec 22, 2026 – Jan 5, 2027". + * + * @param startIso 'YYYY-MM-DD' + * @param endIso 'YYYY-MM-DD' + * @param currentYear the year that goes without saying + */ +export function formatShortRange(startIso: string, endIso: string, currentYear: number): string { + const start = new Date(startIso + 'T00:00:00') + const end = new Date(endIso + 'T00:00:00') + const withYear = start.getFullYear() !== currentYear || end.getFullYear() !== currentYear + const format = new Intl.DateTimeFormat(locale(), withYear ? { year: 'numeric', month: 'short', day: 'numeric' } : { month: 'short', day: 'numeric' }) + return startIso === endIso ? format.format(start) : format.formatRange(start, end) +} + /** * A duration as `H:MM:SS`, for the final stretch before a break starts. Hours are * not padded (and run past 24) because "47:59:12" reads as a countdown while diff --git a/src/utils/vacation.spec.ts b/src/utils/vacation.spec.ts new file mode 100644 index 0000000..4471920 --- /dev/null +++ b/src/utils/vacation.spec.ts @@ -0,0 +1,41 @@ +/** + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +import type { RequestStatus } from '../types/index.ts' + +import { describe, expect, it } from 'vitest' +import { vacationSplit } from './vacation.ts' + +const row = { typeId: 1, year: 2026, used: 24, pending: 2 } +const leave = (startDate: string, endDate: string, workingDays: number, status: RequestStatus = 'APPROVED', typeId = 1) => ({ typeId, status, startDate, endDate, workingDays }) + +describe('vacationSplit', () => { + it('counts the leave starting after today as planned, the rest of the used days as taken', () => { + const split = vacationSplit(row, [leave('2026-03-02', '2026-03-06', 5), leave('2026-12-22', '2026-12-31', 4)], '2026-11-26') + expect(split.taken).toBe(20) + expect(split.planned).toBe(4) + expect(split.pending).toBe(2) + }) + + it('counts leave that started already as taken, and leave starting next year in that year', () => { + const split = vacationSplit(row, [leave('2026-11-25', '2026-11-27', 3), leave('2027-01-04', '2027-01-08', 5)], '2026-11-26') + expect(split.planned).toBe(0) + expect(split.taken).toBe(24) + }) + + it('leaves out other types, cancelled leave and requests still waiting from the planned days', () => { + const split = vacationSplit(row, [leave('2026-12-01', '2026-12-01', 1, 'APPROVED', 2), leave('2026-12-02', '2026-12-02', 1, 'CANCELLED'), leave('2026-12-03', '2026-12-03', 1, 'PENDING')], '2026-11-26') + expect(split.planned).toBe(0) + }) + + it('never plans more than is used', () => { + expect(vacationSplit({ ...row, used: 2 }, [leave('2026-12-22', '2026-12-31', 4)], '2026-11-26')).toMatchObject({ taken: 0, planned: 2 }) + }) + + it('names the next leave, booked or waiting for approval', () => { + const requests = [leave('2027-01-04', '2027-01-08', 5), leave('2026-12-14', '2026-12-15', 2, 'PENDING'), leave('2026-11-02', '2026-11-03', 2)] + expect(vacationSplit(row, requests, '2026-11-26').next).toEqual({ startDate: '2026-12-14', endDate: '2026-12-15', workingDays: 2, pending: true }) + expect(vacationSplit(row, [], '2026-11-26').next).toBeNull() + }) +}) diff --git a/src/utils/vacation.ts b/src/utils/vacation.ts new file mode 100644 index 0000000..282978b --- /dev/null +++ b/src/utils/vacation.ts @@ -0,0 +1,55 @@ +/** + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +import type { BalanceRow, LeaveRequest, RequestStatus } from '../types/index.ts' + +/** Statuses whose days count as used, as on the server (§4.2) */ +const USED: RequestStatus[] = ['APPROVED', 'WITHDRAWAL_PENDING'] +/** Statuses still waiting for a decision */ +const PENDING: RequestStatus[] = ['PENDING', 'ESCALATED'] + +/** The next leave of a type, booked or waiting for approval */ +export interface NextLeave { + startDate: string + endDate: string + workingDays: number + pending: boolean +} + +/** How the vacation days of a year split up */ +export interface VacationSplit { + /** Used days whose leave started today or before */ + taken: number + /** Used days whose leave starts after today */ + planned: number + pending: number + next: NextLeave | null +} + +/** + * Splits the used days of a balance row into taken and planned, the way the + * server counts them: each request's days in the year its leave starts. + * + * @param row the balance of the type and year + * @param requests the person's requests + * @param today 'YYYY-MM-DD' + */ +export function vacationSplit(row: Pick, requests: Pick[], today: string): VacationSplit { + const ofType = requests.filter((request) => request.typeId === row.typeId) + const planned = ofType + .filter((request) => USED.includes(request.status) && request.startDate > today && request.startDate.startsWith(`${row.year}-`)) + .reduce((sum, request) => sum + request.workingDays, 0) + const upcoming = ofType + .filter((request) => (USED.includes(request.status) || PENDING.includes(request.status)) && request.startDate > today) + .sort((a, b) => a.startDate.localeCompare(b.startDate))[0] + const clamped = Math.min(planned, row.used) + return { + taken: row.used - clamped, + planned: clamped, + pending: row.pending, + next: upcoming + ? { startDate: upcoming.startDate, endDate: upcoming.endDate, workingDays: upcoming.workingDays, pending: PENDING.includes(upcoming.status) } + : null, + } +} diff --git a/src/views/ProfileView.vue b/src/views/ProfileView.vue index 2ed9f6f..ef06d0a 100644 --- a/src/views/ProfileView.vue +++ b/src/views/ProfileView.vue @@ -68,12 +68,17 @@

{{ t('absence', 'Vacation {year}', { year: profile.holidays.year }) }}

-
- - - {{ t('absence', 'Details in My leave') }} - -
+ @@ -91,7 +96,7 @@ {{ t('absence', 'Somebody else from HR changes your own days.') }}

-

+

{{ n('absence', 'Next leave: {range}, {count} working day', 'Next leave: {range}, {count} working days', profile.nextLeave.workingDays, { range: formatRange(profile.nextLeave.startDate, profile.nextLeave.endDate), count: profile.nextLeave.workingDays }) }}

@@ -172,7 +177,7 @@