1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-04 00:18:01 +01:00

refactor: add schemas to splash controller (#1697)

This commit is contained in:
olav 2022-06-10 15:11:07 +02:00 committed by GitHub
parent adface17c7
commit 1264f8cb13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 106 additions and 12 deletions

View File

@ -33,6 +33,7 @@ import { variantsSchema } from './spec/variants-schema';
import { versionSchema } from './spec/version-schema'; import { versionSchema } from './spec/version-schema';
import { environmentsSchema } from './spec/environments-schema'; import { environmentsSchema } from './spec/environments-schema';
import { sortOrderSchema } from './spec/sort-order-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". // Schemas must have $id property on the form "#/components/schemas/mySchema".
export type SchemaId = typeof schemas[keyof typeof schemas]['$id']; export type SchemaId = typeof schemas[keyof typeof schemas]['$id'];
@ -82,6 +83,7 @@ export const schemas = {
projectSchema, projectSchema,
projectsSchema, projectsSchema,
sortOrderSchema, sortOrderSchema,
splashSchema,
strategySchema, strategySchema,
tagSchema, tagSchema,
tagsSchema, tagsSchema,

View File

@ -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<typeof splashSchema>;

View File

@ -1,5 +1,4 @@
import { Response } from 'express'; import { Response } from 'express';
import Controller from '../controller'; import Controller from '../controller';
import { Logger } from '../../logger'; import { Logger } from '../../logger';
import { IUnleashConfig } from '../../types/option'; import { IUnleashConfig } from '../../types/option';
@ -7,31 +6,47 @@ import { IUnleashServices } from '../../types/services';
import UserSplashService from '../../services/user-splash-service'; import UserSplashService from '../../services/user-splash-service';
import { IAuthRequest } from '../unleash-types'; import { IAuthRequest } from '../unleash-types';
import { NONE } from '../../types/permissions'; import { NONE } from '../../types/permissions';
import { OpenApiService } from '../../services/openapi-service';
interface ISplashBody { import { createResponseSchema } from '../../openapi';
seen: boolean; import { splashSchema, SplashSchema } from '../../openapi/spec/splash-schema';
splashId: string;
}
class UserSplashController extends Controller { class UserSplashController extends Controller {
private logger: Logger; private logger: Logger;
private userSplashService: UserSplashService; private userSplashService: UserSplashService;
private openApiService: OpenApiService;
constructor( constructor(
config: IUnleashConfig, config: IUnleashConfig,
{ userSplashService }: Pick<IUnleashServices, 'userSplashService'>, {
userSplashService,
openApiService,
}: Pick<IUnleashServices, 'userSplashService' | 'openApiService'>,
) { ) {
super(config); super(config);
this.logger = config.getLogger('splash-controller.ts'); this.logger = config.getLogger('splash-controller.ts');
this.userSplashService = userSplashService; 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( private async updateSplashSettings(
req: IAuthRequest<any, any, ISplashBody, any>, req: IAuthRequest<{ id: string }>,
res: Response, res: Response<SplashSchema>,
): Promise<void> { ): Promise<void> {
const { user } = req; const { user } = req;
const { id } = req.params; const { id } = req.params;
@ -41,8 +56,13 @@ class UserSplashController extends Controller {
userId: user.id, userId: user.id,
seen: true, seen: true,
}; };
const updated = await this.userSplashService.updateSplash(splash);
res.json(updated); this.openApiService.respondWithValidation(
200,
res,
splashSchema.$id,
await this.userSplashService.updateSplash(splash),
);
} }
} }

View File

@ -649,6 +649,26 @@ Object {
}, },
"type": "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 { "strategySchema": Object {
"additionalProperties": false, "additionalProperties": false,
"properties": Object { "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 { "/api/admin/ui-config": Object {
"get": Object { "get": Object {
"operationId": "getUIConfig", "operationId": "getUIConfig",