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

#1357 #1407

Merged
merged 3 commits into from
Nov 28, 2024
Merged

#1357 #1407

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions cypress/integration/organisationSurvey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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')
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
31 changes: 27 additions & 4 deletions src/client/pages/Organisation/CourseSearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,10 +55,18 @@ const CourseSearchInput = ({ organisationCode }: { organisationCode: Organisatio
endDate: dateRange.end,
})

const getOptionLabel = (course: any) => {
const courseRealisation = course.feedbackTargets[0]?.courseRealisation
const options = data
?.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: FeedbackTarget) => {
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})`
}

Expand All @@ -66,7 +89,7 @@ const CourseSearchInput = ({ organisationCode }: { organisationCode: Organisatio
onInputChange={(_, value) => {
setCourseSearch(value)
}}
options={data ?? []}
options={options ?? []}
getOptionLabel={getOptionLabel}
renderInput={params => (
<TextField {...params} label={t('organisationSurveys:addCourses')} variant="outlined" />
Expand Down
35 changes: 10 additions & 25 deletions src/client/pages/Organisation/OrganisationSurveys.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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)

Expand Down Expand Up @@ -76,32 +72,27 @@ const OrganisationSurveys = () => {
setErrors({
studentNumbers: {
text: t('validationErrors:overlappingStudentTeacher'),
data: overlappingStudentTeachers.map((t) => t.studentNumber),
data: overlappingStudentTeachers.map(t => t.studentNumber),
},
})
return
}

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({
Expand Down Expand Up @@ -144,15 +135,9 @@ const OrganisationSurveys = () => {
/>

{surveys.length > 0 ? (
surveys.map((survey) => (
<OrganisationSurveyItem key={survey.id} organisationSurvey={survey} />
))
surveys.map(survey => <OrganisationSurveyItem key={survey.id} organisationSurvey={survey} />)
) : (
<Alert
data-cy="organisation-surveys-no-surveys-alert"
sx={{ mt: 2 }}
severity="info"
>
<Alert data-cy="organisation-surveys-no-surveys-alert" sx={{ mt: 2 }} severity="info">
{t('organisationSurveys:emptySurveys')}
</Alert>
)}
Expand Down
29 changes: 18 additions & 11 deletions src/server/services/organisations/organisationSurveys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -459,7 +466,7 @@ const getStudentNumbersFromCourseIds = async courseIds => {
as: 'feedbackTarget',
required: true,
where: {
courseUnitId: { [Op.in]: courseIds },
courseRealisationId: { [Op.in]: courseIds },
},
},
],
Expand Down