mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-27 15:05:38 -04:00
47 lines
1.8 KiB
TypeScript
47 lines
1.8 KiB
TypeScript
import SQL from 'sql-template-strings';
|
|
import { ulid } from 'ulid';
|
|
|
|
import { auditLog } from '~/server/audit.ts';
|
|
import { getLocale, loadConfig } from '~/server/data.ts';
|
|
import { approveNameEntry } from '~/server/names.ts';
|
|
import { isAllowedToPost } from '~/server/user.ts';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const locale = getLocale(event);
|
|
checkIsConfigEnabledOr404(await loadConfig(locale), 'names');
|
|
|
|
const { user, isGranted } = await useAuthentication(event);
|
|
const db = useDatabase();
|
|
|
|
if (!user || !await isAllowedToPost(db, user)) {
|
|
throw createError({
|
|
status: 401,
|
|
statusMessage: 'Unauthorised',
|
|
});
|
|
}
|
|
|
|
const body = await readBody(event);
|
|
|
|
const id = ulid();
|
|
await db.get(SQL`
|
|
INSERT INTO names (id, name, locale, origin, meaning, usage, legally, pros, cons, notablePeople, links, namedays, namedaysComment, deleted, approved, base_id, author_id)
|
|
VALUES (
|
|
${id},
|
|
${body.name}, ${locale},
|
|
${body.origin || null}, ${body.meaning || null}, ${body.usage || null}, ${body.legally || null},
|
|
${body.pros.length ? body.pros.join('|') : null}, ${body.cons.length ? body.cons.join('|') : null},
|
|
${body.notablePeople.length ? body.notablePeople.join('|') : null}, ${body.links.length ? body.links.join('|') : null},
|
|
${body.namedays.length ? body.namedays.join('|') : null}, ${body.namedaysComment || null},
|
|
0, 0, ${body.base}, ${user.id}
|
|
)
|
|
`);
|
|
await auditLog({ user }, 'names/submitted', body);
|
|
|
|
if (isGranted('names')) {
|
|
await approveNameEntry(db, id, locale);
|
|
await auditLog({ user }, 'names/approved', { id });
|
|
}
|
|
|
|
setResponseStatus(event, 201, 'Created');
|
|
});
|