@@ -23,6 +23,7 @@ import {
23
23
insertFhirServer ,
24
24
updateFhirServer ,
25
25
deleteFhirServer ,
26
+ updateFhirServerConnectionStatus ,
26
27
} from "@/app/backend/fhir-servers" ;
27
28
28
29
const Modal = dynamic < ModalProps > (
@@ -103,6 +104,7 @@ const FhirServers: React.FC = () => {
103
104
setConnectionStatus ( "idle" ) ;
104
105
setDisableCertValidation ( server . disableCertValidation ) ;
105
106
setDefaultServer ( server . defaultServer ) ;
107
+ setErrorMessage ( "" ) ;
106
108
107
109
// Set auth method and corresponding fields based on server data
108
110
if ( server . authType ) {
@@ -207,11 +209,45 @@ const FhirServers: React.FC = () => {
207
209
}
208
210
} ;
209
211
const handleTestConnection = async ( ) => {
210
- const result = await testFhirConnection ( serverUrl ) ;
212
+ // 1. Test the connection (returns { success, error })
213
+ const result = await testFhirServerConnection (
214
+ serverUrl ,
215
+ disableCertValidation ,
216
+ {
217
+ authType : authMethod ,
218
+ bearerToken : authMethod === "basic" ? bearerToken : undefined ,
219
+ clientId : [ "client_credentials" , "SMART" ] . includes ( authMethod )
220
+ ? clientId
221
+ : undefined ,
222
+ clientSecret :
223
+ authMethod === "client_credentials" ? clientSecret : undefined ,
224
+ tokenEndpoint : [ "client_credentials" , "SMART" ] . includes ( authMethod )
225
+ ? tokenEndpoint
226
+ : undefined ,
227
+ scopes : [ "client_credentials" , "SMART" ] . includes ( authMethod )
228
+ ? scopes
229
+ : undefined ,
230
+ } ,
231
+ ) ;
232
+
233
+ // 2. Update connection status in DB
234
+ const updateResult = await updateFhirServerConnectionStatus (
235
+ selectedServer ?. name || serverName ,
236
+ result . success ,
237
+ ) ;
238
+
211
239
setConnectionStatus ( result . success ? "success" : "error" ) ;
212
240
setErrorMessage ( result . error ) ;
213
- } ;
214
241
242
+ // 3. Update the frontend server row to reflect new last checked time
243
+ if ( updateResult . server ) {
244
+ setFhirServers ( ( prev ) =>
245
+ prev . map ( ( srv ) =>
246
+ srv . id === updateResult . server . id ? updateResult . server : srv ,
247
+ ) ,
248
+ ) ;
249
+ }
250
+ } ;
215
251
const handleSave = async ( ) => {
216
252
const connectionResult = await testFhirConnection ( serverUrl ) ;
217
253
0 commit comments