add feedback endpoint (#28) and some other stuff
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				delpoy / build-and-deploy (push) Successful in 1m11s
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	delpoy / build-and-deploy (push) Successful in 1m11s
				
			This commit is contained in:
		
							
								
								
									
										50
									
								
								src/routes/api/feedback/+server.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								src/routes/api/feedback/+server.ts
									
									
									
									
									
										Normal 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; | ||||
							
								
								
									
										6
									
								
								src/routes/api/feedback/schema.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								src/routes/api/feedback/schema.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | ||||
| import { z } from 'zod'; | ||||
|  | ||||
| export const FeedbackAddSchema = z.object({ | ||||
| 	event: z.string(), | ||||
| 	users: z.array(z.string()) | ||||
| }); | ||||
| @@ -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) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user