2024-09-10 06:16:41 +00:00
|
|
|
'use server';
|
|
|
|
|
|
|
|
|
|
import { search } from "@/lib/server/searchService";
|
|
|
|
|
import { searchRequestSchema } from "@/lib/schemas";
|
|
|
|
|
import { isServiceError } from "@/lib/utils";
|
|
|
|
|
import { NextRequest } from "next/server";
|
2025-02-12 21:05:44 +00:00
|
|
|
import { withAuth, withOrgMembership } from "@/actions";
|
|
|
|
|
import { schemaValidationError, serviceErrorResponse } from "@/lib/serviceError";
|
|
|
|
|
import { SearchRequest } from "@/lib/types";
|
2024-09-10 06:16:41 +00:00
|
|
|
|
|
|
|
|
export const POST = async (request: NextRequest) => {
|
2025-02-12 21:05:44 +00:00
|
|
|
const domain = request.headers.get("X-Org-Domain")!;
|
2025-01-28 18:39:59 +00:00
|
|
|
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-02-12 21:05:44 +00:00
|
|
|
|
|
|
|
|
const response = await postSearch(parsed.data, domain);
|
2024-09-10 06:16:41 +00:00
|
|
|
if (isServiceError(response)) {
|
|
|
|
|
return serviceErrorResponse(response);
|
|
|
|
|
}
|
|
|
|
|
return Response.json(response);
|
2025-02-12 21:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const postSearch = (request: SearchRequest, domain: string) =>
|
|
|
|
|
withAuth((session) =>
|
|
|
|
|
withOrgMembership(session, domain, async (orgId) => {
|
|
|
|
|
const response = await search(request, orgId);
|
|
|
|
|
return response;
|
|
|
|
|
}))
|