From 640e4af23bb732615fd2ce785a627b45140ed41b Mon Sep 17 00:00:00 2001 From: tfhuhtal Date: Thu, 28 Nov 2024 15:13:16 +0200 Subject: [PATCH 1/2] fix: using CUR instead of CU --- .../integration/organisationSurvey.spec.js | 16 ++------- .../Edit/EditOrganisationSurvey.jsx | 2 +- .../pages/Organisation/CourseSearchInput.tsx | 10 ++++-- .../Organisation/OrganisationSurveys.jsx | 35 ++++++------------- .../organisations/organisationSurveys.js | 29 +++++++++------ 5 files changed, 39 insertions(+), 53 deletions(-) diff --git a/cypress/integration/organisationSurvey.spec.js b/cypress/integration/organisationSurvey.spec.js index e69319c9..c2a7caf5 100644 --- a/cypress/integration/organisationSurvey.spec.js +++ b/cypress/integration/organisationSurvey.spec.js @@ -479,10 +479,10 @@ describe('Feedback Correspondents', () => { teacherIds: [organisationCorrespondent.hyPersonSisuId], startDate: today, endDate: new Date().setDate(today.getDate() + 1), - courseIds: ['norppa-test-course-unit-id-2'], + courseIds: ['norppa-test-course-unit-realisation-id-2'], } - cy.createFeedbackTarget({ extraStudents: 99 }) + cy.createFeedbackTarget({ extraStudents: 9 }) cy.createOrganisationSurvey(organisationCode, organisationSurveyBody) @@ -493,17 +493,7 @@ describe('Feedback Correspondents', () => { cy.get('[data-cy="feedback-target-edit-organisation-survey"').should('exist').click() - cy.contains('Students: 100').should('exist') - - // Add student Henri to the survey - cy.get('[data-cy="formik-student-number-input-field"]').as('studentInput') - cy.get('@studentInput').type(`${studentHenri.studentNumber}{enter}`) - - cy.get('[data-cy="organisation-survey-editor-save"]').click() - - cy.get('[data-cy="feedback-target-edit-organisation-survey"').should('exist').click() - - cy.contains('Students: 101').should('exist') + cy.contains('Students: 10').should('exist') }) }) diff --git a/src/client/pages/FeedbackTarget/Edit/EditOrganisationSurvey.jsx b/src/client/pages/FeedbackTarget/Edit/EditOrganisationSurvey.jsx index 8d2cfc8c..7faed6a2 100644 --- a/src/client/pages/FeedbackTarget/Edit/EditOrganisationSurvey.jsx +++ b/src/client/pages/FeedbackTarget/Edit/EditOrganisationSurvey.jsx @@ -52,7 +52,7 @@ const EditOrganisationSurvey = () => { surveyId: organisationSurvey.id, ...data, teacherIds: data.teachers.map(t => t.id), - courseIds: data.courses?.map(c => c.id) || [], + courseIds: data.courses?.map(c => c.courseRealisationId), } await editMutation.mutateAsync(values, { diff --git a/src/client/pages/Organisation/CourseSearchInput.tsx b/src/client/pages/Organisation/CourseSearchInput.tsx index afd2246e..2f2ac9f2 100644 --- a/src/client/pages/Organisation/CourseSearchInput.tsx +++ b/src/client/pages/Organisation/CourseSearchInput.tsx @@ -40,10 +40,14 @@ const CourseSearchInput = ({ organisationCode }: { organisationCode: Organisatio endDate: dateRange.end, }) + const options = data + ?.flatMap(({ feedbackTargets }) => feedbackTargets) + .sort((a, b) => a.courseRealisation.name[language].localeCompare(b.courseRealisation.name[language])) + const getOptionLabel = (course: any) => { - const courseRealisation = course.feedbackTargets[0]?.courseRealisation + const { courseRealisation } = course const [startDate, endDate] = getStartAndEndString(courseRealisation.startDate, courseRealisation.endDate) - const courseName = getLanguageValue(course?.name, language) + const courseName = getLanguageValue(courseRealisation?.name, language) return `${courseName || ''} (${startDate} - ${endDate})` } @@ -66,7 +70,7 @@ const CourseSearchInput = ({ organisationCode }: { organisationCode: Organisatio onInputChange={(_, value) => { setCourseSearch(value) }} - options={data ?? []} + options={options ?? []} getOptionLabel={getOptionLabel} renderInput={params => ( diff --git a/src/client/pages/Organisation/OrganisationSurveys.jsx b/src/client/pages/Organisation/OrganisationSurveys.jsx index a2f9850a..c83688e3 100644 --- a/src/client/pages/Organisation/OrganisationSurveys.jsx +++ b/src/client/pages/Organisation/OrganisationSurveys.jsx @@ -16,10 +16,7 @@ import useAuthorizedUser from '../../hooks/useAuthorizedUser' import { LoadingProgress } from '../../components/common/LoadingProgress' -import { - getOverlappingStudentTeachers, - getOrganisationSurveySchema, -} from './utils' +import { getOverlappingStudentTeachers, getOrganisationSurveySchema } from './utils' const styles = { dates: { @@ -45,8 +42,7 @@ const OrganisationSurveys = () => { const { authorizedUser, isLoading: isUserLoading } = useAuthorizedUser() const mutation = useCreateOrganisationSurveyMutation(code) - const { surveys, isLoading: isOrganisationSurveysLoading } = - useOrganisationSurveys(code) + const { surveys, isLoading: isOrganisationSurveysLoading } = useOrganisationSurveys(code) const organisationSurveySchema = getOrganisationSurveySchema(t) @@ -76,7 +72,7 @@ const OrganisationSurveys = () => { setErrors({ studentNumbers: { text: t('validationErrors:overlappingStudentTeacher'), - data: overlappingStudentTeachers.map((t) => t.studentNumber), + data: overlappingStudentTeachers.map(t => t.studentNumber), }, }) return @@ -84,24 +80,19 @@ const OrganisationSurveys = () => { const values = { ...data, - teacherIds: data.teachers.map((t) => t.id), - courseIds: data.courses.map((c) => c.id), + teacherIds: data.teachers.map(t => t.id), + courseIds: data.courses.map(c => c.courseRealisationId), } await mutation.mutateAsync(values, { - onSuccess: (data) => { + onSuccess: data => { handleClose() navigate(`/targets/${data.id}/edit`) enqueueSnackbar(t('common:saveSuccess'), { variant: 'success' }) }, - onError: (error) => { - if ( - error.isAxiosError && - error.response && - error.response.data && - error.response.data.invalidStudentNumbers - ) { + onError: error => { + if (error.isAxiosError && error.response && error.response.data && error.response.data.invalidStudentNumbers) { const { invalidStudentNumbers } = error.response.data setErrors({ @@ -144,15 +135,9 @@ const OrganisationSurveys = () => { /> {surveys.length > 0 ? ( - surveys.map((survey) => ( - - )) + surveys.map(survey => ) ) : ( - + {t('organisationSurveys:emptySurveys')} )} diff --git a/src/server/services/organisations/organisationSurveys.js b/src/server/services/organisations/organisationSurveys.js index 1f402e32..a15dae68 100644 --- a/src/server/services/organisations/organisationSurveys.js +++ b/src/server/services/organisations/organisationSurveys.js @@ -130,17 +130,24 @@ const getStudentIds = async studentNumbers => { } const createUserFeedbackTargets = async (feedbackTargetId, userIds, accessStatus) => { - const userFeedbackTargets = await UserFeedbackTarget.bulkCreate( - userIds.map(userId => ({ - accessStatus, - feedbackTargetId, - userId, - isAdministrativePerson: accessStatus === 'RESPONSIBLE_TEACHER', - userCreated: true, - })) - ) + const createdUserFeedbackTargets = [] - return userFeedbackTargets + for (const userId of userIds) { + try { + const userFeedbackTarget = await UserFeedbackTarget.create({ + accessStatus, + feedbackTargetId, + userId, + isAdministrativePerson: accessStatus === 'RESPONSIBLE_TEACHER', + userCreated: true, + }) + createdUserFeedbackTargets.push(userFeedbackTarget) + } catch (error) { + logger.error(`Error creating feedback target for user ${userId}:`, error.message) + } + } + + return createdUserFeedbackTargets } const getUserFeedbackTargets = async (feedbackTargetId, accessStatus) => { @@ -459,7 +466,7 @@ const getStudentNumbersFromCourseIds = async courseIds => { as: 'feedbackTarget', required: true, where: { - courseUnitId: { [Op.in]: courseIds }, + courseRealisationId: { [Op.in]: courseIds }, }, }, ], From 37aeae61d9544600b09b6f94a4304b8b185b7153 Mon Sep 17 00:00:00 2001 From: tfhuhtal Date: Thu, 28 Nov 2024 15:32:14 +0200 Subject: [PATCH 2/2] fix: added types --- .../pages/Organisation/CourseSearchInput.tsx | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/client/pages/Organisation/CourseSearchInput.tsx b/src/client/pages/Organisation/CourseSearchInput.tsx index 2f2ac9f2..c1d583ed 100644 --- a/src/client/pages/Organisation/CourseSearchInput.tsx +++ b/src/client/pages/Organisation/CourseSearchInput.tsx @@ -24,6 +24,21 @@ interface InitialValues { type OrganisationCode = string +type CourseRealisation = { + id: string + name: Locales + startDate: string + endDate: string +} + +type FeedbackTarget = { + id: number + courseRealisationId: string + courseRealisation: CourseRealisation +} + +type FeedbackTargets = FeedbackTarget[] + const CourseSearchInput = ({ organisationCode }: { organisationCode: OrganisationCode }) => { const { t, i18n } = useTranslation() const { language } = i18n @@ -41,10 +56,14 @@ const CourseSearchInput = ({ organisationCode }: { organisationCode: Organisatio }) const options = data - ?.flatMap(({ feedbackTargets }) => feedbackTargets) - .sort((a, b) => a.courseRealisation.name[language].localeCompare(b.courseRealisation.name[language])) + ?.flatMap(({ feedbackTargets }: { feedbackTargets: FeedbackTargets }) => feedbackTargets) + .sort((a: FeedbackTarget, b: FeedbackTarget) => + a.courseRealisation.name[language as keyof Locales].localeCompare( + b.courseRealisation.name[language as keyof Locales] + ) + ) - const getOptionLabel = (course: any) => { + const getOptionLabel = (course: FeedbackTarget) => { const { courseRealisation } = course const [startDate, endDate] = getStartAndEndString(courseRealisation.startDate, courseRealisation.endDate) const courseName = getLanguageValue(courseRealisation?.name, language)