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

Feature/js updates to accommodate question types with options #179

Merged
merged 4 commits into from
Jan 27, 2025

Conversation

jupiter007
Copy link
Collaborator

Description

Needed to make updated to accommodate question options for question types like radio buttons, checkboxes, and select drop-down

  • Added data-migration scripts to add questionOptions table and seed data
  • Added questionOptions schema, resolver and QuestionOption model, which will be used for option question types
  • Updated questions resolver to add or update questionOptions when passed in the mutation
  • Added questionOptions to schema and updated question query and mutations to include questionOptions

Fixes # (172)

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

This has been manually tested and tested with unit tests.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I updated the CHANGELOG.md and added documentation if necessary
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@jupiter007 jupiter007 requested a review from briri January 27, 2025 01:38
Copy link
Collaborator

@briri briri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall @jupiter007. I have a few change requests with regard to the DB and then some minor cleanup.

@@ -81,7 +84,7 @@ export class Question extends MySqlModel {
this.cleanup();

// Save the record and then fetch it
const newId = await Question.insert(context, this.tableName, this, 'Question.create');
const newId = await Question.insert(context, this.tableName, this, 'Question.create', ['questionOptions']);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll want to add the questionOptions to the list of props to skip on the update as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I missed that one. It has been added.


if (!this.questionId) {
this.errors.push('Question ID can\'t be blank');
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should also check for the existence of text and orderNumber. They are required in the DB, so we should verify that here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks so much for catching that. I made the update.

`createdById` int,
`modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modifiedById` int,
FOREIGN KEY (questionId) REFERENCES questions(id) ON DELETE CASCADE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch text to VARCHAR(255) I can't see a scenario where we'd want the user including more text than that.

Then, I would add the following (below the FOREIGN KEY constraint) to ensure uniqueness of the options and that there are no dupes for the order:

CONSTRAINT unique_question_option UNIQUE (`questionId`, `text`),
CONSTRAINT unique_question_option UNIQUE (`questionId`, `orderNumber`)`

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Thanks. I added the constraints


// Get Question based on provided questionId
const questionData = await Question.findById('updateQuestionOptions resolver', context, questionId);
const existingQuestionOptions = await getExistingQuestionOptions(context, questionId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this. The cascade delete on the DB will remove them automatically.

Just add a comment here noting that the DB will delete them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I keep forgetting about the cascade functionality. I removed the code to delete questionOptions and added a comment

}

"QuestionOption always belongs to a Question"
type QuestionOption {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've already defined QuestionOption within the QuestionOption schema above. I would say leave the type for it and theQuestionOptionInput in the other file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I removed QuestionOption from schema and moved QuestionOptionInput to the questionOption schema

export const getExistingQuestionOptions = async (context: MyContext, questionId: number): Promise<QuestionOption[]> => {

//Get all the existing question options already associated with this question
return await QuestionOption.findByQuestionId('questionService', context, questionId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why extract this one liner out into the service?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to remove this since we already have a findByQuestionId method in QuestionOption model. I removed from service and updated resolver to use the existing method instead.

… duplicated QuestionOption schema and updated data-migration script to use VARCHAR(255) for text
@jupiter007
Copy link
Collaborator Author

Thanks so much for reviewing my PR @briri . I've responded to your comments. Please take another look when you get the chance.

Copy link
Collaborator

@briri briri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making the changes @jupiter007 :)

@briri briri merged commit b33353e into development Jan 27, 2025
@briri briri deleted the feature/JS-updates-to-accommodate-question-types branch January 27, 2025 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants