WP-03: employee records and contracts on real data

Replaces the demo module behind the team directory and the employee
record with scoped queries, and adds contracts, amendments, work
permits and the forfait-jours fields.

Writing the end-to-end test exposed a modelling error worth naming:
first and last names lived only on User, so an employee without an
application account had no name at all — the directory rendered
"— Salarié E0007". Most sales staff never sign in, and the personnel
register requires their name, so the name belongs to the record, not
to the login. Moved to EmployeeProfile with a data migration that
carries the existing names down from User.

Contract rules are pure functions tested at the boundaries. The case
that matters is an open-ended contract: a CDI with no end date overlaps
every later period, which a naive comparison of two date pairs misses,
and two overlapping active contracts would count one employee twice in
payroll. The check runs inside the transaction, not only in the form.

Forfait jours is refused without a written individual agreement and a
dated employee consent: without them the arrangement is unenforceable,
and enabling it would also switch off every weekly-duration control.

Salary and bank details are not merely hidden when the capability is
missing — they are never loaded. A field absent from the response
cannot leak through HTML, a log or an error message.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cr9dkEHwbDgkWPnyGj1Rjv
This commit is contained in:
Claude committed 2026-08-07 23:17:16 +00:00
1 parent f8050b06c7
commit 8a314991d3
18 files changed
+1667 -210

No files matched your search

+112
View File
@@ -0,0 +1,112 @@
/**
* Règles de cohérence des contrats.
*
* Fonctions pures : elles s'appliquent aussi bien à la validation d'un
* formulaire qu'au contrôle en transaction, et se testent sans base.
*/
export interface ContractPeriod {
id?: string;
startDate: Date;
/** `null` = contrat sans terme (CDI en cours). */
endDate: Date | null;
}
/**
* Deux périodes se chevauchent-elles ?
*
* Un contrat sans terme court indéfiniment : il chevauche donc toute période
* qui commence après lui. C'est le cas que l'on oublie en comparant bêtement
* deux couples de dates.
*/
export function periodsOverlap(a: ContractPeriod, b: ContractPeriod): boolean {
const aStart = a.startDate.getTime();
const bStart = b.startDate.getTime();
const aEnd = a.endDate?.getTime() ?? Number.POSITIVE_INFINITY;
const bEnd = b.endDate?.getTime() ?? Number.POSITIVE_INFINITY;
return aStart <= bEnd && bStart <= aEnd;
}
/** Contrats existants qui empêcheraient la création de `candidate`. */
export function findOverlaps(
candidate: ContractPeriod,
existing: ContractPeriod[],
): ContractPeriod[] {
return existing.filter(
(period) => period.id !== candidate.id && periodsOverlap(candidate, period),
);
}
export interface ContractValidationInput extends ContractPeriod {
workTimeArrangement: 'HOURLY' | 'FORFAIT_JOURS';
weeklyHours: number;
forfaitDaysPerYear?: number | null;
forfaitAgreementRef?: string | null;
forfaitAgreedAt?: Date | null;
}
export interface ValidationIssue {
field: string;
message: string;
}
/** Plafond conventionnel IDCC 1517, journée de solidarité incluse. */
export const FORFAIT_JOURS_CAP = 218;
export function validateContract(
input: ContractValidationInput,
): ValidationIssue[] {
const issues: ValidationIssue[] = [];
if (input.endDate && input.endDate < input.startDate) {
issues.push({
field: 'endDate',
message: 'La fin du contrat précède son début.',
});
}
if (input.workTimeArrangement === 'FORFAIT_JOURS') {
// Sans convention individuelle écrite et accord du salarié, le forfait est
// inopposable : l'activer produirait un décompte en jours sans base légale,
// et supprimerait au passage tout contrôle de durée hebdomadaire.
if (!input.forfaitAgreementRef?.trim()) {
issues.push({
field: 'forfaitAgreementRef',
message:
'Le forfait jours exige une convention individuelle écrite (référence du document).',
});
}
if (!input.forfaitAgreedAt) {
issues.push({
field: 'forfaitAgreedAt',
message: 'Le forfait jours exige la date d’accord du salarié.',
});
}
const days = input.forfaitDaysPerYear ?? 0;
if (days <= 0) {
issues.push({
field: 'forfaitDaysPerYear',
message: 'Indiquez le nombre de jours du forfait.',
});
} else if (days > FORFAIT_JOURS_CAP) {
issues.push({
field: 'forfaitDaysPerYear',
message: `Le plafond conventionnel est de ${FORFAIT_JOURS_CAP} jours, journée de solidarité incluse.`,
});
}
} else if (input.weeklyHours <= 0) {
issues.push({
field: 'weeklyHours',
message: 'La durée hebdomadaire doit être positive.',
});
}
return issues;
}
/** Un contrat au forfait ne se planifie pas en heures (PLAN.md §6.4). */
export function isHourScheduled(arrangement: string): boolean {
return arrangement === 'HOURLY';
}