Created
January 19, 2018 16:38
-
-
Save zacharyhill/fc8cd63a610d0695cf3398751b85a7c6 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
const bucket = require('./bucket'); | |
const configure = require('./config'); | |
const db = require('./db'); | |
const parse = require('./parseMail'); | |
let config; | |
const processNewMail = function(optionalCallback) { | |
if (config.Bucket === '') { | |
throw new Error('Bucket name must be set!'); | |
} | |
else { | |
const results = bucket.getKeys() | |
.then(bucket.getEmailsFromKeys) | |
.then(parse) | |
.then(db.save) | |
.then(bucket.removeFromBucket); | |
// process with promise | |
if (!optionalCallback) { | |
return results; | |
} | |
// process with callback | |
else { | |
results.then((data) => { | |
optionalCallback(null, data); | |
}).catch((err) => { | |
optionalCallback(err, null); | |
}); | |
} | |
} | |
}; | |
const configureOptions = function(options) { | |
config = configure(options); | |
bucket.setName(config.Bucket); | |
db.setDB(config.DB); | |
db.setSchema(config.MailSchema); | |
return config; | |
}; | |
module.exports = processNewMail; | |
module.exports.configure = configureOptions; | |
// TESTING CODE | |
configureOptions({ | |
Bucket: 'zhillb-mail', | |
}); | |
processNewMail() | |
.then(console.log) | |
.catch(console.log); | |
processNewMail((err, data) => { | |
if (!err) { | |
console.log('data: ' + data); | |
} | |
else { | |
console.log('error: ' + err); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment