feat(assistant): vacation calendar cards with the days left

This commit is contained in:
Maurice
2026-10-22 15:50:55 +02:00
parent c31b2162ad
commit a8ff98b660
59 changed files with 4783 additions and 70 deletions
@@ -0,0 +1,88 @@
import { ASSISTANT_CHANGE_VERB_KEYS, assistantAppliedSchema } from '../assistant.schema';
import { storedTrekCardSchema, trekCardSchema } from './index';
import { VACAY_CARD_MAX_DATES, VACAY_CARD_MAX_DAYS, vacayCardSchema, type VacayCard } from './vacay.card';
import { VACAY_APPLIED_ENTITIES, VACAY_VERB_KEYS } from './vacay.contract';
import { describe, expect, it } from 'vitest';
/** A change card of one day off, as the server builds it. */
const card = (): VacayCard => ({
kind: 'vacay',
months: [{ year: 2026, month: 10 }],
weekStart: 1,
weekendDays: [0, 6],
people: [{ id: 7, name: 'Ana', avatarKey: null, color: '#6366f1' }],
days: [{ date: '2026-10-26', personId: 7, fraction: 1, kind: 'vacation' }],
company: [{ date: '2026-10-30', fraction: 0.5 }],
holidays: [{ date: '2026-10-03', name: 'Tag der Deutschen Einheit', color: null }],
tripDates: ['2026-10-27'],
proposal: {
date: '2026-10-27',
personId: 7,
kind: 'vacation',
fraction: 1,
outcome: 'add',
before: null,
periodYear: 2026,
notes: [],
deltas: [{ personId: 7, year: 2026, days: 1 }],
},
balances: [
{
personId: 7,
year: 2026,
total: 30,
used: 12.5,
comp: 0,
totalAfter: null,
windowStart: '2026-01-01',
windowEnd: '2027-01-01',
},
],
otherDays: 0,
});
describe('the vacation card', () => {
it('VACAYCARD-001: a change card and an answer card read as themselves in the union', () => {
expect(trekCardSchema.parse(card())).toEqual(card());
const answer = { ...card(), proposal: null, months: [] };
expect(storedTrekCardSchema.parse(answer)).toEqual(answer);
});
it('VACAYCARD-002: a colour that is no hex, half a half day or an outcome it does not know reads as no card', () => {
const people = [{ id: 7, name: 'Ana', avatarKey: null, color: 'url(x)' }];
expect(storedTrekCardSchema.parse({ ...card(), people })).toBeNull();
const days = [{ date: '2026-10-26', personId: 7, fraction: 0.25, kind: 'vacation' }];
expect(storedTrekCardSchema.parse({ ...card(), days })).toBeNull();
const proposal = { ...card().proposal, outcome: 'swap' };
expect(storedTrekCardSchema.parse({ ...card(), proposal })).toBeNull();
});
it('VACAYCARD-003: the lists are bounded, and a field a later build adds is dropped on read', () => {
const many = Array.from({ length: VACAY_CARD_MAX_DAYS + 1 }, () => card().days[0]);
expect(vacayCardSchema.safeParse({ ...card(), days: many }).success).toBe(false);
const months = Array.from({ length: 4 }, () => ({ year: 2026, month: 1 }));
expect(vacayCardSchema.safeParse({ ...card(), months }).success).toBe(false);
// Three long months of trip days and company holidays fit; one date more does not.
const dates = (count: number) => Array.from({ length: count }, () => '2026-10-27');
expect(vacayCardSchema.safeParse({ ...card(), tripDates: dates(VACAY_CARD_MAX_DATES) }).success).toBe(true);
expect(vacayCardSchema.safeParse({ ...card(), tripDates: dates(VACAY_CARD_MAX_DATES + 1) }).success).toBe(false);
const company = dates(VACAY_CARD_MAX_DATES).map((date) => ({ date, fraction: 1 }));
expect(vacayCardSchema.safeParse({ ...card(), company }).success).toBe(true);
expect(storedTrekCardSchema.parse({ ...card(), later: true })).toEqual(card());
});
it('VACAYCARD-005: the carried-over days and the year asked for are optional, so older cards still read', () => {
const balances = [{ ...card().balances[0], carried: 2 }];
expect(vacayCardSchema.parse({ ...card(), balances, year: 2026 })).toMatchObject({ balances, year: 2026 });
expect(vacayCardSchema.safeParse({ ...card(), year: 1999 }).success).toBe(false);
const proposal = { ...card().proposal, kind: 'company', personId: null, notes: ['companyOff'] };
expect(vacayCardSchema.safeParse({ ...card(), proposal }).success).toBe(true);
});
it('VACAYCARD-004: its verbs and its entity join the shared enums', () => {
for (const verb of VACAY_VERB_KEYS) expect(ASSISTANT_CHANGE_VERB_KEYS).toContain(verb);
const applied = { toolCallId: 'c1', entity: VACAY_APPLIED_ENTITIES[0], ids: [], dayId: null, undoUntil: null };
expect(assistantAppliedSchema.parse(applied).entity).toBe('vacay');
});
});
+124 -3
View File
@@ -1,8 +1,129 @@
import { idSchema } from '../../common/primitives.schema';
import { cardDateSchema, cardHexColorSchema, cardPersonSchema, cardText } from './card-parts';
import { z } from 'zod';
/**
* Vacation days on a month calendar, with the days left. Only its kind so far; the vacation planner
* cards give it its fields.
* Vacation days on a month calendar, with the days left. An answer card shows
* what a read found (the days off of a year, the days left, the public
* holidays); a change card shows the one day a held write would take or give
* back, on its month, with the leave before it. The people are the vacation
* plan's, which need not be the trip's: a fused plan holds partners too.
*/
export const vacayCardSchema = z.object({ kind: z.literal('vacay') });
const fractionSchema = z.union([z.literal(0.5), z.literal(1)]);
const yearSchema = z.number().int().min(2000).max(2100);
/** Days are counted in halves, so a count is a multiple of 0.5. */
const daysSchema = z.number().min(0).max(1000);
/** What a day off is: leave that counts, a comp day that does not, or a holiday the company gives everyone. */
export const VACAY_DAY_KINDS = ['vacation', 'comp', 'company'] as const;
/** What a held toggle would do to its day. */
export const VACAY_PROPOSAL_OUTCOMES = ['add', 'remove', 'convert', 'blocked'] as const;
/** What a held toggle should know about its day before it is applied. */
export const VACAY_PROPOSAL_NOTES = [
'weekendBlocked',
'weekend',
'publicHoliday',
'companyHoliday',
'companyHalf',
'notInPlan',
'companyOff',
] as const;
/** Someone of the vacation plan: their own colour colours their days. */
export const vacayCardPersonSchema = cardPersonSchema.extend({ color: cardHexColorSchema.nullable() });
/** A logged day off of one person. */
export const vacayCardDaySchema = z.object({
date: cardDateSchema,
personId: idSchema,
fraction: fractionSchema,
kind: z.enum(['vacation', 'comp']),
});
/** A public holiday, in its calendar's colour when the plan has one. */
export const vacayCardHolidaySchema = z.object({
date: cardDateSchema,
name: cardText(80),
color: cardHexColorSchema.nullable(),
});
/** The day a held write would change, and what that does to the days left. */
export const vacayCardProposalSchema = z.object({
date: cardDateSchema,
/** Null for a company holiday, which is the whole plan's. */
personId: idSchema.nullable(),
kind: z.enum(VACAY_DAY_KINDS),
fraction: fractionSchema,
outcome: z.enum(VACAY_PROPOSAL_OUTCOMES),
/** What the day was before, when it was logged already. */
before: z.object({ fraction: fractionSchema, kind: z.enum(VACAY_DAY_KINDS) }).nullable(),
/** The leave year the day counts in, which runs from the person's own start day. */
periodYear: yearSchema.nullable(),
notes: z.array(z.enum(VACAY_PROPOSAL_NOTES)).max(VACAY_PROPOSAL_NOTES.length),
/** The change to each person's used days in a leave year; a company holiday can give several people days back. */
deltas: z.array(z.object({ personId: idSchema, year: yearSchema, days: z.number().min(-1).max(1) })).max(8),
});
/** One person's leave in one leave year, as it stands before the change. */
export const vacayCardBalanceSchema = z.object({
personId: idSchema,
year: yearSchema,
/** The allowance with what was carried over. */
total: daysSchema,
/** What was carried over into the year, so the allowance alone is `total - carried`; absent on older cards. */
carried: daysSchema.optional(),
used: daysSchema,
comp: daysSchema,
/** The new total a change of the allowance would give; null for anything else. */
totalAfter: daysSchema.nullable(),
windowStart: cardDateSchema,
windowEnd: cardDateSchema,
});
/**
* The bounds keep the heaviest card (three busy months of a plan of eight)
* under MAX_CARD_CHARS; the server keeps the days of the months it shows
* first and drops the rest.
*/
export const VACAY_CARD_MAX_DAYS = 150;
export const VACAY_CARD_MAX_HOLIDAYS = 30;
/** Every date of the three months a card shows at most: the trip days and the company holidays. */
export const VACAY_CARD_MAX_DATES = 93;
export const vacayCardSchema = z.object({
kind: z.literal('vacay'),
/** The months the calendar shows, in order; none for a card of days left only. */
months: z.array(z.object({ year: yearSchema, month: z.number().int().min(1).max(12) })).max(3),
/** The first column: 0 for Sunday, 1 for Monday. */
weekStart: z.union([z.literal(0), z.literal(1)]),
/** The weekdays that count as weekend, 0 for Sunday. */
weekendDays: z.array(z.number().int().min(0).max(6)).max(7),
people: z.array(vacayCardPersonSchema).max(8),
/** The logged days inside `months`. */
days: z.array(vacayCardDaySchema).max(VACAY_CARD_MAX_DAYS),
company: z.array(z.object({ date: cardDateSchema, fraction: fractionSchema })).max(VACAY_CARD_MAX_DATES),
holidays: z.array(vacayCardHolidaySchema).max(VACAY_CARD_MAX_HOLIDAYS),
/** The dated days of this trip inside `months`. */
tripDates: z.array(cardDateSchema).max(VACAY_CARD_MAX_DATES),
/** A change card's one day; null on an answer. */
proposal: vacayCardProposalSchema.nullable(),
balances: z.array(vacayCardBalanceSchema).max(8),
/** The days off of the signed-in user in the year outside `months`. */
otherDays: daysSchema,
/** The leave year a read of the days off asked for, which Vacay opens on; absent elsewhere. */
year: yearSchema.optional(),
});
export type VacayCard = z.infer<typeof vacayCardSchema>;
export type VacayCardPerson = z.infer<typeof vacayCardPersonSchema>;
export type VacayCardDay = z.infer<typeof vacayCardDaySchema>;
export type VacayCardHoliday = z.infer<typeof vacayCardHolidaySchema>;
export type VacayCardProposal = z.infer<typeof vacayCardProposalSchema>;
export type VacayCardBalance = z.infer<typeof vacayCardBalanceSchema>;
export type VacayProposalNote = (typeof VACAY_PROPOSAL_NOTES)[number];
export type VacayProposalOutcome = (typeof VACAY_PROPOSAL_OUTCOMES)[number];
export type VacayDayKind = (typeof VACAY_DAY_KINDS)[number];
+10 -2
View File
@@ -10,5 +10,13 @@ export * from './vacay.card';
* or entity is added here and nowhere else.
*/
export const VACAY_CARD_SCHEMAS = [vacayCardSchema] as const;
export const VACAY_VERB_KEYS = [] as const;
export const VACAY_APPLIED_ENTITIES = [] as const;
/** A day off taken, given back or changed, a company holiday set, the allowance changed. */
export const VACAY_VERB_KEYS = [
'addVacationDay',
'removeVacationDay',
'changeVacationDay',
'setCompanyHoliday',
'setVacationAllowance',
] as const;
/** A day or the allowance of the vacation plan: the receipt opens the vacation planner. */
export const VACAY_APPLIED_ENTITIES = ['vacay'] as const;