|
| 1 | +/** |
| 2 | + * the skill services |
| 3 | + */ |
| 4 | + |
| 5 | +const joi = require('@hapi/joi') |
| 6 | +const _ = require('lodash') |
| 7 | + |
| 8 | +const errors = require('../../common/errors') |
| 9 | +const helper = require('../../common/helper') |
| 10 | +const dbHelper = require('../../common/db-helper') |
| 11 | +const sequelize = require('../../models/index') |
| 12 | + |
| 13 | +const TCSkill = sequelize.models.TCSkill |
| 14 | +const Taxonomy = sequelize.models.Taxonomy |
| 15 | + |
| 16 | +/** |
| 17 | + * get skill by id |
| 18 | + * @param id the skill id |
| 19 | + * @param params the path parameters |
| 20 | + * @param query the query parameters |
| 21 | + * @return the skill |
| 22 | + */ |
| 23 | +async function get (id, params, query = {}) { |
| 24 | + const trueParams = _.assign(params, query) |
| 25 | + |
| 26 | + const recordObj = await dbHelper.get(TCSkill, id) |
| 27 | + if (!recordObj) { |
| 28 | + throw errors.newEntityNotFoundError(`cannot find ${TCSkill.name} where ${_.map(trueParams, (v, k) => `${k}:${v}`).join(', ')}`) |
| 29 | + } |
| 30 | + const skill = recordObj.dataValues |
| 31 | + await populateTaxonomyNames(skill) |
| 32 | + |
| 33 | + return helper.omitAuditFields(skill) |
| 34 | +} |
| 35 | + |
| 36 | +get.schema = { |
| 37 | + id: joi.string().uuid().required(), |
| 38 | + params: joi.object() |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Populates the taxonomy name for each of the skill |
| 43 | + * @param skills individual skill or an array of skills |
| 44 | + * @returns the updated skills object |
| 45 | + */ |
| 46 | +async function populateTaxonomyNames (skills) { |
| 47 | + if (_.isArray(skills)) { |
| 48 | + const taxonomyMap = {} |
| 49 | + for (const skill of skills) { |
| 50 | + // dont populate if we already have the name |
| 51 | + if (skill.taxonomyName) { continue } |
| 52 | + |
| 53 | + if (!_.has(taxonomyMap, skill.taxonomyId)) { |
| 54 | + const taxonomy = await dbHelper.get(Taxonomy, skill.taxonomyId) |
| 55 | + taxonomyMap[skill.taxonomyId] = taxonomy.name |
| 56 | + } |
| 57 | + skill.taxonomyName = taxonomyMap[skill.taxonomyId] |
| 58 | + } |
| 59 | + } else { |
| 60 | + const taxonomy = await dbHelper.get(Taxonomy, skills.taxonomyId) |
| 61 | + skills.taxonomyName = taxonomy.name |
| 62 | + } |
| 63 | + |
| 64 | + return skills |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * search skills by query |
| 69 | + * @param query the search query |
| 70 | + * @return the results |
| 71 | + */ |
| 72 | +async function search (query) { |
| 73 | + let items = await dbHelper.find(TCSkill, query) |
| 74 | + |
| 75 | + items = items.map(item => item.dataValues) |
| 76 | + await populateTaxonomyNames(items) |
| 77 | + items = helper.omitAuditFields(items) |
| 78 | + return { fromDb: true, result: items, total: items.length } |
| 79 | +} |
| 80 | + |
| 81 | +search.schema = { |
| 82 | + query: { |
| 83 | + page: joi.page(), |
| 84 | + perPage: joi.pageSize(), |
| 85 | + taxonomyId: joi.string().uuid(), |
| 86 | + name: joi.string(), |
| 87 | + externalId: joi.string(), |
| 88 | + orderBy: joi.string() |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +module.exports = { |
| 93 | + search, |
| 94 | + get |
| 95 | +} |
0 commit comments