From 676b50db0c187ae78fcdc2384eae530068344800 Mon Sep 17 00:00:00 2001 From: Aaron Lee Date: Wed, 17 Jan 2024 01:08:40 +0800 Subject: [PATCH] temporarily overriding attendance, roster fix --- src/pages/dash/admin/roster.tsx | 86 +++++++++++++++++++++++++++++- src/pages/dash/index.tsx | 77 +++++++++++++------------- src/server/api/routers/admin.ts | 73 ++++++++++++++++++++++++- src/server/api/routers/time-sel.ts | 11 ++-- 4 files changed, 200 insertions(+), 47 deletions(-) diff --git a/src/pages/dash/admin/roster.tsx b/src/pages/dash/admin/roster.tsx index 791055a..046a86c 100644 --- a/src/pages/dash/admin/roster.tsx +++ b/src/pages/dash/admin/roster.tsx @@ -8,9 +8,10 @@ import { api } from "~/utils/api"; export default function Dash() { const { push } = useRouter(); const [date, setDate] = useState(new Date()); - const [genDate, setGenDate] = useState(new Date(0)); + const [genDate, setGenDate] = useState(new Date()); const { toPDF, targetRef: pdfRef } = usePDF({ filename: `入校名單_${date.toLocaleDateString().replace("/", "-")}.pdf` }); + const { toPDF: bigToPdf, targetRef: bigPdfRef } = usePDF({ filename: `Build_Season_總表.pdf` }); const isLoggedIn = api.admin.isLoggedIn.useQuery(); const periods = api.admin.getPeriods.useQuery(); @@ -18,7 +19,9 @@ export default function Dash() { const roster = api.admin.getRoster.useQuery({ date: date }); - + const allRoster = api.admin.getAllRoster.useQuery(); + const allPeriodsHeadcount = api.admin.getAllPeriodsHeadcount.useQuery(); + useEffect(() => { if (isLoggedIn.failureCount > 0) { push("/"); @@ -60,6 +63,16 @@ export default function Dash() { }}> Download PDF + { (roster.isLoading) &&
+ @@ -95,6 +109,7 @@ export default function Dash() { { roster.data?.map((user) => { return ( + @@ -114,6 +129,73 @@ export default function Dash() {
學號 年級 班級 座號
{user.username} {user.grade} {user.class} {user.number}
+
+
+

機器人研究社 Build Season 總表

+
+

列印時間: {genDate.toLocaleString()}

+
+ + + + + + + + { + Object.keys(periods.data ?? {}).map((date) => { + return () + }) + } + + + { + Object.keys(periods.data ?? {}).map((date) => { + return periods.data![date]!.map((period) => { + return () + }) + }) + } + + + + { + allRoster.data?.map((user) => { + return ( + + + + + { + Object.keys(periods.data ?? {}).map((date) => { + return periods.data![date]!.map((thisPeriod) => { + if (user.periods.find((period) => period.id === thisPeriod.id)) { + return () + } else { + return () + } + }) + }) + } + ) + }) + } + + + + + { + allPeriodsHeadcount.data?.map((headcount) => { + return () + }) + } + + +
年級班級座號姓名{date}
{period.timePeriod.name}
{user.grade}{user.class}{user.number}{user.name}
總計{headcount._count.users}
+
+ ) diff --git a/src/pages/dash/index.tsx b/src/pages/dash/index.tsx index 8409f7b..039e433 100644 --- a/src/pages/dash/index.tsx +++ b/src/pages/dash/index.tsx @@ -38,17 +38,17 @@ export default function Dash() {
-
已選取時數
+
時數
{attendTime.data}
-
+ {/*
實際出席時數
{actualAttendTime.data?.toFixed(1)}
-
+
*/}
= 95 ? "bg-emerald-500" : "bg-red-300" @@ -97,7 +97,8 @@ export default function Dash() { {date} { - new Date(date).setHours(0, 0, 0, 0) > new Date().getTime() ? ( + // FIXME: I am temporaily overriding attendance + // new Date(date).setHours(0, 0, 0, 0) > new Date().getTime() ? ( timePeriods.data?.map((timePeriod) => { const thisPeriodId = periods.data![date]![periodCnt]?.id!; if (periods.data![date]![periodCnt]?.timePeriodId == timePeriod.id) { @@ -121,43 +122,43 @@ export default function Dash() { return N/A } }) - ) : ( - timePeriods.data?.map((timePeriod) => { - periodCnt++; + // ) : ( + // timePeriods.data?.map((timePeriod) => { + // periodCnt++; - let data = ""; - const thisPeriodStart = new Date(date); - thisPeriodStart.setHours(parseInt(timePeriod.start.split(":")[0]!), parseInt(timePeriod.start.split(":")[1]!), 0, 0) - const thisPeriodEnd = new Date(date); - thisPeriodEnd.setHours(parseInt(timePeriod.end.split(":")[0]!), parseInt(timePeriod.end.split(":")[1]!), 0, 0) + // let data = ""; + // const thisPeriodStart = new Date(date); + // thisPeriodStart.setHours(parseInt(timePeriod.start.split(":")[0]!), parseInt(timePeriod.start.split(":")[1]!), 0, 0) + // const thisPeriodEnd = new Date(date); + // thisPeriodEnd.setHours(parseInt(timePeriod.end.split(":")[0]!), parseInt(timePeriod.end.split(":")[1]!), 0, 0) - for (let i = attCnt; i < (myAttendance.data ?? []).length; i++) { - const thisAtt = myAttendance.data![i]; - if (thisAtt?.datetime! < thisPeriodStart) continue; - if (thisAtt?.datetime! > thisPeriodEnd) { - attCnt = i; - break; - } - if (!entered) { - data += `${data !== "" ? " / " : ""}${thisAtt?.datetime!.toLocaleTimeString()} ~ `; - } else { - data += `${thisAtt?.datetime!.toLocaleTimeString()}`; - } - entered = !entered; - } + // for (let i = attCnt; i < (myAttendance.data ?? []).length; i++) { + // const thisAtt = myAttendance.data![i]; + // if (thisAtt?.datetime! < thisPeriodStart) continue; + // if (thisAtt?.datetime! > thisPeriodEnd) { + // attCnt = i; + // break; + // } + // if (!entered) { + // data += `${data !== "" ? " / " : ""}${thisAtt?.datetime!.toLocaleTimeString()} ~ `; + // } else { + // data += `${thisAtt?.datetime!.toLocaleTimeString()}`; + // } + // entered = !entered; + // } - if (entered && data === "" && periodCnt !== timePeriods.data!.length) { - return - } - if (entered && periodCnt === timePeriods.data!.length) { - return {data} - } - if (data === "") { - return Absent - } - return {data} - }) - ) + // if (entered && data === "" && periodCnt !== timePeriods.data!.length) { + // return + // } + // if (entered && periodCnt === timePeriods.data!.length) { + // return {data} + // } + // if (data === "") { + // return Absent + // } + // return {data} + // }) + // ) } ) diff --git a/src/server/api/routers/admin.ts b/src/server/api/routers/admin.ts index 6002d0a..e6a8a4e 100644 --- a/src/server/api/routers/admin.ts +++ b/src/server/api/routers/admin.ts @@ -5,7 +5,7 @@ import { adminProcedure, createTRPCRouter, loggedInProcedure, publicProcedure } import { AddAttendance, AddPeriods, AddTimePeriodSchema, AddUserSchema, ChgPasswordSchema, DateRange, LoginSchema, PublicUserType } from "~/utils/types"; import { TRPCError } from "@trpc/server"; import { z } from "zod"; -import { Period } from "@prisma/client"; +import { Period, TimePeriod } from "@prisma/client"; import { lastGotRfid, rfidAttendance, setRfidAttendance } from "~/utils/rfid"; import { actualAttendTime, attendTime as selectedAttendTime, toggleAttendance } from "./time-sel"; @@ -200,13 +200,32 @@ export const adminRouter = createTRPCRouter({ getPeriods: loggedInProcedure .query(async ({ ctx }) => { return await ctx.db.period.findMany({ + include: { + timePeriod: true + }, orderBy: [{ date: "asc", }, { timePeriodId: "asc", }], }).then((periods) => { - const groupedPeriods: { [key: string]: Period[] } = {}; + const groupedPeriods: { [key: string]: ({ + timePeriod: { + id: number; + name: string; + start: string; + end: string; + createdAt: Date; + updatedAt: Date; + }; + } & { + id: number; + date: Date; + timePeriodId: number; + collecting: boolean; + createdAt: Date; + updatedAt: Date; + })[]} = {}; periods.forEach((period) => { const dateStr = period.date.toLocaleDateString(); if (groupedPeriods[dateStr] === undefined) { @@ -402,6 +421,55 @@ export const adminRouter = createTRPCRouter({ message: "RFID attendance toggled.", }; }), + getAllRoster: adminProcedure + .query(async ({ ctx }) => { + return await ctx.db.user.findMany({ + select: { + id: true, + name: true, + grade: true, + class: true, + number: true, + periods: { + select: { + id: true, + }, + }, + }, + orderBy: [ + { + grade: "asc", + }, + { + class: "asc", + }, + { + number: "asc", + }, + ] + }) + }), + getAllPeriodsHeadcount: adminProcedure + .query(async ({ ctx }) => { + return await ctx.db.period.findMany({ + select: { + id: true, + _count: { + select: { + users: true, + } + } + }, + orderBy: [ + { + date: "asc", + }, + { + timePeriodId: "asc", + }, + ] + }) + }), getRoster: adminProcedure .input(z.object({ date: z.date()})) .query(async ({ input, ctx }) => { @@ -415,6 +483,7 @@ export const adminRouter = createTRPCRouter({ }, select: { id: true, + username: true, name: true, grade: true, class: true, diff --git a/src/server/api/routers/time-sel.ts b/src/server/api/routers/time-sel.ts index 18a7bd5..e0096d7 100644 --- a/src/server/api/routers/time-sel.ts +++ b/src/server/api/routers/time-sel.ts @@ -16,11 +16,12 @@ export const attendTime = async (ctx: Context, username: string) => { } } }, - where: { - date: { - gte: new Date(new Date().setHours(23, 59, 59, 999)), - } - } + // FIXME: I am temporaily overriding attendance + // where: { + // date: { + // gte: new Date(new Date().setHours(23, 59, 59, 999)), + // } + // } } } });