All files / ragchat-api/utils handleSchemes.js

100% Statements 67/67
100% Branches 10/10
100% Functions 4/4
100% Lines 67/67

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 681x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 42x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 6x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 50x 13x 13x 4x 4x 4x  
 
/**
 * get confidential fields from given schema based on attribute select: false
 *
 * @param   {model}  model  model to compute
 *
 * @return  {array}         matching field names 
 */
export const getConfidentialFields = (model) => {
  performance.mark('getConfidentialFields:start');
  const schema = Object.entries(model.schema.paths);
  const confidentialFields = schema.filter(function (field) {
    return field[1].selected === false;
  });
  performance.mark('getConfidentialFields:end');
  return confidentialFields.map(field => field[0]);
};
 
 
/**
 * remove fields from given object that are stated as confidential
 *
 * @param   {model}  model   model to compute
 * @param   {object}  object  object to compute
 *
 * @return  {object}          cleansed object
 */
export const hideConfidentialFields = (model, object) => {
  performance.mark('hideConfidentialFields:start');
  const confidentialFields = getConfidentialFields(model);
  // delete from object
  confidentialFields.forEach(field => {
    delete object[field];
  });
  performance.mark('hideConfidentialFields:end');
  return object;
};
 
/**
 * get array of all field names from a given model
 *
 * @param   {model}  model  model to compute
 *
 * @return  {array}         all found field names
 */
const getAllFieldnames = (model) => {
  return Object.keys(model.schema.paths);
};
 
/**
 * get an object with key:value pairs
 * wipe out all fields, not corresponding with given model
 *
 * @param   {model}  model  model to compute
 * @param   {object}  object  object, contains key:value
 *
 * @return  {object}          object with key:values only for fields, found in schema
 */
export const prefillDocumentObject = (model, object) => {
  const allowedFields = getAllFieldnames(model);
  let result = Object.fromEntries(allowedFields.map((field) => {
    if (Object.hasOwnProperty.bind(object)(field)) {
      return [field, object[field]];
    }
  }).filter(field => field));
  return result;
};