Last active
March 17, 2017 07:25
-
-
Save yellowred/4b3333243f61619044cc5646f19b9e69 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
/** | |
* @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