Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions public/_redirects
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* /index.html 200
27 changes: 22 additions & 5 deletions src/infrastructure/sync/google-drive-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,30 @@ export class GoogleDriveSync implements SyncPort {
if (!res.ok) return;

const remote = await res.json();
if (remote?.app !== 'capmark' || remote.version !== 1 || !Array.isArray(remote.obras)) {
return; // El archivo remoto no es un backup válido de CapMark
}

// LWW: solo importamos si el remoto es más reciente que nuestro último export
// LWW por obra: importamos solo si la obra remota fue actualizada después que la local
const localBackup = await this.backup.exportar();
const remoteTs = new Date(remote.exportadoEn ?? 0).getTime();
const localTs = new Date(localBackup.exportadoEn ?? 0).getTime();
if (remoteTs > localTs) {
await this.backup.importar(remote, /* reemplazar= */ true);
const localObrasMap = new Map(localBackup.obras.map((o) => [o.obra.id, o]));

const obrasParaImportar = [];
for (const remoteDetalle of remote.obras) {
const localDetalle = localObrasMap.get(remoteDetalle.obra.id);
if (!localDetalle) {
obrasParaImportar.push(remoteDetalle);
} else {
const remoteActualizada = new Date(remoteDetalle.obra.actualizadaEn || 0).getTime();
const localActualizada = new Date(localDetalle.obra.actualizadaEn || 0).getTime();
if (remoteActualizada > localActualizada) {
obrasParaImportar.push(remoteDetalle);
}
}
}

if (obrasParaImportar.length > 0) {
await this.backup.importar({ ...remote, obras: obrasParaImportar }, /* reemplazar= */ true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/components/SyncPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export default function SyncPanel({ isOpen, onClose, onSyncCompleto }: Props) {
setEstado('sincronizando');
setMensaje('');
try {
await container.sync.push();
await container.sync.pull();
await container.sync.push();
setEstado('conectado');
setMensaje(`Sincronizado · ${new Date().toLocaleTimeString('es', { hour: '2-digit', minute: '2-digit' })}`);
onSyncCompleto?.();
Expand Down
Loading