38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
'use server';
|
|
import { NextResponse } from 'next/server';
|
|
import type { NextRequest } from 'next/server';
|
|
import { getCountdown } from '~/server/functions';
|
|
import { middleware } from '~/middleware';
|
|
import type { Countdown } from '~/server/types';
|
|
|
|
export const GET = async (request: NextRequest) => {
|
|
const middlewareResponse = await middleware(request);
|
|
if (middlewareResponse) return middlewareResponse;
|
|
try {
|
|
const url = new URL(request.url);
|
|
const userId = Number.parseInt(url.searchParams.get('userId') ?? '');
|
|
if (!userId || isNaN(userId))
|
|
return NextResponse.json(
|
|
{ message: 'Missing userId' }, { status: 400 }
|
|
);
|
|
const countdown: Countdown | null = await getCountdown(userId);
|
|
if (!countdown) {
|
|
return NextResponse.json(
|
|
{ message: 'No countdown found' }, { status: 404 }
|
|
);
|
|
}
|
|
return NextResponse.json(countdown);
|
|
} catch (error) {
|
|
console.error('Error getting countdown:', error);
|
|
if (error instanceof Error) {
|
|
return NextResponse.json(
|
|
{ message: error.message }, { status: 500 }
|
|
);
|
|
} else {
|
|
return NextResponse.json(
|
|
{ message: 'Unknown error occurred' }, { status: 500 }
|
|
);
|
|
}
|
|
}
|
|
};
|