Add legacy APIs so that I can migrate new app to techtracker.gibbyb.com and keep iOS app working

This commit is contained in:
2024-07-20 20:39:29 -05:00
parent 707ab6515c
commit f1947ccb62
7 changed files with 213 additions and 2 deletions

View File

@ -0,0 +1,23 @@
"use server";
import { NextResponse } from 'next/server';
import { legacyGetHistory } 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);
return NextResponse.json(historyData, { status: 200 });
} catch (error) {
console.error('Error fetching history data:', error);
return NextResponse.json({ message: 'Internal server error' }, { status: 500 });
}
};

View File

@ -0,0 +1,33 @@
"use server";
import { NextResponse } from 'next/server';
import { legacyGetEmployees } from '~/server/functions';
type Technician = {
name: string;
status: string;
updatedAt: Date;
};
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();
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 });
}
};

View File

@ -0,0 +1,44 @@
// src/app/api/update_technicians/route.ts
"use server";
import { NextResponse } from 'next/server';
import { legacyUpdateEmployeeStatusByName } from '~/server/functions';
interface Technician {
name: string;
status: string;
}
// Type guard to check if an object is a Technician
const isTechnician = (technician: any): technician is Technician => {
return typeof technician.name === 'string' && typeof 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') {
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
}
const { technicians } = await request.json() as { technicians: Technician[] };
if (!Array.isArray(technicians) || technicians.length === 0) {
return NextResponse.json({ message: 'Invalid input: expecting an array of technicians.' }, { status: 400 });
}
for (const technician of technicians) {
if (!isTechnician(technician)) {
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 });
} catch (error) {
console.error('Error updating technicians:', error);
return NextResponse.json({ message: 'Internal server error' }, { status: 500 });
}
};