Skip to content

Commit

Permalink
feat: add utility functions for getting the correct names for CUs and…
Browse files Browse the repository at this point in the history
… CURs
  • Loading branch information
HRemonen committed May 14, 2024
1 parent 6c5114d commit 4794d3f
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/updater/updateCoursesAndTeacherFeedbackTargets/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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-<nnn>") contain course type in "name" field and descriptive name in "nameSpecifier" field.
* Realisations created in Optime (id format "hy-opt-cur-<nnn>") or Oodi (id format "hy-CUR-<nnn>") 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,
}

0 comments on commit 4794d3f

Please sign in to comment.