<commit_before>var mongoose = require('mongoose');
var async = require('async');
var config = require('meanio').loadConfig();
var ret = { };
var app;

ret.createPost = function(data) {
  var socket = this;
  var user = socket.user;

  var Post = mongoose.model('Post');
  
  var post = new Post({
    poster : user._id,
    amount : data.amount,
    interest : data.interest,
    monthly_bill : data.monthly_bill,
    estimated_completion : 0, //TODO Save
    desired_rating : data.desired_rating,
    lender : user.user_role
  });
  post.save(function(err) {
    if (err) {
      socket.emit('createPost', false);
    } else {
      socket.emit('createPost', true);
    }
  });
};

ret.takeLoan = function(data) {
  var socket = this;
  var user = socket.user;

  var postId = data.postId;

  var Loan = mongoose.model('Loan');
  var Post = mongoose.model('Post');

  async.waterfall([
    function(d) {
      //Load post object
      Post.findOne({
        _id : postId
      }).exec(function(err, post) {
        d(err, post);
      });
    },
    function(post, d) {
      var lender;
      var borrower;
      
      if (post.role === 'Lender') {
        lender = post.poster;
        borrower = user._id;
      } else {
        lender = user._id;
        borrower = post.poster;
      }

      var loan = new Load({
        lender : lender,
        borrower : borrower,
        monthly_fee : post.monthly_fee,
        amount_left : post.amount,
        monthly_fee_left_to_pay_this_month : post.monthly_fee,
        pay_day : (new Date()).getDay(),
        missed_last_payment : false,
        estimated_time_left: 0 //TODO use func
      });
      loan.save(function(err) {
        d(err, loan);
      });
    },
    function(loan) {
      Post.remove({
        _id : postId
      }).exec(c);
    }
  ], function(err, result) {
    if (err) {
      console.error(err);
      socket.emit('takeLoan', false);
    } else {
      socket.emit('takeLoan', true);
    }
  });
};

ret.query = function(data) {
  var socket = this;
  
  var Post = mongoose.model('Post');
  Post.find({
    amount : {
      $gte : data.min_amount,
      $lte : data.max_amount
    },
    interest : {
      $gte : data.min_interest,
      $lte : data.max_amount
    },
    monthly_bill : {
      $gte : data.min_monthly,
      $lte : data.max_monthly
    }
  }).limit(20).exec(function(err, posts) {
    if (err) {
      console.error(err);
    }
    socket.emit('query', posts);
  });
};

ret.loadPosts = function(data) {
  var socket = this;

  var Post = mongoose.model('Post');
  Post.find({}).exec(function(err, posts) {
    socket.emit('loadPosts', posts);
  });
};
<<<<<<< HEAD

=======
/*i
>>>>>>> 8ded52e9bab5d9c00cb0670c4be795aefc98ffc4
// get open loans
ret.openLoans = function() {
  var socket = this;
  var user = socket.user;
  var Loan = mongoose.model('Loan');
  Loan.find({
    $and: [{
      state : 'Open'<commit_msg>stop merging conflicts with auth.js<commit_after>var mongoose = require('mongoose');
var async = require('async');
var config = require('meanio').loadConfig();
var ret = { };
var app;

ret.createPost = function(data) {
  var socket = this;
  var user = socket.user;

  var Post = mongoose.model('Post');
  
  var post = new Post({
    poster : user._id,
    amount : data.amount,
    interest : data.interest,
    monthly_bill : data.monthly_bill,
    estimated_completion : 0, //TODO Save
    desired_rating : data.desired_rating,
    lender : user.user_role
  });
  post.save(function(err) {
    if (err) {
      socket.emit('createPost', false);
    } else {
      socket.emit('createPost', true);
    }
  });
};

ret.takeLoan = function(data) {
  var socket = this;
  var user = socket.user;

  var postId = data.postId;

  var Loan = mongoose.model('Loan');
  var Post = mongoose.model('Post');

  async.waterfall([
    function(d) {
      //Load post object
      Post.findOne({
        _id : postId
      }).exec(function(err, post) {
        d(err, post);
      });
    },
    function(post, d) {
      var lender;
      var borrower;
      
      if (post.role === 'Lender') {
        lender = post.poster;
        borrower = user._id;
      } else {
        lender = user._id;
        borrower = post.poster;
      }

      var loan = new Load({
        lender : lender,
        borrower : borrower,
        monthly_fee : post.monthly_fee,
        amount_left : post.amount,
        monthly_fee_left_to_pay_this_month : post.monthly_fee,
        pay_day : (new Date()).getDay(),
        missed_last_payment : false,
        estimated_time_left: 0 //TODO use func
      });
      loan.save(function(err) {
        d(err, loan);
      });
    },
    function(loan) {
      Post.remove({
        _id : postId
      }).exec(c);
    }
  ], function(err, result) {
    if (err) {
      console.error(err);
      socket.emit('takeLoan', false);
    } else {
      socket.emit('takeLoan', true);
    }
  });
};

ret.query = function(data) {
  var socket = this;
  
  var Post = mongoose.model('Post');
  Post.find({
    amount : {
      $gte : data.min_amount,
      $lte : data.max_amount
    },
    interest : {
      $gte : data.min_interest,
      $lte : data.max_amount
    },
    monthly_bill : {
      $gte : data.min_monthly,
      $lte : data.max_monthly
    }
  }).limit(20).exec(function(err, posts) {
    if (err) {
      console.error(err);
    }
    socket.emit('query', posts);
  });
};

ret.loadPosts = function(data) {
  var socket = this;

  var Post = mongoose.model('Post');
  Post.find({}).exec(function(err, posts) {
    socket.emit('loadPosts', posts);
  });
};

// get open loans
ret.openLoans = function() {
  var socket = this;
  var user = socket.user;
  var Loan = mongoose.model('Loan');
  Loan.find({
    $and: [{
      state : 'Open'
{'fraction_non_alphanumeric': 0.15318118948824344, 'fraction_numerical': 0.00466804979253112, 'mean_word_length': 2.2850653038046564, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '736058', 'n_tokens_mistral': 2030, 'n_tokens_neox': 2024, 'n_words': 410}