Created
December 1, 2019 16:54
-
-
Save vinicius73/eaf4619d4c30ca12b8fb07d63a73894c to your computer and use it in GitHub Desktop.
Script de sorteio inimigo/amigo oculto
This file contains hidden or 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 { shuffle, map } = require('lodash') | |
const { sendMessages } = require('./send-email') | |
const input = shuffle(require('./input.json')) | |
function assign (array) { | |
return map(array, (person, index, array) => { | |
const friend = array[index + 1] || array[0] | |
return { | |
person, | |
friend | |
} | |
}) | |
} | |
sendMessages(assign(input)) | |
.then(result => { | |
console.log(result) | |
}) | |
.catch(err => { | |
console.error(err) | |
}) |
This file contains hidden or 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
[{ | |
"name": "Fulano 01", | |
"email": "[email protected]" | |
}, { | |
"name": "Fulano 02", | |
"email": "[email protected]" | |
}] |
This file contains hidden or 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 mailgun = require('mailgun-js') | |
const DOMAIN = '<--->' | |
const mg = mailgun({ apiKey: '<--->', domain: DOMAIN }) | |
const buildMessageData = drawData => { | |
const { person, friend } = drawData | |
return { | |
from: 'MailBô <postmaster@--->', | |
to: person.email, | |
subject: 'Inimigo Secreto - 2019', | |
text: `Olá! ${person.name}, seu inimigo oculto é ${friend.name}`, | |
html: ` | |
Olá! <b>${person.name}</b> seu inimigo oculto é <b>${friend.name}</b>` | |
} | |
} | |
const sendMessages = data => { | |
const messages = data.map(buildMessageData) | |
const promises = messages.map(data => { | |
return mg.messages().send(data) | |
}) | |
return Promise.all(promises) | |
} | |
module.exports = { sendMessages } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment