2021-06-07 11:11:42 +02:00
|
|
|
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';
|
2021-08-13 10:36:19 +02:00
|
|
|
import { IAuthRequest } from '../unleash-types';
|
2021-12-03 12:46:50 +01:00
|
|
|
import { NONE } from '../../types/permissions';
|
2022-06-17 08:15:56 +02:00
|
|
|
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';
|
2022-07-01 08:06:33 +02:00
|
|
|
import { createRequestSchema } from '../../openapi/util/create-request-schema';
|
|
|
|
import { createResponseSchema } from '../../openapi/util/create-response-schema';
|
2022-06-17 08:15:56 +02:00
|
|
|
import BadDataError from '../../error/bad-data-error';
|
2021-06-07 11:11:42 +02:00
|
|
|
|
|
|
|
class UserFeedbackController extends Controller {
|
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
private userFeedbackService: UserFeedbackService;
|
|
|
|
|
2022-06-17 08:15:56 +02:00
|
|
|
private openApiService: OpenApiService;
|
|
|
|
|
2021-06-07 11:11:42 +02:00
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
2022-06-17 08:15:56 +02:00
|
|
|
{
|
|
|
|
userFeedbackService,
|
|
|
|
openApiService,
|
|
|
|
}: Pick<IUnleashServices, 'userFeedbackService' | 'openApiService'>,
|
2021-06-07 11:11:42 +02:00
|
|
|
) {
|
|
|
|
super(config);
|
|
|
|
this.logger = config.getLogger('feedback-controller.ts');
|
|
|
|
this.userFeedbackService = userFeedbackService;
|
2022-06-17 08:15:56 +02:00
|
|
|
this.openApiService = openApiService;
|
2021-06-07 11:11:42 +02:00
|
|
|
|
2022-06-17 08:15:56 +02:00
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '',
|
|
|
|
handler: this.createFeedback,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Admin UI'],
|
2022-06-17 08:15:56 +02:00
|
|
|
operationId: 'createFeedback',
|
|
|
|
requestBody: createRequestSchema('feedbackSchema'),
|
|
|
|
responses: { 200: createResponseSchema('feedbackSchema') },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'put',
|
|
|
|
path: '/:id',
|
|
|
|
handler: this.updateFeedback,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
2022-08-12 11:37:57 +02:00
|
|
|
tags: ['Admin UI'],
|
2022-06-17 08:15:56 +02:00
|
|
|
operationId: 'updateFeedback',
|
|
|
|
requestBody: createRequestSchema('feedbackSchema'),
|
|
|
|
responses: { 200: createResponseSchema('feedbackSchema') },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2021-06-07 11:11:42 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 08:15:56 +02:00
|
|
|
private async createFeedback(
|
|
|
|
req: IAuthRequest<unknown, unknown, FeedbackSchema>,
|
|
|
|
res: Response<FeedbackSchema>,
|
2021-06-07 11:11:42 +02:00
|
|
|
): Promise<void> {
|
2022-06-17 08:15:56 +02:00
|
|
|
if (!req.body.feedbackId) {
|
|
|
|
throw new BadDataError('Missing feedbackId');
|
2021-06-07 11:11:42 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 08:15:56 +02:00
|
|
|
const updated = await this.userFeedbackService.updateFeedback({
|
|
|
|
feedbackId: req.body.feedbackId,
|
|
|
|
userId: req.user.id,
|
2021-06-07 11:11:42 +02:00
|
|
|
given: new Date(),
|
|
|
|
neverShow: req.body.neverShow || false,
|
2022-06-17 08:15:56 +02:00
|
|
|
});
|
2021-06-07 11:11:42 +02:00
|
|
|
|
2022-06-17 08:15:56 +02:00
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
200,
|
|
|
|
res,
|
|
|
|
feedbackSchema.$id,
|
|
|
|
serializeDates(updated),
|
|
|
|
);
|
2021-06-07 11:11:42 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 08:15:56 +02:00
|
|
|
private async updateFeedback(
|
|
|
|
req: IAuthRequest<{ id: string }, unknown, FeedbackSchema>,
|
|
|
|
res: Response<FeedbackSchema>,
|
2021-06-07 11:11:42 +02:00
|
|
|
): Promise<void> {
|
2022-06-17 08:15:56 +02:00
|
|
|
const updated = await this.userFeedbackService.updateFeedback({
|
|
|
|
feedbackId: req.params.id,
|
|
|
|
userId: req.user.id,
|
2021-06-07 11:11:42 +02:00
|
|
|
neverShow: req.body.neverShow || false,
|
2022-06-17 08:15:56 +02:00
|
|
|
given: req.body.given && parseISO(req.body.given),
|
|
|
|
});
|
2021-06-07 11:11:42 +02:00
|
|
|
|
2022-06-17 08:15:56 +02:00
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
200,
|
|
|
|
res,
|
|
|
|
feedbackSchema.$id,
|
|
|
|
serializeDates(updated),
|
|
|
|
);
|
2021-06-07 11:11:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = UserFeedbackController;
|
|
|
|
export default UserFeedbackController;
|