Created
May 16, 2016 14:21
-
-
Save steal9pro/f15d2af9cc8a341ec612e730de2a79de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var User = require('../models').User; | |
var Letter = require('../models').Email; | |
var tempMail = require('tempmail-wrapper/tempmail'); | |
var co = require('co'); | |
var wait = require('co-wait'); | |
var time = 1; | |
function getAllLetters(email) { | |
return tempMail.getInbox(email) | |
.then(function(letters) { | |
return letters; | |
}); | |
} | |
function deleteLetter(id) { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
console.log(Date.now()); | |
tempMail.deleteMail(id).then(function() { | |
console.log('deted mail'); | |
resolve(); | |
}, function(err) { | |
reject(err); | |
}); | |
}, 2000); | |
}); | |
} | |
function saveNewLetter(userId, letter) { | |
console.log('save mail'); | |
var newLetter = ''; | |
newLetter = Letter.build({ | |
mail_subject: letter.mail_subject, | |
mail_text: letter.mail_text, | |
mail_from: letter.mail_from, | |
mail_html: letter.mail_html, | |
user_id: userId | |
}); | |
return newLetter.save(); | |
} | |
function processScript() { | |
co(function*() { | |
var users = yield User.findAll(); | |
users.forEach(function(user) { | |
co(function*() { | |
var letters = yield getAllLetters(user.email); | |
if (letters.error) { | |
console.log(letters.error); | |
} else { | |
console.log(letters); | |
letters.forEach(function(letter) { | |
co(function*() { | |
yield saveNewLetter(user.id, letter); | |
}); | |
}); | |
letters.forEach(function(letter) { | |
co(function*() { | |
// yield wait(2000); | |
yield deleteLetter(letter.mail_id); | |
}); | |
}); | |
} | |
}); | |
}); | |
}); | |
} | |
processScript(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment