Added Pilot Stats in Admin view
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { PersonIcon } from "@radix-ui/react-icons";
|
||||
import { PrismaClient, User } from "@repo/db";
|
||||
import { AdminForm, ProfileForm } from "./_components/forms";
|
||||
import { AdminForm, ConnectionHistory, ProfileForm } from "./_components/forms";
|
||||
import { Error } from "../../../../_components/Error";
|
||||
|
||||
export default async ({ params }: { params: { id: string } }) => {
|
||||
const Page = async ({ params }: { params: { id: string } }) => {
|
||||
const prisma = new PrismaClient();
|
||||
const { id } = await params;
|
||||
|
||||
@@ -12,6 +12,56 @@ export default async ({ params }: { params: { id: string } }) => {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
const dispoSessions = await prisma.connectedDispatcher.findMany({
|
||||
where: {
|
||||
userId: user?.id,
|
||||
logoutTime: {
|
||||
not: null,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
loginTime: true,
|
||||
logoutTime: true,
|
||||
},
|
||||
});
|
||||
const totalDispoTime = dispoSessions.reduce((acc, session) => {
|
||||
const logoffTime = new Date(session.logoutTime!).getTime();
|
||||
const logonTime = new Date(session.loginTime).getTime();
|
||||
return acc + (logoffTime - logonTime);
|
||||
}, 0);
|
||||
|
||||
const dispoTime = {
|
||||
hours: Math.floor(totalDispoTime / (1000 * 60 * 60)),
|
||||
minutes: Math.floor((totalDispoTime % (1000 * 60 * 60)) / (1000 * 60)),
|
||||
lastLogin: dispoSessions[dispoSessions.length - 1]?.loginTime,
|
||||
};
|
||||
|
||||
const pilotSessions = await prisma.connectedAircraft.findMany({
|
||||
where: {
|
||||
userId: user?.id,
|
||||
logoutTime: {
|
||||
not: null,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
loginTime: true,
|
||||
logoutTime: true,
|
||||
},
|
||||
});
|
||||
|
||||
const totalPilotTime = pilotSessions.reduce((acc, session) => {
|
||||
const logoffTime = new Date(session.logoutTime!).getTime();
|
||||
const logonTime = new Date(session.loginTime).getTime();
|
||||
return acc + (logoffTime - logonTime);
|
||||
}, 0);
|
||||
|
||||
const pilotTime = {
|
||||
hours: Math.floor(totalPilotTime / (1000 * 60 * 60)),
|
||||
minutes: Math.floor((totalPilotTime % (1000 * 60 * 60)) / (1000 * 60)),
|
||||
lastLogin: pilotSessions[pilotSessions.length - 1]?.loginTime,
|
||||
};
|
||||
|
||||
if (!user) return <Error statusCode={404} title="User not found" />;
|
||||
return (
|
||||
<div className="grid grid-cols-6 gap-4">
|
||||
@@ -25,8 +75,13 @@ export default async ({ params }: { params: { id: string } }) => {
|
||||
<ProfileForm user={user} />
|
||||
</div>
|
||||
<div className="card bg-base-200 shadow-xl mb-4 col-span-6 xl:col-span-3">
|
||||
<AdminForm user={user} />
|
||||
<AdminForm user={user} dispoTime={dispoTime} pilotTime={pilotTime} />
|
||||
</div>
|
||||
<div className="card bg-base-200 shadow-xl mb-4 col-span-6 xl:col-span-6">
|
||||
<ConnectionHistory user={user} />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Page;
|
||||
|
||||
Reference in New Issue
Block a user