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:
18 files changed
+1667
-210
No files matched your search
@@ -0,0 +1,222 @@
|
||||
import { can } from '@/domain/access/authorize';
|
||||
import { decryptOptional } from '@/server/crypto';
|
||||
import { query } from '@/server/context';
|
||||
|
||||
/**
|
||||
* Lecture des dossiers salariés.
|
||||
*
|
||||
* `members.salary.view` ne masque pas seulement l'affichage : la rémunération
|
||||
* n'est **pas chargée** quand la capacité manque. Un champ absent de la réponse
|
||||
* ne peut pas fuiter par le HTML, un journal ou une erreur — contrairement à un
|
||||
* champ chargé puis caché à l'écran.
|
||||
*/
|
||||
|
||||
export interface EmployeeListRow {
|
||||
id: string;
|
||||
employeeNumber: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string | null;
|
||||
status: string;
|
||||
hasAccount: boolean;
|
||||
roleName: string;
|
||||
contract: {
|
||||
type: string;
|
||||
label: string;
|
||||
since: Date;
|
||||
trialEndDate: Date | null;
|
||||
forfaitJours: boolean;
|
||||
} | null;
|
||||
locationName: string | null;
|
||||
}
|
||||
|
||||
const CONTRACT_LABELS: Record<string, string> = {
|
||||
APPRENTISSAGE: 'Apprentissage',
|
||||
CDD: 'CDD',
|
||||
CDI: 'CDI',
|
||||
DIRIGEANT_ASSIMILE_SALARIE: 'Dirigeant assimilé salarié',
|
||||
DIRIGEANT_NON_SALARIE: 'Dirigeant non salarié',
|
||||
EXTRA: 'Extra',
|
||||
INTERIM: 'Intérim',
|
||||
STAGIAIRE: 'Stagiaire',
|
||||
SAISONNIER: 'Saisonnier',
|
||||
};
|
||||
|
||||
export function contractLabel(type: string): string {
|
||||
return CONTRACT_LABELS[type] ?? type;
|
||||
}
|
||||
|
||||
export async function listEmployees(): Promise<EmployeeListRow[]> {
|
||||
return query('members.view', async (db) => {
|
||||
const memberships = await db.membership.findMany({
|
||||
where: { archivedAt: null },
|
||||
include: {
|
||||
profile: { select: { firstName: true, lastName: true } },
|
||||
user: { select: { email: true } },
|
||||
role: { select: { name: true } },
|
||||
contracts: {
|
||||
where: { status: 'ACTIVE' },
|
||||
orderBy: { startDate: 'desc' },
|
||||
take: 1,
|
||||
},
|
||||
},
|
||||
orderBy: { employeeNumber: 'asc' },
|
||||
});
|
||||
|
||||
const locationIds = [
|
||||
...new Set(
|
||||
memberships
|
||||
.flatMap((membership) => membership.contracts)
|
||||
.map((contract) => contract.locationId),
|
||||
),
|
||||
];
|
||||
const locations = await db.location.findMany({
|
||||
where: { id: { in: locationIds } },
|
||||
select: { id: true, name: true },
|
||||
});
|
||||
const locationNames = new Map(locations.map((l) => [l.id, l.name]));
|
||||
|
||||
return memberships.map((membership) => {
|
||||
const contract = membership.contracts[0];
|
||||
return {
|
||||
id: membership.id,
|
||||
employeeNumber: membership.employeeNumber,
|
||||
firstName: membership.profile?.firstName ?? '',
|
||||
lastName: membership.profile?.lastName ?? membership.employeeNumber,
|
||||
email: membership.user?.email ?? null,
|
||||
status: membership.status,
|
||||
hasAccount: membership.userId !== null,
|
||||
roleName: membership.role.name,
|
||||
contract: contract
|
||||
? {
|
||||
type: contract.contractType,
|
||||
label: contractLabel(contract.contractType),
|
||||
since: contract.startDate,
|
||||
trialEndDate: contract.trialEndDate,
|
||||
forfaitJours: contract.workTimeArrangement === 'FORFAIT_JOURS',
|
||||
}
|
||||
: null,
|
||||
locationName: contract
|
||||
? (locationNames.get(contract.locationId) ?? null)
|
||||
: null,
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export interface EmployeeDetail extends EmployeeListRow {
|
||||
profile: {
|
||||
birthDate: Date | null;
|
||||
city: string | null;
|
||||
phone: string | null;
|
||||
personalEmail: string | null;
|
||||
/** Chiffré au repos, déchiffré seulement pour qui a le droit de le lire. */
|
||||
socialSecurityNumber: string | null;
|
||||
iban: string | null;
|
||||
} | null;
|
||||
contracts: Array<{
|
||||
id: string;
|
||||
type: string;
|
||||
label: string;
|
||||
startDate: Date;
|
||||
endDate: Date | null;
|
||||
weeklyHours: string;
|
||||
forfaitJours: boolean;
|
||||
forfaitDaysPerYear: string | null;
|
||||
status: string;
|
||||
/** Absent quand `members.salary.view` manque. */
|
||||
monthlySalary: string | null;
|
||||
amendments: Array<{ id: string; effectiveDate: Date; reason: string | null }>;
|
||||
}>;
|
||||
canSeeSalary: boolean;
|
||||
}
|
||||
|
||||
export async function getEmployee(id: string): Promise<EmployeeDetail | null> {
|
||||
return query('members.view', async (db, actor) => {
|
||||
const canSeeSalary = can(actor, 'members.salary.view');
|
||||
const canSeeDocuments = can(actor, 'members.documents.view');
|
||||
|
||||
const membership = await db.membership.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
user: { select: { email: true } },
|
||||
role: { select: { name: true } },
|
||||
profile: true,
|
||||
contracts: {
|
||||
orderBy: { startDate: 'desc' },
|
||||
include: {
|
||||
amendments: { orderBy: { effectiveDate: 'desc' } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!membership) return null;
|
||||
|
||||
const active = membership.contracts.find(
|
||||
(contract) => contract.status === 'ACTIVE',
|
||||
);
|
||||
const location = active
|
||||
? await db.location.findUnique({
|
||||
where: { id: active.locationId },
|
||||
select: { name: true },
|
||||
})
|
||||
: null;
|
||||
|
||||
return {
|
||||
id: membership.id,
|
||||
employeeNumber: membership.employeeNumber,
|
||||
firstName: membership.profile?.firstName ?? '',
|
||||
lastName: membership.profile?.lastName ?? membership.employeeNumber,
|
||||
email: membership.user?.email ?? null,
|
||||
status: membership.status,
|
||||
hasAccount: membership.userId !== null,
|
||||
roleName: membership.role.name,
|
||||
locationName: location?.name ?? null,
|
||||
contract: active
|
||||
? {
|
||||
type: active.contractType,
|
||||
label: contractLabel(active.contractType),
|
||||
since: active.startDate,
|
||||
trialEndDate: active.trialEndDate,
|
||||
forfaitJours: active.workTimeArrangement === 'FORFAIT_JOURS',
|
||||
}
|
||||
: null,
|
||||
profile: membership.profile
|
||||
? {
|
||||
birthDate: membership.profile.birthDate,
|
||||
city: membership.profile.city,
|
||||
phone: membership.profile.phone,
|
||||
personalEmail: membership.profile.personalEmail,
|
||||
// Le NIR et l'IBAN ne sont déchiffrés que pour un profil habilité.
|
||||
socialSecurityNumber: canSeeDocuments
|
||||
? decryptOptional(membership.profile.socialSecurityNumberEnc)
|
||||
: null,
|
||||
iban: canSeeDocuments
|
||||
? decryptOptional(membership.profile.ibanEnc)
|
||||
: null,
|
||||
}
|
||||
: null,
|
||||
contracts: membership.contracts.map((contract) => ({
|
||||
id: contract.id,
|
||||
type: contract.contractType,
|
||||
label: contractLabel(contract.contractType),
|
||||
startDate: contract.startDate,
|
||||
endDate: contract.endDate,
|
||||
weeklyHours: contract.weeklyHours.toString(),
|
||||
forfaitJours: contract.workTimeArrangement === 'FORFAIT_JOURS',
|
||||
forfaitDaysPerYear: contract.forfaitDaysPerYear?.toString() ?? null,
|
||||
status: contract.status,
|
||||
monthlySalary: canSeeSalary
|
||||
? (contract.monthlySalary?.toString() ?? null)
|
||||
: null,
|
||||
amendments: contract.amendments.map((amendment) => ({
|
||||
id: amendment.id,
|
||||
effectiveDate: amendment.effectiveDate,
|
||||
reason: amendment.reason,
|
||||
})),
|
||||
})),
|
||||
canSeeSalary,
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user