| import { CalculateWeightPayload, WeightBody } from "@/types/calculate"; |
| import { FilterFormValues } from "@/types/candidate-table"; |
| import { cookies } from "next/headers"; |
| import { NextRequest, NextResponse } from "next/server"; |
|
|
| function toFilterBody(filters: FilterFormValues) { |
| return { |
| gpa_edu_1: filters.educations[0]?.gpa || 0, |
| univ_edu_1: filters.educations[0]?.university || [], |
| major_edu_1: filters.educations[0]?.major || [], |
| gpa_edu_2: filters.educations[1]?.gpa || 0, |
| univ_edu_2: filters.educations[1]?.university || [], |
| major_edu_2: filters.educations[1]?.major || [], |
| gpa_edu_3: filters.educations[2]?.gpa || 0, |
| univ_edu_3: filters.educations[2]?.university || [], |
| major_edu_3: filters.educations[2]?.major || [], |
| domicile: filters.domicile || "", |
| yoe: filters.yoe || 0, |
| hardskills: filters.hardskills?.length ? filters.hardskills : [], |
| softskills: filters.softskills?.length ? filters.softskills : [], |
| certifications: filters.certifications?.length ? filters.certifications : [], |
| business_domain: filters.businessDomain?.length ? filters.businessDomain : [], |
| }; |
| } |
|
|
| function toWeightBody(value: CalculateWeightPayload): WeightBody { |
| const edu = (i: number) => |
| value.education[i] ?? { university: 0, major: 0, gpa: 0 }; |
|
|
| return { |
| univ_edu_1: edu(0).university / 100, |
| major_edu_1: edu(0).major / 100, |
| gpa_edu_1: edu(0).gpa / 100, |
| univ_edu_2: edu(1).university / 100, |
| major_edu_2: edu(1).major / 100, |
| gpa_edu_2: edu(1).gpa / 100, |
| univ_edu_3: edu(2).university / 100, |
| major_edu_3: edu(2).major / 100, |
| gpa_edu_3: edu(2).gpa / 100, |
| domicile: value.others.domicile / 100, |
| yoe: value.others.yearOfExperiences / 100, |
| hardskills: value.others.hardskills / 100, |
| softskills: value.others.softskills / 100, |
| certifications: value.others.certifications / 100, |
| business_domain: value.others.businessDomain / 100, |
| }; |
| } |
|
|
| export async function POST(request: NextRequest) { |
| try { |
| const cookieStore = await cookies(); |
| const token = cookieStore.get("auth_token")?.value; |
|
|
| const requestJSON = await request.json(); |
|
|
| const body = { |
| criteria: toFilterBody(requestJSON.filters), |
| criteria_weight: toWeightBody(requestJSON.weights), |
| }; |
|
|
| console.log("π΅ Outgoing Request Body:", body); |
|
|
| const res = await fetch( |
| "https://byteriot-candidateexplorer.hf.space/CandidateExplorer/agentic/v2/calculate_score", |
| { |
| method: "POST", |
| headers: { |
| Authorization: `Bearer ${token}`, |
| "Content-Type": "application/json", |
| }, |
| body: JSON.stringify(body), |
| } |
| ); |
|
|
| const text = await res.text(); |
|
|
| console.log("π‘ External API Status:", res.status); |
| console.log("π‘ External API Raw Response:", text); |
|
|
| let data; |
| try { |
| data = text ? JSON.parse(text) : null; |
| } catch (parseError) { |
| console.error("β JSON Parse Error:", parseError); |
| return NextResponse.json( |
| { |
| error: "Invalid JSON response from external API", |
| raw: text, |
| }, |
| { status: 500 } |
| ); |
| } |
|
|
| if (!res.ok) { |
| console.error("β External API Error:", data); |
| return NextResponse.json( |
| { |
| error: data?.detail || data?.message || "External API error", |
| status: res.status, |
| }, |
| { status: res.status } |
| ); |
| } |
|
|
| return NextResponse.json(data, { status: res.status }); |
| } catch (error: any) { |
| console.error("π₯ Route Crash:", error); |
|
|
| return NextResponse.json( |
| { |
| error: "Internal server error", |
| message: error.message, |
| }, |
| { status: 500 } |
| ); |
| } |
| } |