Skip to content

Commit

Permalink
Sync db models and migrations with Norppa
Browse files Browse the repository at this point in the history
  • Loading branch information
Keskimaki committed Oct 20, 2023
1 parent d9d96c4 commit 271f925
Show file tree
Hide file tree
Showing 20 changed files with 378 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@


module.exports = {
up: async () => {
try {
const { initialiseSummaryView, initialiseCountsView } = require('../services/summary/sql')
await initialiseSummaryView()
await initialiseCountsView()
} catch (error) {
console.error("Failed to initialize summary mat views: ", error?.message)
console.error('Failed to initialize summary mat views: ', error?.message)
}
},
down: async queryInterface => {
Expand Down
11 changes: 11 additions & 0 deletions src/migrations/20230313_00_add_index_to_groups_on_fbt_id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
up: async queryInterface => {
await queryInterface.addIndex('groups', {
fields: ['feedback_target_id'],
name: 'groups_feedback_target_id',
})
},
down: async queryInterface => {
await queryInterface.removeIndex('groups')
},
}
13 changes: 13 additions & 0 deletions src/migrations/20230320_00_add_secondaryType_to_questions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { DataTypes } = require('sequelize')

module.exports = {
up: async queryInterface => {
await queryInterface.addColumn('questions', 'secondary_type', {
type: DataTypes.STRING,
allowNull: true,
})
},
down: async queryInterface => {
await queryInterface.dropColumn('questions', 'secondary_type')
},
}
38 changes: 38 additions & 0 deletions src/migrations/20230824_00_init_summaries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { DataTypes } = require('sequelize')

module.exports = {
up: async queryInterface => {
await queryInterface.createTable(
'summaries',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
entity_id: {
type: DataTypes.STRING,
allowNull: true,
},
start_date: {
type: DataTypes.DATEONLY,
allowNull: false,
},
end_date: {
type: DataTypes.DATEONLY,
allowNull: false,
},
data: {
type: DataTypes.JSONB,
allowNull: false,
},
},
{
unlogged: true,
}
)
},
down: async queryInterface => {
await queryInterface.dropTable('summaries')
},
}
32 changes: 32 additions & 0 deletions src/migrations/20230921_00_add_user_created_to_multiple_tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { BOOLEAN } = require('sequelize')

module.exports = {
up: async queryInterface => {
await queryInterface.addColumn('feedback_targets', 'user_created', {
type: BOOLEAN,
allowNull: false,
defaultValue: false,
})
await queryInterface.addColumn('user_feedback_targets', 'user_created', {
type: BOOLEAN,
allowNull: false,
defaultValue: false,
})
await queryInterface.addColumn('course_realisations', 'user_created', {
type: BOOLEAN,
allowNull: false,
defaultValue: false,
})
await queryInterface.addColumn('course_units', 'user_created', {
type: BOOLEAN,
allowNull: false,
defaultValue: false,
})
},
down: async queryInterface => {
await queryInterface.dropColumn('feedback_targets', 'user_created')
await queryInterface.dropColumn('user_feedback_targets', 'user_created')
await queryInterface.dropColumn('course_realisations', 'user_created')
await queryInterface.dropColumn('course_units', 'user_created')
},
}
12 changes: 12 additions & 0 deletions src/migrations/20231013_00_add_summaries_uniq_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { DataTypes } = require('sequelize')

module.exports = {
up: async queryInterface => {
await queryInterface.addIndex('summaries', {
unique: true,
fields: ['entity_id', 'start_date', 'end_date'],
})
await queryInterface.sequelize.query('ALTER TABLE summaries SET UNLOGGED')
},
down: async queryInterface => {},
}
10 changes: 10 additions & 0 deletions src/migrations/20231020_00_drop_summaries_start_date_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
up: async queryInterface => {
try {
await queryInterface.sequelize.query('DROP INDEX summaries_entity_id_start_date;')
} catch (e) {
console.log('Tried to drop summaries_entity_id_start_date but index does not exist. Ok.')
}
},
down: async queryInterface => {},
}
5 changes: 2 additions & 3 deletions src/models/banner.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
const { ENUM, DATE, Op } = require('sequelize')
const { Model, JSONB } = require('sequelize')
const { sequelize } = require('../db/dbConnection')
const { ADMINS } = require('../util/config')

class Banner extends Model {
static async getForUser(user) {
let access = ['STUDENT']
const isAdmin = ADMINS.includes(user.username)

const isTeacher = await user.isTeacher()
const hasOrgAccess = (await user.getOrganisationAccess())?.length > 0

if (isAdmin) {
if (user.isAdmin) {
access = access.concat(['TEACHER', 'ORG', 'ADMIN'])
} else if (hasOrgAccess) {
access = access.concat(['TEACHER', 'ORG'])
Expand Down
5 changes: 5 additions & 0 deletions src/models/courseRealisation.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ CourseRealisation.init(
defaultValue: false,
allowNull: false,
},
userCreated: {
type: BOOLEAN,
allowNull: false,
defaultValue: false,
},
teachingLanguages: {
type: JSONB,
allowNull: true,
Expand Down
10 changes: 9 additions & 1 deletion src/models/courseUnit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Model, JSONB, STRING } = require('sequelize')
const { Model, JSONB, STRING, BOOLEAN, VIRTUAL } = require('sequelize')
const { sequelize } = require('../db/dbConnection')
const { STUDENT_LIST_BY_COURSE_ENABLED } = require('../util/config')
const logger = require('../util/logger')
Expand Down Expand Up @@ -52,6 +52,14 @@ CourseUnit.init(
type: JSONB,
allowNull: false,
},
userCreated: {
type: BOOLEAN,
allowNull: false,
defaultValue: false,
},
summary: {
type: VIRTUAL,
},
},
{
underscored: true,
Expand Down
8 changes: 8 additions & 0 deletions src/models/feedbackTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ class FeedbackTarget extends Model {
publicityConfigurableQuestionIds,
}

// Do not accidentally send this to client
delete feedbackTarget.userFeedbackTargets

return feedbackTarget
}

Expand Down Expand Up @@ -373,6 +376,11 @@ FeedbackTarget.init(
allowNull: false,
defaultValue: false,
},
userCreated: {
type: BOOLEAN,
allowNull: false,
defaultValue: false,
},
},
{
underscored: true,
Expand Down
7 changes: 7 additions & 0 deletions src/models/group.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { INTEGER } = require('sequelize')
const { VIRTUAL } = require('sequelize')
const { Model, JSONB, STRING } = require('sequelize')
const { sequelize } = require('../db/dbConnection')

Expand All @@ -18,6 +19,12 @@ Group.init(
name: {
type: JSONB,
},
teachers: {
type: VIRTUAL,
},
studentCount: {
type: VIRTUAL,
},
},
{
underscored: true,
Expand Down
58 changes: 51 additions & 7 deletions src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const CourseUnitsOrganisation = require('./courseUnitsOrganisation')
const CourseRealisationsOrganisation = require('./courseRealisationsOrganisation')
const NorppaFeedback = require('./norppaFeedback')
const UpdaterStatus = require('./updaterStatus')
const FeedbackTargetDateCheck = require('./feedbackTargetDateCheck')
const OrganisationLog = require('./organisationLog')
const FeedbackTargetLog = require('./feedbackTargetLog')
const ContinuousFeedback = require('./continuousFeedback')
Expand All @@ -23,6 +22,7 @@ const Banner = require('./banner')
const InactiveCourseRealisation = require('./inactiveCourseRealisation')
const CourseUnitsTag = require('./courseUnitsTag')
const Group = require('./group')
const Summary = require('./summary')

FeedbackTarget.belongsTo(CourseUnit, {
as: 'courseUnit',
Expand All @@ -32,6 +32,10 @@ FeedbackTarget.belongsTo(CourseRealisation, {
foreignKey: 'courseRealisationId',
as: 'courseRealisation',
})
CourseRealisation.hasMany(FeedbackTarget, {
foreignKey: 'courseRealisationId',
as: 'feedbackTargets',
})

UserFeedbackTarget.belongsTo(FeedbackTarget, { as: 'feedbackTarget' })
FeedbackTarget.hasMany(UserFeedbackTarget, { as: 'userFeedbackTargets' })
Expand Down Expand Up @@ -59,11 +63,27 @@ Survey.belongsTo(CourseUnit, {
targetKey: 'courseCode',
})

Survey.belongsTo(Organisation, {
as: 'organisation',
foreignKey: 'typeId',
targetKey: 'code',
})

Organisation.hasMany(Survey, {
as: 'surveys',
foreignKey: 'typeId',
sourceKey: 'code',
})

CourseUnit.belongsToMany(Organisation, {
through: CourseUnitsOrganisation,
as: 'organisations',
})

CourseUnit.hasMany(CourseUnitsOrganisation, {
as: 'courseUnitsOrganisations',
})

Organisation.belongsToMany(CourseUnit, {
through: CourseUnitsOrganisation,
as: 'courseUnits',
Expand All @@ -74,11 +94,25 @@ CourseRealisation.belongsToMany(Organisation, {
as: 'organisations',
})

CourseRealisation.hasMany(CourseRealisationsOrganisation, {
as: 'courseRealisationsOrganisations',
})

Organisation.belongsToMany(CourseRealisation, {
through: CourseRealisationsOrganisation,
as: 'courseRealisations',
})

Organisation.belongsTo(Organisation, {
foreignKey: 'parent_id',
as: 'parentOrganisation',
})

Organisation.hasMany(Organisation, {
foreignKey: 'parent_id',
as: 'childOrganisations',
})

FeedbackTarget.belongsToMany(User, {
through: UserFeedbackTarget,
as: 'users',
Expand All @@ -95,11 +129,6 @@ NorppaFeedback.belongsTo(User, {
as: 'user',
})

FeedbackTargetDateCheck.belongsTo(FeedbackTarget, {
as: 'feedback_target',
foreign_key: 'feedback_target_id',
})

OrganisationLog.belongsTo(Organisation, {
as: 'organisation',
foreign_key: 'organisation_id',
Expand Down Expand Up @@ -188,11 +217,25 @@ Tag.hasMany(CourseUnitsTag)

FeedbackTarget.hasMany(Group, {
foreignKey: 'feedbackTargetId',
as: 'groups',
})
Group.belongsTo(FeedbackTarget, {
foreignKey: 'feedbackTargetId',
as: 'feedbackTarget',
})

/**
* Summary associations
*/
Summary.belongsTo(Organisation, { foreignKey: 'entityId', as: 'organisation' })
Organisation.hasMany(Summary, { foreignKey: 'entityId', as: 'summaries' })

Summary.belongsTo(CourseUnit, { foreignKey: 'entityId', as: 'courseUnit' })
CourseUnit.hasMany(Summary, { foreignKey: 'entityId', as: 'summaries' })

Summary.belongsTo(CourseRealisation, { foreignKey: 'entityId', as: 'courseRealisation' })
CourseRealisation.hasOne(Summary, { foreignKey: 'entityId', as: 'summary' })

module.exports = {
Feedback,
User,
Expand All @@ -207,7 +250,6 @@ module.exports = {
CourseRealisationsOrganisation,
NorppaFeedback,
UpdaterStatus,
FeedbackTargetDateCheck,
OrganisationLog,
FeedbackTargetLog,
ContinuousFeedback,
Expand All @@ -217,5 +259,7 @@ module.exports = {
InactiveCourseRealisation,
Banner,
Tag,
CourseUnitsTag,
Group,
Summary,
}
Loading

0 comments on commit 271f925

Please sign in to comment.