mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-25 00:07:47 +01:00
refactor: add schemas to splash controller (#1697)
This commit is contained in:
parent
adface17c7
commit
1264f8cb13
@ -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,
|
||||
|
22
src/lib/openapi/spec/splash-schema.ts
Normal file
22
src/lib/openapi/spec/splash-schema.ts
Normal 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>;
|
@ -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<IUnleashServices, 'userSplashService'>,
|
||||
{
|
||||
userSplashService,
|
||||
openApiService,
|
||||
}: Pick<IUnleashServices, 'userSplashService' | 'openApiService'>,
|
||||
) {
|
||||
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<any, any, ISplashBody, any>,
|
||||
res: Response,
|
||||
req: IAuthRequest<{ id: string }>,
|
||||
res: Response<SplashSchema>,
|
||||
): Promise<void> {
|
||||
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),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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",
|
||||
|
Loading…
Reference in New Issue
Block a user