Add proclamations api endpoint

This commit is contained in:
NeilClack
2026-03-18 01:09:21 -05:00
parent 791114d2aa
commit 56c688f674
17 changed files with 2044 additions and 126 deletions
+38 -6
View File
@@ -2,6 +2,11 @@ import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { prisma } from '$lib/db';
// /api/v1/proclamations
// GET only
// ?email, ?key (license key)
// Returns all proclamations without email and key, else returns proclamations
// associated with the user associated with the key and email
export const GET: RequestHandler = async ({ url }) => {
const key = url.searchParams.get('key') ?? '';
const email = url.searchParams.get('email') ?? '';
@@ -20,11 +25,38 @@ export const GET: RequestHandler = async ({ url }) => {
}
// Check that the key exists
// Check that the email exists
// Check that the email belongs to a user
// Check that the key belongs to the user
// Write a helper function for this
// Return all proclamations attached to the user
const verifiedKey = await prisma.licenseKey.findUnique({
where: {
key: key
}
});
if (!verifiedKey) {
return error(400, 'not-authorized');
}
return new Response(JSON.stringify({ error: 'not implemented' }), { status: 501 });
// Check that the email exists
const existingUser = await prisma.user.findUnique({
where: {
email: email
},
include: {
licenseKeys: true
}
});
if (!existingUser) {
return error(400, 'not-authorized');
}
// Check that the key belongs to the user
if (!existingUser.licenseKeys.includes(verifiedKey)) {
return error(400, "this key doesn't appear to belong to you");
}
// Return all proclamations attached to the user
const userProclamations = await prisma.userProclamations.findMany({
where: {
userId: existingUser.id
}
});
return new Response(JSON.stringify(userProclamations));
};