mirror of
https://github.com/pupperpowell/bibdle.git
synced 2026-08-22 22:32:28 -04:00
New scheduling UI!
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { db } from '$lib/server/db';
|
||||
import { dailyCompletions } from '$lib/server/db/schema';
|
||||
import { eq, and } from 'drizzle-orm';
|
||||
import {
|
||||
scheduleSubmission,
|
||||
validateSelection,
|
||||
getCooldownState
|
||||
} from '$lib/server/verse-submission';
|
||||
|
||||
/**
|
||||
* POST /api/submit-verse
|
||||
*
|
||||
* Accepts a user-chosen verse (bookId/chapter/verse) and reserves a concrete
|
||||
* future date for it via the scheduling scan. Auth required, solved-today gate,
|
||||
* 7-day rolling cooldown, and structural validation all enforced here.
|
||||
*
|
||||
* Body: { bookId, chapter, verse, localDate }
|
||||
*/
|
||||
export const POST: RequestHandler = async ({ request, locals }) => {
|
||||
// 1. Auth.
|
||||
if (!locals.user) {
|
||||
return json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
const userId = locals.user.id;
|
||||
|
||||
let body: any;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return json({ error: 'Invalid JSON body' }, { status: 400 });
|
||||
}
|
||||
|
||||
const { bookId, chapter, verse, localDate } = body ?? {};
|
||||
|
||||
// 2. Solved-today gate (engagement gate, not security).
|
||||
if (typeof localDate !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(localDate)) {
|
||||
return json({ error: 'A valid localDate (YYYY-MM-DD) is required' }, { status: 400 });
|
||||
}
|
||||
const [completion] = await db
|
||||
.select({ id: dailyCompletions.id })
|
||||
.from(dailyCompletions)
|
||||
.where(
|
||||
and(
|
||||
eq(dailyCompletions.anonymousId, userId),
|
||||
eq(dailyCompletions.date, localDate)
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
if (!completion) {
|
||||
return json({ error: "Solve today's puzzle first" }, { status: 403 });
|
||||
}
|
||||
|
||||
// 3. Cooldown (rolling 7×24h, server UTC).
|
||||
const cooldown = await getCooldownState(db, userId);
|
||||
if (cooldown.onCooldown) {
|
||||
return json(
|
||||
{ error: 'Cooldown active', cooldownEndsAt: cooldown.cooldownEndsAt },
|
||||
{ status: 429 }
|
||||
);
|
||||
}
|
||||
|
||||
// 4. Structural validation.
|
||||
const validationError = validateSelection({ bookId, chapter, verse });
|
||||
if (validationError) {
|
||||
return json({ error: validationError }, { status: 400 });
|
||||
}
|
||||
|
||||
// 5–7. Compute window + scheduling scan + transactional insert (with retry).
|
||||
try {
|
||||
const result = await scheduleSubmission(
|
||||
db,
|
||||
{ bookId, chapter, verse },
|
||||
userId
|
||||
);
|
||||
return json(
|
||||
{
|
||||
scheduledDate: result.scheduledDate,
|
||||
reference: result.reference,
|
||||
windowText: result.windowText
|
||||
},
|
||||
{ status: 201 }
|
||||
);
|
||||
} catch (err) {
|
||||
console.error('submit-verse failed:', err);
|
||||
return json({ error: 'Failed to schedule submission' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,69 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { db } from '$lib/server/db';
|
||||
import { verseSubmissions, dailyVerses } from '$lib/server/db/schema';
|
||||
import { eq, desc, asc } from 'drizzle-orm';
|
||||
import { getCooldownState, todayUtcStr } from '$lib/server/verse-submission';
|
||||
|
||||
/**
|
||||
* GET /api/submit-verse/status?localDate=YYYY-MM-DD
|
||||
*
|
||||
* Returns the state needed to render the win-screen submit button:
|
||||
* whether the user can submit, the active cooldown (if any), their most
|
||||
* recent submission, and their not-yet-reached upcoming submissions.
|
||||
*/
|
||||
export const GET: RequestHandler = async ({ locals }) => {
|
||||
if (!locals.user) {
|
||||
return json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
const userId = locals.user.id;
|
||||
|
||||
const cooldown = await getCooldownState(db, userId);
|
||||
|
||||
// Most recent submission (by submitted_at) — joined to daily_verses for the
|
||||
// canonical reference/window text that will actually play on its day.
|
||||
const [lastRow] = await db
|
||||
.select({
|
||||
scheduledDate: verseSubmissions.scheduledDate,
|
||||
reference: dailyVerses.reference
|
||||
})
|
||||
.from(verseSubmissions)
|
||||
.leftJoin(dailyVerses, eq(verseSubmissions.scheduledDate, dailyVerses.date))
|
||||
.where(eq(verseSubmissions.userId, userId))
|
||||
.orderBy(desc(verseSubmissions.submittedAt))
|
||||
.limit(1);
|
||||
|
||||
const lastSubmission = lastRow
|
||||
? {
|
||||
scheduledDate: lastRow.scheduledDate,
|
||||
reference: lastRow.reference
|
||||
}
|
||||
: null;
|
||||
|
||||
// Upcoming = this user's submissions whose scheduled date hasn't been
|
||||
// reached yet (server UTC today).
|
||||
const today = todayUtcStr();
|
||||
const upcomingRows = await db
|
||||
.select({
|
||||
scheduledDate: verseSubmissions.scheduledDate,
|
||||
reference: dailyVerses.reference
|
||||
})
|
||||
.from(verseSubmissions)
|
||||
.leftJoin(dailyVerses, eq(verseSubmissions.scheduledDate, dailyVerses.date))
|
||||
.where(eq(verseSubmissions.userId, userId))
|
||||
.orderBy(asc(verseSubmissions.scheduledDate));
|
||||
|
||||
const upcoming = upcomingRows
|
||||
.filter((r) => r.scheduledDate > today)
|
||||
.map((r) => ({
|
||||
scheduledDate: r.scheduledDate,
|
||||
reference: r.reference
|
||||
}));
|
||||
|
||||
return json({
|
||||
canSubmit: !cooldown.onCooldown,
|
||||
cooldownEndsAt: cooldown.cooldownEndsAt,
|
||||
lastSubmission,
|
||||
upcoming
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user