mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-30 12:55:02 +00:00
get the most recent task for a given service, source, and source ID
This commit is contained in:
parent
23b1bf5ebc
commit
ddaed9c14a
1 changed files with 45 additions and 0 deletions
45
src/app/api/task/route.ts
Normal file
45
src/app/api/task/route.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import { db } from "@/app/db/db";
|
||||
import { tasks } from "@/app/db/schema/tasks/tasks";
|
||||
import { InferSelectModel, eq, and, desc } from "drizzle-orm";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
type Task = InferSelectModel<typeof tasks>;
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
|
||||
const source = searchParams.get("source");
|
||||
const sourceId = searchParams.get("source_id");
|
||||
const service = searchParams.get("service");
|
||||
|
||||
if (!source || !sourceId || !service) {
|
||||
return NextResponse.json(
|
||||
{ error: "Missing required query params" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const result: Task[] = await db
|
||||
.select()
|
||||
.from(tasks)
|
||||
.where(
|
||||
and(
|
||||
eq(tasks.source, source as any), // cast needed for enum
|
||||
eq(tasks.sourceId, sourceId),
|
||||
eq(tasks.service, service),
|
||||
),
|
||||
)
|
||||
.orderBy(desc(tasks.jobStarted))
|
||||
.limit(1);
|
||||
|
||||
const task = result[0];
|
||||
|
||||
if (!task) {
|
||||
return NextResponse.json(
|
||||
{ error: "Task not found" },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ task });
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue