44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { and, eq } from 'drizzle-orm';
|
|
import { topics } from '~~/drizzle/schema';
|
|
import { db } from '~~/server/lib/db';
|
|
import { cancelPendingRename } from '~~/server/utils/renames';
|
|
import { userEvents } from '~~/server/utils/events';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
await protectRoute(event);
|
|
|
|
const topicId = getRouterParam(event, 'topicId')!;
|
|
|
|
const success = cancelPendingRename(topicId);
|
|
if (success) {
|
|
const res = await db.update(topics).set({
|
|
renaming: false,
|
|
}).where(and(eq(topics.id, topicId), eq(topics.userId, event.context.user!.id)));
|
|
if (res.rowCount === 0) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Invalid topic',
|
|
data: {
|
|
code: 'INVALID_TOPIC',
|
|
ok: false,
|
|
}
|
|
});
|
|
}
|
|
|
|
userEvents.emit(topicId, 'topics', {
|
|
op: 'update',
|
|
payload: {
|
|
topicId,
|
|
renaming: false,
|
|
},
|
|
});
|
|
|
|
return {
|
|
ok: true,
|
|
};
|
|
}
|
|
|
|
return {
|
|
ok: false,
|
|
};
|
|
}); |