<commit_before>Resources = new Mongo.Collection('resources');

Resources.attachSchema(new SimpleSchema({
  title: {
    type: String,
    label: 'Title',
    max: 200
  },
  reference: {
    type: String,
    label: 'Reference',
    max: 200,
    unique: true
  },
  group: {
    type: String,
    label: 'Editor',
    max: 200
  },
  editor: {
    type: String,
    label: 'Editor',
    max: 200
  },
  quantity: {
    type: Number,
    label: 'Quantity',
    defaultValue: 0,
    optional: true
  },
  subject: {
    type: String,
    label: 'Subject',
    max: 200
  }
}));

Resources.allow({
  update: function(userId) {
    if (userId) {
      return true;
    }
  }
});

Resources.importFromCsv = function(csvFile) {
  var resourceId;
  var lines = [];

  CSV.readCsvFileLineByLine(csvFile, {
    headers: true
  }, function(line) {
    lines.push(line);
  });

  schoolNames(lines[0]).forEach(function(school) {
    log.info('write school: ' + school);
    if (!Schools.findOne({name: school})) {
      Schools.insert({
        name: school
      });
    }
  });

  lines.forEach(function(line) {
    log.info('processing:' + line);
    var resource = Resources.findOne({reference: line.Reference});
    if (!resource) {
      resourceId = Resources.insert({
        title:     line.Title,
        reference: line.Reference,
        editor:    line.Editor,
        subject:   line.Subject,
        group:     line.group
      });
    } else {
      resourceId = resource._id;
    }

    schoolNames(lines[0]).forEach(function(school) {
      var courseName = school + '-' + line.Year;
      if ( line[school] === 'x' ) {
        log.info('school' + school);
        Courses.upsert({
          name: courseName
        }, {
          $setOnInsert: {
            name: courseName,
            year: line.Year,
            schoolId: Schools.findOne({name: school})._id,
            resources: [resourceId]
          }, 
          $addToSet: {
            resources: resourceId
          }
        });
      } else {
      
      }
    });
  });
};

<commit_msg>Removes resources from courses if no more x<commit_after>Resources = new Mongo.Collection('resources');

Resources.attachSchema(new SimpleSchema({
  title: {
    type: String,
    label: 'Title',
    max: 200
  },
  reference: {
    type: String,
    label: 'Reference',
    max: 200,
    unique: true
  },
  group: {
    type: String,
    label: 'Editor',
    max: 200
  },
  editor: {
    type: String,
    label: 'Editor',
    max: 200
  },
  quantity: {
    type: Number,
    label: 'Quantity',
    defaultValue: 0,
    optional: true
  },
  subject: {
    type: String,
    label: 'Subject',
    max: 200
  }
}));

Resources.allow({
  update: function(userId) {
    if (userId) {
      return true;
    }
  }
});

Resources.importFromCsv = function(csvFile) {
  var resourceId;
  var lines = [];

  CSV.readCsvFileLineByLine(csvFile, {
    headers: true
  }, function(line) {
    lines.push(line);
  });

  schoolNames(lines[0]).forEach(function(school) {
    log.info('write school: ' + school);
    if (!Schools.findOne({name: school})) {
      Schools.insert({
        name: school
      });
    }
  });

  lines.forEach(function(line) {
    log.info('processing:' + line);
    var resource = Resources.findOne({reference: line.Reference});
    if (!resource) {
      resourceId = Resources.insert({
        title:     line.Title,
        reference: line.Reference,
        editor:    line.Editor,
        subject:   line.Subject,
        group:     line.group
      });
    } else {
      resourceId = resource._id;
    }

    schoolNames(lines[0]).forEach(function(school) {
      var courseName = school + '-' + line.Year;
      if ( line[school] === 'x' ) {
        log.info('school' + school);
        Courses.upsert({
          name: courseName
        }, {
          $setOnInsert: {
            name: courseName,
            year: line.Year,
            schoolId: Schools.findOne({name: school})._id,
            resources: [resourceId]
          }, 
          $addToSet: {
            resources: resourceId
          }
        });
      } else {
        Courses.update({
          name: courseName,
        }, {
          $pull: {
            resources: resourceId
          }
        });
      }
    });
  });
};


{'fraction_non_alphanumeric': 0.14478584729981378, 'fraction_numerical': 0.008379888268156424, 'mean_word_length': 1.9573296627666896, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4006167', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1345, 'n_words': 273}