From 4794d3f37e3b6123d1c8de57d3f263946d3f84c6 Mon Sep 17 00:00:00 2001 From: Henri Remonen Date: Tue, 14 May 2024 15:08:24 +0300 Subject: [PATCH] feat: add utility functions for getting the correct names for CUs and CURs --- .../utils.js | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/updater/updateCoursesAndTeacherFeedbackTargets/utils.js b/src/updater/updateCoursesAndTeacherFeedbackTargets/utils.js index 76d9fa2..299ea31 100644 --- a/src/updater/updateCoursesAndTeacherFeedbackTargets/utils.js +++ b/src/updater/updateCoursesAndTeacherFeedbackTargets/utils.js @@ -33,7 +33,76 @@ const getFeedbackCount = async (courseRealisationId) => { return feedbackCount } +export const getLanguageValue = (values, preferred) => { + if (!values) { + return null + } + + const possibleLangs = ['fi', 'en', 'sv'] + + if (values[preferred]) return values[preferred] + + // eslint-disable-next-line + for (const lang of possibleLangs) { + if (values[lang]) return values[lang] + } + + return null +} + +// Documents created in sisu have id format of `otm-{UUID}` +const isSisuNativeId = (id) => id && id.startsWith('otm-') + +const isAiliOriginatingId = (id) => id && id.startsWith('hy-cur-aili-') + +const hasSisuLikeNamingConvention = (id) => id.startsWith('otm-') || id.startsWith('hy-cur-aili-') + +const isOptimeOriginatingId = (id) => id && id.startsWith('hy-opt-cur-') + +const courseNameWithCourseType = (name, type, lang) => { + const nameTranslated = typeof (name) === 'string' ? name : getLanguageValue(name, lang); + const typeTranslated = typeof (type) === 'string' ? type : getLanguageValue(type, lang); + + if (!nameTranslated) { + return typeTranslated; + } + if (!typeTranslated) { + return nameTranslated; + } + return `${nameTranslated}, ${typeTranslated}`; +}; + +/** + * Translate and format course name. + * + * Realisations created in Sisu (id format "otm-") contain course type in "name" field and descriptive name in "nameSpecifier" field. + * Realisations created in Optime (id format "hy-opt-cur-") or Oodi (id format "hy-CUR-") are opposite of this. + * + * Returns course name as "Descriptive name, Course type" for sisu native and oodi courses and courses descriptive name for + * realisations created in Optime. + * + * @param {string} id - Course unit realisation id + * @param {LocalizedText|string} name - Course unit realisation name from sisu + * @param {LocalizedText|string} nameSpecifier - Course unit realisation nameSpecifier from sisu + * @param {string} lang - Language to translate name in + * @returns {string} - Formatted course name + */ +const formatCourseName = (id, name, nameSpecifier, lang) => { + if (hasSisuLikeNamingConvention(id)) { + return courseNameWithCourseType(nameSpecifier, name, lang); + } if (isOptimeOriginatingId(id)) { + return courseNameWithCourseType(name, null, lang); + } + return courseNameWithCourseType(name, nameSpecifier, lang); +} + module.exports = { formatWithHours, getFeedbackCount, + isSisuNativeId, + isAiliOriginatingId, + hasSisuLikeNamingConvention, + isOptimeOriginatingId, + courseNameWithCourseType, + formatCourseName, }