File size: 3,744 Bytes
3253dc0
 
 
 
 
 
 
1e81caa
 
 
 
 
 
 
 
 
 
 
41d7fe0
 
 
 
1e81caa
3253dc0
 
 
1e81caa
 
 
3253dc0
1e81caa
 
 
 
 
 
 
 
 
 
 
 
 
 
3253dc0
1e81caa
3253dc0
 
 
1e81caa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58a84ad
1e81caa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3253dc0
 
1e81caa
 
 
 
 
 
 
 
 
 
3253dc0
1e81caa
 
 
3253dc0
1e81caa
 
 
 
 
 
 
 
3253dc0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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(); // SAFER than res.json()

    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 }
    );
  }
}