KV to D1 migration (this is totally gonna break something)

This commit is contained in:
2024-05-12 01:25:46 -04:00
parent a2b3391bda
commit e00b7e8c55
24 changed files with 1835 additions and 669 deletions

View File

@ -41,18 +41,17 @@ export async function onRequestPost(context: RequestContext) {
if (!currentUser.email) return jsonError("No email for this session", 403);
const existingAppeals = await context.env.DATA.list({
prefix: `appeal_${currentUser.id}`,
});
const existingBlockedAppeal = await context.env.DATA.get(
`blockedappeal_${currentUser.id}`,
);
if (
existingBlockedAppeal ||
existingAppeals.keys.find(
(appeal) => (appeal.metadata as { [k: string]: any })?.open,
(await context.env.D1.prepare(
"SELECT approved FROM appeals WHERE approved IS NULL AND json_extract(user, '$.id') = ?;",
)
.bind(currentUser.id)
.first())
)
return jsonError("Appeal already submitted", 403);
@ -74,33 +73,31 @@ export async function onRequestPost(context: RequestContext) {
.randomUUID()
.replaceAll("-", "")}`;
await context.env.DATA.put(
`appeal_${appealId}`,
JSON.stringify({
ban_reason: whyBanned,
created_at: Date.now(),
fcm_token: typeof senderTokenId === "string" ? senderTokenId : undefined,
await context.env.D1.prepare(
"INSERT INTO appeals (ban_reason, created_at, id, learned, reason_for_unban, user) VALUES (?, ?, ?, ?, ?, ?);",
)
.bind(
whyBanned,
Date.now(),
appealId,
learned,
id: appealId,
open: true,
reason_for_unban: whyUnban,
user: {
whyUnban,
JSON.stringify({
email: currentUser.email,
id: currentUser.id,
username: currentUser.username,
},
}),
{
expirationTtl: 94608000,
},
);
await context.env.D1.prepare(
"INSERT INTO appeals (created_at, id, open, user) VALUES (?, ?, ?, ?)",
)
.bind(Date.now(), appealId, 1, currentUser.id)
}),
)
.run();
if (typeof senderTokenId === "string") {
await context.env.D1.prepare(
"INSERT INTO push_notifications (created_at, event_id, event_type, token) VALUES (?, ?, 'appeal', ?)",
)
.bind(Date.now(), appealId, senderTokenId)
.run();
}
await fetch(context.env.APPEALS_WEBHOOK, {
body: JSON.stringify({
embeds: [