Skip to content

Commit

Permalink
Merge pull request #23 from wbruno/master
Browse files Browse the repository at this point in the history
Do not insert data from tomorrow and beyond
  • Loading branch information
rafaell-lycan committed Apr 18, 2015
2 parents a03bbd4 + 0e2b0b3 commit 729e604
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var express = require('express'),
router = express.Router(),
Promise = require('bluebird'),
debug = require('debug')('sabesp:routes'),
moment = require('moment'),
Mongo = require('./lib/Mongo'),
Helper = require('./lib/Helper'),
Sabesp = require('./lib/Sabesp'),
Expand Down Expand Up @@ -46,13 +47,13 @@ function _isCached (date) {
if (result) {
resolve(result);
} else {
Sabesp.fetch(date, token).then(function(data) {
if ((date !== '') && (data.date !== '') && (date !== Helper.today())) {
Mongo.insert('dams', data, function(err, result) {
Sabesp.fetch(date, token).then(function(result) {
if (_isValidDate(result, date)) {
Mongo.insert('dams', result, function(err, result) {
if (err) { debug('err', err); }
});
}
resolve(data);
resolve(result);
})
.catch(function(err) {
reject(err);
Expand All @@ -62,4 +63,8 @@ function _isCached (date) {
});
}

function _isValidDate(result, date) {
return (date !== '') && (moment(date) < moment(Helper.today())) && (result.date !== '');
}

module.exports = router;

0 comments on commit 729e604

Please sign in to comment.