Clean up all code

This commit is contained in:
2024-07-21 21:12:34 -05:00
parent 017c07a1cf
commit 88f36531b1
10 changed files with 256 additions and 258 deletions

View File

@@ -1,23 +1,25 @@
"use server";
import { NextResponse } from 'next/server';
import { legacyGetHistory } from '~/server/functions';
import { getHistory } from '~/server/functions';
export const GET = async (request: Request) => {
try {
const url = new URL(request.url);
const apiKey = url.searchParams.get('apikey');
const page = Number(url.searchParams.get('page')) || 1;
if (apiKey !== 'zAf4vYVN2pszrK') {
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
}
const perPage = 50; // You can adjust the perPage value as needed
const historyData = await legacyGetHistory(page, perPage);
if (apiKey !== process.env.API_KEY)
return NextResponse.json(
{ message: 'Unauthorized' },
{ status: 401 }
);
const perPage = 50;
const historyData = await getHistory(page, perPage);
return NextResponse.json(historyData, { status: 200 });
} catch (error) {
console.error('Error fetching history data:', error);
return NextResponse.json({ message: 'Internal server error' }, { status: 500 });
return NextResponse.json(
{ message: 'Internal server error' },
{ status: 500 }
);
}
};

View File

@@ -1,6 +1,6 @@
"use server";
import { NextResponse } from 'next/server';
import { legacyGetEmployees } from '~/server/functions';
import { getEmployees } from '~/server/functions';
type Technician = {
name: string;
@@ -12,22 +12,25 @@ export const GET = async (request: Request) => {
try {
const url = new URL(request.url);
const apiKey = url.searchParams.get('apikey');
if (apiKey !== 'zAf4vYVN2pszrK') {
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
}
const employees = await legacyGetEmployees();
if (apiKey !== process.env.API_KEY)
return NextResponse.json(
{ message: 'Unauthorized' },
{ status: 401 }
);
const employees = await getEmployees();
// Necessary because I haven't updated the iOS app
// yet to expect updatedAt rather than time
const formattedEmployees = employees.map((employee: Technician) => ({
name: employee.name,
status: employee.status,
time: employee.updatedAt
}));
return NextResponse.json(formattedEmployees, { status: 200 });
} catch (error) {
console.error('Error fetching employees:', error);
return NextResponse.json({ message: 'Internal server error' }, { status: 500 });
return NextResponse.json(
{ message: 'Internal server error' },
{ status: 500 }
);
}
};

View File

@@ -1,8 +1,7 @@
"use server";
import { NextResponse } from 'next/server';
import { legacyUpdateEmployeeStatusByName } from '~/server/functions';
import { updateEmployeeStatusByName } from '~/server/functions';
// Define the Technician type directly in the file
interface Technician {
name: string;
status: string;
@@ -11,37 +10,42 @@ interface Technician {
// Type guard to check if an object is a Technician
const isTechnician = (technician: unknown): technician is Technician => {
if (typeof technician !== 'object' || technician === null) return false;
return 'name' in technician && typeof (technician as Technician).name === 'string' &&
'status' in technician && typeof (technician as Technician).status === 'string';
return 'name' in technician &&
typeof (technician as Technician).name === 'string' &&
'status' in technician &&
typeof (technician as Technician).status === 'string';
};
export const POST = async (request: Request) => {
try {
const url = new URL(request.url);
const apiKey = url.searchParams.get('apikey');
if (apiKey !== 'zAf4vYVN2pszrK') {
if (apiKey !== process.env.API_KEY)
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
}
const body: unknown = await request.json();
// Validate the body and its technicians property
if (typeof body !== 'object' || body === null || !Array.isArray((body as { technicians?: unknown[] }).technicians)) {
return NextResponse.json({ message: 'Invalid input: expecting an array of technicians.' }, { status: 400 });
}
if (typeof body !== 'object' || body === null ||
!Array.isArray((body as { technicians?: unknown[] }).technicians))
return NextResponse.json(
{ message: 'Invalid input: expecting an array of technicians.' },
{ status: 400 }
);
const technicians = (body as { technicians: unknown[] }).technicians;
if (!technicians.every(isTechnician)) {
return NextResponse.json({ message: 'Invalid input: missing name or status for a technician.' }, { status: 400 });
}
await legacyUpdateEmployeeStatusByName(technicians);
return NextResponse.json({ message: 'Technicians updated successfully.' }, { status: 200 });
if (!technicians.every(isTechnician))
return NextResponse.json(
{ message: 'Invalid input: missing name or status for a technician.' },
{ status: 400 }
);
await updateEmployeeStatusByName(technicians);
return NextResponse.json(
{ message: 'Technicians updated successfully.' },
{ status: 200 }
);
} catch (error) {
console.error('Error updating technicians:', error);
return NextResponse.json({ message: 'Internal server error' }, { status: 500 });
return NextResponse.json(
{ message: 'Internal server error' },
{ status: 500 }
);
}
};

View File

@@ -1,5 +1,4 @@
"use server";
import { NextResponse } from 'next/server';
import { getEmployees } from '~/server/functions';
import { auth } from '~/auth';

View File

@@ -12,19 +12,27 @@ type UpdateStatusBody = {
export const POST = async (req: NextRequest) => {
const session = await auth();
if (!session)
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
return NextResponse.json(
{ message: 'Unauthorized' },
{ status: 401 }
);
const { employeeIds, newStatus } = await req.json() as UpdateStatusBody;
if (!Array.isArray(employeeIds) || typeof newStatus !== 'string') {
return NextResponse.json({ message: 'Invalid input' }, { status: 400 });
}
if (!Array.isArray(employeeIds) || typeof newStatus !== 'string')
return NextResponse.json(
{ message: 'Invalid input' },
{ status: 400 }
);
try {
await updateEmployeeStatus(employeeIds, newStatus);
return NextResponse.json({ message: 'Status updated successfully' }, { status: 200 });
return NextResponse.json(
{ message: 'Status updated successfully' },
{ status: 200 }
);
} catch (error) {
console.error('Error updating status:', error);
return NextResponse.json({ message: 'Internal server error' }, { status: 500 });
return NextResponse.json(
{ message: 'Internal server error' },
{ status: 500 }
);
}
};

View File

@@ -7,7 +7,8 @@ import Sign_Out from "~/components/auth/Sign_Out";
import { type Metadata } from "next";
export const metadata: Metadata = {
title: "Tech Tracker",
description: "App used by COG IT employees to update their status throughout the day.",
description: "App used by COG IT employees to \
update their status throughout the day.",
icons: [
{
rel: 'icon',

View File

@@ -9,8 +9,8 @@ export default async function HomePage() {
return <No_Session />
} else {
return (
<main className="min-h-screen bg-gradient-to-b
from-[#111111] to-[#212325]">
<main className="min-h-screen
bg-gradient-to-b from-[#111111] to-[#212325]">
<TT_Header />
<Techs />
</main>