refactor: cleanup auto-rename code

This commit is contained in:
Zoe
2026-02-25 20:38:55 +00:00
parent e4536cbb8c
commit c3370c27a7
6 changed files with 71 additions and 40 deletions
+42
View File
@@ -0,0 +1,42 @@
import * as z from 'zod';
import { httpClient } from '~~/server/lib/triplit';
import { cancelPendingRename } from '~~/server/utils/renames';
import { assert } from '~~/utils/assert';
export default defineEventHandler(async (event) => {
await protectRoute(event);
const body = await readValidatedBody(event, (body) =>
z
.object({
renameId: z.string(),
})
.safeParse(body),
);
if (!body.success) {
throw createError({
statusCode: 400,
message: 'Invalid rename ID',
});
}
const { renameId } = body.data;
assert(renameId);
const [success, pendingRename] = cancelPendingRename(renameId);
if (success) {
const topic = await httpClient.fetchOne(httpClient.query('topics').Where('id', '=', pendingRename!.topicId));
if (topic !== null && topic.renaming) {
await httpClient.update('topics', topic.id, {
renaming: false,
});
}
return {
ok: true,
};
}
return {
ok: false,
};
});