@@ -40,6 +40,7 @@ export async function POST(req: Request) {
40
40
const files : File [ ] = formData . getAll ( "files" ) as File [ ] ;
41
41
const isPdf = formData . get ( "isPdf" ) === "true" ; // Convert string to boolean
42
42
43
+
43
44
let imageURL = "" ;
44
45
if ( isPdf ) {
45
46
imageURL = formData . get ( "image" ) as string ;
@@ -58,53 +59,53 @@ export async function POST(req: Request) {
58
59
const slot = finalTags . slot ;
59
60
const exam = finalTags [ "exam-type" ] ;
60
61
const year = finalTags . year ;
61
- const campus = formData . get ( "campus" ) as string
62
+ const campus = formData . get ( "campus" ) as string ;
62
63
const semester = finalTags . semester ;
63
64
64
65
if ( ! courses . includes ( subject ) ) {
65
66
return NextResponse . json (
66
67
{ message : "The course subject is invalid." } ,
67
- { status : 400 }
68
+ { status : 400 } ,
68
69
) ;
69
70
}
70
-
71
+
71
72
if ( ! slots . includes ( slot ) ) {
72
73
return NextResponse . json (
73
74
{ message : "The slot is invalid." } ,
74
- { status : 400 }
75
+ { status : 400 } ,
75
76
) ;
76
77
}
77
-
78
+
78
79
if ( ! exam . includes ( exam ) ) {
79
80
return NextResponse . json (
80
81
{ message : "The exam type is invalid." } ,
81
- { status : 400 }
82
+ { status : 400 } ,
82
83
) ;
83
84
}
84
-
85
+
85
86
if ( ! years . includes ( year ) ) {
86
87
return NextResponse . json (
87
88
{ message : "The year is invalid." } ,
88
- { status : 400 }
89
+ { status : 400 } ,
89
90
) ;
90
91
}
91
-
92
+
92
93
if ( ! campuses . includes ( campus ) ) {
93
94
return NextResponse . json (
94
- { message :`The ${ campus } is invalid.` } ,
95
- { status : 400 }
95
+ { message : `The ${ campus } is invalid.` } ,
96
+ { status : 400 } ,
96
97
) ;
97
98
}
98
-
99
+
99
100
if ( ! semesters . includes ( semester ) ) {
100
101
return NextResponse . json (
101
102
{ message : "The semester is invalid." } ,
102
- { status : 400 }
103
+ { status : 400 } ,
103
104
) ;
104
105
}
105
-
106
+
106
107
// If all checks pass, continue with the rest of the logic
107
-
108
+
108
109
await connectToDatabase ( ) ;
109
110
let finalUrl : string | undefined = "" ;
110
111
let public_id_cloudinary : string | undefined = "" ;
@@ -160,10 +161,7 @@ export async function POST(req: Request) {
160
161
semester,
161
162
} ) ;
162
163
await paper . save ( ) ;
163
- return NextResponse . json (
164
- { status : "success" , } ,
165
- { status : 201 } ,
166
- ) ;
164
+ return NextResponse . json ( { status : "success" } , { status : 201 } ) ;
167
165
} catch ( error ) {
168
166
console . error ( error ) ;
169
167
return NextResponse . json (
@@ -201,12 +199,13 @@ async function uploadFile(
201
199
}
202
200
}
203
201
204
- async function CreatePDF ( files : File [ ] ) {
202
+ async function CreatePDF ( orderedFiles : File [ ] ) {
205
203
const pdfDoc = await PDFDocument . create ( ) ;
206
204
//sort files using name. Later remove to see if u can without names
207
- const orderedFiles = Array . from ( files ) . sort ( ( a , b ) => {
208
- return a . name . localeCompare ( b . name ) ;
209
- } ) ;
205
+ // moved to main function
206
+ // const orderedFiles = Array.from(files).sort((a, b) => {
207
+ // return a.name.localeCompare(b.name);
208
+ // });
210
209
211
210
for ( const file of orderedFiles ) {
212
211
const fileBlob = new Blob ( [ file ] ) ;
@@ -286,11 +285,6 @@ async function setTagsFromCurrentLists(
286
285
}
287
286
function findMatch < T > ( arr : T [ ] , value : string | undefined ) : T | undefined {
288
287
if ( ! value ) return undefined ; // Handle undefined case
289
- const pattern = new RegExp (
290
- value
291
- . split ( "" )
292
- . map ( ( char ) => `(?=.*${ char } )` )
293
- . join ( "" ) ,
294
- "i"
295
- ) ; return arr . find ( ( item ) => pattern . test ( String ( item ) ) ) ;
296
- }
288
+ const pattern = new RegExp ( value , "i" ) ;
289
+ return arr . find ( ( item ) => pattern . test ( String ( item ) ) ) ;
290
+ }
0 commit comments