Skip to content

Commit

Permalink
Moved findTerm and newTerm into handle-terms
Browse files Browse the repository at this point in the history
  • Loading branch information
runely committed Apr 3, 2022
1 parent 32803a8 commit 0632dbc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
32 changes: 4 additions & 28 deletions UpdateTerms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,9 @@ const { uniqueValues } = require('@vtfk/utilities')
const withTokenAuth = require('../lib/with-token-auth')
const getGraphToken = require('../lib/graph/get-graph-token')
const { TERMSTORE: { setId } } = require('../config')
const { getTermSetTerms, addTermSetTerm, addTermSetTermChildTerm } = require('../lib/termstore/handle-terms')
const { getTermSetTerms, addTermSetTerm, addTermSetTermChildTerm, findTermByLabel, newTerm } = require('../lib/termstore/handle-terms')
const HTTPError = require('../lib/http-error')

const findTerm = (terms, name) => terms.find(term => term.labels && term.labels.filter(label => label.name === name).length > 0)

const newTerm = (name, description, languageTag = 'en-US') => {
const term = {
labels: [
{
languageTag,
name,
isDefault: true
}
]
}
if (description) {
term.descriptions = [
{
description,
languageTag
}
]
}

return term
}

const updateTermStore = async (context, req) => {
try {
const sectors = req.body
Expand All @@ -54,7 +30,7 @@ const updateTermStore = async (context, req) => {
for (const sectorId of sectorIds) {
// handle sector
const sector = sectors.find(item => item.SektorId === sectorId)
let sectorTerm = findTerm(termSetTerms, sectorId)
let sectorTerm = findTermByLabel(termSetTerms, sectorId)
if (sectorTerm) {
logger('debug', ['sector', sector.Sektor, sectorTerm.id])
outputLog.push({
Expand Down Expand Up @@ -120,7 +96,7 @@ const updateTermStore = async (context, req) => {
}

// get sectionTerm
let sectionTerm = findTerm(termSetTerms, section.Seksjon)
let sectionTerm = findTermByLabel(termSetTerms, section.Seksjon)
if (sectionTerm) {
logger('debug', ['section', section.Seksjon, sectionTerm.id])
outputLog.push({
Expand Down Expand Up @@ -184,7 +160,7 @@ const updateTermStore = async (context, req) => {
for (const team of teams) {
// handle teams

let teamTerm = findTerm(termSetTerms, team.Team)
let teamTerm = findTermByLabel(termSetTerms, team.Team)
if (teamTerm) {
logger('debug', ['team', team.Team, teamTerm.id])
outputLog.push({
Expand Down
43 changes: 42 additions & 1 deletion lib/termstore/handle-terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,45 @@ const addTermSetTermChildTerm = async (token, setId, termId, payload) => {
return data
}

/**
* Create a new term object to pass to {@link addTermSetTerm} or {@link addTermSetTermChildTerm}
* @param {String} name Label name for the new term
* @param {String} [description] Description for the new term
* @param {String} [languageTag] ISO language code for the new term (default is 'en-US')
* @returns {Object} New term object to pass to {@link addTermSetTerm} or {@link addTermSetTermChildTerm}
*/
const newTerm = (name, description, languageTag = 'en-US') => {
const term = {
labels: [
{
languageTag,
name,
isDefault: true
}
]
}
if (description) {
term.descriptions = [
{
description,
languageTag
}
]
}

return term
}

/* ################################## FINDERS ################################## */

/**
* Find term object by label name
* @param {Array} terms Array of term objects found by {@link getTermSetTerms}
* @param {String} name Label name of the term to find
* @returns {Object} Term object or undefined
*/
const findTermByLabel = (terms, name) => terms.find(term => term.labels && term.labels.filter(label => label.name === name).length > 0)

module.exports = {
getTermStore,
getTermGroups,
Expand All @@ -119,5 +158,7 @@ module.exports = {
getTermSetTerms,
getTermSetTermChildren,
addTermSetTerm,
addTermSetTermChildTerm
addTermSetTermChildTerm,
findTermByLabel,
newTerm
}

0 comments on commit 0632dbc

Please sign in to comment.