Const
// Offline progress synchronization
const syncData = {
completions: [
{
lesson_id: 'offline-lesson-01',
completed_at: '2024-01-20T08:30:00Z',
score: 92,
time_spent_seconds: 300
},
{
lesson_id: 'offline-lesson-02',
completed_at: '2024-01-20T09:15:00Z',
score: 78,
time_spent_seconds: 420
}
],
experience_gained: 150,
last_activity: '2024-01-20T09:45:00Z'
};
const result = ProgressSyncSchema.parse(syncData);
console.log('Valid sync data:', result);
// Progress synchronization endpoint
router.post('/progress/sync',
validate({ body: ProgressSyncSchema }),
async (req, res) => {
const syncData = req.body; // Validated sync data
const userId = req.user.id;
const syncResult = await progressService.syncOfflineProgress(userId, syncData);
// Update user's total experience points
if (syncData.experience_gained) {
await gamificationService.addExperience(userId, syncData.experience_gained);
}
res.json({
success: true,
synced: syncResult.syncedCompletions,
skipped: syncResult.skippedDuplicates,
conflicts: syncResult.conflicts
});
}
);
// Mobile app batch synchronization
const mobileAppSync = async (offlineCompletions: any[]) => {
const syncPayload = {
completions: offlineCompletions.map(completion => ({
lesson_id: completion.lessonId,
completed_at: completion.timestamp,
score: completion.finalScore,
time_spent_seconds: completion.duration
})),
experience_gained: offlineCompletions.reduce((total, c) => total + (c.experienceEarned || 0), 0),
last_activity: new Date().toISOString()
};
const validation = ProgressSyncSchema.safeParse(syncPayload);
if (!validation.success) {
throw new Error('Invalid sync data: ' + validation.error.message);
}
return await fetch('/api/progress/sync', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(validation.data)
});
};
// Conflict resolution during synchronization
const handleSyncConflicts = async (syncData: ProgressSyncRequest) => {
// Check for existing completions to prevent duplicates
const existingCompletions = await progressService.getCompletions(
userId,
syncData.completions.map(c => c.lesson_id)
);
const newCompletions = syncData.completions.filter(completion =>
!existingCompletions.some(existing =>
existing.lesson_id === completion.lesson_id &&
new Date(existing.completed_at) >= new Date(completion.completed_at)
)
);
return {
...syncData,
completions: newCompletions
};
};
// Progressive sync for large offline sessions
const progressiveSync = async (allCompletions: any[], batchSize = 10) => {
const results = [];
for (let i = 0; i < allCompletions.length; i += batchSize) {
const batch = allCompletions.slice(i, i + batchSize);
const syncData = {
completions: batch,
experience_gained: batch.reduce((sum, c) => sum + (c.experience || 0), 0),
last_activity: batch[batch.length - 1].completed_at
};
const validation = ProgressSyncSchema.safeParse(syncData);
if (validation.success) {
const result = await syncBatch(validation.data);
results.push(result);
}
}
return results;
};
Progress synchronization validation schema for offline learning support
Comprehensive validation schema for synchronizing learning progress from offline or mobile learning sessions with the central learning platform. This schema supports batch processing of lesson completions, experience point reconciliation, and activity timestamp tracking, enabling seamless offline-to-online learning transitions and comprehensive progress continuity.
The synchronization schema ensures data integrity during batch operations through comprehensive completion validation, prevents data loss through required completion arrays, supports gamification continuity through experience point tracking, and maintains temporal consistency through activity timestamp validation.
Offline learning support includes mobile app synchronization for disconnected learning, batch progress upload for intermittent connectivity scenarios, conflict resolution support through timestamp validation, and comprehensive progress reconciliation for multi-device learning environments.
The schema is designed to handle various synchronization scenarios including initial sync after extended offline periods, incremental sync for regular connectivity, conflict resolution for overlapping learning sessions, and progress recovery for interrupted learning experiences.