fix(sync): состав сетов ездит на сервер — колонка set_groups, upsert, bootstrap; счётчик синка считает и обновления

This commit is contained in:
Иван Овчинников
2026-11-22 00:53:32 +03:00
parent d7b9d0fa2d
commit b72f5f8e36
6 changed files with 83 additions and 1 deletions
+42 -1
View File
@@ -263,6 +263,31 @@ function convertBackendWorkoutToFrontend(w) {
};
}
/**
* Серверный состав сетов → локальные setGroups. Участники приходят как
* sync_id упражнений тренировки; локальные id конвертируются из тех же
* sync_id, поэтому ссылка восстанавливается по месту.
*/
function serverSetGroupsToLocal(w) {
let sg = w.set_groups || [];
if (typeof sg === "string") {
try { sg = JSON.parse(sg); } catch { sg = []; }
}
if (!Array.isArray(sg)) return [];
const known = new Set(
(w.workout_exercises || []).map((ex) => ex.sync_id).filter(Boolean)
);
return sg
.map((g) => ({
id: g.sync_id || generateUUID(),
name: g.name || "Сет",
exerciseIds: (g.exercise_sync_ids || []).filter((sid) => known.has(sid)),
}))
.filter((g) => g.exerciseIds.length >= 2);
}
function convertBackendWorkout(w, workoutTypeNameById) {
const workoutSyncId = w.sync_id || generateUUID();
@@ -278,6 +303,7 @@ function convertBackendWorkout(w, workoutTypeNameById) {
: "",
notes: w.notes || "",
synced: true,
setGroups: serverSetGroupsToLocal(w),
exercises: (w.workout_exercises || []).map((ex) => {
const exSyncId = ex.sync_id || generateUUID();
@@ -723,6 +749,18 @@ function buildWorkoutsPayload(dirty) {
end_time: w.endTime || null,
notes: w.notes || "",
// Состав сетов: участники ссылками на sync_id упражнений этой тренировки
set_groups: (w.setGroups || []).map((g) => ({
sync_id: g.id || null,
name: g.name || "Сет",
exercise_sync_ids: (g.exerciseIds || [])
.map((id) => {
const ex = (w.exercises || []).find((e) => e.id === id);
return ex ? ensureSyncId(ex) : null;
})
.filter(Boolean),
})),
workout_exercises: buildWorkoutExercisesPayload(w.exercises),
cardio_sessions: buildCardioPayload(w.cardio),
};
@@ -775,9 +813,12 @@ export async function sync() {
}
const createdW = response.created?.workouts || 0;
const updatedW = response.updated?.workouts || 0;
const createdM = response.created?.body_metrics || 0;
let message = `Синхронизировано:\nТренировок: ${createdW}\nЗамеров: ${createdM}`;
let message = `Синхронизировано:\nТренировок: ${createdW + updatedW}` +
(updatedW ? ` (новых ${createdW}, обновлено ${updatedW})` : "") +
`\nЗамеров: ${createdM}`;
if (response.warnings?.length) {
message += `\n\nПредупреждения:\n${response.warnings.join("\n")}`;