1- import { ForbiddenException , Injectable } from '@nestjs/common' ;
1+ import { ForbiddenException , Injectable , ServiceUnavailableException } from '@nestjs/common' ;
22import { type CreateAdminData } from '@opendatacapture/schemas/setup' ;
3- import type { InitAppOptions , SetupState } from '@opendatacapture/schemas/setup' ;
3+ import type { InitAppOptions , SetupState , UpdateSetupStateData } from '@opendatacapture/schemas/setup' ;
44
55import { ConfigurationService } from '@/configuration/configuration.service' ;
66import { DemoService } from '@/demo/demo.service' ;
@@ -27,14 +27,15 @@ export class SetupService {
2727 const savedOptions = await this . getSavedOptions ( ) ;
2828 return {
2929 isDemo : Boolean ( savedOptions ?. isDemo ) ,
30+ isExperimentalFeaturesEnabled : Boolean ( savedOptions ?. isExperimentalFeaturesEnabled ) ,
3031 isGatewayEnabled : this . configurationService . get ( 'GATEWAY_ENABLED' ) ,
3132 isSetup : Boolean ( savedOptions ?. isSetup ) ,
3233 release : __RELEASE__ ,
3334 uptime : Math . round ( process . uptime ( ) )
3435 } satisfies SetupState ;
3536 }
3637
37- async initApp ( { admin, dummySubjectCount, initDemo, recordsPerSubject } : InitAppOptions ) {
38+ async initApp ( { admin, dummySubjectCount, enableExperimentalFeatures , initDemo, recordsPerSubject } : InitAppOptions ) {
3839 const isDev = this . configurationService . get ( 'NODE_ENV' ) === 'development' ;
3940 const savedOptions = await this . getSavedOptions ( ) ;
4041 if ( savedOptions ?. isSetup && ! isDev ) {
@@ -48,10 +49,25 @@ export class SetupService {
4849 recordsPerSubject : recordsPerSubject ?? 0
4950 } ) ;
5051 }
51- await this . setupStateModel . create ( { data : { isDemo : initDemo , isSetup : true } } ) ;
52+ await this . setupStateModel . create ( {
53+ data : { isDemo : initDemo , isExperimentalFeaturesEnabled : enableExperimentalFeatures , isSetup : true }
54+ } ) ;
5255 return { success : true } ;
5356 }
5457
58+ async updateState ( data : UpdateSetupStateData ) : Promise < Partial < SetupState > > {
59+ const setupState = await this . getSavedOptions ( ) ;
60+ if ( ! setupState ?. isSetup ) {
61+ throw new ServiceUnavailableException ( 'Cannot update state before setup' ) ;
62+ }
63+ return this . setupStateModel . update ( {
64+ data,
65+ where : {
66+ id : setupState . id
67+ }
68+ } ) ;
69+ }
70+
5571 private async getSavedOptions ( ) {
5672 return await this . setupStateModel . findFirst ( ) ;
5773 }
0 commit comments