mirror of
https://github.com/UniversityOfHelsinkiCS/ohtuilmo.git
synced 2026-10-12 19:03:41 +03:00
Fix issues causing updating of configurations not to work
This commit is contained in:
@@ -7,18 +7,20 @@ const includeArray = [
|
||||
'review_question_set1',
|
||||
'review_question_set2',
|
||||
'registration_question_set',
|
||||
'customer_review_question_set'
|
||||
'customer_review_question_set',
|
||||
]
|
||||
|
||||
const handleDatabaseError = (res, error) => {
|
||||
console.log(error)
|
||||
res.status(500).json({ error: 'Something is wrong... try reloading the page' })
|
||||
res
|
||||
.status(500)
|
||||
.json({ error: 'Something is wrong... try reloading the page' })
|
||||
}
|
||||
|
||||
const returnPopulatedConfiguration = (req, res, unPopulatedConfiguration) => {
|
||||
db.Configuration.findOne({
|
||||
where: { id: unPopulatedConfiguration.id },
|
||||
include: includeArray
|
||||
include: includeArray,
|
||||
})
|
||||
.then((foundConfiguration) => {
|
||||
res.status(200).json({ configuration: foundConfiguration })
|
||||
@@ -28,33 +30,29 @@ const returnPopulatedConfiguration = (req, res, unPopulatedConfiguration) => {
|
||||
|
||||
const setForeignKeys = async (configuration, req, res) => {
|
||||
try {
|
||||
if (req.body.review_question_set1_id) {
|
||||
if (req.body.review_question_set_1_id) {
|
||||
const foundSet = await db.ReviewQuestionSet.findOne({
|
||||
where: { id: req.body.review_question_set1_id }
|
||||
where: { id: req.body.review_question_set_1_id },
|
||||
})
|
||||
if (!foundSet)
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: 'no peer review question set with that id' })
|
||||
await configuration.setReview_question_set_1(
|
||||
req.body.review_question_set1_id
|
||||
)
|
||||
await configuration.setReview_question_set1(foundSet)
|
||||
}
|
||||
if (req.body.review_question_set2_id) {
|
||||
const foundSet = await db.ReviewQuestionSet.findOne({
|
||||
where: { id: req.body.review_question_set2_id }
|
||||
where: { id: req.body.review_question_set_2_id },
|
||||
})
|
||||
if (!foundSet)
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: 'no peer review question set with that id' })
|
||||
await configuration.setReview_question_set_2(
|
||||
req.body.review_question_set2_id
|
||||
)
|
||||
await configuration.setRegistration_question_set2(foundSet)
|
||||
}
|
||||
if (req.body.registration_question_set_id) {
|
||||
const foundSet = await db.RegistrationQuestionSet.findOne({
|
||||
where: { id: req.body.registration_question_set_id }
|
||||
where: { id: req.body.registration_question_set_id },
|
||||
})
|
||||
if (!foundSet)
|
||||
return res
|
||||
@@ -66,15 +64,13 @@ const setForeignKeys = async (configuration, req, res) => {
|
||||
}
|
||||
if (req.body.customer_review_question_set_id) {
|
||||
const foundSet = await db.CustomerReviewQuestionSet.findOne({
|
||||
where: { id: req.body.customer_review_question_set_id }
|
||||
where: { id: req.body.customer_review_question_set_id },
|
||||
})
|
||||
if (!foundSet)
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: 'no customer review question set with that id' })
|
||||
await configuration.setCustomer_review_question_set(
|
||||
req.body.customer_review_question_set_id
|
||||
)
|
||||
await configuration.setCustomer_review_question_set(foundSet)
|
||||
}
|
||||
returnPopulatedConfiguration(req, res, configuration)
|
||||
} catch (error) {
|
||||
@@ -85,7 +81,7 @@ const setForeignKeys = async (configuration, req, res) => {
|
||||
const createConfiguration = (req, res) => {
|
||||
db.Configuration.create({
|
||||
name: req.body.name,
|
||||
content: req.body.content
|
||||
content: req.body.content,
|
||||
})
|
||||
.then((created) => setForeignKeys(created, req, res))
|
||||
.catch((error) => handleDatabaseError(res, error))
|
||||
@@ -95,7 +91,11 @@ const updateConfiguration = (configuration, req, res) => {
|
||||
configuration
|
||||
.update({
|
||||
name: req.body.name,
|
||||
content: req.body.content
|
||||
content: req.body.content,
|
||||
customerReviewQuestionSetId: req.body.customer_review_question_set_id,
|
||||
registrationQuestionSetId: req.body.registration_question_set_id,
|
||||
reviewQuestionSet1Id: req.body.review_question_set_1_id,
|
||||
reviewQuestionSet2Id: req.body.review_question_set_2_id,
|
||||
})
|
||||
.then((updated) => setForeignKeys(updated, req, res))
|
||||
.catch((error) => handleDatabaseError(res, error))
|
||||
@@ -106,7 +106,7 @@ const createChecks = async (req, res) => {
|
||||
|
||||
try {
|
||||
const configuration = await db.Configuration.findOne({
|
||||
where: { name: req.body.name }
|
||||
where: { name: req.body.name },
|
||||
})
|
||||
if (configuration) {
|
||||
return res.status(400).json({ error: 'name already in use' })
|
||||
@@ -123,7 +123,7 @@ const updateChecks = async (req, res) => {
|
||||
|
||||
try {
|
||||
const duplicateName = await db.Configuration.findOne({
|
||||
where: { name: req.body.name }
|
||||
where: { name: req.body.name },
|
||||
})
|
||||
|
||||
if (duplicateName && duplicateName.id !== parseInt(req.params.id)) {
|
||||
@@ -131,7 +131,7 @@ const updateChecks = async (req, res) => {
|
||||
}
|
||||
|
||||
const configuration = await db.Configuration.findOne({
|
||||
where: { id: req.params.id }
|
||||
where: { id: req.params.id },
|
||||
})
|
||||
|
||||
if (!configuration) {
|
||||
@@ -156,7 +156,7 @@ configurationsRouter.put('/:id', checkAdmin, (req, res) => {
|
||||
|
||||
configurationsRouter.get('/', checkAdmin, (req, res) => {
|
||||
db.Configuration.findAll({
|
||||
include: includeArray
|
||||
include: includeArray,
|
||||
})
|
||||
.then((configurations) => {
|
||||
res.status(200).json({ configurations })
|
||||
@@ -167,7 +167,7 @@ configurationsRouter.get('/', checkAdmin, (req, res) => {
|
||||
configurationsRouter.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const response = await db.Configuration.findByPk(req.params.id, {
|
||||
include: includeArray
|
||||
include: includeArray,
|
||||
})
|
||||
res.status(200).json(response)
|
||||
} catch (error) {
|
||||
@@ -184,8 +184,8 @@ configurationsRouter.get(
|
||||
include: [
|
||||
'review_question_set1',
|
||||
'review_question_set2',
|
||||
'registration_question_set'
|
||||
]
|
||||
'registration_question_set',
|
||||
],
|
||||
})
|
||||
if (req.params.reviewround === '1') {
|
||||
res.status(200).json(response.review_question_set1)
|
||||
@@ -203,7 +203,7 @@ configurationsRouter.get(
|
||||
configurationsRouter.get('/:id/customerreviewquestions', async (req, res) => {
|
||||
try {
|
||||
const response = await db.Configuration.findByPk(req.params.id, {
|
||||
include: 'customer_review_question_set'
|
||||
include: 'customer_review_question_set',
|
||||
})
|
||||
res.status(200).json(response.customer_review_question_set)
|
||||
} catch (error) {
|
||||
@@ -211,13 +211,18 @@ configurationsRouter.get('/:id/customerreviewquestions', async (req, res) => {
|
||||
}
|
||||
})
|
||||
|
||||
configurationsRouter.delete('/:configurationId', checkAdmin, async (req, res) => {
|
||||
const success = await db.Configuration.destroy({ where: { id: req.params.configurationId } })
|
||||
success
|
||||
? console.log(`Configuration ${req.params.configurationId} destroyed.`)
|
||||
: console.log('Nothing to delete.')
|
||||
return res.status(204).end()
|
||||
})
|
||||
|
||||
configurationsRouter.delete(
|
||||
'/:configurationId',
|
||||
checkAdmin,
|
||||
async (req, res) => {
|
||||
const success = await db.Configuration.destroy({
|
||||
where: { id: req.params.configurationId },
|
||||
})
|
||||
success
|
||||
? console.log(`Configuration ${req.params.configurationId} destroyed.`)
|
||||
: console.log('Nothing to delete.')
|
||||
return res.status(204).end()
|
||||
}
|
||||
)
|
||||
|
||||
module.exports = configurationsRouter
|
||||
|
||||
@@ -5,27 +5,27 @@ module.exports = (sequelize, Sequelize) => {
|
||||
id: {
|
||||
type: Sequelize.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
autoIncrement: true,
|
||||
},
|
||||
name: {
|
||||
type: Sequelize.STRING
|
||||
type: Sequelize.STRING,
|
||||
},
|
||||
content: {
|
||||
type: Sequelize.JSONB
|
||||
}
|
||||
type: Sequelize.JSONB,
|
||||
},
|
||||
},
|
||||
{
|
||||
underscored: true
|
||||
underscored: true,
|
||||
}
|
||||
)
|
||||
|
||||
Configuration.associate = (models) => {
|
||||
Configuration.hasMany(models.Registration) // used only for creating the foreign key in registrations, should be removed and implemented from registration side
|
||||
Configuration.belongsTo(models.ReviewQuestionSet, {
|
||||
as: 'review_question_set1'
|
||||
as: 'review_question_set1',
|
||||
})
|
||||
Configuration.belongsTo(models.ReviewQuestionSet, {
|
||||
as: 'review_question_set2'
|
||||
as: 'review_question_set2',
|
||||
})
|
||||
Configuration.belongsTo(models.RegistrationQuestionSet)
|
||||
Configuration.belongsTo(models.CustomerReviewQuestionSet)
|
||||
|
||||
@@ -1,20 +1,32 @@
|
||||
module.exports = (sequelize, Sequelize) => {
|
||||
const Review_question_set = sequelize.define('review_question_set', {
|
||||
id: {
|
||||
type: Sequelize.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
const Review_question_set = sequelize.define(
|
||||
'review_question_set',
|
||||
{
|
||||
id: {
|
||||
type: Sequelize.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true,
|
||||
},
|
||||
name: {
|
||||
type: Sequelize.STRING,
|
||||
},
|
||||
questions: {
|
||||
type: Sequelize.JSONB,
|
||||
},
|
||||
},
|
||||
name: {
|
||||
type: Sequelize.STRING
|
||||
},
|
||||
questions: {
|
||||
type: Sequelize.JSONB
|
||||
{
|
||||
underscored: true,
|
||||
}
|
||||
},
|
||||
{
|
||||
underscored: true
|
||||
})
|
||||
)
|
||||
|
||||
Review_question_set.associate = (models) => {
|
||||
Review_question_set.hasMany(models.Configuration, {
|
||||
foreignKey: 'review_question_set1Id',
|
||||
})
|
||||
Review_question_set.hasMany(models.Configuration, {
|
||||
foreignKey: 'review_question_set2Id',
|
||||
})
|
||||
}
|
||||
|
||||
return Review_question_set
|
||||
}
|
||||
|
||||
@@ -45,12 +45,8 @@ const replaceEditingQuestionSet = (questionSetItemSelector, newQuestions) => {
|
||||
}
|
||||
|
||||
const clearEditingQuestionSetInputs = (selector) => {
|
||||
cy.get(selector)
|
||||
.find('.question-set-form__name')
|
||||
.clear()
|
||||
cy.get(selector)
|
||||
.find('.question-set-form__questions')
|
||||
.clear()
|
||||
cy.get(selector).find('.question-set-form__name').clear()
|
||||
cy.get(selector).find('.question-set-form__questions').clear()
|
||||
}
|
||||
|
||||
const saveEditingQuestionSet = (questionSetItemSelector) => {
|
||||
@@ -60,9 +56,7 @@ const saveEditingQuestionSet = (questionSetItemSelector) => {
|
||||
}
|
||||
|
||||
const cancelEditingQuestionSet = (selector) => {
|
||||
cy.get(selector)
|
||||
.find('.question-set-item-editor__cancel-button')
|
||||
.click()
|
||||
cy.get(selector).find('.question-set-item-editor__cancel-button').click()
|
||||
}
|
||||
|
||||
describe('Registration-questions page', () => {
|
||||
@@ -96,16 +90,16 @@ describe('Registration-questions page', () => {
|
||||
.should('have.length', 1)
|
||||
})
|
||||
|
||||
it.skip('creates questions of type scale, text and <empty>', () => {
|
||||
it('creates questions of type scale, text and <empty>', () => {
|
||||
createQuestionSet('k2000', [
|
||||
{
|
||||
question: 'This is a title'
|
||||
question: 'This is a title',
|
||||
},
|
||||
{
|
||||
question: 'Rate your yeet 1-5',
|
||||
type: 'scale'
|
||||
type: 'scale',
|
||||
},
|
||||
{ question: 'Your life story plz', type: 'text' }
|
||||
{ question: 'Your life story plz', type: 'text' },
|
||||
])
|
||||
|
||||
cy.get(
|
||||
@@ -141,7 +135,7 @@ describe('Registration-questions page', () => {
|
||||
describe('Registration question set editing', () => {
|
||||
beforeEach(() => {
|
||||
cy.createRegistrationQuestionSet('k2000', [
|
||||
{ question: 'foo?', type: 'text' }
|
||||
{ question: 'foo?', type: 'text' },
|
||||
])
|
||||
cy.visit('/administration/registration-questions')
|
||||
})
|
||||
@@ -188,7 +182,7 @@ describe('Registration-questions page', () => {
|
||||
it('edits questions without changing name', () => {
|
||||
editQuestionSet('.question-set-item')
|
||||
replaceEditingQuestionSet('.question-set-item-editor', [
|
||||
{ question: 'foo!', type: 'text' }
|
||||
{ question: 'foo!', type: 'text' },
|
||||
])
|
||||
saveEditingQuestionSet('.question-set-item-editor')
|
||||
|
||||
@@ -204,7 +198,7 @@ describe('Registration-questions page', () => {
|
||||
editQuestionSet('.question-set-item')
|
||||
replaceEditingQuestionSetName('.question-set-item-editor', 'foobar')
|
||||
replaceEditingQuestionSet('.question-set-item-editor', [
|
||||
{ question: 'foo!', type: 'text' }
|
||||
{ question: 'foo!', type: 'text' },
|
||||
])
|
||||
cancelEditingQuestionSet('.question-set-item-editor')
|
||||
|
||||
@@ -221,7 +215,7 @@ describe('Registration-questions page', () => {
|
||||
replaceEditingQuestionSetName('.question-set-item-editor', 'k5555')
|
||||
replaceEditingQuestionSet('.question-set-item-editor', [
|
||||
{ question: 'this is a title' },
|
||||
{ question: 'this is a scale', type: 'scale' }
|
||||
{ question: 'this is a scale', type: 'scale' },
|
||||
])
|
||||
saveEditingQuestionSet('.question-set-item-editor')
|
||||
|
||||
|
||||
@@ -45,12 +45,8 @@ const replaceEditingQuestionSet = (questionSetItemSelector, newQuestions) => {
|
||||
}
|
||||
|
||||
const clearEditingQuestionSetInputs = (selector) => {
|
||||
cy.get(selector)
|
||||
.find('.question-set-form__name')
|
||||
.clear()
|
||||
cy.get(selector)
|
||||
.find('.question-set-form__questions')
|
||||
.clear()
|
||||
cy.get(selector).find('.question-set-form__name').clear()
|
||||
cy.get(selector).find('.question-set-form__questions').clear()
|
||||
}
|
||||
|
||||
const saveEditingQuestionSet = (questionSetItemSelector) => {
|
||||
@@ -60,9 +56,7 @@ const saveEditingQuestionSet = (questionSetItemSelector) => {
|
||||
}
|
||||
|
||||
const cancelEditingQuestionSet = (selector) => {
|
||||
cy.get(selector)
|
||||
.find('.question-set-item-editor__cancel-button')
|
||||
.click()
|
||||
cy.get(selector).find('.question-set-item-editor__cancel-button').click()
|
||||
}
|
||||
|
||||
describe('Review-questions page', () => {
|
||||
@@ -81,8 +75,8 @@ describe('Review-questions page', () => {
|
||||
{
|
||||
type: 'radio',
|
||||
header: 'Questions without options?',
|
||||
description: 'Generate options 1 to 5'
|
||||
}
|
||||
description: 'Generate options 1 to 5',
|
||||
},
|
||||
])
|
||||
|
||||
cy.get('.notification').contains(
|
||||
@@ -116,9 +110,9 @@ describe('Review-questions page', () => {
|
||||
'Little',
|
||||
'Decent',
|
||||
'Much',
|
||||
'Super'
|
||||
]
|
||||
}
|
||||
'Super',
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
cy.get(
|
||||
@@ -140,15 +134,15 @@ describe('Review-questions page', () => {
|
||||
{
|
||||
type: 'info',
|
||||
header: 'Just a text field plz?',
|
||||
description: 'Right?'
|
||||
}
|
||||
description: 'Right?',
|
||||
},
|
||||
])
|
||||
createQuestionSet('Info tester', [
|
||||
{
|
||||
type: 'info',
|
||||
header: 'Just a text field plz?',
|
||||
description: 'Right?'
|
||||
}
|
||||
description: 'Right?',
|
||||
},
|
||||
])
|
||||
cy.get('.notification').contains('name already in use')
|
||||
cy.get('.peer-review-question-set-list')
|
||||
@@ -171,7 +165,7 @@ describe('Review-questions page', () => {
|
||||
cy.visit('/administration/peer-review-questions')
|
||||
})
|
||||
|
||||
it.skip('does not save changes if both name and questions are empty', () => {
|
||||
it('does not save changes if both name and questions are empty', () => {
|
||||
createQuestionSet('With options tester', [
|
||||
{
|
||||
header: 'Do we have a right header here?',
|
||||
@@ -183,9 +177,9 @@ describe('Review-questions page', () => {
|
||||
'Little',
|
||||
'Decent',
|
||||
'Much',
|
||||
'Super'
|
||||
]
|
||||
}
|
||||
'Super',
|
||||
],
|
||||
},
|
||||
])
|
||||
editQuestionSet('.question-set-item')
|
||||
clearEditingQuestionSetInputs('.question-set-item-editor')
|
||||
@@ -196,13 +190,13 @@ describe('Review-questions page', () => {
|
||||
cy.get('.question-set-item-editor')
|
||||
})
|
||||
|
||||
it.skip('shows error if new JSON is invalid', () => {
|
||||
it('shows error if new JSON is invalid', () => {
|
||||
createQuestionSet('No options tester', [
|
||||
{
|
||||
header: 'Do we have a right header here?',
|
||||
description: 'I hope we do have a descirption also',
|
||||
type: 'radio'
|
||||
}
|
||||
type: 'radio',
|
||||
},
|
||||
])
|
||||
editQuestionSet('.question-set-item')
|
||||
replaceEditingQuestionSetJson(
|
||||
@@ -218,8 +212,8 @@ describe('Review-questions page', () => {
|
||||
{
|
||||
header: 'Do we have a right header here?',
|
||||
description: 'I hope we do have a descirption also',
|
||||
type: 'number'
|
||||
}
|
||||
type: 'number',
|
||||
},
|
||||
])
|
||||
editQuestionSet('.question-set-item')
|
||||
replaceEditingQuestionSetJson('.question-set-item-editor', '{"foo":123}')
|
||||
@@ -239,9 +233,9 @@ describe('Review-questions page', () => {
|
||||
'Little',
|
||||
'Decent',
|
||||
'Much',
|
||||
'Super'
|
||||
]
|
||||
}
|
||||
'Super',
|
||||
],
|
||||
},
|
||||
])
|
||||
editQuestionSet('.question-set-item')
|
||||
replaceEditingQuestionSetName('.question-set-item-editor', 'New name')
|
||||
@@ -254,21 +248,21 @@ describe('Review-questions page', () => {
|
||||
cy.get('.question-set-item__content').contains('Little')
|
||||
})
|
||||
|
||||
it.skip('edits questions without changing name', () => {
|
||||
it('edits questions without changing name', () => {
|
||||
createQuestionSet('Number question tester', [
|
||||
{
|
||||
header: 'Do we have a right header here?',
|
||||
description: 'I hope we do have a descirption also',
|
||||
type: 'number'
|
||||
}
|
||||
type: 'number',
|
||||
},
|
||||
])
|
||||
editQuestionSet('.question-set-item')
|
||||
replaceEditingQuestionSet('.question-set-item-editor', [
|
||||
{
|
||||
header: 'Nice, edited question',
|
||||
description: 'Some edited questions',
|
||||
type: 'radio'
|
||||
}
|
||||
type: 'radio',
|
||||
},
|
||||
])
|
||||
saveEditingQuestionSet('.question-set-item-editor')
|
||||
|
||||
@@ -287,8 +281,8 @@ describe('Review-questions page', () => {
|
||||
{
|
||||
header: 'Info tester',
|
||||
description: 'This is info tester',
|
||||
type: 'info'
|
||||
}
|
||||
type: 'info',
|
||||
},
|
||||
])
|
||||
editQuestionSet('.question-set-item')
|
||||
replaceEditingQuestionSetName('.question-set-item-editor', 'foobar')
|
||||
@@ -296,8 +290,8 @@ describe('Review-questions page', () => {
|
||||
{
|
||||
header: 'This could be edited but will be canceled..',
|
||||
description: 'Is this real?',
|
||||
type: 'info'
|
||||
}
|
||||
type: 'info',
|
||||
},
|
||||
])
|
||||
cancelEditingQuestionSet('.question-set-item-editor')
|
||||
|
||||
@@ -314,8 +308,8 @@ describe('Review-questions page', () => {
|
||||
{
|
||||
header: 'Info tester',
|
||||
description: 'This is info tester',
|
||||
type: 'info'
|
||||
}
|
||||
type: 'info',
|
||||
},
|
||||
])
|
||||
editQuestionSet('.question-set-item')
|
||||
replaceEditingQuestionSetName('.question-set-item-editor', 'New name 2')
|
||||
@@ -323,8 +317,8 @@ describe('Review-questions page', () => {
|
||||
{
|
||||
header: 'Radio tester edited',
|
||||
description: 'This is now radio tester',
|
||||
type: 'radio'
|
||||
}
|
||||
type: 'radio',
|
||||
},
|
||||
])
|
||||
saveEditingQuestionSet('.question-set-item-editor')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user