diff --git a/src/lib/openapi/index.ts b/src/lib/openapi/index.ts index 3fef56e82c..59f0e20be2 100644 --- a/src/lib/openapi/index.ts +++ b/src/lib/openapi/index.ts @@ -33,6 +33,7 @@ import { variantsSchema } from './spec/variants-schema'; import { versionSchema } from './spec/version-schema'; import { environmentsSchema } from './spec/environments-schema'; import { sortOrderSchema } from './spec/sort-order-schema'; +import { splashSchema } from './spec/splash-schema'; // Schemas must have $id property on the form "#/components/schemas/mySchema". export type SchemaId = typeof schemas[keyof typeof schemas]['$id']; @@ -82,6 +83,7 @@ export const schemas = { projectSchema, projectsSchema, sortOrderSchema, + splashSchema, strategySchema, tagSchema, tagsSchema, diff --git a/src/lib/openapi/spec/splash-schema.ts b/src/lib/openapi/spec/splash-schema.ts new file mode 100644 index 0000000000..c21cad3f6a --- /dev/null +++ b/src/lib/openapi/spec/splash-schema.ts @@ -0,0 +1,22 @@ +import { FromSchema } from 'json-schema-to-ts'; + +export const splashSchema = { + $id: '#/components/schemas/splashSchema', + type: 'object', + additionalProperties: false, + required: ['userId', 'splashId', 'seen'], + properties: { + userId: { + type: 'number', + }, + splashId: { + type: 'string', + }, + seen: { + type: 'boolean', + }, + }, + components: {}, +} as const; + +export type SplashSchema = FromSchema; diff --git a/src/lib/routes/admin-api/user-splash.ts b/src/lib/routes/admin-api/user-splash.ts index d0f21c8fe8..d05c701aaf 100644 --- a/src/lib/routes/admin-api/user-splash.ts +++ b/src/lib/routes/admin-api/user-splash.ts @@ -1,5 +1,4 @@ import { Response } from 'express'; - import Controller from '../controller'; import { Logger } from '../../logger'; import { IUnleashConfig } from '../../types/option'; @@ -7,31 +6,47 @@ import { IUnleashServices } from '../../types/services'; import UserSplashService from '../../services/user-splash-service'; import { IAuthRequest } from '../unleash-types'; import { NONE } from '../../types/permissions'; - -interface ISplashBody { - seen: boolean; - splashId: string; -} +import { OpenApiService } from '../../services/openapi-service'; +import { createResponseSchema } from '../../openapi'; +import { splashSchema, SplashSchema } from '../../openapi/spec/splash-schema'; class UserSplashController extends Controller { private logger: Logger; private userSplashService: UserSplashService; + private openApiService: OpenApiService; + constructor( config: IUnleashConfig, - { userSplashService }: Pick, + { + userSplashService, + openApiService, + }: Pick, ) { super(config); this.logger = config.getLogger('splash-controller.ts'); this.userSplashService = userSplashService; + this.openApiService = openApiService; - this.post('/:id', this.updateSplashSettings, NONE); + this.route({ + method: 'post', + path: '/:id', + handler: this.updateSplashSettings, + permission: NONE, + middleware: [ + openApiService.validPath({ + tags: ['admin'], + operationId: 'updateSplashSettings', + responses: { 200: createResponseSchema('splashSchema') }, + }), + ], + }); } private async updateSplashSettings( - req: IAuthRequest, - res: Response, + req: IAuthRequest<{ id: string }>, + res: Response, ): Promise { const { user } = req; const { id } = req.params; @@ -41,8 +56,13 @@ class UserSplashController extends Controller { userId: user.id, seen: true, }; - const updated = await this.userSplashService.updateSplash(splash); - res.json(updated); + + this.openApiService.respondWithValidation( + 200, + res, + splashSchema.$id, + await this.userSplashService.updateSplash(splash), + ); } } diff --git a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap index 00164a9947..42a291b6df 100644 --- a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap +++ b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap @@ -649,6 +649,26 @@ Object { }, "type": "object", }, + "splashSchema": Object { + "additionalProperties": false, + "properties": Object { + "seen": Object { + "type": "boolean", + }, + "splashId": Object { + "type": "string", + }, + "userId": Object { + "type": "number", + }, + }, + "required": Array [ + "userId", + "splashId", + "seen", + ], + "type": "object", + }, "strategySchema": Object { "additionalProperties": false, "properties": Object { @@ -2364,6 +2384,36 @@ Object { ], }, }, + "/api/admin/splash/{id}": Object { + "post": Object { + "operationId": "updateSplashSettings", + "parameters": Array [ + Object { + "in": "path", + "name": "id", + "required": true, + "schema": Object { + "type": "string", + }, + }, + ], + "responses": Object { + "200": Object { + "content": Object { + "application/json": Object { + "schema": Object { + "$ref": "#/components/schemas/splashSchema", + }, + }, + }, + "description": "splashSchema", + }, + }, + "tags": Array [ + "admin", + ], + }, + }, "/api/admin/ui-config": Object { "get": Object { "operationId": "getUIConfig",