1- import { HttpClient } from '@angular/common/http' ;
21import { computed , Injectable , inject , signal } from '@angular/core' ;
3- import { catchError , EMPTY , map , Observable , tap } from 'rxjs' ;
42import { environment } from 'src/environments/environment' ;
53import { AlertActionType , AlertType } from '../models/alert' ;
64import { NotificationsService } from './notifications.service' ;
@@ -22,7 +20,6 @@ export interface CreateInitialUserResponse {
2220 providedIn : 'root' ,
2321} )
2422export class SelfhostedService {
25- private _http = inject ( HttpClient ) ;
2623 private _notifications = inject ( NotificationsService ) ;
2724
2825 private _isConfigured = signal < boolean | null > ( null ) ;
@@ -32,46 +29,60 @@ export class SelfhostedService {
3229 public readonly isCheckingConfiguration = this . _isCheckingConfiguration . asReadonly ( ) ;
3330 public readonly isSelfHosted = computed ( ( ) => ! ( environment as any ) . saas ) ;
3431
35- checkConfiguration ( ) : Observable < IsConfiguredResponse > {
32+ async checkConfiguration ( ) : Promise < IsConfiguredResponse > {
3633 this . _isCheckingConfiguration . set ( true ) ;
37- return this . _http . get < IsConfiguredResponse > ( '/selfhosted/is-configured' ) . pipe (
38- tap ( ( response ) => {
39- this . _isConfigured . set ( response . isConfigured ) ;
40- this . _isCheckingConfiguration . set ( false ) ;
41- } ) ,
42- catchError ( ( err ) => {
43- console . error ( 'Failed to check configuration:' , err ) ;
44- this . _isCheckingConfiguration . set ( false ) ;
45- // If the endpoint fails, assume configured to avoid blocking login
46- this . _isConfigured . set ( true ) ;
47- return EMPTY ;
48- } ) ,
49- ) ;
34+ try {
35+ const response = await fetch ( '/api/selfhosted/is-configured' ) ;
36+ if ( ! response . ok ) {
37+ throw new Error ( `HTTP error: ${ response . status } ` ) ;
38+ }
39+ const data : IsConfiguredResponse = await response . json ( ) ;
40+ this . _isConfigured . set ( data . isConfigured ) ;
41+ this . _isCheckingConfiguration . set ( false ) ;
42+ return data ;
43+ } catch ( err ) {
44+ console . error ( 'Failed to check configuration:' , err ) ;
45+ this . _isCheckingConfiguration . set ( false ) ;
46+ // If the endpoint fails, assume configured to avoid blocking login
47+ this . _isConfigured . set ( true ) ;
48+ return { isConfigured : true } ;
49+ }
5050 }
5151
52- createInitialUser ( userData : CreateInitialUserRequest ) : Observable < CreateInitialUserResponse > {
53- return this . _http . post < CreateInitialUserResponse > ( '/selfhosted/initial-user' , userData ) . pipe (
54- map ( ( res ) => {
55- this . _notifications . showSuccessSnackbar ( 'Admin account created successfully.' ) ;
56- this . _isConfigured . set ( true ) ;
57- return res ;
58- } ) ,
59- catchError ( ( err ) => {
60- console . error ( 'Failed to create initial user:' , err ) ;
61- this . _notifications . showAlert (
62- AlertType . Error ,
63- { abstract : err . error ?. message || err . message , details : err . error ?. originalMessage } ,
64- [
65- {
66- type : AlertActionType . Button ,
67- caption : 'Dismiss' ,
68- action : ( ) => this . _notifications . dismissAlert ( ) ,
69- } ,
70- ] ,
71- ) ;
72- return EMPTY ;
73- } ) ,
74- ) ;
52+ async createInitialUser ( userData : CreateInitialUserRequest ) : Promise < CreateInitialUserResponse > {
53+ try {
54+ const response = await fetch ( '/api/selfhosted/initial-user' , {
55+ method : 'POST' ,
56+ headers : {
57+ 'Content-Type' : 'application/json' ,
58+ } ,
59+ body : JSON . stringify ( userData ) ,
60+ } ) ;
61+
62+ if ( ! response . ok ) {
63+ const errorData = await response . json ( ) . catch ( ( ) => ( { } ) ) ;
64+ throw { error : errorData , message : `HTTP error: ${ response . status } ` } ;
65+ }
66+
67+ const data : CreateInitialUserResponse = await response . json ( ) ;
68+ this . _notifications . showSuccessSnackbar ( 'Admin account created successfully.' ) ;
69+ this . _isConfigured . set ( true ) ;
70+ return data ;
71+ } catch ( err : any ) {
72+ console . error ( 'Failed to create initial user:' , err ) ;
73+ this . _notifications . showAlert (
74+ AlertType . Error ,
75+ { abstract : err . error ?. message || err . message , details : err . error ?. originalMessage } ,
76+ [
77+ {
78+ type : AlertActionType . Button ,
79+ caption : 'Dismiss' ,
80+ action : ( ) => this . _notifications . dismissAlert ( ) ,
81+ } ,
82+ ] ,
83+ ) ;
84+ throw err ;
85+ }
7586 }
7687
7788 resetConfigurationState ( ) : void {
0 commit comments