Skip to content

Commit

Permalink
ref: move FormFeedback related types to root shared folder
Browse files Browse the repository at this point in the history
  • Loading branch information
karrui committed Jul 14, 2021
1 parent b5aaba5 commit 493c25b
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 74 deletions.
4 changes: 4 additions & 0 deletions shared/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export interface ErrorDto {
message: string
}

export interface SuccessMessageDto {
message: string
}

export interface PrivateFormErrorDto extends ErrorDto {
isPageFound: true
formTitle: string
Expand Down
37 changes: 37 additions & 0 deletions shared/types/form/form_feedback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { DateString } from '../generic'
import { FormDto } from './form'

export type SubmitFormFeedbackBodyDto = {
isPreview?: boolean
rating: number
comment?: string
}

/**
* Typing for individual form feedback
*/
export type FormFeedbackBase = {
rating: number
comment?: string
formId: FormDto['_id']
created?: DateString
lastModified?: DateString
}
export type FormFeedbackDto = FormFeedbackBase

export type ProcessedFeedbackMeta = {
index: number
timestamp: number
rating: number
comment: string
// Note that this date is not a DateString, it is actually "D MMM YYYY"
// format.
date: string
dateShort: string
}

export type FormFeedbackMetaDto = {
average?: string
count: number
feedback: ProcessedFeedbackMeta[]
}
4 changes: 2 additions & 2 deletions src/app/modules/feedback/__tests__/feedback.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import getFormFeedbackModel from 'src/app/models/form_feedback.server.model'

import dbHandler from 'tests/unit/backend/helpers/jest-db'

import { GetFormFeedbackDto } from '../../../../types/api/form_feedback'
import { FormFeedbackMetaDto } from '../../../../types/api/form_feedback'
import { DatabaseError } from '../../core/core.errors'
import * as FeedbackService from '../feedback.service'

Expand Down Expand Up @@ -201,7 +201,7 @@ describe('feedback.service', () => {
const actualResult = await FeedbackService.getFormFeedbacks(mockFormId)

// Assert
const expectedResult: GetFormFeedbackDto = {
const expectedResult: FormFeedbackMetaDto = {
count: 1,
average: '3.00',
feedback: [
Expand Down
8 changes: 4 additions & 4 deletions src/app/modules/feedback/feedback.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import mongoose from 'mongoose'
import { ResultAsync } from 'neverthrow'

import { IFormFeedbackSchema } from '../../../types'
import { GetFormFeedbackDto } from '../../../types/api/form_feedback'
import { ProcessedFeedback } from '../../../types/form_feedback'
import { FormFeedbackMetaDto } from '../../../types/api/form_feedback'
import { ProcessedFeedbackMeta } from '../../../types/form_feedback'
import { createLoggerWithLabel } from '../../config/logger'
import getFormFeedbackModel from '../../models/form_feedback.server.model'
import { getMongoErrorMessage } from '../../utils/handle-mongo-error'
Expand Down Expand Up @@ -61,7 +61,7 @@ export const getFormFeedbackStream = (
*/
export const getFormFeedbacks = (
formId: string,
): ResultAsync<GetFormFeedbackDto, DatabaseError> => {
): ResultAsync<FormFeedbackMetaDto, DatabaseError> => {
return ResultAsync.fromPromise(
FormFeedbackModel.find({ formId }).sort({ created: 1 }).exec(),
(error) => {
Expand Down Expand Up @@ -89,7 +89,7 @@ export const getFormFeedbacks = (
let totalRating = 0
const processedFeedback = feedbacks.map((fb, idx) => {
totalRating += fb.rating
const response: ProcessedFeedback = {
const response: ProcessedFeedbackMeta = {
// 1-based indexing.
index: idx + 1,
timestamp: moment(fb.created).valueOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import {
FieldCreateDto,
FieldUpdateDto,
} from 'src/types/api'
import { GetFormFeedbackDto } from 'src/types/api/form_feedback'
import { FormFeedbackMetaDto } from 'src/types/api/form_feedback'

import {
generateDefaultField,
Expand Down Expand Up @@ -2421,7 +2421,7 @@ describe('admin-form.controller', () => {
it('should return 200 with feedback response successfully', async () => {
// Arrange
const mockRes = expressHandler.mockResponse()
const expectedFormFeedback: GetFormFeedbackDto = {
const expectedFormFeedback: FormFeedbackMetaDto = {
count: 212,
feedback: [
{
Expand Down
15 changes: 9 additions & 6 deletions src/app/modules/form/admin-form/admin-form.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { celebrate, Joi as BaseJoi, Segments } from 'celebrate'
import { StatusCodes } from 'http-status-codes'
import JSONStream from 'JSONStream'
import { ResultAsync } from 'neverthrow'
import { FormFeedbackMetaDto } from 'shared/types/form/form_feedback'

import {
MAX_UPLOAD_FILE_SIZE,
Expand Down Expand Up @@ -589,9 +590,10 @@ export const handleCountFormSubmissions = [
* @returns 422 when user in session cannot be retrieved from the database
* @returns 500 when database error occurs
*/
export const handleCountFormFeedback: ControllerHandler<{
formId: string
}> = async (req, res) => {
export const handleCountFormFeedback: ControllerHandler<
{ formId: string },
number | ErrorDto
> = async (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id

Expand Down Expand Up @@ -732,9 +734,10 @@ export const handleStreamFormFeedback: ControllerHandler<{
* @returns 422 when user in session cannot be retrieved from the database
* @returns 500 when database error occurs
*/
export const handleGetFormFeedback: ControllerHandler<{
formId: string
}> = (req, res) => {
export const handleGetFormFeedback: ControllerHandler<
{ formId: string },
FormFeedbackMetaDto | ErrorDto
> = (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as Express.AuthedSession).user._id

Expand Down
4 changes: 2 additions & 2 deletions src/public/modules/forms/helpers/FeedbackCsvGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import moment from 'moment-timezone'

import { FormFeedbackResponseDto } from '../../../../types/api/form_feedback'
import { FormFeedbackDto } from '../../../../../shared/types/form/form_feedback'

import { CsvGenerator } from './CsvGenerator'

Expand All @@ -16,7 +16,7 @@ export class FeedbackCsvGenerator extends CsvGenerator {
/**
* Generate a string representing a form feedback CSV line record
*/
addLineFromFeedback(feedback: FormFeedbackResponseDto) {
addLineFromFeedback(feedback: FormFeedbackDto) {
const createdAt = moment(feedback.created)
.tz('Asia/Singapore')
.format('DD MMM YYYY hh:mm:ss A')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ObjectId } from 'bson'
import moment from 'moment-timezone'

import { FormFeedbackResponseDto } from '../../../../../types/api/form_feedback'
import { FormFeedbackDto } from '../../../../../../shared/types/form/form_feedback'
import { DateString } from '../../../../../../shared/types/generic'
import { FeedbackCsvGenerator } from '../FeedbackCsvGenerator'

describe('FeedbackCsvGenerator', () => {
Expand All @@ -21,12 +22,14 @@ describe('FeedbackCsvGenerator', () => {
it('should add line successfully if created and lastmodified dates are provided', () => {
// Arrange
const feedbackCsv = new FeedbackCsvGenerator(1)
const feedback: FormFeedbackResponseDto = {
const feedback: FormFeedbackDto = {
rating: 3,
comment: 'some comment',
formId: new ObjectId().toHexString(),
created: new Date('2019-11-05T13:12:14'),
lastModified: new Date('2019-11-05T13:12:14'),
created: new Date('2019-11-05T13:12:14').toISOString() as DateString,
lastModified: new Date(
'2019-11-05T13:12:14',
).toISOString() as DateString,
}
const insertedCreatedDate = moment(feedback.created)
.tz('Asia/Singapore')
Expand All @@ -47,7 +50,7 @@ describe('FeedbackCsvGenerator', () => {
it('should add line successfully if comment, created and lastmodified dates are not provided', () => {
// Arrange
const feedbackCsv = new FeedbackCsvGenerator(1)
const feedback: FormFeedbackResponseDto = {
const feedback: FormFeedbackDto = {
rating: 3,
formId: new ObjectId().toHexString(),
}
Expand Down
23 changes: 12 additions & 11 deletions src/public/services/FormFeedbackService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import axios from 'axios'

import { SuccessMessageDto } from '../../../shared/types/core'
import {
FormFeedbackPostDto,
FormFeedbackResponseDto,
GetFormFeedbackDto,
} from '../../types/api/form_feedback'
FormFeedbackDto,
FormFeedbackMetaDto,
SubmitFormFeedbackBodyDto,
} from '../../../shared/types/form/form_feedback'
import { FeedbackCsvGenerator } from '../modules/forms/helpers/FeedbackCsvGenerator'

// Exported for testing
Expand All @@ -15,14 +16,14 @@ export const ADMIN_FORM_ENDPOINT = '/api/v3/admin/forms'
* Post feedback for a given form.
* @param formId the id of the form to post feedback for
* @param feedbackToPost object containing the feedback
* @returns the posted feedback
* @returns success message
*/
export const postFeedback = async (
formId: string,
feedbackToPost: FormFeedbackPostDto,
): Promise<FormFeedbackResponseDto> => {
feedbackToPost: SubmitFormFeedbackBodyDto,
): Promise<SuccessMessageDto> => {
return axios
.post<FormFeedbackResponseDto>(
.post<SuccessMessageDto>(
`${PUBLIC_FORM_ENDPOINT}/${formId}/feedback`,
feedbackToPost,
)
Expand All @@ -36,9 +37,9 @@ export const postFeedback = async (
*/
export const getFeedback = async (
formId: string,
): Promise<GetFormFeedbackDto> => {
): Promise<FormFeedbackMetaDto> => {
return axios
.get<GetFormFeedbackDto>(`${ADMIN_FORM_ENDPOINT}/${formId}/feedback`)
.get<FormFeedbackMetaDto>(`${ADMIN_FORM_ENDPOINT}/${formId}/feedback`)
.then(({ data }) => data)
}

Expand All @@ -65,7 +66,7 @@ export const downloadFeedback = async (
const expectedNumResponses = await countFeedback(formId)

return axios
.get<FormFeedbackResponseDto[]>(
.get<FormFeedbackDto[]>(
`${ADMIN_FORM_ENDPOINT}/${formId}/feedback/download`,
)
.then(({ data }) => {
Expand Down
22 changes: 1 addition & 21 deletions src/types/api/form_feedback.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
import { ProcessedFeedback } from '../form_feedback'

export type FormFeedbackPostDto = {
isPreview?: boolean
rating: number
comment?: string
}

export type FormFeedbackResponseDto = {
rating: number
comment?: string
formId: string
created?: Date
lastModified?: Date
}

export type GetFormFeedbackDto = {
average?: string
count: number
feedback: ProcessedFeedback[]
}
export { FormFeedbackMetaDto } from '../../../shared/types/form/form_feedback'
24 changes: 3 additions & 21 deletions src/types/form_feedback.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import { Document, Model, QueryCursor } from 'mongoose'

import { IFormSchema } from './form'
import { FormFeedbackBase } from '../../shared/types/form/form_feedback'

export type ProcessedFeedback = {
index: number
timestamp: number
rating: number
comment: string
date: string
dateShort: string
}
export { ProcessedFeedbackMeta } from '../../shared/types/form/form_feedback'

export interface IFormFeedback {
formId: IFormSchema['_id']
rating: number
comment?: string
}
export type IFormFeedback = FormFeedbackBase
export interface IFormFeedbackSchema extends IFormFeedback, Document {
created?: Date
lastModified?: Date
Expand All @@ -26,13 +15,6 @@ export interface IFormFeedbackDocument extends IFormFeedbackSchema {
lastModified: Date
}

export interface IFormFeedbackSchema extends Document, IFormFeedback {}

export interface IFormFeedbackDoc extends IFormFeedbackSchema {
lastModified: Date
created: Date
}

export interface IFormFeedbackModel extends Model<IFormFeedbackSchema> {
/**
* Returns a cursor for all feedback for the form with formId.
Expand Down

0 comments on commit 493c25b

Please sign in to comment.