@@ -7,38 +7,67 @@ import {
77 getAllSessionService ,
88 createSessionService ,
99} from "../services/sessionService" ;
10+ import { logError , logInfo } from "../config/winstonConfig" ;
1011
1112const SESSION_EXPIRY = 3600 ; // 1 hour
1213const COOKIE_OPTIONS = { maxAge : SESSION_EXPIRY , httpOnly : true } ;
1314
1415// Helper function to set session cookie
1516const setSessionCookie = ( res : Response , sessionId : string ) => {
17+ logInfo ( {
18+ message : 'Entering setSessionCookie Controller Function' ,
19+ meta : { sessionId }
20+ } )
1621 res . cookie ( "sessionId" , sessionId , COOKIE_OPTIONS ) ;
22+ logInfo ( {
23+ message : 'Exiting setSessionCookie Controller Function' ,
24+ meta : { sessionId }
25+ } )
1726} ;
1827
1928export const getSession = async ( req : Request , res : Response ) => {
29+ logInfo ( {
30+ message : 'Entering getSession Controller Function'
31+ } )
2032 const subscriber_url = req . query . subscriber_url as string ;
2133
2234 if ( ! subscriber_url ) {
35+ logInfo ( {
36+ message : 'Exiting getSession Controller Function'
37+ } )
2338 res . status ( 400 ) . send ( { message : "Session Key is required." } ) ;
2439 return ;
2540 }
2641
2742 try {
2843 const sessionData = await getSessionService ( subscriber_url ) ;
44+ logInfo ( {
45+ message : 'Exiting getSession Controller Function' ,
46+ meta : { sessionId : subscriber_url }
47+ } )
2948 res . status ( 200 ) . send ( sessionData ) ;
3049 } catch ( error : any ) {
31- console . error ( error ) ;
50+ logError ( {
51+ message : 'Error in getSession Controller Function' ,
52+ meta : { sessionId : subscriber_url } ,
53+ error
54+ } )
3255 res
3356 . status ( 500 )
3457 . send ( { message : "Error fetching session" , error : error . message } ) ;
3558 }
3659} ;
3760
3861export const updateSession = async ( req : Request , res : Response ) => {
62+ logInfo ( {
63+ message : 'Entering updateSession Controller Function' ,
64+ } )
3965 const subscriber_url = req . query . subscriber_url as string ;
4066
4167 if ( ! subscriber_url ) {
68+ logInfo ( {
69+ message : 'Exiting updateSession Controller Function'
70+ } )
4271 res . status ( 400 ) . send ( { message : "subscriber url is required." } ) ;
4372 return ;
4473 }
@@ -47,16 +76,24 @@ export const updateSession = async (req: Request, res: Response) => {
4776 try {
4877 const response = await updateSessionService ( subscriber_url , sessionData ) ;
4978 setSessionCookie ( res , subscriber_url ) ;
79+ logInfo ( {
80+ message : 'Exiting updateSession Controller Function' ,
81+ meta : { sessionId : subscriber_url }
82+ } )
5083 res . status ( 200 ) . send ( { message : response } ) ;
5184 } catch ( error : any ) {
52- console . error ( error ) ;
85+ logError ( {
86+ message : 'Error in updateSession Controller Function' , error, meta : { sessionId : subscriber_url } } ) ;
5387 res
5488 . status ( 500 )
5589 . send ( { message : "Error updating session" , error : error . message } ) ;
5690 }
5791} ;
5892
5993export const deleteSession = async ( req : Request , res : Response ) => {
94+ logInfo ( {
95+ message : 'Entering deleteSession Controller Function'
96+ } )
6097 const subscriber_url = req . query . subscriber_url as string ;
6198
6299 if ( ! subscriber_url ) {
@@ -67,34 +104,59 @@ export const deleteSession = async (req: Request, res: Response) => {
67104 try {
68105 const response = await deleteService ( subscriber_url ) ;
69106 setSessionCookie ( res , subscriber_url ) ;
107+ logInfo ( {
108+ message : 'Exiting deleteSession Controller Function' ,
109+ meta : { sessionId : subscriber_url }
110+ } )
70111 res . status ( 200 ) . send ( { message : response } ) ;
71112 } catch ( error : any ) {
72- console . error ( error ) ;
113+ logError ( {
114+ message : 'Error in deleteSession Controller Function' ,
115+ error,
116+ meta : { sessionId : subscriber_url }
117+ } )
73118 res
74119 . status ( 500 )
75120 . send ( { message : "Error deleting session" , error : error . message } ) ;
76121 }
77122} ;
78123
79124export const updateCacheDb = async ( req : Request , res : Response ) => {
125+ logInfo ( {
126+ message : 'Entering updateCacheDb Controller Function'
127+ } )
80128 const db_id = parseInt ( req . query . db_id as string ) ;
81- console . log ( "Switching db" , db_id ) ;
82129
83130 if ( ! db_id && db_id !== 0 ) {
131+ logInfo ( {
132+ message : 'Exiting updateCacheDb Controller Function'
133+ } )
84134 res . status ( 400 ) . send ( { message : "db_id is required." } ) ;
85135 return ;
86136 }
87-
88137 switchCacheDb ( db_id ) ;
138+ logInfo ( {
139+ message : 'Exiting updateCacheDb Controller Function' ,
140+ meta : { db_id }
141+ } )
89142 res . send ( { message : "Cache DB swithced" } ) ;
90143} ;
91144
92145export const getAllSession = async ( req : Request , res : Response ) => {
146+ logInfo ( {
147+ message : 'Entering getAllSession Controller Function'
148+ } )
93149 try {
94150 const sessionData = await getAllSessionService ( ) ;
151+ logInfo ( {
152+ message : 'Exiting getAllSession Controller Function'
153+ } )
95154 res . status ( 200 ) . send ( sessionData ) ;
96155 } catch ( error : any ) {
97- console . error ( error ) ;
156+ logError ( {
157+ message : 'Error in getAllSession Controller Function' ,
158+ error
159+ } ) ;
98160 res
99161 . status ( 500 )
100162 . send ( { message : "Error fetching session" , error : error . message } ) ;
@@ -103,14 +165,22 @@ export const getAllSession = async (req: Request, res: Response) => {
103165
104166export const createSession = async ( req : Request , res : Response ) => {
105167 const { sessionID, payload } = req . body ;
106-
168+ logInfo ( {
169+ message : 'Entering createSession Controller Function'
170+ } )
107171 try {
108172 const response = await createSessionService ( sessionID , payload ) ;
173+ logInfo ( {
174+ message : 'Exiting createSession Controller Function'
175+ } )
109176 res . status ( 200 ) . send ( { message : response } ) ;
110177 } catch ( error : any ) {
111- console . error ( error ) ;
178+ logError ( {
179+ message : 'Error in getAllSession Controller Function' ,
180+ error
181+ } ) ;
112182 res
113183 . status ( 500 )
114184 . send ( { message : "Error creating session" , error : error . message } ) ;
115185 }
116- } ;
186+ } ;
0 commit comments