11// necessary immediate imports
2- import 'reflect-metadata' ;
3- import 'dotenv/config' ;
4-
5-
6- import dotenv from 'dotenv' ;
7- import { createExpressServer , ForbiddenError , UnauthorizedError , useContainer as routingUseContainer , HttpError } from 'routing-controllers' ;
8-
9- import { EntityManager , getManager , useContainer } from 'typeorm' ;
10- import { Container } from 'typeorm-typedi-extensions' ;
11- import { Express } from 'express' ;
12- import * as swaggerUi from 'swagger-ui-express' ;
13- import * as path from 'path' ;
14- import * as admin from 'firebase-admin' ;
2+ import "reflect-metadata" ;
3+ import "dotenv/config" ;
4+
5+ import dotenv from "dotenv" ;
6+ import {
7+ createExpressServer ,
8+ ForbiddenError ,
9+ UnauthorizedError ,
10+ useContainer as routingUseContainer ,
11+ HttpError ,
12+ } from "routing-controllers" ;
13+
14+ import { EntityManager , getManager , useContainer } from "typeorm" ;
15+ import { Container } from "typeorm-typedi-extensions" ;
16+ import { Express } from "express" ;
17+ import * as swaggerUi from "swagger-ui-express" ;
18+ import * as path from "path" ;
19+ import * as admin from "firebase-admin" ;
1520
1621dotenv . config ( ) ;
1722
23+ console . log (
24+ "FIREBASE_SERVICE_ACCOUNT_PATH:" ,
25+ process . env . FIREBASE_SERVICE_ACCOUNT_PATH ,
26+ ) ;
1827var serviceAccountPath = process . env . FIREBASE_SERVICE_ACCOUNT_PATH ! ;
1928const serviceAccount = require ( serviceAccountPath ) ;
2029
2130if ( ! serviceAccountPath ) {
22- throw new Error ( 'FIREBASE_SERVICE_ACCOUNT_PATH environment variable is not set.' ) ;
31+ throw new Error (
32+ "FIREBASE_SERVICE_ACCOUNT_PATH environment variable is not set." ,
33+ ) ;
2334}
2435
2536if ( ! admin . apps . length ) {
2637 admin . initializeApp ( {
2738 credential : admin . credential . cert ( serviceAccount ) ,
28-
2939 } ) ;
3040}
3141
32-
33- export { admin } ; // Export the admin instance
34-
35- import { controllers } from './api/controllers' ;
36- import { middlewares } from './api/middlewares' ;
37- import { UserModel } from './models/UserModel' ;
38- import { ReportPostRequest , ReportProfileRequest , ReportMessageRequest } from './types' ;
39- import { GetReportsResponse , Report } from './types/ApiResponses' ;
40- import { ReportController } from './api/controllers/ReportController' ;
41- import resellConnection from './utils/DB' ;
42- import { ReportService } from './services/ReportService' ;
43- import { ReportRepository } from './repositories/ReportRepository' ;
44- import { reportToString } from './utils/Requests' ;
45- import { CurrentUserChecker } from 'routing-controllers/types/CurrentUserChecker' ;
42+ export { admin } ; // Export the admin instance
43+
44+ import { controllers } from "./api/controllers" ;
45+ import { middlewares } from "./api/middlewares" ;
46+ import { UserModel } from "./models/UserModel" ;
47+ import {
48+ ReportPostRequest ,
49+ ReportProfileRequest ,
50+ ReportMessageRequest ,
51+ } from "./types" ;
52+ import { GetReportsResponse , Report } from "./types/ApiResponses" ;
53+ import { ReportController } from "./api/controllers/ReportController" ;
54+ import resellConnection from "./utils/DB" ;
55+ import { ReportService } from "./services/ReportService" ;
56+ import { ReportRepository } from "./repositories/ReportRepository" ;
57+ import { reportToString } from "./utils/Requests" ;
58+ import { CurrentUserChecker } from "routing-controllers/types/CurrentUserChecker" ;
4659// import { getLoadedModel } from './utils/SentenceEncoder';
4760
4861dotenv . config ( ) ;
@@ -61,16 +74,16 @@ async function main() {
6174
6275 const app : Express = createExpressServer ( {
6376 cors : true ,
64- routePrefix : ' /api/' ,
77+ routePrefix : " /api/" ,
6578 controllers : controllers ,
6679 middlewares : middlewares ,
6780 currentUserChecker : async ( action : any ) => {
68- console . log ( ' AUTH MIDDLEWARE CALLED for path:' , action . request . path ) ;
81+ console . log ( " AUTH MIDDLEWARE CALLED for path:" , action . request . path ) ;
6982 const authHeader = action . request . headers [ "authorization" ] ;
7083 if ( ! authHeader ) {
7184 throw new ForbiddenError ( "No authorization token provided" ) ;
7285 }
73- const token = authHeader . split ( ' ' ) [ 1 ] ;
86+ const token = authHeader . split ( " " ) [ 1 ] ;
7487 if ( ! token ) {
7588 throw new ForbiddenError ( "Invalid authorization token format" ) ;
7689 }
@@ -84,22 +97,30 @@ async function main() {
8497 console . log ( "uid" ) ;
8598 action . request . firebaseUid = userId ;
8699 console . log ( "here" ) ;
87- if ( ! email || ! email . endsWith ( ' @cornell.edu' ) ) {
88- throw new ForbiddenError ( ' Only Cornell email addresses are allowed' ) ;
100+ if ( ! email || ! email . endsWith ( " @cornell.edu" ) ) {
101+ throw new ForbiddenError ( " Only Cornell email addresses are allowed" ) ;
89102 }
90103 // Find or create user in your database using Firebase UID
91- const manager = getManager ( ) ;
92- let user = await manager . findOne ( UserModel , { firebaseUid : userId } ,
93- { relations : [ "posts" , "saved" , "feedbacks" , "requests" ] } ) ;
104+ const manager = getManager ( ) ;
105+ let user = await manager . findOne (
106+ UserModel ,
107+ { firebaseUid : userId } ,
108+ { relations : [ "posts" , "saved" , "feedbacks" , "requests" ] } ,
109+ ) ;
94110 if ( ! user ) {
95111 // Check if this is the user creation route or authorization route
96- const isUserCreateRoute = action . request . path === '/api/user/create/' ||
97- action . request . path === '/api/user/create' ||
98- action . request . path === '/api/authorize' ||
99- action . request . path === 'api/authorize' ;
100- console . log ( `User not found for path: ${ action . request . path } , isUserCreateRoute: ${ isUserCreateRoute } ` ) ;
112+ const isUserCreateRoute =
113+ action . request . path === "/api/user/create/" ||
114+ action . request . path === "/api/user/create" ||
115+ action . request . path === "/api/authorize" ||
116+ action . request . path === "api/authorize" ;
117+ console . log (
118+ `User not found for path: ${ action . request . path } , isUserCreateRoute: ${ isUserCreateRoute } ` ,
119+ ) ;
101120 if ( ! isUserCreateRoute ) {
102- throw new ForbiddenError ( 'User not found. Please create an account first.' ) ;
121+ throw new ForbiddenError (
122+ "User not found. Please create an account first." ,
123+ ) ;
103124 }
104125 // For user creation routes, return a minimal UserModel
105126 const tempUser = new UserModel ( ) ;
@@ -109,24 +130,27 @@ async function main() {
109130 tempUser . username = `temp_${ decodedToken . uid } ` ;
110131 tempUser . isActive = true ;
111132 tempUser . admin = false ;
112- tempUser . isNewUser = true ;
133+ tempUser . isNewUser = true ;
113134 return tempUser ;
114- }
135+ }
115136 if ( ! user ) {
116- throw new ForbiddenError ( ' User authentication failed' ) ;
137+ throw new ForbiddenError ( " User authentication failed" ) ;
117138 }
118139 return user ;
119140 } catch ( error ) {
120141 if ( error instanceof ForbiddenError ) {
121142 throw error ;
122143 }
123- if ( error . code == 'auth/argument-error' ) {
124- throw new HttpError ( 408 , 'Request timed out while waiting for response' ) ;
144+ if ( error . code == "auth/argument-error" ) {
145+ throw new HttpError (
146+ 408 ,
147+ "Request timed out while waiting for response" ,
148+ ) ;
125149 }
126- if ( error . code === ' auth/id-token-expired' ) {
127- throw new UnauthorizedError ( ' Token has expired' ) ;
150+ if ( error . code === " auth/id-token-expired" ) {
151+ throw new UnauthorizedError ( " Token has expired" ) ;
128152 }
129- throw new UnauthorizedError ( ' Invalid authorization token' ) ;
153+ throw new UnauthorizedError ( " Invalid authorization token" ) ;
130154 }
131155 } ,
132156 defaults : {
@@ -142,26 +166,27 @@ async function main() {
142166 defaultErrorHandler : false ,
143167 } ) ;
144168
145- const host = process . env . HOST ?? 'localhost' ;
146169 const port = process . env . PORT ?? 3000 ;
147170
148- const swaggerDocument = require ( path . join ( __dirname , '../swagger.json' ) ) ;
149- app . use ( '/api-docs' , swaggerUi . serve , swaggerUi . setup ( swaggerDocument ) ) ;
150- console . log ( `Swagger documentation available at http://${ host } :${ port } /api-docs` ) ;
151-
171+ const swaggerDocument = require ( path . join ( __dirname , "../swagger.json" ) ) ;
172+ app . use ( "/api-docs" , swaggerUi . serve , swaggerUi . setup ( swaggerDocument ) ) ;
173+ console . log (
174+ `Swagger documentation available at http://localhost:${ port } /api-docs` ,
175+ ) ;
176+
152177 const entityManager = getManager ( ) ;
153178 const reportService = new ReportService ( entityManager ) ;
154179 const reportController = new ReportController ( reportService ) ;
155180
156- app . set ( ' view engine' , ' pug' )
181+ app . set ( " view engine" , " pug" ) ;
157182
158- app . get ( ' /api/reports/admin/' , async ( req : any , res : any ) => {
183+ app . get ( " /api/reports/admin/" , async ( req : any , res : any ) => {
159184 const userCheck = async ( action : any ) => {
160185 const authHeader = req . headers [ "authorization" ] ;
161186 if ( ! authHeader ) {
162187 throw new ForbiddenError ( "No authorization token provided" ) ;
163188 }
164- const token = authHeader . split ( ' ' ) [ 1 ] ;
189+ const token = authHeader . split ( " " ) [ 1 ] ;
165190 if ( ! token ) {
166191 throw new ForbiddenError ( "Invalid authorization token format" ) ;
167192 }
@@ -174,25 +199,26 @@ async function main() {
174199 if ( ! user || ! user . admin ) throw new ForbiddenError ( "User unauthorized" ) ;
175200 return user ;
176201 } catch ( error ) {
177- if ( error . code === ' auth/id-token-expired' ) {
178- throw new UnauthorizedError ( ' Token has expired' ) ;
202+ if ( error . code === " auth/id-token-expired" ) {
203+ throw new UnauthorizedError ( " Token has expired" ) ;
179204 }
180- throw new UnauthorizedError ( ' Invalid authorization token' ) ;
205+ throw new UnauthorizedError ( " Invalid authorization token" ) ;
181206 }
182- }
207+ } ;
183208 const user = await userCheck ( req ) ;
184209 user . admin = true ;
185210 const postReports = await reportController . getAllPostReports ( user ) ;
186211 const profileReports = await reportController . getAllProfileReports ( user ) ;
187212 const messageReports = await reportController . getAllMessageReports ( user ) ;
188- res . render ( 'admin' , {
189- postReports : reportToString ( postReports ) ,
190- profileReports : reportToString ( profileReports ) ,
191- messageReports : reportToString ( messageReports ) } ) ;
213+ res . render ( "admin" , {
214+ postReports : reportToString ( postReports ) ,
215+ profileReports : reportToString ( profileReports ) ,
216+ messageReports : reportToString ( messageReports ) ,
217+ } ) ;
192218 } ) ;
193-
219+
194220 app . listen ( port , ( ) => {
195- console . log ( `Resell backend bartering on ${ host } :${ port } ` ) ;
221+ console . log ( `Resell backend bartering on http://localhost :${ port } ` ) ;
196222 } ) ;
197223}
198224
0 commit comments