phase 3: all variable cateogry references simplified
This commit is contained in:
@@ -333,19 +333,12 @@ function CategoriesSettingsInner(
|
||||
await categoriesApi.delete(cat.id);
|
||||
}
|
||||
|
||||
// Creates
|
||||
const toCreate = normalizedCats.filter((c) => c._isNew && !c._isDeleted);
|
||||
for (const cat of toCreate) {
|
||||
await categoriesApi.create({
|
||||
name: normalizeName(cat.name),
|
||||
percent: cat.percent,
|
||||
priority: cat.priority,
|
||||
isSavings: cat.isSavings,
|
||||
});
|
||||
}
|
||||
|
||||
// Updates
|
||||
const toUpdate = normalizedCats.filter((c) => !c._isNew && !c._isDeleted);
|
||||
const updateOps: Array<{
|
||||
id: string;
|
||||
patch: Partial<Row>;
|
||||
percentDelta: number;
|
||||
}> = [];
|
||||
for (const local of toUpdate) {
|
||||
const server = serverCats.find((s) => s.id === local.id);
|
||||
if (!server) continue;
|
||||
@@ -358,10 +351,37 @@ function CategoriesSettingsInner(
|
||||
patch.isSavings = local.isSavings;
|
||||
|
||||
if (Object.keys(patch).length > 0) {
|
||||
await categoriesApi.update(local.id, patch);
|
||||
updateOps.push({
|
||||
id: local.id,
|
||||
patch,
|
||||
// Apply percent decreases first to avoid temporary >100 totals on server.
|
||||
percentDelta: local.percent - server.percent,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const preCreateUpdates = updateOps.filter((op) => op.percentDelta < 0);
|
||||
const postCreateUpdates = updateOps.filter((op) => op.percentDelta >= 0);
|
||||
|
||||
for (const op of preCreateUpdates) {
|
||||
await categoriesApi.update(op.id, op.patch);
|
||||
}
|
||||
|
||||
// Creates
|
||||
const toCreate = normalizedCats.filter((c) => c._isNew && !c._isDeleted);
|
||||
for (const cat of toCreate) {
|
||||
await categoriesApi.create({
|
||||
name: normalizeName(cat.name),
|
||||
percent: cat.percent,
|
||||
priority: cat.priority,
|
||||
isSavings: cat.isSavings,
|
||||
});
|
||||
}
|
||||
|
||||
for (const op of postCreateUpdates) {
|
||||
await categoriesApi.update(op.id, op.patch);
|
||||
}
|
||||
|
||||
if (hasNew) {
|
||||
try {
|
||||
await categoriesApi.rebalance();
|
||||
|
||||
Reference in New Issue
Block a user