Skip to content

Commit

Permalink
Merge pull request #1 from Darrin-Lin/Camp_2024
Browse files Browse the repository at this point in the history
Camp 2024 updates
  • Loading branch information
morris46202 authored Mar 25, 2024
2 parents 4f305a2 + 7fed974 commit 0f48b30
Show file tree
Hide file tree
Showing 12 changed files with 607 additions and 60 deletions.
15 changes: 12 additions & 3 deletions d1/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ CREATE TABLE TaskProgress (
-- 完成日期
avatar TEXT,
-- 完成日期
quiz TEXT,
quiz TEXT
-- GitHub 帳號
github TEXT
-- github TEXT
);

DROP TABLE IF EXISTS Application;
Expand Down Expand Up @@ -65,5 +65,14 @@ CREATE TABLE IF NOT EXISTS Payment (
email TEXT NOT NULL PRIMARY KEY,
account TEXT NOT NULL,
time TEXT NOT NULL,
updated TEXT NOT NULL
updated TEXT NOT NULL,
correct TEXT
);

CREATE TABLE IF NOT EXISTS Voting (
email TEXT NOT NULL,
target TEXT NOT NULL,
vote INT NOT NULL,
PRIMARY KEY (email, target)

);
2 changes: 1 addition & 1 deletion src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const task = store<Record<z.infer<typeof TaskSchema>["task"], string | nu
profile: null,
avatar: null,
quiz: null,
github: null,
// github: null,
});

token.subscribe((jwt) => {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@ export const ProfileSchema = z.object({
});

export const TaskSchema = z.object({
task: z.enum(["profile", "avatar", "quiz", "github"]),
// task: z.enum(["profile", "avatar", "quiz", "github"]),
task: z.enum(["profile", "avatar", "quiz"]),

});
3 changes: 2 additions & 1 deletion src/lib/server/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface TaskProgress {
profile: string;
avatar: string;
quiz: string;
github: string;
// github: string;
}

export interface Voting {
Expand Down Expand Up @@ -61,6 +61,7 @@ export interface Payment {
account: string;
time: string;
updated: string;
correct: string;
}

export interface Database {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/server/task.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { db } from "./db";

export const tasks = ["profile", "avatar", "quiz", "github"] as const;
// export const tasks = ["profile", "avatar", "quiz", "github"] as const;
export const tasks = ["profile", "avatar", "quiz"] as const;
export const selfchecks = ["quiz"];

export async function complete(
Expand Down
14 changes: 12 additions & 2 deletions src/routes/admin/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<script lang="ts">
import { hash } from "$lib/hash";
import { spacing } from "pangu";
// import { spacing } from "pangu";
import pkg from "pangu";
const { spacing } = pkg;
import Icon from "@iconify/svelte";
import type { PageData } from "./$types";
Expand Down Expand Up @@ -40,7 +42,7 @@
alert("投票失敗");
console.error("投票失敗", await res.text());
}
console.log("投票成功", target, choice);
data.applications.map((app) => {
if (app.email === target) {
app.vote = choice;
Expand Down Expand Up @@ -109,11 +111,19 @@
</span>
</h1>
<p class="py-6">
自我介紹:
{spacing(app.self_intro)}
</p>
<p class="py-6">
動機:
{spacing(app.motivation)}
</p>
<p class="py-6">
報名時間:
{spacing(app.created).split("T")[0] +
" " +
spacing(app.created).split("T")[1].split(".")[0]}
</p>
<div class="rounded-lg border border-primary p-4">
{app.status}
</div>
Expand Down
37 changes: 37 additions & 0 deletions src/routes/admin/api/admit/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { db } from "$lib/server/db";
import type { RequestHandler } from "./$types";
import { json, redirect } from "@sveltejs/kit";
import { env } from "$env/dynamic/private";


export const PATCH: RequestHandler = async ({ locals, request }) => {
if (!locals.token) {
console.log("Someone is trying to access admin page without token");
throw redirect(302, "/login");
}

if (!env.ADMIN_EMAIL_REGEX) {
console.error("ADMIN_EMAIL_REGEX is not set");
throw new Error("ADMIN_EMAIL_REGEX is not set");
}

const email_regex = new RegExp(env.ADMIN_EMAIL_REGEX);
if (!email_regex.test(locals.token.email)) {
console.log(
`${locals.token.email} is trying to access admin page which does not match ${env.ADMIN_EMAIL_REGEX}`,
);
throw redirect(302, "/login");
}


// get email and input_status from request body
const { email, input_status }: { email: string, input_status: string } = await request.json();
const now = new Date().toISOString();
await db
.updateTable("Application")
.set({ status: input_status, updated: now })
.where("email", "=", email)
.execute();

return json({ ok: true });
}
33 changes: 33 additions & 0 deletions src/routes/admin/api/payment/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { env } from "$env/dynamic/private";
import { db } from "$lib/server/db";
import { json, redirect } from "@sveltejs/kit";
import type { RequestHandler } from "./$types";

export const PATCH: RequestHandler = async ({ locals, request }) => {
if (!locals.token) {
console.log("Someone is trying to access admin page without token");
throw redirect(302, "/login");
}

if (!env.ADMIN_EMAIL_REGEX) {
console.error("ADMIN_EMAIL_REGEX is not set");
throw new Error("ADMIN_EMAIL_REGEX is not set");
}

const email_regex = new RegExp(env.ADMIN_EMAIL_REGEX);
if (!email_regex.test(locals.token.email)) {
console.log(
`${locals.token.email} is trying to access admin page which does not match ${env.ADMIN_EMAIL_REGEX}`,
);
throw redirect(302, "/login");
}

const { email, correctness }: { email: string; correctness: string } = await request.json();
await db
.updateTable("Payment")
.set({ correct: correctness })
.where("email", "=", email)
.execute();
console.log(`Payment correctness for ${email} is set to ${correctness}`);
return json({ ok: true });
};
2 changes: 2 additions & 0 deletions src/routes/admin/scoreboard/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ export const load: PageServerLoad = async ({ locals, platform }) => {
"Profile.skill_mastered",
(b) => b.fn.sum("vote").as("score"),
"Application.status",
"Application.created",
"Payment.account",
"Payment.time as pay_date",
"Payment.correct",
"Attachment.file",
])
.orderBy("score", "desc");
Expand Down
Loading

0 comments on commit 0f48b30

Please sign in to comment.