All files / common/models education.js

11.76% Statements 6/51
0% Branches 0/21
10% Functions 1/10
12.77% Lines 6/47

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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137    1x                                   1x 1x                                                                                                                     1x                                     1x                                             1x                            
'use strict';
 
const logger = require('../../services/logger');
 
function filterWithMinCount(suggestions, minCount) {
  return suggestions.reduce((validated, suggestion) => {
    let value = suggestion.Value;
    if (!value) return validated;
    if (validated.includes(value)) return validated;
 
    const resourceId = suggestion.ResourceId;
    const count = suggestions.reduce((count, tr) => {
      if (tr.Value === value && tr.ResourceId !== resourceId) return count + 1;
      return count;
    }, 0);
 
    return count > minCount ? [...validated, value] : validated;
  }, []);
}
 
module.exports = function(Education) {
  Education.educationSuggestions = async (type, language) => {
    // Language code should be 2 characters long. Anything longer
    // is a invalid parameter and possibly a SQL injection.
    if (language.length !== 2) {
      const error = new Error(`Invalid language parameter ${language}`);
      error.status = 400;
      throw error;
    }
 
    if (type !== 'school' && type !== 'certificate' && type !== 'course') {
      const error = new Error(`Invalid type parameter ${type}`);
      error.status = 400;
      throw error;
    }
 
    try {
      const educationItems = await Education.app.models.education.find({
        include: {
          relation: 'educationTranslations',
          scope: {
            where: {LanguageId: language}
          }
        },
        where: {Type: type}
      });
 
      const validEducationItems = educationItems.filter(education => {
        const educationJSON = education.toJSON();
        return educationJSON.educationTranslations.length > 0;
      });
 
      const organizers = validEducationItems.map(education => {
        const educationJSON = education.toJSON();
        const translation = educationJSON.educationTranslations[0];
        return {
          ResourceId: education.ResourceId,
          Value: translation.Organizer
        };
      });
 
      const achievements = validEducationItems.map(education => {
        const educationJSON = education.toJSON();
        const translation = educationJSON.educationTranslations[0];
        return {
          ResourceId: education.ResourceId,
          Value: translation.Achievement
        };
      });
 
      return {
        organizers: filterWithMinCount(organizers, 2),
        achievements: filterWithMinCount(achievements, 2)
      };
    } catch (e) {
      logger.error(`Error getting education suggestions: ${e}`);
      throw e;
    }
  };
 
  Education.remoteMethod('educationSuggestions', {
    http: {verb: 'get'},
    accepts: [
      {
        arg: 'type',
        type: 'string',
        required: true
      },
      {
        arg: 'language',
        type: 'string',
        required: true
      }
    ],
    returns: {
      arg: 'suggestions',
      type: 'object'
    }
  });
  Education.getEducation = function(resourceId, cb) {
    if (!resourceId)
      throw new Error('ResourceId is required!');
 
    try{
      Education.find(
        {
          include: 'educationTranslations',
          where: {
            ResourceId: resourceId
          }
        },
        function(err, education) {
          if (err) return cb(err);
          return cb(null, education);
        }
      );
    } catch(e) {
      logger.error(`Error getting education: ${e}`);
      throw e;
    }
  };
 
  Education.remoteMethod('getEducation', {
    http: {verb: 'get'},
    accepts: [
      {
        arg: 'resourceId',
        type: 'number'
      }
    ],
    returns: {
      arg: 'result',
      type: 'object'
    }
  });
};