add feedback endpoint (#28) and some other stuff
All checks were successful
delpoy / build-and-deploy (push) Successful in 1m11s

This commit is contained in:
2024-11-29 01:52:19 +01:00
parent dc86dceb2f
commit dc3a404a5b
30 changed files with 599 additions and 104 deletions

View File

@ -0,0 +1,50 @@
import type { RequestHandler } from '@sveltejs/kit';
import { env } from '$env/dynamic/private';
import { FeedbackAddSchema } from './schema';
import { Feedback, User } from '$lib/server/database';
import crypto from 'crypto';
import type { CreationAttributes } from 'sequelize';
import { env as public_env } from '$env/dynamic/public';
export const POST = (async ({ request, url }) => {
if (env.REPORT_SECRET && url.searchParams.get('secret') !== env.REPORT_SECRET)
return new Response(null, { status: 401 });
const parseResult = await FeedbackAddSchema.safeParseAsync(await request.json());
if (!parseResult.success) {
return new Response(null, { status: 400 });
}
const data = parseResult.data;
const feedback = {} as { [k: string]: CreationAttributes<Feedback> };
for (const user of await User.findAll({
where: { uuid: data.users },
attributes: ['id', 'uuid']
})) {
feedback[user.uuid] = {
url_hash: crypto.randomBytes(18).toString('hex'),
event: data.event,
draft: true,
user_id: user.id
};
}
await Feedback.bulkCreate(Object.values(feedback));
console.log(Object.entries(feedback));
return new Response(
JSON.stringify(
Object.entries(feedback).reduce(
(curr, [k, v]) => {
curr[k] = `${url.protocol}//${url.host}${public_env.PUBLIC_BASE_PATH || ''}/feedback/${
v.url_hash
}`;
return curr;
},
{} as { [k: string]: string }
)
),
{ status: 201 }
);
}) satisfies RequestHandler;

View File

@ -0,0 +1,6 @@
import { z } from 'zod';
export const FeedbackAddSchema = z.object({
event: z.string(),
users: z.array(z.string())
});

View File

@ -5,7 +5,7 @@ import { env as public_env } from '$env/dynamic/public';
import { env } from '$env/dynamic/private';
import { ReportAddSchema } from './schema';
export const GET = async ({ url }) => {
export const GET = (async ({ url }) => {
if (env.REPORT_SECRET && url.searchParams.get('secret') !== env.REPORT_SECRET)
return new Response(null, { status: 401 });
@ -52,7 +52,7 @@ export const GET = async ({ url }) => {
};
return new Response(JSON.stringify(reports), { status: 200 });
};
}) satisfies RequestHandler;
export const POST = (async ({ request, url }) => {
if (env.REPORT_SECRET && url.searchParams.get('secret') !== env.REPORT_SECRET)