diff --git a/app/components/ConsolePage.vue b/app/components/ConsolePage.vue index 1a5d84b..d2bb7ed 100644 --- a/app/components/ConsolePage.vue +++ b/app/components/ConsolePage.vue @@ -568,7 +568,9 @@ const { needLogin, error: groupsError, load: loadGroups, - save: saveGroups, + update: updateGroup, + create: createGroup, + remove: removeGroup, rename: groupsRename, } = useGroupsApi(); const { @@ -1036,8 +1038,8 @@ function openEditGroup(group: Group): void { } /** - * Group writes replace the whole group list, so they run one at a time, each - * built from the list the previous one saved; true when one may start. + * Group writes run one at a time, each built on what the previous one saved; + * true when one may start. */ function canWriteGroups(): boolean { if (!savingGroup.value) return true; @@ -1049,8 +1051,7 @@ async function onSaveGroupFromPanel(group: Group): Promise { if (!canWriteGroups()) return; savingGroup.value = true; try { - const next = groups.value.map((g) => (g.id === group.id ? group : g)); - await saveGroups(next); + await updateGroup(group); push(t("toast.groupSaved")); } catch (err) { push(t("toast.saveFailed", { msg: err instanceof Error ? err.message : String(err) }), "bad"); @@ -1066,25 +1067,34 @@ async function onSaveGroup(group: Group): Promise { savingGroup.value = true; try { const editing = editingGroup.value; - let next: Group[]; if (editing) { + // The editor does not show these fields; keep what the group has. + const edited: Group = { ...group, isolated: editing.isolated, revision: editing.revision }; if (group.id !== editing.id) { - // Id changed: rename first so routes/webhook secret/invites follow, - // then persist the remaining edits under the new id. - await groupsRename(editing.id, group.id); - // A list loaded meanwhile may already hold the new id as well. - next = [...groups.value.filter((g) => g.id !== editing.id && g.id !== group.id), group]; + // Id changed: rename first so routes/webhook secret/invites follow + // (refused if the group changed since the editor opened), then + // persist the remaining edits under the new id, on top of exactly the + // renamed group. If that second step fails, the editor already points + // at the new id, so saving again only retries the update. + const renamed = await groupsRename(editing.id, group.id, editing.revision); + if (session === groupEditorSession) editingGroup.value = renamed; + // Server-decided fields (e.g. isolation made explicit by the rename) + // come from the renamed group, not from the editor's older copy. + await updateGroup({ + ...edited, + isolated: renamed.isolated, + revision: renamed.revision, + }); } else { - next = groups.value.map((g) => (g.id === editing.id ? group : g)); + await updateGroup(edited); } } else { if (groups.value.some((g) => g.id === group.id)) { push(t("toast.groupIdExists"), "bad"); return; } - next = [...groups.value, group]; + await createGroup(group); } - await saveGroups(next); if (session === groupEditorSession) groupEditorOpen.value = false; push(t("toast.groupSaved")); await loadGroups(); @@ -1101,13 +1111,25 @@ async function onDeleteGroup(group: Group): Promise { // Fetch the real route count for this group instead of relying on the // routes of whichever group happens to be open in the detail view. let used = 0; + let routesRevision: string | undefined; + let version: number | null = null; try { - const data = await apiFetch<{ routes?: Route[] }>( + const data = await apiFetch<{ routes?: Route[]; revision?: string; version?: number | null }>( `/admin/api/groups/${encodeURIComponent(group.id)}/routes`, ); used = (data.routes ?? []).length; - } catch { - // Count is best-effort; proceed without the warning. + routesRevision = data.revision; + version = data.version ?? null; + } catch (err) { + // The deletion is confirmed on what the routes are now: without them, + // a route added meanwhile could be deleted unseen. + if (visit === pageVisit) { + push( + t("toast.deleteFailed", { msg: err instanceof Error ? err.message : String(err) }), + "bad", + ); + } + return; } // The list was left while the count loaded: never ask on another page. if (visit !== pageVisit) return; @@ -1118,7 +1140,8 @@ async function onDeleteGroup(group: Group): Promise { if (!confirmed || visit !== pageVisit || !canWriteGroups()) return; savingGroup.value = true; try { - await saveGroups(groups.value.filter((g) => g.id !== group.id)); + // Deletes the group as confirmed: a route added meanwhile makes it fail. + await removeGroup(group.id, group.revision, routesRevision, version); push(t("toast.groupDeleted")); } catch (err) { push(t("toast.deleteFailed", { msg: err instanceof Error ? err.message : String(err) }), "bad"); diff --git a/app/composables/useGroups.ts b/app/composables/useGroups.ts index 822a7a5..8077c88 100644 --- a/app/composables/useGroups.ts +++ b/app/composables/useGroups.ts @@ -52,22 +52,87 @@ export function useGroupsApi() { } } - async function save(next: Group[]): Promise { - await apiFetch("/admin/api/groups", { - method: "PUT", - body: JSON.stringify({ groups: next }), - }); + /** Apply a write's result locally; a load that started earlier must not land. */ + function settle(next: Group[]): void { request++; loading.value = false; groups.value = next; } - /** Rename a group; routes, webhook secret and invites follow automatically. */ - async function rename(oldId: string, newId: string): Promise { - await apiFetch(`/admin/api/groups/${encodeURIComponent(oldId)}/rename`, { - method: "PUT", - body: JSON.stringify({ newId }), + /** Reload after a refused write (409): someone changed the group meanwhile. */ + async function guard(write: () => Promise): Promise { + try { + return await write(); + } catch (err) { + if (isConflict(err)) void load(); + throw err; + } + } + + /** + * Update one group. Its `revision` (from the last load) travels along, so an + * edit made from a stale copy is refused instead of reverting newer changes. + */ + async function update(group: Group): Promise { + const data = await guard(() => + apiFetch<{ group: Group }>(`/admin/api/groups/${encodeURIComponent(group.id)}`, { + method: "PUT", + body: JSON.stringify({ group }), + }), + ); + settle(groups.value.map((g) => (g.id === data.group.id ? data.group : g))); + return data.group; + } + + /** Create one group (super admins). */ + async function create(group: Group): Promise { + const data = await apiFetch<{ group: Group }>("/admin/api/groups", { + method: "POST", + body: JSON.stringify({ group }), }); + settle([...groups.value.filter((g) => g.id !== data.group.id), data.group]); + return data.group; + } + + /** + * Delete one group with its routes. `revision` (the group) and + * `routesRevision` (its routes) are what the deletion was confirmed on: if + * either changed meanwhile, the server refuses (409) instead of deleting + * something nobody saw. + */ + async function remove( + id: string, + revision?: string, + routesRevision?: string, + version?: number | null, + ): Promise { + const query = new URLSearchParams(); + if (revision) query.set("revision", revision); + if (routesRevision) query.set("routesRevision", routesRevision); + if (version != null) query.set("version", String(version)); + const qs = query.toString(); + await guard(() => + apiFetch(`/admin/api/groups/${encodeURIComponent(id)}${qs ? `?${qs}` : ""}`, { + method: "DELETE", + }), + ); + settle(groups.value.filter((g) => g.id !== id)); + } + + /** + * Rename a group; routes, webhook secret and invites follow automatically. + * `revision` is the group's revision the rename was decided on. + */ + async function rename(oldId: string, newId: string, revision?: string): Promise { + const data = await guard(() => + apiFetch<{ group: Group }>(`/admin/api/groups/${encodeURIComponent(oldId)}/rename`, { + method: "PUT", + body: JSON.stringify({ newId, revision }), + }), + ); + // Show the group under its new id right away (a later step may still fail). + settle(groups.value.map((g) => (g.id === oldId ? data.group : g))); + return data.group; } return { @@ -82,7 +147,9 @@ export function useGroupsApi() { needLogin, error, load, - save, + update, + create, + remove, rename, }; }