Skip to content

Commit 0446ea3

Browse files
committed
feat: implement setup on backend
1 parent ba20a3c commit 0446ea3

4 files changed

Lines changed: 45 additions & 4 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ValidationSchema } from '@douglasneuroinformatics/libnest/core';
2+
import { ApiProperty } from '@nestjs/swagger';
3+
import { $UpdateSetupStateData } from '@opendatacapture/schemas/setup';
4+
import type { UpdateSetupStateData } from '@opendatacapture/schemas/setup';
5+
6+
@ValidationSchema($UpdateSetupStateData)
7+
export class UpdateSetupStateDto implements UpdateSetupStateData {
8+
@ApiProperty()
9+
isExperimentalFeaturesEnabled?: boolean;
10+
}

apps/api/src/setup/setup.controller.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Body, Controller, Get, Post } from '@nestjs/common';
1+
import { Body, Controller, Get, Patch, Post } from '@nestjs/common';
22
import { ApiOperation, ApiTags } from '@nestjs/swagger';
33
import type { SetupState } from '@opendatacapture/schemas/setup';
44

55
import { RouteAccess } from '@/core/decorators/route-access.decorator';
66

77
import { InitAppDto } from './dto/init-app.dto';
8+
import { UpdateSetupStateDto } from './dto/update-setup-state.dto';
89
import { SetupService } from './setup.service';
910

1011
@ApiTags('Setup')
@@ -34,4 +35,14 @@ export class SetupController {
3435
initApp(@Body() initAppDto: InitAppDto): Promise<{ success: boolean }> {
3536
return this.setupService.initApp(initAppDto);
3637
}
38+
39+
@ApiOperation({
40+
description: 'Update the setup state',
41+
summary: 'Update State'
42+
})
43+
@Patch()
44+
@RouteAccess({ action: 'manage', subject: 'all' })
45+
updateState(@Body() data: UpdateSetupStateDto): Promise<Partial<SetupState>> {
46+
return this.setupService.updateState(data);
47+
}
3748
}

apps/api/src/setup/setup.service.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ForbiddenException, Injectable } from '@nestjs/common';
1+
import { ForbiddenException, Injectable, ServiceUnavailableException } from '@nestjs/common';
22
import { 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

55
import { ConfigurationService } from '@/configuration/configuration.service';
66
import { DemoService } from '@/demo/demo.service';
@@ -55,6 +55,19 @@ export class SetupService {
5555
return { success: true };
5656
}
5757

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+
5871
private async getSavedOptions() {
5972
return await this.setupStateModel.findFirst();
6073
}

packages/schemas/src/setup/setup.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ export const $ReleaseInfo = z.union([$DevelopmentReleaseInfo, $ProductionRelease
2626
export type SetupState = z.infer<typeof $SetupState>;
2727
export const $SetupState = z.object({
2828
isDemo: z.boolean(),
29-
isExperimentalFeaturesEnabled: z.boolean(),
29+
isExperimentalFeaturesEnabled: z.boolean().nullish(),
3030
isGatewayEnabled: z.boolean(),
3131
isSetup: z.boolean().nullable(),
3232
release: $ReleaseInfo,
3333
uptime: z.number()
3434
});
3535

36+
export type UpdateSetupStateData = z.infer<typeof $UpdateSetupStateData>;
37+
export const $UpdateSetupStateData = $SetupState
38+
.pick({
39+
isExperimentalFeaturesEnabled: true
40+
})
41+
.partial();
42+
3643
export type CreateAdminData = z.infer<typeof $CreateAdminData>;
3744
export const $CreateAdminData = $CreateUserData.omit({
3845
basePermissionLevel: true,

0 commit comments

Comments
 (0)