11import express , { Request , Response , Router , NextFunction } from "express" ;
22import { DatabaseController } from "../database-controller" ;
3- import { GetQuery , InsertData , RadData , Testing } from "../types" ;
3+ import { GetQuery , GPTResponse , InsertData , RadData , Testing } from "../types" ;
44import axios from "axios" ;
55import jwt from "jsonwebtoken" ;
66import authenticateJWT from "../auth/jwt-auth" ;
7+ import { GPTController } from "../gpt-controller" ;
8+ import multer from "multer" ;
79
8- export default function adminRouter ( dbController : DatabaseController ) : Router {
10+ const router = express . Router ( ) ;
11+ const upload = multer ( { dest : "../../pdfData/papers" } ) ;
12+
13+ export default function adminRouter (
14+ dbController : DatabaseController ,
15+ gptController : GPTController ,
16+ ) : Router {
917 const router = Router ( ) ;
1018
1119 // This takes a data response from the GUI after fixes have been made.
@@ -30,10 +38,11 @@ export default function adminRouter(dbController: DatabaseController): Router {
3038 router . post (
3139 "/parseRequest" ,
3240 authenticateJWT ,
41+ upload . array ( "pdfs" ) ,
3342 ( req : Request , res : Response ) => {
3443 try {
3544 // TODO
36- parsePapers ( req . body ) . then ( ( result : InsertData [ ] ) => {
45+ parsePapers ( req . files , gptController ) . then ( ( result : GPTResponse [ ] ) => {
3746 res . send ( responseToJSON ( result ) ) ;
3847 } ) ;
3948 } catch ( error ) {
@@ -68,30 +77,25 @@ export default function adminRouter(dbController: DatabaseController): Router {
6877 } ,
6978 ) ;
7079
71-
72-
7380 const casData = casResponse . data ; // assumed user CAS info, need to test to see
74- if ( casData . includes ( '<cas:authenticationSuccess>' ) ) {
75- const nsid = casData . match ( / < c a s : u s e r > ( .* ?) < \/ c a s : u s e r > / ) [ 1 ] ;
76- console . log ( `User logged in with nsid: ${ nsid } ` ) ;
77- if ( ! nsid ) {
78- res . status ( 401 ) . json ( { error : "Invalid CAS Ticket" } ) ;
79- }
80-
81- if ( ! allowedNSIDs . includes ( nsid ) ) {
82- console . log ( `User attempted to login with nsid: ${ nsid } ` ) ;
83- res . status ( 403 ) . json ( { error : "Access denied" } ) ;
84- }
85- const token = jwt . sign (
86- { username : nsid } ,
87- process . env . JWT_SECRET ! ,
88- { expiresIn : "3h" } ,
89- ) ;
90- res . json ( { token : token , nsid : nsid } ) ;
91- } else {
92- res . status ( 401 ) . json ( { error : 'CAS authentication failed' } ) ;
93-
94- }
81+ if ( casData . includes ( "<cas:authenticationSuccess>" ) ) {
82+ const nsid = casData . match ( / < c a s : u s e r > ( .* ?) < \/ c a s : u s e r > / ) [ 1 ] ;
83+ console . log ( `User logged in with nsid: ${ nsid } ` ) ;
84+ if ( ! nsid ) {
85+ res . status ( 401 ) . json ( { error : "Invalid CAS Ticket" } ) ;
86+ }
87+
88+ if ( ! allowedNSIDs . includes ( nsid ) ) {
89+ console . log ( `User attempted to login with nsid: ${ nsid } ` ) ;
90+ res . status ( 403 ) . json ( { error : "Access denied" } ) ;
91+ }
92+ const token = jwt . sign ( { username : nsid } , process . env . JWT_SECRET ! , {
93+ expiresIn : "3h" ,
94+ } ) ;
95+ res . json ( { token : token , nsid : nsid } ) ;
96+ } else {
97+ res . status ( 401 ) . json ( { error : "CAS authentication failed" } ) ;
98+ }
9599 } catch ( error ) {
96100 res . status ( 500 ) . json ( { error : "CAS validation failed" } ) ;
97101 }
@@ -109,8 +113,13 @@ async function insertRows(
109113 }
110114}
111115
112- async function parsePapers ( body : any ) {
113- const temp : InsertData [ ] = [ ] ;
116+ async function parsePapers (
117+ files : any ,
118+ gptController : GPTController ,
119+ ) : Promise < GPTResponse [ ] > {
120+ const fileList : string [ ] = files . forEach ( ( file : any ) => file . path ) ;
121+ gptController . runGPTAnalysis ( fileList ) ;
122+ const temp : GPTResponse [ ] = [ ] ;
114123 return temp ;
115124}
116125
@@ -127,6 +136,6 @@ function requestFromJSON(body: any): InsertData[] {
127136 } ) ;
128137}
129138
130- function responseToJSON ( radDataArray : RadData [ ] ) : string {
139+ function responseToJSON ( radDataArray : GPTResponse [ ] ) : string {
131140 return JSON . stringify ( radDataArray , null , 2 ) ; // null and 2 prettify the JSON
132141}
0 commit comments