All files / common/models skill.js

18.75% Statements 6/32
0% Branches 0/14
14.29% Functions 1/7
23.08% Lines 6/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83    1x   1x 1x                                                                       1x                       1x                             1x                            
'use strict';
 
const logger = require('../../services/logger');
 
module.exports = function(Skill) {
  Skill.skillSuggestions = async language => {
    try {
      const skillTranslations = await Skill.app.models.skillTranslations.find({
        include: 'skill',
        where: {LanguageId: language}
      });
 
      const skillTranslationsJSON = skillTranslations.map(tr => tr.toJSON());
 
      const validSuggestions = skillTranslationsJSON.reduce((validSkills, skillTranslation) => {
        const name = skillTranslation.Name;
        if (validSkills.includes(name)) return validSkills;
 
        const skill = skillTranslation.skill;
        if (!skill) return validSkills;
 
        const resourceId = skillTranslation.skill.ResourceId;
 
        const skillCount = skillTranslationsJSON.reduce((count, tr) => {
          if (!tr.skill) return count;
          if (tr.Name === name && tr.skill.ResourceId !== resourceId) return count + 1;
 
          return count;
        }, 0);
 
        return skillCount > 1 ? [...validSkills, name] : validSkills;
      }, []);
 
      return validSuggestions;
    } catch (error) {
      logger.error(`Error getting skill suggestions, ${error}`);
    }
 
    return [];
  };
 
  Skill.remoteMethod('skillSuggestions', {
    http: {verb: 'get'},
    accepts: {
      arg: 'language',
      type: 'string'
    },
    returns: {
      arg: 'skills',
      type: 'array'
    }
  });
 
  Skill.getSkills = function(resourceId, cb) {
    Skill.find(
      {
        include: 'skillTranslations',
        where: {
          ResourceId: resourceId
        }
      },
      function(err, result) {
        if (err) return cb(err);
        return cb(null, result);
      }
    );
  };
 
  Skill.remoteMethod('getSkills', {
    http: {verb: 'get'},
    accepts: [
      {
        arg: 'resourceId',
        type: 'number'
      }
    ],
    returns: {
      arg: 'result',
      type: 'object'
    }
  });
};