Skip to content

Commit

Permalink
feat: -refs: #1408
Browse files Browse the repository at this point in the history
  • Loading branch information
tfhuhtal committed Dec 9, 2024
1 parent a00db80 commit cda1510
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const EditOrganisationSurvey = () => {
endDate: organisationSurvey.closesAt,
studentNumbers: organisationSurvey.students.map(s => s.user.studentNumber),
teachers: organisationSurvey.userFeedbackTargets.map(t => t.user),
courses: organisationSurvey.organisationSurveyCourses,
}

const organisationSurveySchema = getOrganisationSurveySchema(t)
Expand Down
13 changes: 11 additions & 2 deletions src/client/pages/Organisation/CourseSearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const CourseSearchInput = ({ organisationCode }: { organisationCode: Organisatio

const getOptionLabel = (course: FeedbackTarget) => {
const { courseRealisation } = course
const [startDate, endDate] = getStartAndEndString(courseRealisation.startDate, courseRealisation.endDate)
const [startDate, endDate] = getStartAndEndString(courseRealisation?.startDate, courseRealisation?.endDate)
const courseName = getLanguageValue(courseRealisation?.name, language)
return `${courseName || ''} (${startDate} - ${endDate})`
}
Expand All @@ -74,14 +74,23 @@ const CourseSearchInput = ({ organisationCode }: { organisationCode: Organisatio
setDateRange(nextDateRange)
}

console.log(formikProps.initialValues.courses)

const getInitialValues = (courses: CourseRealisation[]) =>
options
?.map(({ courseRealisationId }: FeedbackTarget) => {
return courses.find(({ id }: CourseRealisation) => id === courseRealisationId)
})
.filter(Boolean) || []

return (
<>
<Grid size={10}>
<Autocomplete
id="courses"
disableCloseOnSelect
multiple
defaultValue={formikProps.initialValues.courses}
defaultValue={getInitialValues(formikProps.initialValues.courses)}
onChange={(_, courses) => {
formikProps.setFieldValue('courses', courses)
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
autoIncrement: true,
},
feedback_target_id: {
type: DataTypes.STRING,
type: DataTypes.INTEGER,
allowNull: false,
},
course_realisation_id: {
Expand Down
3 changes: 3 additions & 0 deletions src/server/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ FeedbackTarget.hasOne(Summary, { foreignKey: 'feedbackTargetId', as: 'summary' }
Summary.belongsTo(CourseRealisation, { foreignKey: 'entityId', as: 'courseRealisation' })
CourseRealisation.hasOne(Summary, { foreignKey: 'entityId', as: 'summary' })

FeedbackTarget.hasMany(OrganisationSurveyCourse, { foreignKey: 'feedbackTargetId', as: 'organisationSurveyCourses' })
OrganisationSurveyCourse.belongsTo(FeedbackTarget, { foreignKey: 'feedbackTargetId', as: 'feedbackTarget' })

export {
Feedback,
User,
Expand Down
2 changes: 1 addition & 1 deletion src/server/models/organisationSurveyCourse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ OrganisationSurveyCourse.init(
autoIncrement: true,
},
feedbackTargetId: {
type: STRING,
type: INTEGER,
allowNull: true,
},
courseRealisationId: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,12 @@ const createOrganisationSurvey = async (req, res) => {

const survey = await getSurveyById(feedbackTarget.id)

await createOrganisationSurveyCourses(feedbackTarget.id, studentDataFromCourseIds)

return res.status(201).send({
...survey.dataValues,
userFeedbackTargets: [...studentFeedbackTargets, ...teacherFeedbackTargets],
courseIds: initialCourseIds,
})
}

Expand Down
11 changes: 11 additions & 0 deletions src/server/services/organisations/organisationSurveys.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,22 @@ const getSurveyById = async feedbackTargetId => {
as: 'user',
},
},
{
model: OrganisationSurveyCourse,
as: 'organisationSurveyCourses',
attributes: ['courseRealisationId'], // Include all without DISTINCT
required: false,
where: { feedbackTargetId },
},
],
})

if (!organisationSurvey) throw new Error('Organisation survey not found')

const courseIds = [...new Set(organisationSurvey.organisationSurveyCourses.map(c => c.courseRealisationId))]

organisationSurvey.organisationSurveyCourses = courseIds

return organisationSurvey
}

Expand Down

0 comments on commit cda1510

Please sign in to comment.