<commit_before>var mongoose = require('mongoose');
var ScholarshipModel = require('./models/Scholarship');
var InactiveScholarshipModel = require('./models/InactiveScholarship');


// Connect to DB
mongoose.connect('mongodb://localhost/scholarshipsIST');
var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function () {
  console.log("Connected Successfully to DB");
});


// Module methods
var getScholarshipByID = function (id) {
  ScholarshipModel.findOne({ id: scholarship.id }, function (scholarship) {
    return scholarship;
  });
};


var getAllScholarships = function (callback) {
  ScholarshipModel.find({}, function (err, allDocs) {
    if(err) console.log(err);
    callback(allDocs);
  });
};


var moveInactiveScholarship = function(scholarship) {

    console.log("Move inactive scholarship!");

    var releaseDate = scholarship.releaseDate.split('.');
    var closeDate = scholarship.closeDate.split('.');

    var inactiveScholarship  = new InactiveScholarshipModel({
        slots: parseInt(scholarship.slots, 10),
        type: scholarship.type,
        holder: scholarship.holder,
        link: scholarship.link,
        id: scholarship.id,
        field: scholarship.field,
        releaseDate: new Date(releaseDate[2], parseInt(releaseDate[1],10)-1, releaseDate[0]),
        closeDate: new Date(closeDate[2], parseInt(closeDate[1],10)-1, closeDate[0])
    });

    ScholarshipModel.findOne({ id: scholarship.id}, function(err, scholarship) {
        inactiveScholarship.save(function(err, scholarship) {
            if(err)
                console.log(err);
            else
                ScholarshipModel.where('id').equals(scholarship.id).remove(function(err,info) {
                    if(err)
                        console.log("Remove not sucessful!");
                });
        });
    });
};

var saveScholarship = function (scholarship) {
  ScholarshipModel.findOne({ id: scholarship.id }, function (err, isThere) {

    var releaseDate = scholarship.releaseDate.split('.');
    var closeDate = scholarship.closeDate.split('.');

    console.log("");
    console.log(scholarship.id);

    if (!isThere){

    console.log("Dont exist!");

    var scholarshipToMongo = new ScholarshipModel({
        slots: parseInt(scholarship.slots, 10),
        type: scholarship.type,
        holder: scholarship.holder,
        link: scholarship.link,
        id: scholarship.id,
        field: scholarship.field,
        releaseDate: new Date(releaseDate[2], parseInt(releaseDate[1],10)-1, releaseDate[0]),
        closeDate: new Date(closeDate[2], parseInt(closeDate[1],10)-1, closeDate[0])
    });

    scholarshipToMongo.save(function (err, scholarship){
        if(err)
            console.log(err);
    });

    }else{
        console.log("Already exists!");
        console.log(new Date());
        console.log(new Date(closeDate[2], closeDate[1], closeDate[0]));

        if(new Date(closeDate[2], parseInt(closeDate[1],10)-1, closeDate[0]) < new Date()) {
            console.log("Inactive Scholarship!");
            moveInactiveScholarship(scholarship);
        }
    }
  });
};

//just one note: var date =new Date(79,5,24) YY,MM,DD

module.exports = {
  getScholarshipByID : getScholarshipByID,
  getAllScholarships : getAllScholarships,
  saveScholarship : saveScholarship,
  moveInactiveScholarship: moveInactiveScholarship
};


<commit_msg>Modularized removeInactiveScholarships function. The function now should be called by the scheduler and not in fetch time.<commit_after>var mongoose = require('mongoose');
var ScholarshipModel = require('./models/Scholarship');
var InactiveScholarshipModel = require('./models/InactiveScholarship');


// Connect to DB
mongoose.connect('mongodb://localhost/scholarshipsIST');
var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function () {
  console.log("Connected Successfully to DB");
});


// Module methods
var getScholarshipByID = function (id) {
  ScholarshipModel.findOne({ id: scholarship.id }, function (scholarship) {
    return scholarship;
  });
};


var getAllScholarships = function (callback) {
  ScholarshipModel.find({}, function (err, allDocs) {
    if(err) console.log(err);
    callback(allDocs);
  });
};


var removeInactiveScholarships = function() {

    ScholarshipModel.find(function(err, scholarships){
        scholarships.forEach(function(scholarship) {
            if(scholarship.closeDate < new Date()){

                ScholarshipModel.remove({ id: scholarship.id}, function(err, scholar) {
                    if(err) console.log(err);
                });

                new InactiveScholarshipModel(scholarship).save(function(err, scholar) {
                    if(err) console.log(err);
                });
            }
        });
    });
};

var saveScholarship = function (scholarship) {
    ScholarshipModel.findOne({ id: scholarship.id }, function (err, isThere) {

        if (!isThere){

            var releaseDate = scholarship.releaseDate.split('.');
            var closeDate = scholarship.closeDate.split('.');

            var scholarshipToMongo = new ScholarshipModel({
                slots: parseInt(scholarship.slots, 10),
                type: scholarship.type,
                holder: scholarship.holder,
                link: scholarship.link,
                id: scholarship.id,
                field: scholarship.field,
                releaseDate: new Date(releaseDate[2], parseInt(releaseDate[1],10)-1, releaseDate[0]),
                closeDate: new Date(closeDate[2], parseInt(closeDate[1],10)-1, closeDate[0])
            });

            scholarshipToMongo.save(function (err, scholarship){
                if(err)
                    console.log(err);
            });
        }
    });
};

//just one note: var date =new Date(79,5,24) YY,MM,DD

module.exports = {
  getScholarshipByID: getScholarshipByID,
  getAllScholarships: getAllScholarships,
  saveScholarship: saveScholarship,
  removeInactiveScholarships: removeInactiveScholarships
};



{'fraction_non_alphanumeric': 0.13319169798986763, 'fraction_numerical': 0.009968949174701749, 'mean_word_length': 3.005235602094241, 'pattern_counts': {'":': 0, '<': 5, '<?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': '304139', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1786, 'n_words': 347}