-
Notifications
You must be signed in to change notification settings - Fork 0
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
Updates required for the https://github.com/CDLUC3/dmsp_frontend_prototype/issues/188 frontend Edit Question ticket #194
Changes from 4 commits
ff4574d
ab3f3e9
2a569cb
1cb33df
8e049c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ import { MyContext } from "../context"; | |
import { QuestionOption } from "../models/QuestionOption"; | ||
import { Question } from "../models/Question"; | ||
import { Section } from "../models/Section"; | ||
import { hasPermissionOnQuestion } from "../services/questionService"; | ||
import { getQuestionOptionsToRemove, hasPermissionOnQuestion } from "../services/questionService"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed to add getQuestionOptionsToRemove function for when a user removes options from an existing questions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a list of new tickets I am going to create and consolidating how associations like this are handled is one of them. Let's merge this fix for now and we can come back afterward to make sure we're handling them all in the same way. |
||
import { BadUserInputError, ForbiddenError, NotFoundError } from "../utils/graphQLErrors"; | ||
import { QuestionCondition } from "../models/QuestionCondition"; | ||
|
||
|
@@ -140,15 +140,33 @@ export const resolvers: Resolvers = { | |
existingQuestionOptions.map(option => [option.id, option]) | ||
); | ||
|
||
|
||
// Get list of options that need to be removed | ||
const optionsToRemove = await getQuestionOptionsToRemove(questionOptions as QuestionOption[], context, questionId); | ||
|
||
// Remove question options that are no longer in the updated questionOptions array | ||
if (optionsToRemove.length > 0) { | ||
await Promise.all( | ||
optionsToRemove.map(async (option) => { | ||
const questionOption = new QuestionOption({ | ||
questionId: option.questionId, | ||
id: option.id | ||
}); | ||
|
||
await questionOption.delete(context); | ||
}) | ||
); | ||
} | ||
|
||
// Separate incoming options into "to update" and "to create" | ||
const optionsToUpdate = []; | ||
const optionsToCreate = []; | ||
|
||
questionOptions.forEach(option => { | ||
if (existingOptionsMap.has(option.questionOptionId)) { | ||
if (existingOptionsMap.has(option.id)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switched to using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah that makes sense. You're working within the context of the QuestionOption itself here. |
||
// Add to update list, merging the new data with the existing data | ||
optionsToUpdate.push({ | ||
...existingOptionsMap.get(option.questionOptionId), // existing option data | ||
...existingOptionsMap.get(option.id), // existing option data | ||
...option // updated fields from input | ||
}); | ||
} else { | ||
|
@@ -180,6 +198,7 @@ export const resolvers: Resolvers = { | |
); | ||
} | ||
|
||
|
||
// Return newly updated question | ||
return await Question.findById('updateQuestion resolver', context, updatedQuestion.id); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,8 @@ export const resolvers: Resolvers = { | |
questionOptions: async (_, { questionId }, context: MyContext): Promise<QuestionOption[]> => { | ||
return await QuestionOption.findByQuestionId('questionOption resolver', context, questionId); | ||
}, | ||
questionOption: async (_, { questionOptionId }, context: MyContext): Promise<QuestionOption> => { | ||
return await QuestionOption.findByQuestionOptionId('questionOption resolver', context, questionOptionId); | ||
questionOption: async (_, { id }, context: MyContext): Promise<QuestionOption> => { | ||
return await QuestionOption.findByQuestionOptionId('questionOption resolver', context, id); | ||
}, | ||
}, | ||
Mutation: { | ||
|
@@ -42,21 +42,21 @@ export const resolvers: Resolvers = { | |
} | ||
}, | ||
updateQuestionOption: async (_, { input: { | ||
questionOptionId, | ||
id, | ||
text, | ||
orderNumber, | ||
isDefault } }, context: MyContext): Promise<QuestionOption> => { | ||
|
||
// Get QuestionOption based on provided questionOptionId | ||
const questionOptionData = await QuestionOption.findByQuestionOptionId('questionOption resolver', context, questionOptionId); | ||
const questionOptionData = await QuestionOption.findByQuestionOptionId('questionOption resolver', context, id); | ||
|
||
// Throw Not Found error if QuestionOption data is not found | ||
if (!questionOptionData) { | ||
throw NotFoundError('QuestionOption not found') | ||
} | ||
|
||
const questionOption = new QuestionOption({ | ||
id: questionOptionId, | ||
id: questionOptionData.id, | ||
questionId: questionOptionData.questionId, | ||
text: text || questionOptionData.text, | ||
orderNumber: orderNumber || questionOptionData.orderNumber, | ||
|
@@ -74,22 +74,5 @@ export const resolvers: Resolvers = { | |
return await QuestionOption.findByQuestionOptionId('updateQuestion resolver', context, updatedQuestionOption.id); | ||
} | ||
}, | ||
removeQuestionOption: async (_, { questionOptionId }, context: MyContext): Promise<QuestionOption> => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was also thinking about situations like this. There are a few cases where we exposed individual As part of the ticket I mentioned above, we should probably figure out which approaches we are using for each scenario and delete any unused ones |
||
// Retrieve existing questionOption | ||
const questionOptionData = await QuestionOption.findByQuestionOptionId('removeQuestion resolver', context, questionOptionId); | ||
|
||
// Throw Not Found error if QuestionOption is not found | ||
if (!questionOptionData) { | ||
throw NotFoundError('QuestionOption not found') | ||
} | ||
|
||
//Need to create a new instance of QuestionOption so that it recognizes the 'delete' function of that instance | ||
const questionOption = new QuestionOption({ | ||
...questionOptionData, | ||
id: questionOptionId | ||
}); | ||
|
||
return await questionOption.delete(context); | ||
}, | ||
}, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to remove these constraints because they were preventing me from updating existing question options on the Edit Question page