@@ -2,13 +2,19 @@ import { NextResponse } from "next/server";
2
2
import { PDFDocument } from "pdf-lib" ;
3
3
import {
4
4
campuses ,
5
+ courses ,
6
+ exams ,
5
7
semesters ,
6
8
slots ,
7
9
years ,
8
10
} from "@/components/select_options" ;
9
11
import { connectToDatabase } from "@/lib/mongoose" ;
10
12
import cloudinary from "cloudinary" ;
11
- import { type ICourses , type CloudinaryUploadResult } from "@/interface" ;
13
+ import type {
14
+ ICourses ,
15
+ CloudinaryUploadResult ,
16
+ ExamDetail ,
17
+ } from "@/interface" ;
12
18
import { PaperAdmin } from "@/db/papers" ;
13
19
import axios from "axios" ;
14
20
import processAndAnalyze from "@/util/mistral" ;
@@ -38,53 +44,34 @@ export async function POST(req: Request) {
38
44
} else {
39
45
const bytes = await files [ 0 ] ?. arrayBuffer ( ) ;
40
46
if ( bytes ) {
41
- const buffer = Buffer . from ( bytes ) ;
47
+ const buffer = Buffer . from ( bytes ) ;
42
48
imageURL = `data:${ "image/png" } ;base64,${ buffer . toString ( "base64" ) } ` ;
43
49
}
44
50
}
45
51
const tags = await processAndAnalyze ( { imageURL } ) ;
46
- let subject = "" ;
47
- let slot = "" ;
48
- let exam = "" ;
49
- let year = "" ;
50
- let campus = "" ;
51
- let semester = "" ;
52
- const { data } = await axios . get < ICourses [ ] > (
53
- `${ process . env . SERVER_URL } /api/course-list` ,
54
- ) ;
55
- const courses = data . map ( ( course : { name : string } ) => course . name ) ;
56
- const coursesFuzy = new Fuse ( courses ) ;
57
- if ( ! tags ) {
58
- console . log ( "Anaylsis failed, inputing blank strings as fields" ) ;
59
- } else {
60
- const subjectSearch = coursesFuzy . search ( tags [ "course-name" ] ) [ 0 ] ;
61
- if ( subjectSearch ) {
62
- subject = subjectSearch . item ;
63
- }
64
- const slotPattern = new RegExp ( `[${ tags . slot } ]` , "i" ) ;
65
- const slotSearchResult = slots . find ( ( s ) => slotPattern . test ( s ) ) ;
66
- slot = slotSearchResult ? slotSearchResult : tags . slot ;
67
- if ( "exam-type" in tags && tags [ "exam-type" ] in examMap ) {
68
- const examType = tags [ "exam-type" ] as keyof typeof examMap ;
69
- exam = examMap [ examType ] ;
70
- }
71
- year = formData . get ( "year" ) as string ;
72
- campus = formData . get ( "campus" ) as string ;
73
- semester = formData . get ( "semester" ) as string ;
74
- }
75
- console . log ( exam , slot , subject ) ;
52
+ const finalTags = await setTagsFromCurrentLists ( tags ) ;
53
+ console . log ( finalTags )
54
+ const subject = finalTags [ "course-name" ] ;
55
+ const slot = finalTags . slot ;
56
+ const exam = finalTags [ "exam-type" ] ;
57
+ const year = formData . get ( "year" ) as string ;
58
+ const campus = formData . get ( "campus" ) as string ;
59
+ const semester = formData . get ( "semester" ) as string ;
76
60
61
+
62
+ console . log ( subject , slot , exam )
77
63
if (
78
64
! (
65
+ courses . includes ( subject ) &&
66
+ slots . includes ( slot ) &&
79
67
exam . includes ( exam ) &&
80
68
years . includes ( year ) &&
81
69
campuses . includes ( campus ) &&
82
70
semesters . includes ( semester )
83
71
)
84
72
) {
85
- return NextResponse . json ( { message : "Bad request" } , { status : 400 } ) ;
73
+ return NextResponse . json ( { message : "Bad request/AI couldn't set tags " } , { status : 400 } ) ;
86
74
}
87
-
88
75
await connectToDatabase ( ) ;
89
76
let finalUrl : string | undefined = "" ;
90
77
let public_id_cloudinary : string | undefined = "" ;
@@ -213,3 +200,45 @@ async function CreatePDF(files: File[]) {
213
200
const mergedPdfBytes = await pdfDoc . save ( ) ;
214
201
return mergedPdfBytes ;
215
202
}
203
+
204
+ //sets course-name to corresponding course name from our api
205
+ async function setTagsFromCurrentLists (
206
+ tags : ExamDetail | undefined ,
207
+ ) : Promise < ExamDetail > {
208
+ const { data } = await axios . get < ICourses [ ] > (
209
+ `${ process . env . SERVER_URL } /api/course-list` ,
210
+ ) ;
211
+ const courses = data . map ( ( course : { name : string } ) => course . name ) ;
212
+ if ( ! courses [ 0 ] || ! slots [ 0 ] || ! exams [ 0 ] ) {
213
+ throw "Cannot fetch default value for courses/slot/exam!" ;
214
+ }
215
+ const newTags : ExamDetail = {
216
+ "course-name" : courses [ 0 ] ,
217
+ slot : slots [ 0 ] ,
218
+ "course-code" : "notInUse" ,
219
+ "exam-type" : exams [ 0 ] ,
220
+ } ;
221
+ const coursesFuzy = new Fuse ( courses ) ;
222
+ if ( ! tags ) {
223
+ console . log ( "Anaylsis failed setting random courses as fields" ) ;
224
+ return newTags ;
225
+ } else {
226
+ const subjectSearch = coursesFuzy . search (
227
+ tags [ "course-name" ] + "|" + tags [ "course-code" ] ,
228
+ ) [ 0 ] ;
229
+ if ( subjectSearch ) {
230
+ newTags [ "course-name" ] = subjectSearch . item ;
231
+ }
232
+ const slotPattern = new RegExp ( `[${ tags . slot } ]` , "i" ) ;
233
+ const slotSearchResult = slots . find ( ( s ) => slotPattern . test ( s ) ) ;
234
+ if ( slotSearchResult )
235
+ {
236
+ newTags . slot = slotSearchResult
237
+ }
238
+ if ( "exam-type" in tags && tags [ "exam-type" ] in examMap ) {
239
+ const examType = tags [ "exam-type" ] as keyof typeof examMap ;
240
+ newTags [ "exam-type" ] = examMap [ examType ] ;
241
+ }
242
+ }
243
+ return newTags ;
244
+ }
0 commit comments