@@ -14,6 +14,7 @@ const client = new MongoClient(uri, { serverApi: ServerApiVersion.v1 });
1414/* Swagger/OpenAPI Documentation */
1515const swaggerJsdoc = require ( 'swagger-jsdoc' ) ;
1616const swaggerUi = require ( 'swagger-ui-express' ) ;
17+ const { track } = require ( '@vercel/analytics/server' ) ;
1718
1819
1920/* AWS Connection */
@@ -194,6 +195,25 @@ app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, {
194195 customSiteTitle : 'EDMS API Documentation'
195196} ) ) ;
196197
198+ // Vercel Analytics - track is used directly in routes
199+
200+ // Performance monitoring middleware
201+ app . use ( ( req , res , next ) => {
202+ const start = Date . now ( ) ;
203+ res . on ( 'finish' , ( ) => {
204+ const duration = Date . now ( ) - start ;
205+ if ( duration > 1000 ) { // Log slow requests
206+ track ( 'slow_request' , {
207+ path : req . path ,
208+ method : req . method ,
209+ duration : duration ,
210+ statusCode : res . statusCode
211+ } ) ;
212+ }
213+ } ) ;
214+ next ( ) ;
215+ } ) ;
216+
197217// Serve static files
198218app . use ( express . static ( __dirname ) ) ;
199219app . use ( "/styles" , express . static ( path . join ( __dirname , "styles" ) ) ) ;
@@ -780,6 +800,12 @@ app.post('/registerSubmit', async function (req, res) {
780800 else console . log ( "Welcome email sent successfully" ) ;
781801 } ) ;
782802
803+ // Track user registration
804+ track ( 'user_registered' , {
805+ userId : req . body . userid ,
806+ role : req . body . role || 'contributor'
807+ } ) ;
808+
783809 return res . render ( 'success' , {
784810 title : "Registration Complete" ,
785811 message : "Your account has been successfully created." ,
@@ -1020,6 +1046,14 @@ app.post("/upload", upload.single("document"), async (req, res) => {
10201046 else console . log ( "Email sent successfully" ) ;
10211047 } ) ;
10221048
1049+ // Track file upload
1050+ track ( 'file_uploaded' , {
1051+ userId : req . session . user . userid ,
1052+ fileSize : file . size ,
1053+ fileType : file . mimetype ,
1054+ category : category
1055+ } ) ;
1056+
10231057 res . render ( 'success' , {
10241058 title : "Upload Successful" ,
10251059 message : "Your file has been uploaded to the system." ,
@@ -1378,6 +1412,13 @@ app.post('/api/update-user-role', async (req, res) => {
13781412 return res . status ( 404 ) . json ( { error : 'User not found' } ) ;
13791413 }
13801414
1415+ // Track admin action
1416+ track ( 'user_role_updated' , {
1417+ adminId : req . session . user . userid ,
1418+ targetUserId : userId ,
1419+ newRole : newRole
1420+ } ) ;
1421+
13811422 res . json ( { success : true , message : 'User role updated successfully' } ) ;
13821423 } catch ( error ) {
13831424 console . error ( 'Error updating user role:' , error ) ;
0 commit comments