11function findRow ( identifier , byId = false ) {
2- const CACHE_EXPIRATION = 300 ; // 5 minutes instead of 10
2+ const CACHE_EXPIRATION = 300 ; // 5 minutes
33 const cache = CacheService . getScriptCache ( ) ;
4- const cacheKey = ( byId ? 'id_' : 'link_' ) + identifier . toLowerCase ( ) . trim ( ) ;
5-
4+ const cacheKey = ( byId ? 'id_' : 'link_' ) + identifier . trim ( ) ;
5+
66 // Check cache
77 const cachedData = cache . get ( cacheKey ) ;
88 if ( cachedData ) {
@@ -23,86 +23,93 @@ function findRow(identifier, byId = false) {
2323 const lastRow = sheet . getLastRow ( ) ;
2424 const lastCol = sheet . getLastColumn ( ) ;
2525 if ( lastRow <= 1 ) return null ; // No data rows
26-
26+
2727 const data = sheet . getRange ( 1 , 1 , lastRow , lastCol ) . getValues ( ) ;
2828 const headers = data [ 0 ] ;
29-
30- // Clean and normalize identifier
31- const cleanIdentifier = identifier . trim ( ) . toLowerCase ( ) ;
29+
30+ // Clean identifier (no lowercasing)
31+ const cleanIdentifier = identifier . trim ( ) ;
3232 const columnName = byId ? 'ID' : 'Link' ;
3333 const columnIndex = headers . findIndex ( h => h . toString ( ) . trim ( ) === columnName ) ;
3434 if ( columnIndex === - 1 ) throw new Error ( `${ columnName } column not found` ) ;
3535
36- // Search for match
36+ // Search for match (case-sensitive, exact)
3737 for ( let i = 1 ; i < data . length ; i ++ ) {
38- const rowValue = String ( data [ i ] [ columnIndex ] ) . trim ( ) . toLowerCase ( ) ;
38+ const rowValue = String ( data [ i ] [ columnIndex ] ) . trim ( ) ;
3939 const toCompare = byId ? cleanIdentifier : cleanIdentifier . replace ( / ^ @ / , '' ) ;
40-
40+
4141 if ( rowValue === toCompare || ( ! byId && rowValue === `@${ toCompare } ` ) ) {
4242 const responseData = { } ;
4343 headers . forEach ( ( header , index ) => {
4444 // Basic data cleaning
4545 responseData [ header ] = data [ i ] [ index ] !== null ?
4646 String ( data [ i ] [ index ] ) . trim ( ) : '' ;
4747 } ) ;
48-
48+
4949 // Validate required fields
5050 if ( ! responseData . Name ) {
5151 throw new Error ( 'Profile data missing required Name field' ) ;
5252 }
53-
53+
5454 try {
5555 cache . put ( cacheKey , JSON . stringify ( responseData ) , CACHE_EXPIRATION ) ;
5656 } catch ( e ) {
5757 console . error ( 'Failed to cache data:' , e ) ;
5858 }
59-
59+
6060 return responseData ;
6161 }
6262 }
63-
63+
6464 return null ;
6565}
6666
6767function doGet ( e ) {
6868 try {
6969 if ( ! e . parameter ) throw new Error ( 'Missing parameters' ) ;
70-
70+
7171 // Validate identifier
7272 const identifier = e . parameter . id || e . parameter . link ;
7373 if ( ! identifier || typeof identifier !== 'string' ) {
7474 throw new Error ( 'Invalid identifier parameter' ) ;
7575 }
76-
76+
7777 // Search for profile
7878 const data = findRow ( identifier , ! ! e . parameter . id ) ;
7979 if ( ! data ) throw new Error ( 'Profile not found' ) ;
8080
8181 // Prepare safe response data
82+ const now = Date . now ( ) ;
83+ // If the data has a timestamp field, use it, else add one
84+ let timestamp = data . timestamp ? Number ( data . timestamp ) : now ;
85+ // If the timestamp is not a valid number, use now
86+ if ( isNaN ( timestamp ) ) timestamp = now ;
87+
8288 const response = {
8389 status : 'success' ,
8490 data : {
8591 status : data . Status || 'Inactive' ,
8692 Name : data . Name || '' ,
8793 Link : data . Link || '' ,
94+ timestamp : timestamp ,
8895 // Add other fields as needed
8996 ...sanitizeProfileData ( data )
9097 }
9198 } ;
9299
93100 // Return response
94101 const output = ContentService . createTextOutput (
95- e . parameter . callback
102+ e . parameter . callback
96103 ? `${ e . parameter . callback } (${ JSON . stringify ( response ) } )`
97104 : JSON . stringify ( response )
98105 ) ;
99106
100107 output . setMimeType (
101- e . parameter . callback
102- ? ContentService . MimeType . JAVASCRIPT
108+ e . parameter . callback
109+ ? ContentService . MimeType . JAVASCRIPT
103110 : ContentService . MimeType . JSON
104111 ) ;
105-
112+ output . setHeader ( "Access-Control-Allow-Origin" , "*" ) ;
106113 return output ;
107114
108115 } catch ( error ) {
@@ -114,6 +121,7 @@ function doGet(e) {
114121 } )
115122 ) ;
116123 errorOutput . setMimeType ( ContentService . MimeType . JSON ) ;
124+ errorOutput . setHeader ( "Access-Control-Allow-Origin" , "*" ) ;
117125 return errorOutput ;
118126 }
119127}
0 commit comments