WP-02: locations, teams and the legal configuration register
Adds the referential models, the first database-backed settings screens, and the register the compliance matrix requires before any parameter is enforceable. The register is the point of the lot. The matrix is explicit that copying another product's configuration is not enough — each parameter must carry its value, source, effective date, population and an approver. Approval records the session's actor, never a form field: a signature you can type yourself is worth nothing. The screen names the domains that have no approved parameter yet, so the gap is visible rather than assumed closed. Two bugs of the same family, both now structurally impossible: - The Prisma scoping extension read a hand-written list of models carrying accountId. The four models added here were missing from it, so writes failed with an opaque Prisma error — and a read would have silently returned every account's rows. The list is now derived from the schema itself. - The RLS policies were likewise per-table. A new integration test fails if any table with an accountId column lacks forced RLS and both policies, which is the failure mode that hides best: nobody writes a wrong rule, someone forgets to write one. An end-to-end test signs in as a manager and confirms the settings screens refuse to render — the sidebar hiding them is a convenience, the server check is the control. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Cr9dkEHwbDgkWPnyGj1Rjv
This commit is contained in:
20 files changed
+1475
-17
No files matched your search
@@ -0,0 +1,57 @@
|
||||
'use client';
|
||||
|
||||
import { useActionState } from 'react';
|
||||
|
||||
import { Field, FormError, SubmitButton } from '@/components/ui/Form';
|
||||
import {
|
||||
createLocationAction,
|
||||
type ActionState,
|
||||
} from '@/server/settings/locations';
|
||||
|
||||
export function AddLocationForm() {
|
||||
const [state, formAction] = useActionState<ActionState, FormData>(
|
||||
createLocationAction,
|
||||
{},
|
||||
);
|
||||
|
||||
return (
|
||||
<form action={formAction} className="flex flex-col gap-3">
|
||||
<div className="flex flex-wrap gap-3">
|
||||
<Field label="Nom" name="name" required placeholder="Nantes Atlantis" />
|
||||
<Field
|
||||
label="SIRET"
|
||||
name="siret"
|
||||
inputMode="numeric"
|
||||
placeholder="14 chiffres"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-3">
|
||||
<Field
|
||||
label="Fuseau horaire"
|
||||
name="timezone"
|
||||
defaultValue="Europe/Paris"
|
||||
hint="Les durées se calculent depuis les instants ; le fuseau ne sert qu'à l'affichage et au regroupement par jour."
|
||||
/>
|
||||
<Field
|
||||
label="Cotisations patronales"
|
||||
name="employerContributionRate"
|
||||
type="number"
|
||||
step="0.01"
|
||||
min="0"
|
||||
max="100"
|
||||
defaultValue="42"
|
||||
hint="Taux moyen, utilisé pour le coût prévisionnel du planning."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FormError>{state.error}</FormError>
|
||||
|
||||
<div className="flex items-center gap-3">
|
||||
<SubmitButton>Créer l’établissement</SubmitButton>
|
||||
{state.ok ? (
|
||||
<span className="text-xs text-ok-soft-ink">Établissement créé.</span>
|
||||
) : null}
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
'use client';
|
||||
|
||||
import { useActionState } from 'react';
|
||||
|
||||
import { Field, FormError, SubmitButton } from '@/components/ui/Form';
|
||||
import {
|
||||
createTeamAction,
|
||||
type ActionState,
|
||||
} from '@/server/settings/locations';
|
||||
|
||||
export function AddTeamForm({ locationId }: { locationId: string }) {
|
||||
const [state, formAction] = useActionState<ActionState, FormData>(
|
||||
createTeamAction,
|
||||
{},
|
||||
);
|
||||
|
||||
return (
|
||||
<form action={formAction} className="flex flex-wrap items-end gap-2">
|
||||
<input type="hidden" name="locationId" value={locationId} />
|
||||
<Field label="Nouvelle équipe" name="name" placeholder="Caisse" required />
|
||||
<SubmitButton size="md">Ajouter</SubmitButton>
|
||||
<div className="w-full">
|
||||
<FormError>{state.error}</FormError>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import { PageBody, PageHeader } from '@/components/shell/PageHeader';
|
||||
import { Badge } from '@/components/ui/Badge';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { Card, CardHeader, EmptyState } from '@/components/ui/Card';
|
||||
import { AddLocationForm } from '@/app/(app)/reglages/etablissements/AddLocationForm';
|
||||
import { AddTeamForm } from '@/app/(app)/reglages/etablissements/AddTeamForm';
|
||||
import { archiveLocationAction, listLocations } from '@/server/settings/locations';
|
||||
|
||||
export const metadata = { title: 'Établissements · PlanFlow' };
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export default async function EtablissementsPage() {
|
||||
const locations = await listLocations();
|
||||
|
||||
return (
|
||||
<PageBody>
|
||||
<PageHeader
|
||||
title="Établissements"
|
||||
subtitle={`${locations.length} établissement${locations.length > 1 ? 's' : ''} actif${locations.length > 1 ? 's' : ''}`}
|
||||
/>
|
||||
|
||||
{locations.length === 0 ? (
|
||||
<Card>
|
||||
<EmptyState
|
||||
title="Aucun établissement"
|
||||
description="Créez le premier établissement pour commencer à planifier."
|
||||
/>
|
||||
</Card>
|
||||
) : null}
|
||||
|
||||
<div className="flex flex-col gap-3">
|
||||
{locations.map((location) => (
|
||||
<Card key={location.id}>
|
||||
<CardHeader
|
||||
title={location.name}
|
||||
badge={
|
||||
<Badge tone="neutral">
|
||||
{location.teams.length} équipe
|
||||
{location.teams.length > 1 ? 's' : ''}
|
||||
</Badge>
|
||||
}
|
||||
action={
|
||||
<form action={archiveLocationAction}>
|
||||
<input type="hidden" name="id" value={location.id} />
|
||||
<Button size="sm" variant="ghost" type="submit">
|
||||
Archiver
|
||||
</Button>
|
||||
</form>
|
||||
}
|
||||
/>
|
||||
|
||||
<dl className="grid gap-x-8 gap-y-2 px-4 py-3 text-sm [grid-template-columns:repeat(auto-fit,minmax(180px,1fr))]">
|
||||
<div>
|
||||
<dt className="text-micro text-ink-3">SIRET</dt>
|
||||
<dd className="tnum">{location.siret ?? '—'}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt className="text-micro text-ink-3">Fuseau horaire</dt>
|
||||
<dd>{location.timezone}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt className="text-micro text-ink-3">
|
||||
Cotisations patronales
|
||||
</dt>
|
||||
<dd className="tnum">{location.employerContributionRate} %</dd>
|
||||
</div>
|
||||
</dl>
|
||||
|
||||
<div className="border-t border-line-1 px-4 py-3">
|
||||
<p className="mb-2 text-micro font-semibold tracking-[0.06em] text-ink-3 uppercase">
|
||||
Équipes
|
||||
</p>
|
||||
<ul className="mb-3 flex flex-wrap gap-1.5">
|
||||
{location.teams.length === 0 ? (
|
||||
<li className="text-sm text-ink-3">Aucune équipe</li>
|
||||
) : (
|
||||
location.teams.map((team) => (
|
||||
<li key={team.id}>
|
||||
<Badge tone="neutral">{team.name}</Badge>
|
||||
</li>
|
||||
))
|
||||
)}
|
||||
</ul>
|
||||
<AddTeamForm locationId={location.id} />
|
||||
</div>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader title="Nouvel établissement" />
|
||||
<div className="p-4">
|
||||
<AddLocationForm />
|
||||
</div>
|
||||
</Card>
|
||||
</PageBody>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
'use client';
|
||||
|
||||
import { useActionState } from 'react';
|
||||
|
||||
import { Field, FormError, SubmitButton } from '@/components/ui/Form';
|
||||
import { LEGAL_DOMAINS } from '@/domain/legal/domains';
|
||||
import {
|
||||
addLegalEntryAction,
|
||||
type ActionState,
|
||||
} from '@/server/settings/legal-register';
|
||||
|
||||
export function AddEntryForm() {
|
||||
const [state, formAction] = useActionState<ActionState, FormData>(
|
||||
addLegalEntryAction,
|
||||
{},
|
||||
);
|
||||
|
||||
return (
|
||||
<form action={formAction} className="flex flex-col gap-3">
|
||||
<div className="flex flex-wrap gap-3">
|
||||
<label className="flex min-w-0 flex-1 flex-col gap-1.5">
|
||||
<span className="text-sm font-medium">Domaine</span>
|
||||
<select
|
||||
name="domain"
|
||||
required
|
||||
className="h-9 rounded-2 border border-line-2 bg-surface px-3 text-sm text-ink-1 outline-none focus-visible:border-focus"
|
||||
>
|
||||
{LEGAL_DOMAINS.map((domain) => (
|
||||
<option key={domain.key} value={domain.key}>
|
||||
{domain.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<Field
|
||||
label="Paramètre"
|
||||
name="key"
|
||||
required
|
||||
placeholder="Durée quotidienne maximale"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap gap-3">
|
||||
<Field label="Valeur" name="value" required placeholder="10 h" />
|
||||
<Field
|
||||
label="Source"
|
||||
name="source"
|
||||
required
|
||||
placeholder="IDCC 1517, texte consolidé Legifrance du…"
|
||||
hint="Texte, article, ou référence de l’accord. Une valeur sans source n’est pas opposable."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap gap-3">
|
||||
<Field
|
||||
label="Date d’effet"
|
||||
name="effectiveFrom"
|
||||
type="date"
|
||||
required
|
||||
defaultValue="2026-01-01"
|
||||
/>
|
||||
<Field
|
||||
label="Population"
|
||||
name="population"
|
||||
required
|
||||
defaultValue="Tous les salariés"
|
||||
placeholder="Cadres autonomes niveaux VII à IX"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FormError>{state.error}</FormError>
|
||||
|
||||
<div className="flex items-center gap-3">
|
||||
<SubmitButton>Consigner</SubmitButton>
|
||||
{state.ok ? (
|
||||
<span className="text-xs text-ok-soft-ink">
|
||||
Paramètre consigné — il reste à approuver.
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
import { AddEntryForm } from '@/app/(app)/reglages/registre/AddEntryForm';
|
||||
import { PageBody, PageHeader } from '@/components/shell/PageHeader';
|
||||
import { Badge } from '@/components/ui/Badge';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { Card, CardHeader, EmptyState } from '@/components/ui/Card';
|
||||
import { LEGAL_DOMAINS } from '@/domain/legal/domains';
|
||||
import {
|
||||
approveLegalEntryAction,
|
||||
readLegalRegister,
|
||||
} from '@/server/settings/legal-register';
|
||||
|
||||
export const metadata = { title: 'Registre de paramétrage · PlanFlow' };
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
const DOMAIN_LABELS = new Map<string, string>(
|
||||
LEGAL_DOMAINS.map((domain) => [domain.key, domain.label]),
|
||||
);
|
||||
|
||||
const dateFormat = new Intl.DateTimeFormat('fr-FR', { dateStyle: 'long' });
|
||||
|
||||
export default async function RegistrePage() {
|
||||
const register = await readLegalRegister();
|
||||
|
||||
return (
|
||||
<PageBody>
|
||||
<PageHeader
|
||||
title="Registre de paramétrage juridique"
|
||||
subtitle={`${register.approvedCount} paramètre${register.approvedCount > 1 ? 's' : ''} approuvé${register.approvedCount > 1 ? 's' : ''} · ${register.pendingCount} en attente`}
|
||||
/>
|
||||
|
||||
<Card>
|
||||
<div className="p-4 text-sm leading-[var(--lh-prose)] text-ink-2">
|
||||
<p>
|
||||
Chaque paramètre appliqué par PlanFlow doit porter sa{' '}
|
||||
<strong className="font-semibold text-ink-1">valeur</strong>, sa{' '}
|
||||
<strong className="font-semibold text-ink-1">source</strong>, sa{' '}
|
||||
<strong className="font-semibold text-ink-1">date d’effet</strong>,
|
||||
la <strong className="font-semibold text-ink-1">population</strong>{' '}
|
||||
concernée et un{' '}
|
||||
<strong className="font-semibold text-ink-1">approbateur</strong>.
|
||||
</p>
|
||||
<p className="mt-2">
|
||||
Recopier la configuration d’un autre logiciel ne suffit pas : sans
|
||||
justification conservée, un paramètre n’est pas opposable en cas de
|
||||
contrôle. Les valeurs de la convention IDCC 1517 déjà chargées dans
|
||||
le moteur restent à recouper avec le texte consolidé et à faire
|
||||
valider par le gestionnaire de paie.
|
||||
</p>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
{register.missingDomains.length > 0 ? (
|
||||
<Card className="border-warn">
|
||||
<CardHeader
|
||||
title="Domaines sans paramètre approuvé"
|
||||
badge={<Badge tone="warn">{register.missingDomains.length}</Badge>}
|
||||
/>
|
||||
<ul className="flex flex-wrap gap-1.5 p-4">
|
||||
{register.missingDomains.map((domain) => (
|
||||
<li key={domain}>
|
||||
<Badge tone="warn">{domain}</Badge>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</Card>
|
||||
) : null}
|
||||
|
||||
<Card>
|
||||
<CardHeader title="Paramètres enregistrés" />
|
||||
{register.entries.length === 0 ? (
|
||||
<EmptyState
|
||||
title="Registre vide"
|
||||
description="Aucun paramètre n’a encore été consigné. Tant que le registre est vide, aucune valeur appliquée par l’application n’est justifiée."
|
||||
/>
|
||||
) : (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full min-w-[900px] border-collapse text-sm">
|
||||
<thead>
|
||||
<tr className="border-b border-line-2 bg-surface-2 text-left text-micro font-semibold tracking-[0.06em] text-ink-3 uppercase">
|
||||
<th className="px-4 py-2.5">Domaine</th>
|
||||
<th className="px-4 py-2.5">Paramètre</th>
|
||||
<th className="px-4 py-2.5">Valeur</th>
|
||||
<th className="px-4 py-2.5">Source</th>
|
||||
<th className="px-4 py-2.5">Effet</th>
|
||||
<th className="px-4 py-2.5">Population</th>
|
||||
<th className="px-4 py-2.5">Approbation</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{register.entries.map((entry) => (
|
||||
<tr
|
||||
key={entry.id}
|
||||
className="border-b border-line-1 last:border-b-0"
|
||||
>
|
||||
<td className="px-4 py-2.5 text-ink-2">
|
||||
{DOMAIN_LABELS.get(entry.domain) ?? entry.domain}
|
||||
</td>
|
||||
<td className="px-4 py-2.5 font-medium">{entry.key}</td>
|
||||
<td className="tnum px-4 py-2.5">{entry.value}</td>
|
||||
<td className="px-4 py-2.5 text-ink-2">{entry.source}</td>
|
||||
<td className="tnum px-4 py-2.5 text-ink-2">
|
||||
{dateFormat.format(entry.effectiveFrom)}
|
||||
</td>
|
||||
<td className="px-4 py-2.5 text-ink-2">
|
||||
{entry.population}
|
||||
</td>
|
||||
<td className="px-4 py-2.5">
|
||||
{entry.approvedAt ? (
|
||||
<Badge tone="ok">
|
||||
Approuvé le {dateFormat.format(entry.approvedAt)}
|
||||
</Badge>
|
||||
) : (
|
||||
<form action={approveLegalEntryAction}>
|
||||
<input type="hidden" name="id" value={entry.id} />
|
||||
<Button size="sm" type="submit">
|
||||
Approuver
|
||||
</Button>
|
||||
</form>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader title="Consigner un paramètre" />
|
||||
<div className="p-4">
|
||||
<AddEntryForm />
|
||||
</div>
|
||||
</Card>
|
||||
</PageBody>
|
||||
);
|
||||
}
|
||||
@@ -75,9 +75,10 @@ export const NAVIGATION: NavSection[] = [
|
||||
id: 'reglages',
|
||||
label: 'Réglages',
|
||||
items: [
|
||||
{ id: 'sites', label: 'Établissements', href: '/reglages/etablissements' },
|
||||
{ id: 'registre', label: 'Registre de paramétrage', href: '/reglages/registre' },
|
||||
{ id: 'convention', label: 'Convention collective' },
|
||||
{ id: 'postes', label: 'Postes et étiquettes' },
|
||||
{ id: 'sites', label: 'Établissements' },
|
||||
{ id: 'roles', label: 'Rôles et permissions' },
|
||||
],
|
||||
},
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
'use client';
|
||||
|
||||
import type { InputHTMLAttributes, ReactNode } from 'react';
|
||||
import { useFormStatus } from 'react-dom';
|
||||
|
||||
import { Button, type ButtonProps } from '@/components/ui/Button';
|
||||
import { cx } from '@/lib/cx';
|
||||
|
||||
export interface FieldProps extends InputHTMLAttributes<HTMLInputElement> {
|
||||
label: string;
|
||||
hint?: string;
|
||||
}
|
||||
|
||||
export function Field({ label, hint, className, ...rest }: FieldProps) {
|
||||
return (
|
||||
<label className="flex min-w-0 flex-1 flex-col gap-1.5">
|
||||
<span className="text-sm font-medium">{label}</span>
|
||||
<input
|
||||
{...rest}
|
||||
className={cx(
|
||||
'h-9 rounded-2 border border-line-2 bg-surface px-3 text-sm text-ink-1',
|
||||
'outline-none placeholder:text-ink-3 focus-visible:border-focus',
|
||||
className,
|
||||
)}
|
||||
/>
|
||||
{hint ? <span className="text-micro text-ink-3">{hint}</span> : null}
|
||||
</label>
|
||||
);
|
||||
}
|
||||
|
||||
/** Bouton de soumission qui se désactive pendant l'envoi, pour éviter le double clic. */
|
||||
export function SubmitButton({ children, ...rest }: ButtonProps) {
|
||||
const { pending } = useFormStatus();
|
||||
return (
|
||||
<Button type="submit" variant="primary" disabled={pending} {...rest}>
|
||||
{pending ? 'Enregistrement…' : children}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
|
||||
export function FormError({ children }: { children: ReactNode }) {
|
||||
if (!children) return null;
|
||||
return (
|
||||
<p
|
||||
role="alert"
|
||||
className="rounded-2 border border-danger bg-danger-soft px-3 py-2 text-xs text-danger-soft-ink"
|
||||
>
|
||||
{children}
|
||||
</p>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Domaines du registre de paramétrage juridique — matrice, section
|
||||
* « Paramétrage juridique minimal à faire signer avant migration ».
|
||||
*
|
||||
* Dans son propre module car un fichier « use server » ne peut exporter que des
|
||||
* fonctions asynchrones, et ces constantes sont aussi lues côté client.
|
||||
*/
|
||||
export const LEGAL_DOMAINS = [
|
||||
{ key: 'identite', label: 'Identité juridique' },
|
||||
{ key: 'populations', label: 'Populations' },
|
||||
{ key: 'temps', label: 'Temps' },
|
||||
{ key: 'remuneration', label: 'Rémunération' },
|
||||
{ key: 'absences', label: 'Absences' },
|
||||
{ key: 'paie', label: 'Paie et déclarations' },
|
||||
{ key: 'vie-privee', label: 'Vie privée' },
|
||||
{ key: 'securite', label: 'Sécurité' },
|
||||
] as const;
|
||||
|
||||
export type LegalDomainKey = (typeof LEGAL_DOMAINS)[number]['key'];
|
||||
|
||||
export const LEGAL_DOMAIN_KEYS: readonly string[] = LEGAL_DOMAINS.map(
|
||||
(domain) => domain.key,
|
||||
);
|
||||
@@ -0,0 +1,55 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
import {
|
||||
authorize,
|
||||
type Actor,
|
||||
type ResourceRef,
|
||||
} from '@/domain/access/authorize';
|
||||
import type { PermissionCode } from '@/domain/access/permissions';
|
||||
import { currentSession, type SessionContext } from '@/server/auth/session';
|
||||
import { withTenant, type ScopedClient } from '@/server/tenant';
|
||||
|
||||
/**
|
||||
* Contexte d'exécution d'une requête authentifiée.
|
||||
*
|
||||
* Toute lecture et toute écriture métier passent par ici. Le compte vient de la
|
||||
* session serveur, jamais d'un paramètre : c'est ce qui empêche un client de
|
||||
* désigner lui-même le périmètre qu'il veut lire.
|
||||
*/
|
||||
|
||||
export async function requireSession(): Promise<SessionContext> {
|
||||
const session = await currentSession();
|
||||
if (!session) redirect('/connexion');
|
||||
return session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exécute une lecture dans le périmètre de la session.
|
||||
*
|
||||
* `permission` est vérifiée **avant** d'ouvrir la transaction : un refus ne
|
||||
* doit pas laisser de trace d'accès en base.
|
||||
*/
|
||||
export async function query<T>(
|
||||
permission: PermissionCode,
|
||||
fn: (db: ScopedClient, actor: Actor) => Promise<T>,
|
||||
resource?: ResourceRef,
|
||||
): Promise<T> {
|
||||
const session = await requireSession();
|
||||
authorize(session.actor, permission, resource);
|
||||
return withTenant(session.actor.accountId, (db) => fn(db, session.actor));
|
||||
}
|
||||
|
||||
/**
|
||||
* Exécute une mutation dans le périmètre de la session.
|
||||
*
|
||||
* Identique à `query` par construction, mais nommée distinctement : une revue
|
||||
* de code doit pouvoir repérer d'un coup d'œil les points d'écriture, et
|
||||
* l'oubli d'un `authorize` s'y voit.
|
||||
*/
|
||||
export async function mutate<T>(
|
||||
permission: PermissionCode,
|
||||
fn: (db: ScopedClient, actor: Actor) => Promise<T>,
|
||||
resource?: ResourceRef,
|
||||
): Promise<T> {
|
||||
return query(permission, fn, resource);
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
'use server';
|
||||
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { AuthorizationError } from '@/domain/access/authorize';
|
||||
import { recordAudit } from '@/server/audit';
|
||||
import { LEGAL_DOMAINS, LEGAL_DOMAIN_KEYS } from '@/domain/legal/domains';
|
||||
import { mutate, query } from '@/server/context';
|
||||
|
||||
/**
|
||||
* Registre de paramétrage juridique — PLAN.md §12.7, matrice n° 1.
|
||||
*
|
||||
* La matrice est explicite : « Il ne suffit pas de copier la configuration d'un
|
||||
* autre logiciel : il faut conserver la justification de chaque paramètre. »
|
||||
*
|
||||
* Une valeur sans source, sans date d'effet et sans approbateur n'est pas
|
||||
* opposable. Ce registre est donc la contrepartie du jeu de paramètres IDCC
|
||||
* 1517 chargé en §6.3 : les valeurs existent, ce sont les preuves qui manquent.
|
||||
*/
|
||||
|
||||
export interface LegalEntryRow {
|
||||
id: string;
|
||||
domain: string;
|
||||
key: string;
|
||||
value: string;
|
||||
source: string;
|
||||
effectiveFrom: Date;
|
||||
population: string;
|
||||
approvedBy: string | null;
|
||||
approvedAt: Date | null;
|
||||
}
|
||||
|
||||
export interface LegalRegisterView {
|
||||
entries: LegalEntryRow[];
|
||||
/** Domaines sans aucune entrée approuvée. */
|
||||
missingDomains: string[];
|
||||
approvedCount: number;
|
||||
pendingCount: number;
|
||||
}
|
||||
|
||||
export async function readLegalRegister(): Promise<LegalRegisterView> {
|
||||
return query('settings.access', async (db) => {
|
||||
const entries = await db.legalConfigEntry.findMany({
|
||||
orderBy: [{ domain: 'asc' }, { key: 'asc' }],
|
||||
});
|
||||
|
||||
const approved = entries.filter((entry) => entry.approvedAt !== null);
|
||||
const domainsWithApproval = new Set(approved.map((entry) => entry.domain));
|
||||
|
||||
return {
|
||||
entries: entries.map((entry) => ({
|
||||
id: entry.id,
|
||||
domain: entry.domain,
|
||||
key: entry.key,
|
||||
value: entry.value,
|
||||
source: entry.source,
|
||||
effectiveFrom: entry.effectiveFrom,
|
||||
population: entry.population,
|
||||
approvedBy: entry.approvedBy,
|
||||
approvedAt: entry.approvedAt,
|
||||
})),
|
||||
missingDomains: LEGAL_DOMAINS.filter(
|
||||
(domain) => !domainsWithApproval.has(domain.key),
|
||||
).map((domain) => domain.label),
|
||||
approvedCount: approved.length,
|
||||
pendingCount: entries.length - approved.length,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
const entryInput = z.object({
|
||||
domain: z.enum(LEGAL_DOMAIN_KEYS as unknown as [string, ...string[]]),
|
||||
key: z.string().trim().min(1, 'Paramètre requis').max(120),
|
||||
value: z.string().trim().min(1, 'Valeur requise').max(500),
|
||||
source: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, 'Source requise — texte, article ou référence de l’accord')
|
||||
.max(500),
|
||||
effectiveFrom: z.coerce.date(),
|
||||
population: z.string().trim().min(1, 'Population requise').max(200),
|
||||
});
|
||||
|
||||
export interface ActionState {
|
||||
error?: string;
|
||||
ok?: boolean;
|
||||
}
|
||||
|
||||
export async function addLegalEntryAction(
|
||||
_previous: ActionState,
|
||||
formData: FormData,
|
||||
): Promise<ActionState> {
|
||||
const parsed = entryInput.safeParse({
|
||||
domain: formData.get('domain'),
|
||||
key: formData.get('key'),
|
||||
value: formData.get('value'),
|
||||
source: formData.get('source'),
|
||||
effectiveFrom: formData.get('effectiveFrom'),
|
||||
population: formData.get('population'),
|
||||
});
|
||||
|
||||
if (!parsed.success) {
|
||||
return { error: parsed.error.issues[0]?.message ?? 'Formulaire invalide' };
|
||||
}
|
||||
|
||||
try {
|
||||
await mutate('settings.agreement.manage', async (db, actor) => {
|
||||
const created = await db.legalConfigEntry.create({
|
||||
data: {
|
||||
domain: parsed.data.domain,
|
||||
key: parsed.data.key,
|
||||
value: parsed.data.value,
|
||||
source: parsed.data.source,
|
||||
effectiveFrom: parsed.data.effectiveFrom,
|
||||
population: parsed.data.population,
|
||||
} as never,
|
||||
});
|
||||
|
||||
await recordAudit(db, {
|
||||
actorMembershipId: actor.membershipId,
|
||||
action: 'legal_config.create',
|
||||
entityType: 'LegalConfigEntry',
|
||||
entityId: created.id,
|
||||
after: {
|
||||
domain: created.domain,
|
||||
key: created.key,
|
||||
value: created.value,
|
||||
source: created.source,
|
||||
},
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthorizationError) {
|
||||
return { error: "Vous n'avez pas le droit de gérer la convention." };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
revalidatePath('/reglages/registre');
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
export async function approveLegalEntryAction(
|
||||
formData: FormData,
|
||||
): Promise<void> {
|
||||
const id = String(formData.get('id') ?? '');
|
||||
if (!id) return;
|
||||
|
||||
await mutate('settings.agreement.manage', async (db, actor) => {
|
||||
const before = await db.legalConfigEntry.findUnique({ where: { id } });
|
||||
if (!before || before.approvedAt) return;
|
||||
|
||||
// L'approbateur est l'acteur de la session, jamais un champ du formulaire :
|
||||
// une signature qu'on peut saisir soi-même ne vaut rien.
|
||||
await db.legalConfigEntry.update({
|
||||
where: { id },
|
||||
data: { approvedBy: actor.membershipId, approvedAt: new Date() },
|
||||
});
|
||||
|
||||
await recordAudit(db, {
|
||||
actorMembershipId: actor.membershipId,
|
||||
action: 'legal_config.approve',
|
||||
entityType: 'LegalConfigEntry',
|
||||
entityId: id,
|
||||
before: { approvedAt: null },
|
||||
after: { approvedBy: actor.membershipId },
|
||||
reason: `Approbation du paramètre ${before.domain}.${before.key}`,
|
||||
});
|
||||
});
|
||||
|
||||
revalidatePath('/reglages/registre');
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
'use server';
|
||||
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { AuthorizationError } from '@/domain/access/authorize';
|
||||
import { recordAudit } from '@/server/audit';
|
||||
import { mutate, query } from '@/server/context';
|
||||
|
||||
/**
|
||||
* Établissements et équipes.
|
||||
*
|
||||
* Chaque mutation est autorisée puis journalisée **dans la même transaction**
|
||||
* que l'écriture : une modification sans trace, ou une trace sans modification,
|
||||
* seraient toutes deux des mensonges pour le contrôle.
|
||||
*/
|
||||
|
||||
export interface LocationRow {
|
||||
id: string;
|
||||
name: string;
|
||||
siret: string | null;
|
||||
timezone: string;
|
||||
employerContributionRate: string;
|
||||
archivedAt: Date | null;
|
||||
teams: Array<{ id: string; name: string; archivedAt: Date | null }>;
|
||||
}
|
||||
|
||||
export async function listLocations(
|
||||
includeArchived = false,
|
||||
): Promise<LocationRow[]> {
|
||||
return query('settings.access', async (db) => {
|
||||
const locations = await db.location.findMany({
|
||||
where: includeArchived ? {} : { archivedAt: null },
|
||||
orderBy: { name: 'asc' },
|
||||
include: {
|
||||
teams: {
|
||||
where: includeArchived ? {} : { archivedAt: null },
|
||||
orderBy: { position: 'asc' },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return locations.map((location) => ({
|
||||
id: location.id,
|
||||
name: location.name,
|
||||
siret: location.siret,
|
||||
timezone: location.timezone,
|
||||
employerContributionRate: location.employerContributionRate.toString(),
|
||||
archivedAt: location.archivedAt,
|
||||
teams: location.teams.map((team) => ({
|
||||
id: team.id,
|
||||
name: team.name,
|
||||
archivedAt: team.archivedAt,
|
||||
})),
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
const locationInput = z.object({
|
||||
name: z.string().trim().min(1, 'Nom requis').max(120),
|
||||
siret: z
|
||||
.string()
|
||||
.trim()
|
||||
.regex(/^\d{14}$/, 'Le SIRET compte 14 chiffres')
|
||||
.or(z.literal('')),
|
||||
timezone: z.string().trim().min(1),
|
||||
employerContributionRate: z.coerce
|
||||
.number()
|
||||
.min(0, 'Taux négatif impossible')
|
||||
.max(100, 'Un taux de cotisations dépasse rarement 100 %'),
|
||||
});
|
||||
|
||||
export interface ActionState {
|
||||
error?: string;
|
||||
ok?: boolean;
|
||||
}
|
||||
|
||||
export async function createLocationAction(
|
||||
_previous: ActionState,
|
||||
formData: FormData,
|
||||
): Promise<ActionState> {
|
||||
const parsed = locationInput.safeParse({
|
||||
name: formData.get('name'),
|
||||
siret: formData.get('siret') ?? '',
|
||||
timezone: formData.get('timezone') || 'Europe/Paris',
|
||||
employerContributionRate: formData.get('employerContributionRate') ?? 0,
|
||||
});
|
||||
|
||||
if (!parsed.success) {
|
||||
return { error: parsed.error.issues[0]?.message ?? 'Formulaire invalide' };
|
||||
}
|
||||
|
||||
try {
|
||||
await mutate('settings.locations.manage', async (db, actor) => {
|
||||
const created = await db.location.create({
|
||||
data: {
|
||||
name: parsed.data.name,
|
||||
siret: parsed.data.siret || null,
|
||||
timezone: parsed.data.timezone,
|
||||
employerContributionRate: parsed.data.employerContributionRate,
|
||||
} as never,
|
||||
});
|
||||
|
||||
await recordAudit(db, {
|
||||
actorMembershipId: actor.membershipId,
|
||||
action: 'location.create',
|
||||
entityType: 'Location',
|
||||
entityId: created.id,
|
||||
after: {
|
||||
name: created.name,
|
||||
siret: created.siret,
|
||||
timezone: created.timezone,
|
||||
},
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthorizationError) {
|
||||
return { error: "Vous n'avez pas le droit de gérer les établissements." };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
revalidatePath('/reglages/etablissements');
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
export async function archiveLocationAction(formData: FormData): Promise<void> {
|
||||
const id = String(formData.get('id') ?? '');
|
||||
if (!id) return;
|
||||
|
||||
await mutate('settings.locations.manage', async (db, actor) => {
|
||||
const before = await db.location.findUnique({ where: { id } });
|
||||
if (!before) return;
|
||||
|
||||
// Archiver plutôt que supprimer : un établissement fermé garde des
|
||||
// plannings, des contrats et des variables de paie dont la conservation
|
||||
// court encore (PLAN.md §12.5).
|
||||
await db.location.update({
|
||||
where: { id },
|
||||
data: { archivedAt: new Date() },
|
||||
});
|
||||
|
||||
await recordAudit(db, {
|
||||
actorMembershipId: actor.membershipId,
|
||||
action: 'location.archive',
|
||||
entityType: 'Location',
|
||||
entityId: id,
|
||||
before: { archivedAt: before.archivedAt },
|
||||
after: { archivedAt: new Date().toISOString() },
|
||||
});
|
||||
});
|
||||
|
||||
revalidatePath('/reglages/etablissements');
|
||||
}
|
||||
|
||||
const teamInput = z.object({
|
||||
locationId: z.string().min(1),
|
||||
name: z.string().trim().min(1, "Nom d'équipe requis").max(120),
|
||||
});
|
||||
|
||||
export async function createTeamAction(
|
||||
_previous: ActionState,
|
||||
formData: FormData,
|
||||
): Promise<ActionState> {
|
||||
const parsed = teamInput.safeParse({
|
||||
locationId: formData.get('locationId'),
|
||||
name: formData.get('name'),
|
||||
});
|
||||
|
||||
if (!parsed.success) {
|
||||
return { error: parsed.error.issues[0]?.message ?? 'Formulaire invalide' };
|
||||
}
|
||||
|
||||
try {
|
||||
await mutate(
|
||||
'settings.teams.manage',
|
||||
async (db, actor) => {
|
||||
// Le périmètre est revérifié en base : l'établissement doit exister
|
||||
// *dans ce compte*. Sans cette lecture, un identifiant soumis depuis le
|
||||
// formulaire pourrait désigner celui d'un autre client.
|
||||
const location = await db.location.findUnique({
|
||||
where: { id: parsed.data.locationId },
|
||||
});
|
||||
if (!location) {
|
||||
throw new AuthorizationError('settings.teams.manage');
|
||||
}
|
||||
|
||||
const count = await db.team.count({
|
||||
where: { locationId: location.id },
|
||||
});
|
||||
|
||||
const created = await db.team.create({
|
||||
data: {
|
||||
locationId: location.id,
|
||||
name: parsed.data.name,
|
||||
position: count,
|
||||
} as never,
|
||||
});
|
||||
|
||||
await recordAudit(db, {
|
||||
actorMembershipId: actor.membershipId,
|
||||
action: 'team.create',
|
||||
entityType: 'Team',
|
||||
entityId: created.id,
|
||||
after: { name: created.name, locationId: location.id },
|
||||
});
|
||||
},
|
||||
{ locationId: parsed.data.locationId },
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof AuthorizationError) {
|
||||
return { error: "Vous n'avez pas le droit de gérer les équipes." };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
revalidatePath('/reglages/etablissements');
|
||||
return { ok: true };
|
||||
}
|
||||
+19
-14
@@ -1,4 +1,4 @@
|
||||
import type { Prisma, PrismaClient } from '@prisma/client';
|
||||
import { Prisma, type PrismaClient } from '@prisma/client';
|
||||
|
||||
import { prisma } from '@/server/db';
|
||||
|
||||
@@ -17,18 +17,24 @@ import { prisma } from '@/server/db';
|
||||
* dire pourquoi ; la seconde seule tomberait avec le premier `$queryRaw`.
|
||||
*/
|
||||
|
||||
/** Tables portant une colonne `accountId`. */
|
||||
const SCOPED_MODELS = new Set([
|
||||
'Location',
|
||||
'Team',
|
||||
'Membership',
|
||||
'MembershipScope',
|
||||
'Invitation',
|
||||
'Role',
|
||||
'AuditLog',
|
||||
'RetentionPolicy',
|
||||
'FeatureFlag',
|
||||
]);
|
||||
/**
|
||||
* Modèles portant une colonne `accountId`, **dérivés du schéma**.
|
||||
*
|
||||
* Une liste tenue à la main se périme au premier modèle ajouté, et l'oubli est
|
||||
* silencieux : le scoping ne s'applique plus, et selon les cas la requête
|
||||
* échoue avec un message obscur ou — bien pire — réussit sans filtre.
|
||||
* La dériver du DMMF supprime le mode de défaillance plutôt que de compter sur
|
||||
* la vigilance.
|
||||
*/
|
||||
const SCOPED_MODELS = new Set(
|
||||
Prisma.dmmf.datamodel.models
|
||||
.filter((model) =>
|
||||
model.fields.some(
|
||||
(field) => field.name === 'accountId' && field.kind === 'scalar',
|
||||
),
|
||||
)
|
||||
.map((model) => model.name),
|
||||
);
|
||||
|
||||
const READ_OPERATIONS = new Set([
|
||||
'findFirst',
|
||||
@@ -135,4 +141,3 @@ export function unscoped(): PrismaClient {
|
||||
return prisma;
|
||||
}
|
||||
|
||||
export type { Prisma };
|
||||
Reference in new issue
Block a user