Skip to content

Instantly share code, notes, and snippets.

@yellowred
Last active March 17, 2017 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yellowred/4b3333243f61619044cc5646f19b9e69 to your computer and use it in GitHub Desktop.
Save yellowred/4b3333243f61619044cc5646f19b9e69 to your computer and use it in GitHub Desktop.
/**
* @author Oleg Kubrakov yellowred.github.com
* @since 2017
**/
const Promise = require('Bluebird')
const jwt = require('jsonwebtoken');
let generateOneTimeToken = (value) => {
// we use promise to stub a request to MongoDB
let token = jwt.sign({ value }, 'privkey');
return Promise.resolve({value, token});
}
let data = [
{
name: "Michelle Rung",
contacts: [
{ phone: "7281681726" },
{ emailAddress: "michelle@gmail.com" },
{ emailAddress: "michelle@gmail.com" },
{ emailAddress: "" }
]
},
{
name: "Johann Sigh",
contacts: [
{ phone: "7812787-2667" },
{ emailAddress: "johann@gmail.com" },
{ emailAddress: "johann-md@gmail.com" }
]
}
]
let generateResponse = (data) => {
return Promise.coroutine(function* () {
let electronicContacts = []
let tmpArray = []
for (let dataValue of data) {
for (let individualValue of dataValue.contacts) {
if (individualValue.emailAddress && tmpArray.indexOf(individualValue.emailAddress) < 0) {
let tokenObject = yield generateOneTimeToken(individualValue.emailAddress)
electronicContacts.push({
electronicAddress: individualValue.emailAddress,
token: tokenObject.token
})
tmpArray.push(individualValue.emailAddress)
}
} // end for (let individualValue of dataValue.individualAddress)
}
return electronicContacts
})()
}
generateResponse(data).then(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment