refactor: cleanup auto-rename code
This commit is contained in:
@@ -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,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user