Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup deprecated feedback API #4085

Merged
merged 2 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions shared/types/form/form_feedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export type FormFeedbackBase = {
formId: FormDto['_id']
created?: Date
lastModified?: Date
// TODO #3964: Update to not optional once we fully migrate to /submissions/{submissionId}/feedback endpoint
submissionId?: SubmissionResponseDto['submissionId']
submissionId: SubmissionResponseDto['submissionId']
}

// Convert to serialized version.
Expand Down
15 changes: 15 additions & 0 deletions src/app/models/__tests__/form_feedback.server.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('form_feedback.server.model', () => {
describe('Schema', () => {
const DEFAULT_PARAMS: IFormFeedback = {
formId: new ObjectId(),
submissionId: new ObjectId(),
rating: 5,
comment: 'feedback comment',
}
Expand Down Expand Up @@ -74,6 +75,18 @@ describe('form_feedback.server.model', () => {
mongoose.Error.ValidationError,
)
})

it('should throw validation error when submissionId param is missing', async () => {
// Act
const actualPromise = new FeedbackModel(
omit(DEFAULT_PARAMS, 'submissionId'),
).save()

// Assert
await expect(actualPromise).rejects.toThrowError(
mongoose.Error.ValidationError,
)
})
})

describe('Statics', () => {
Expand All @@ -82,8 +95,10 @@ describe('form_feedback.server.model', () => {
// Arrange
// Create document
const mockFormId = new ObjectId()
const mockSubmissionId = new ObjectId()
const mockFeedbackDoc = await FeedbackModel.create({
formId: mockFormId,
submissionId: mockSubmissionId,
rating: 5,
comment: 'feedback comment',
})
Expand Down
3 changes: 1 addition & 2 deletions src/app/models/form_feedback.server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ const FormFeedbackSchema = new Schema<IFormFeedbackSchema, IFormFeedbackModel>(
submissionId: {
type: Schema.Types.ObjectId,
ref: SUBMISSION_SCHEMA_ID,
// TODO #3964: Update to true once we fully migrate to /submissions/{submissionId}/feedback endpoint
required: false,
required: true,
},
rating: {
type: Number,
Expand Down
14 changes: 14 additions & 0 deletions src/app/modules/examples/__tests__/helpers/prepareTestData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
IAgencySchema,
IFormDocument,
IFormFeedbackSchema,
ISubmissionSchema,
IUserSchema,
} from 'src/types'

Expand Down Expand Up @@ -43,6 +44,8 @@ export type TestData = {
expectedFormInfo: FormInfo[]
// Feedbacks for each of the forms.
feedbacks: IFormFeedbackSchema[]
// Submissions for each of the forms.
submissions: ISubmissionSchema[]
}
}

Expand All @@ -64,6 +67,7 @@ const prepareTestData = async (
forms: [],
expectedFormInfo: [],
feedbacks: [],
submissions: [],
},
second: {
searchTerm: SearchTerm.Second,
Expand All @@ -72,6 +76,7 @@ const prepareTestData = async (
forms: [],
expectedFormInfo: [],
feedbacks: [],
submissions: [],
},
total: {
searchTerm: '',
Expand All @@ -80,6 +85,7 @@ const prepareTestData = async (
forms: [],
expectedFormInfo: [],
feedbacks: [],
submissions: [],
},
}

Expand Down Expand Up @@ -126,6 +132,8 @@ const prepareTestData = async (
),
),
)
testData.first.submissions = await Promise.all(firstSubmissionPromises)

const secondSubmissionPromises = flatten(
testData.second.forms.map((form) =>
times(testData.second.submissionCount, () =>
Expand All @@ -138,10 +146,15 @@ const prepareTestData = async (
),
),
)
testData.second.submissions = await Promise.all(secondSubmissionPromises)

// Assign all forms in test data.
testData.total.forms = testData.first.forms.concat(testData.second.forms)

testData.total.submissions = testData.first.submissions.concat(
testData.second.submissions,
)

// Add form statistics for "submissions" for both form prefixes.
const formStatsPromises = testData.first.forms
.map((form) =>
Expand Down Expand Up @@ -176,6 +189,7 @@ const prepareTestData = async (
return FeedbackModel.create({
rating: score,
formId: testData.total.forms[i]._id,
submissionId: testData.total.submissions[i]._id,
comment: 'very good test',
})
})
Expand Down
8 changes: 8 additions & 0 deletions src/app/modules/feedback/__tests__/feedback.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ describe('feedback.service', () => {
// Arrange
// Insert 3 form feedbacks.
const validFormId = new ObjectId()
const validSubmissionId = new ObjectId().toHexString()
const expectedFeedbackCount = 3
const feedbackPromises = times(expectedFeedbackCount, (count) =>
FormFeedback.create({
comment: `test feedback ${count}`,
formId: validFormId,
submissionId: validSubmissionId,
rating: 5,
}),
)
Expand Down Expand Up @@ -109,20 +111,24 @@ describe('feedback.service', () => {
})

describe('getFormFeedbacks', () => {
const MOCK_SUBMISSION_ID = new ObjectId().toHexString()

it('should return correct feedback responses', async () => {
// Arrange
const expectedCount = 3
const mockFormId = new ObjectId().toHexString()
const expectedFbPromises = times(expectedCount, (count) =>
FormFeedback.create({
formId: mockFormId,
submissionId: MOCK_SUBMISSION_ID,
comment: `cool form ${count}`,
rating: 5 - count,
}),
)
// Add another feedback with a different form id.
await FormFeedback.create({
formId: new ObjectId(),
submissionId: MOCK_SUBMISSION_ID,
comment: 'boo this form sux',
rating: 1,
})
Expand Down Expand Up @@ -194,6 +200,7 @@ describe('feedback.service', () => {
const mockFormId = new ObjectId().toHexString()
const createdFb = await FormFeedback.create({
formId: mockFormId,
submissionId: MOCK_SUBMISSION_ID,
// Missing comment key value.
rating: 3,
})
Expand Down Expand Up @@ -281,6 +288,7 @@ describe('feedback.service', () => {
await FormFeedback.create({
comment: `test feedback`,
formId: MOCK_FORM_ID,
submissionId: MOCK_SUBMISSION_ID,
rating: 5,
})

Expand Down
8 changes: 3 additions & 5 deletions src/app/modules/feedback/feedback.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@ const validateSubmitFormFeedbackParams = celebrate({
* @returns 422 if duplicate feedback with the same submissionId and formId exists
* @returns 410 if form has been archived
* @returns 500 if database error occurs
*
* TODO #3964: Rename to `submitFormFeedback` once we fully migrate feedback endpoint to /submissions/{submissionId}/feedback
*/
const submitFormFeedbackV2: ControllerHandler<
const submitFormFeedback: ControllerHandler<
{ formId: string; submissionId: string },
{ message: string } | ErrorDto | PrivateFormErrorDto,
{ rating: number; comment: string }
> = async (req, res) => {
const { formId, submissionId } = req.params
const { rating, comment } = req.body
const logMeta = {
action: 'submitFormFeedbackV2',
action: 'submitFormFeedback',
...createReqMeta(req),
formId,
submissionId,
Expand Down Expand Up @@ -91,5 +89,5 @@ const submitFormFeedbackV2: ControllerHandler<

export const handleSubmitFormFeedback = [
validateSubmitFormFeedbackParams,
submitFormFeedbackV2,
submitFormFeedback,
] as ControllerHandler[]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { ObjectId } from 'bson-ext'
import { format, subDays } from 'date-fns'
import { cloneDeep, omit, times } from 'lodash'
import { ObjectID } from 'mongodb'
import mongoose from 'mongoose'
import { errAsync, okAsync } from 'neverthrow'
import SparkMD5 from 'spark-md5'
Expand Down Expand Up @@ -3128,8 +3129,18 @@ describe('admin-form.routes', () => {
it('should return 200 with form feedback meta when feedback exists', async () => {
// Arrange
const formFeedbacks = [
{ formId: formForFeedback._id, rating: 5, comment: 'nice' },
{ formId: formForFeedback._id, rating: 2, comment: 'not nice' },
{
formId: formForFeedback._id,
rating: 5,
comment: 'nice',
submissionId: new ObjectID().toHexString(),
},
{
formId: formForFeedback._id,
rating: 2,
comment: 'not nice',
submissionId: new ObjectID().toHexString(),
},
]
await insertFormFeedback(formFeedbacks[0])
await insertFormFeedback(formFeedbacks[1])
Expand Down Expand Up @@ -3308,8 +3319,18 @@ describe('admin-form.routes', () => {
it('should return 200 with feedback count when feedback exists', async () => {
// Arrange
const formFeedbacks = [
{ formId: formForFeedback._id, rating: 5, comment: 'nice' },
{ formId: formForFeedback._id, rating: 2, comment: 'not nice' },
{
formId: formForFeedback._id,
rating: 5,
comment: 'nice',
submissionId: new ObjectID().toHexString(),
},
{
formId: formForFeedback._id,
rating: 2,
comment: 'not nice',
submissionId: new ObjectID().toHexString(),
},
]
await insertFormFeedback(formFeedbacks[0])
await insertFormFeedback(formFeedbacks[1])
Expand Down Expand Up @@ -3452,8 +3473,18 @@ describe('admin-form.routes', () => {
it('should return 200 with feedback stream when feedbacks exist', async () => {
// Arrange
const formFeedbacks = [
{ formId: formForFeedback._id, rating: 5, comment: 'nice' },
{ formId: formForFeedback._id, rating: 2, comment: 'not nice' },
{
formId: formForFeedback._id,
rating: 5,
comment: 'nice',
submissionId: new ObjectID().toHexString(),
},
{
formId: formForFeedback._id,
rating: 2,
comment: 'not nice',
submissionId: new ObjectID().toHexString(),
},
]
await insertFormFeedback(formFeedbacks[0])
await insertFormFeedback(formFeedbacks[1])
Expand Down
Loading