1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00
unleash.unleash/src/lib/routes/admin-api/user-feedback.ts
Thomas Heartman 9448461aaa
docs: prep to add OpenAPI spec to Unleash docs (#1907)
* Docs: start experimenting with OpenAPI and docusaurus

* Docs: add docusaurus-theme-openapi-docs pkg

* Wip: current status

* Docs: Add 'docusaurus-plugin-api-docs'

* Move openapi into own sidebar; generate from localhost

* Chore: Update docusaurus plugin for OpenAPI

* Add website/yarn.lock to git

* Fix: fix CSS warning by using flex-end instead of end

* docs: make openapi generated code work again

* docs: make tags work properly with openapi sidebar

* Docs/chore: update OpenAPI tag scheme.

Add a whole bunch of new tags to make it easier to understand
available tags in OpenAPI.

* docs: point to new openapi docs from old api docs

* docs: typo

* Docs:  link restructure

* docs: add operation indicators to openapi docs

* docs: change badge color for operations

* docs: update openapi-docs package

It now sorts tags the same as the schema

* docs: pluralize APIs in slug

* docs: update links to generated api docs

* docs: update openapi snapshot tests with new tags

* docs: conditionally load spec from localhost or from file

* docs: Remove changes relating to immediate switchover

* refactor: rename types; extract into separate file

* docs: fix api doc links
2022-08-12 11:37:57 +02:00

115 lines
3.7 KiB
TypeScript

import { Response } from 'express';
import Controller from '../controller';
import { Logger } from '../../logger';
import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types/services';
import UserFeedbackService from '../../services/user-feedback-service';
import { IAuthRequest } from '../unleash-types';
import { NONE } from '../../types/permissions';
import { OpenApiService } from '../../services/openapi-service';
import {
feedbackSchema,
FeedbackSchema,
} from '../../openapi/spec/feedback-schema';
import { serializeDates } from '../../types/serialize-dates';
import { parseISO } from 'date-fns';
import { createRequestSchema } from '../../openapi/util/create-request-schema';
import { createResponseSchema } from '../../openapi/util/create-response-schema';
import BadDataError from '../../error/bad-data-error';
class UserFeedbackController extends Controller {
private logger: Logger;
private userFeedbackService: UserFeedbackService;
private openApiService: OpenApiService;
constructor(
config: IUnleashConfig,
{
userFeedbackService,
openApiService,
}: Pick<IUnleashServices, 'userFeedbackService' | 'openApiService'>,
) {
super(config);
this.logger = config.getLogger('feedback-controller.ts');
this.userFeedbackService = userFeedbackService;
this.openApiService = openApiService;
this.route({
method: 'post',
path: '',
handler: this.createFeedback,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Admin UI'],
operationId: 'createFeedback',
requestBody: createRequestSchema('feedbackSchema'),
responses: { 200: createResponseSchema('feedbackSchema') },
}),
],
});
this.route({
method: 'put',
path: '/:id',
handler: this.updateFeedback,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['Admin UI'],
operationId: 'updateFeedback',
requestBody: createRequestSchema('feedbackSchema'),
responses: { 200: createResponseSchema('feedbackSchema') },
}),
],
});
}
private async createFeedback(
req: IAuthRequest<unknown, unknown, FeedbackSchema>,
res: Response<FeedbackSchema>,
): Promise<void> {
if (!req.body.feedbackId) {
throw new BadDataError('Missing feedbackId');
}
const updated = await this.userFeedbackService.updateFeedback({
feedbackId: req.body.feedbackId,
userId: req.user.id,
given: new Date(),
neverShow: req.body.neverShow || false,
});
this.openApiService.respondWithValidation(
200,
res,
feedbackSchema.$id,
serializeDates(updated),
);
}
private async updateFeedback(
req: IAuthRequest<{ id: string }, unknown, FeedbackSchema>,
res: Response<FeedbackSchema>,
): Promise<void> {
const updated = await this.userFeedbackService.updateFeedback({
feedbackId: req.params.id,
userId: req.user.id,
neverShow: req.body.neverShow || false,
given: req.body.given && parseISO(req.body.given),
});
this.openApiService.respondWithValidation(
200,
res,
feedbackSchema.$id,
serializeDates(updated),
);
}
}
module.exports = UserFeedbackController;
export default UserFeedbackController;