mirror of
https://github.com/siop-spelev/siop2.git
synced 2026-08-08 12:41:54 +00:00
feat(r4.3): file d'écriture, verrou optimiste D2, Synchro & conflits
Serveur : updatedAt exposé au détail OT ; baseUpdatedAt optionnel sur transition/coche/bilan → 409 « Conflit de version » contextualisé (qui, quand) ; toute écriture secondaire (coche, bilan, commentaire, conso, MO) fait avancer la version — sans version fournie, le web est inchangé. ADR-003 : sécurité & protocole de routage mobile (qui vit où sur l'appareil, purge complète à la déconnexion — correctif réel : la file et le cache persisté survivaient au logout). Mobile : file persistée AsyncStorage rejouée dans l'ordre — succès → propagation de la version fraîche aux saisies restantes du même OT (nos écritures ne se conflictent pas entre elles, un écart étranger reste détecté) ; coupure → tout attend ; refus → CONFLIT, la file s'arrête, l'humain tranche (voir l'OT / rejouer sur version à jour / abandonner). Transitions, coches, bilan et photos (D5, compressées ~1600 px) passent par la file avec patch optimiste du cache ; écran Synchro (badge tabbar ambre/rouge) ; préchargement parc + référentiels (le bilan hors-ligne a ses vocabulaires). Recette « mode avion » 13/13 en Expo web piloté : gestes hors-ligne → 3 en file → modification concurrente de Salma → conflit tranché → serveur Terminé avec bilan. 17 tests jest-expo, 74 tests API. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { router, useLocalSearchParams } from 'expo-router';
|
||||
import { useState } from 'react';
|
||||
import { Alert, ScrollView, Text, View } from 'react-native';
|
||||
import { ScrollView, Text, View } from 'react-native';
|
||||
import { SafeAreaView } from 'react-native-safe-area-context';
|
||||
import {
|
||||
BILAN_FIELD_LABELS,
|
||||
@@ -9,16 +9,27 @@ import {
|
||||
type BilanField,
|
||||
type ReportUpsert,
|
||||
} from '@siop/shared';
|
||||
import { useBilan, useReferenceValues, useTransition, useWorkOrder } from '@/api/exploitation';
|
||||
import { useQueryClient } from '@tanstack/react-query';
|
||||
import { useReferenceValues, useWorkOrder } from '@/api/exploitation';
|
||||
import { useHorsLigne } from '@/auth/session';
|
||||
import { BoutonTel, Carte, ChoixTel, EnteteFiche } from '@/composants/ui';
|
||||
import { enfilerBilan, enfilerTransition } from '@/file/actions';
|
||||
import { useTokens } from '@/theme/tokens';
|
||||
|
||||
/** Écran 3 de la maquette R4 : le bilan codé R2 au pouce — 3 champs requis,
|
||||
* garde visible et bloquante (les MESSAGES viennent de l'API : une seule
|
||||
* source de vérité, comme le web). Clôture en ligne en R4.2. */
|
||||
* garde visible et bloquante. Depuis R4.3, bilan et clôture partent EN
|
||||
* FILE (D1) : hors-ligne, l'OT passe localement à « Terminé (en file) »
|
||||
* et le serveur tranchera au rejeu (verrou D2 si l'OT a bougé). */
|
||||
|
||||
const CHAMP_VERS_ID: Record<BilanField, keyof ReportUpsert> = {
|
||||
type ChampBilanId =
|
||||
| 'doorStateId'
|
||||
| 'cabinPositionId'
|
||||
| 'anomalyId'
|
||||
| 'externalCauseId'
|
||||
| 'actionTakenId'
|
||||
| 'componentConcernedId';
|
||||
|
||||
const CHAMP_VERS_ID: Record<BilanField, ChampBilanId> = {
|
||||
DOOR_STATE: 'doorStateId',
|
||||
CABIN_POSITION: 'cabinPositionId',
|
||||
ANOMALY: 'anomalyId',
|
||||
@@ -41,8 +52,7 @@ export default function PageCloture() {
|
||||
const { id } = useLocalSearchParams<{ id: string }>();
|
||||
const { data: ot } = useWorkOrder(id);
|
||||
const { data: valeurs } = useReferenceValues();
|
||||
const bilan = useBilan(id);
|
||||
const transition = useTransition(id);
|
||||
const queryClient = useQueryClient();
|
||||
const [choix, setChoix] = useState<Partial<Record<BilanField, string | null>>>({});
|
||||
|
||||
if (!ot) return null;
|
||||
@@ -61,27 +71,23 @@ export default function PageCloture() {
|
||||
return ot.report?.[CHAMP_VERS_VALEUR[champ]] ?? null;
|
||||
};
|
||||
|
||||
const enregistrer = (apres?: () => void) => {
|
||||
const enregistrer = () => {
|
||||
const corps: ReportUpsert = {};
|
||||
const labels: Parameters<typeof enfilerBilan>[3] = {};
|
||||
for (const champ of BILAN_FIELDS) {
|
||||
if (champ in choix) corps[CHAMP_VERS_ID[champ]] = choix[champ] ?? null;
|
||||
if (champ in choix) {
|
||||
corps[CHAMP_VERS_ID[champ]] = choix[champ] ?? null;
|
||||
labels[CHAMP_VERS_ID[champ]] = valeurDe(champ);
|
||||
}
|
||||
}
|
||||
bilan.mutate(corps, {
|
||||
onSuccess: apres,
|
||||
onError: (e) => Alert.alert('Bilan refusé', e.message),
|
||||
});
|
||||
enfilerBilan(queryClient, ot, corps, labels);
|
||||
};
|
||||
|
||||
const cloturer = () =>
|
||||
enregistrer(() =>
|
||||
transition.mutate(
|
||||
{ to: 'DONE' },
|
||||
{
|
||||
onSuccess: () => router.replace(`/ot/${id}`),
|
||||
onError: (e) => Alert.alert('Clôture bloquée', e.message),
|
||||
},
|
||||
),
|
||||
);
|
||||
const cloturer = () => {
|
||||
enregistrer();
|
||||
enfilerTransition(queryClient, ot, 'DONE');
|
||||
router.replace(`/ot/${id}`);
|
||||
};
|
||||
|
||||
const manquants = REQUIRED_BILAN_FIELDS.filter((c) => !valeurDe(c));
|
||||
|
||||
@@ -116,26 +122,29 @@ export default function PageCloture() {
|
||||
</Carte>
|
||||
|
||||
<BoutonTel
|
||||
libelle={bilan.isPending ? 'Enregistrement…' : 'Enregistrer le bilan'}
|
||||
libelle="Enregistrer le bilan"
|
||||
variante="contour"
|
||||
desactive={horsLigne || bilan.isPending}
|
||||
surAppui={() => enregistrer()}
|
||||
surAppui={() => {
|
||||
enregistrer();
|
||||
router.replace(`/ot/${id}`);
|
||||
}}
|
||||
/>
|
||||
<BoutonTel
|
||||
libelle={
|
||||
manquants.length
|
||||
? 'Clôturer (bilan incomplet)'
|
||||
: transition.isPending
|
||||
? 'Clôture…'
|
||||
: horsLigne
|
||||
? 'Clôturer — partira à la synchro'
|
||||
: "Clôturer l'intervention"
|
||||
}
|
||||
variante={manquants.length ? 'gris' : 'vert'}
|
||||
desactive={horsLigne || manquants.length > 0 || bilan.isPending || transition.isPending}
|
||||
desactive={manquants.length > 0}
|
||||
surAppui={cloturer}
|
||||
/>
|
||||
{horsLigne ? (
|
||||
<Text style={{ color: t.stAttente, fontFamily: 'Manrope_600SemiBold', fontSize: 12 }}>
|
||||
Hors-ligne : la clôture en file arrive en R4.3 — pour l’instant, revenez au réseau.
|
||||
Hors-ligne : la clôture est enregistrée sur le téléphone et sera rejouée telle quelle
|
||||
au retour du réseau. Si l’OT a changé entre-temps, VOUS trancherez (écran Synchro).
|
||||
</Text>
|
||||
) : null}
|
||||
</ScrollView>
|
||||
|
||||
Reference in New Issue
Block a user