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:
pr-daaif
2026-07-17 09:50:02 +01:00
parent a22ea60f83
commit c8b3c1769a
26 changed files with 1218 additions and 84 deletions

View File

@@ -0,0 +1,98 @@
import { useQueryClient } from '@tanstack/react-query';
import * as ImageManipulator from 'expo-image-manipulator';
import * as ImagePicker from 'expo-image-picker';
import { Alert, Pressable, Text, View } from 'react-native';
import type { WorkOrderDetail } from '@siop/shared';
import { useDocumentsOT } from '@/api/exploitation';
import { Carte } from '@/composants/ui';
import { enfilerPhoto } from '@/file/actions';
import { useFile } from '@/file/store';
import { useTokens } from '@/theme/tokens';
/** Photos d'intervention (D5) : compressées côté app (~1 600 px) puis mises
* EN FILE comme le reste — rattachées à l'OT dans la bibliothèque R3.
* Ni audio ni géolocalisation en R4 (loi 09-08, minimisation). */
export function CartePhotos({ ot }: { ot: WorkOrderDetail }) {
const t = useTokens();
const queryClient = useQueryClient();
const file = useFile();
const { data: documents } = useDocumentsOT(ot.id);
const photosServeur = (documents ?? []).filter((d) => d.contentType.startsWith('image/'));
const photosEnFile = file.filter((s) => s.otId === ot.id && s.type === 'PHOTO');
const prendre = async () => {
const resultat = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ['images'],
quality: 0.9,
});
const image = resultat.assets?.[0];
if (!image) return;
try {
// Compression D5 : jamais un original de 12 Mo dans la file.
const contexte = ImageManipulator.ImageManipulator.manipulate(image.uri);
if (image.width > 1600) contexte.resize({ width: 1600 });
const rendu = await contexte.renderAsync();
const sauve = await rendu.saveAsync({
compress: 0.7,
format: ImageManipulator.SaveFormat.JPEG,
});
enfilerPhoto(queryClient, ot, {
uri: sauve.uri,
nom: `intervention-${ot.reference}-${Date.now().toString(36)}.jpg`,
mime: 'image/jpeg',
});
} catch (e) {
Alert.alert('Photo impossible', e instanceof Error ? e.message : 'Erreur inconnue');
}
};
return (
<Carte titre={`Photos (${photosServeur.length + photosEnFile.length})`}>
<View style={{ flexDirection: 'row', flexWrap: 'wrap', gap: 8 }}>
{photosServeur.map((d) => (
<View key={d.id} style={vignette(t.surface2, t.bordure)}>
<Text style={{ fontSize: 18 }}>🖼</Text>
</View>
))}
{photosEnFile.map((s) => (
<View key={s.id} style={vignette(t.stAttenteFond, t.stAttente)}>
<Text style={{ fontSize: 18 }}>🖼</Text>
<Text
style={{
position: 'absolute',
bottom: 2,
fontFamily: 'Manrope_800ExtraBold',
fontSize: 8,
color: t.stAttente,
}}
>
en file
</Text>
</View>
))}
<Pressable
accessibilityRole="button"
accessibilityLabel="Ajouter une photo"
onPress={() => void prendre()}
style={vignette('transparent', t.bordureForte, true)}
>
<Text style={{ fontSize: 22, color: t.primaire }}>+</Text>
</Pressable>
</View>
</Carte>
);
}
const vignette = (fond: string, bordure: string, pointille = false) =>
({
width: 56,
height: 56,
borderRadius: 9,
backgroundColor: fond,
borderWidth: 1.5,
borderColor: bordure,
borderStyle: pointille ? ('dashed' as const) : ('solid' as const),
alignItems: 'center' as const,
justifyContent: 'center' as const,
}) as const;