2024-09-10 06:16:41 +00:00
|
|
|
'use server';
|
|
|
|
|
|
|
|
|
|
import { search } from "@/lib/server/searchService";
|
|
|
|
|
import { searchRequestSchema } from "@/lib/schemas";
|
|
|
|
|
import { schemaValidationError, serviceErrorResponse } from "@/lib/serviceError";
|
|
|
|
|
import { isServiceError } from "@/lib/utils";
|
|
|
|
|
import { NextRequest } from "next/server";
|
2025-01-28 18:39:59 +00:00
|
|
|
import { getCurrentUserOrg } from "../../../../auth";
|
2024-09-10 06:16:41 +00:00
|
|
|
|
|
|
|
|
export const POST = async (request: NextRequest) => {
|
2025-01-28 18:39:59 +00:00
|
|
|
const orgId = await getCurrentUserOrg();
|
|
|
|
|
if (isServiceError(orgId)) {
|
|
|
|
|
return orgId;
|
|
|
|
|
}
|
2025-01-15 00:46:36 +00:00
|
|
|
|
2025-01-28 18:39:59 +00:00
|
|
|
console.log(`Searching for org ${orgId}`);
|
|
|
|
|
const body = await request.json();
|
|
|
|
|
const parsed = await searchRequestSchema.safeParseAsync(body);
|
2024-09-10 06:16:41 +00:00
|
|
|
if (!parsed.success) {
|
|
|
|
|
return serviceErrorResponse(
|
|
|
|
|
schemaValidationError(parsed.error)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-15 00:46:36 +00:00
|
|
|
|
2025-01-28 18:39:59 +00:00
|
|
|
const response = await search(parsed.data, orgId);
|
2024-09-10 06:16:41 +00:00
|
|
|
if (isServiceError(response)) {
|
|
|
|
|
return serviceErrorResponse(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Response.json(response);
|
|
|
|
|
}
|