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' ;
2023-07-13 13:27:52 +02:00
import { FeedbackCreateSchema } from '../../openapi/spec/feedback-create-schema' ;
import { FeedbackUpdateSchema } from '../../openapi/spec/feedback-update-schema' ;
import { FeedbackResponseSchema } from '../../openapi/spec/feedback-response-schema' ;
2022-06-17 08:15:56 +02:00
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' ;
2023-07-13 13:27:52 +02:00
import { feedbackResponseSchema , getStandardResponses } from '../../openapi' ;
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' ,
2023-07-13 13:27:52 +02:00
summary : 'Send Unleash feedback' ,
description :
'Sends feedback gathered from the Unleash UI to the Unleash server. Must be called with a token with an identifiable user (either from being sent from the UI or from using a [PAT](https://docs.getunleash.io/reference/api-tokens-and-client-keys#personal-access-tokens)).' ,
requestBody : createRequestSchema ( 'feedbackCreateSchema' ) ,
responses : {
200 : createResponseSchema ( 'feedbackResponseSchema' ) ,
. . . getStandardResponses ( 400 , 401 , 415 ) ,
} ,
2022-06-17 08:15:56 +02:00
} ) ,
] ,
} ) ;
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' ,
2023-07-13 13:27:52 +02:00
summary : 'Update Unleash feedback' ,
description :
'Updates the feedback with the provided ID. Only provided fields are updated. Fields left out are left untouched. Must be called with a token with an identifiable user (either from being sent from the UI or from using a [PAT](https://docs.getunleash.io/reference/api-tokens-and-client-keys#personal-access-tokens)).' ,
requestBody : createRequestSchema ( 'feedbackUpdateSchema' ) ,
responses : {
200 : createResponseSchema ( 'feedbackResponseSchema' ) ,
. . . getStandardResponses ( 400 , 401 , 415 ) ,
} ,
2022-06-17 08:15:56 +02:00
} ) ,
] ,
} ) ;
2021-06-07 11:11:42 +02:00
}
2022-06-17 08:15:56 +02:00
private async createFeedback (
2023-07-13 13:27:52 +02:00
req : IAuthRequest < unknown , unknown , FeedbackCreateSchema > ,
res : Response < FeedbackResponseSchema > ,
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 ,
2023-07-13 13:27:52 +02:00
feedbackResponseSchema . $id ,
2022-06-17 08:15:56 +02:00
serializeDates ( updated ) ,
) ;
2021-06-07 11:11:42 +02:00
}
2022-06-17 08:15:56 +02:00
private async updateFeedback (
2023-07-13 13:27:52 +02:00
req : IAuthRequest < { id : string } , unknown , FeedbackUpdateSchema > ,
res : Response < FeedbackResponseSchema > ,
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 ,
2023-07-13 13:27:52 +02:00
feedbackResponseSchema . $id ,
2022-06-17 08:15:56 +02:00
serializeDates ( updated ) ,
) ;
2021-06-07 11:11:42 +02:00
}
}
module .exports = UserFeedbackController ;
export default UserFeedbackController ;