Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Removed unused variables, methods, simplified structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ricalo committed Jul 14, 2016
1 parent c13b079 commit 3e5c1e5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 72 deletions.
32 changes: 12 additions & 20 deletions requestUtil.js
Expand Up @@ -5,7 +5,7 @@
var https = require('https');

/**
* Generates a GET request to the specified host
* Generates a GET request the user endpoint
* @param {string} accessToken the access token with which the request should be authenticated
* @param {callback} callback
*/
Expand Down Expand Up @@ -43,54 +43,46 @@ function getUserData(accessToken, callback) {
}

/**
* Generates a POST request (of Content-type ```application/json```)
* @param {string} host the host to whom this request will be sent
* @param {string} path the path, relative to the host, to which this request will be sent
* Generates a POST request to the SendMail endpoint
* @param {string} accessToken the access token with which the request should be authenticated
* @param {string} data the data which will be 'POST'ed
* @param {callback} callback
*/
function postData(host, path, accessToken, data, callback) {
function postSendMail(accessToken, mailBody, callback) {
var outHeaders = {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + accessToken,
'Content-Length': data.length
'Content-Length': mailBody.length
};
var options = {
host: host,
path: path,
host: 'graph.microsoft.com',
path: '/v1.0/me/microsoft.graph.sendMail',
method: 'POST',
headers: outHeaders
};

// Set up the request
var post = https.request(options, function (response) {
if(response.statusCode === 202) {
response.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
response.on('end', function () {
callback(null, response);
});
callback(null);
} else {
// We consider everything else an error, even if statusCode is not greater than 400.
// We expect statusCode === 202 in this call, nothing else.
var error = new Error();
error.code = response.statusCode;
error.message = response.statusMessage;
callback(error, null);
error.innerError = JSON.parse(body).error;
callback(error);
}
});

// write the outbound data to it
post.write(data);
post.write(mailBody);
// we're done!
post.end();

post.on('error', function (e) {
callback(e, null);
callback(e);
});
}

exports.getUserData = getUserData;
exports.postData = postData;
exports.postSendMail = postSendMail;
59 changes: 15 additions & 44 deletions routes/index.js
Expand Up @@ -84,53 +84,24 @@ function renderSendMail(req, res) {

router.post('/', function (req, res) {
var destinationEmailAddress = req.body.default_email;
wrapRequestAsCallback(req.cookies.REFRESH_TOKEN_CACHE_KEY, {

onSuccess: function (accessToken) {
// send the mail with a callback and report back that page...
var postBody = emailer.generatePostBody(
req.session.user.displayName,
destinationEmailAddress
);
requestUtil.postData(
'graph.microsoft.com',
'/v1.0/me/microsoft.graph.sendMail',
accessToken,
JSON.stringify(postBody),
function (e, response) {
var templateData = {
display_name: req.session.user.displayName,
user_principal_name: req.session.user.userPrincipalName,
actual_recipient: destinationEmailAddress
};
if (response.statusCode >= 400) {
templateData.status_code = response.statusCode;
}
res.render('sendMail', templateData);
});
},

onFailure: function (err) {
res.status(err.code);
console.log(err.message);
res.send();
var mailBody = emailer.generateMailBody(
req.session.user.displayName,
destinationEmailAddress
);
requestUtil.postSendMail(
req.cookies.ACCESS_TOKEN_CACHE_KEY,
JSON.stringify(mailBody),
function (e) {
var templateData = {
display_name: req.session.user.displayName,
user_principal_name: req.session.user.userPrincipalName,
actual_recipient: destinationEmailAddress
};
res.render('sendMail', templateData);
}
});
);
});

function wrapRequestAsCallback(refreshToken, callback) {
authHelper.getTokenFromRefreshToken(refreshToken, function (e, results) {
if (results !== null) {
callback.onSuccess(results);
} else {
callback.onFailure({
code: 500,
message: 'An unexpected error was encountered acquiring access token from refresh token'
});
}
});
}

function hasAccessTokenExpired(e) {
if(!e.innerError) {
return false;
Expand Down
10 changes: 2 additions & 8 deletions views/sendMail.hbs
Expand Up @@ -19,16 +19,10 @@
<span class="ms-Button-label">Send mail</span>
</button>

{{#if status_code}}
{{#if actual_recipient}}
<div>
<p class="ms-font-m ms-fontColor-redDark">Something went wrong, couldn't send an email ({{status_code}})</p>
<p class="ms-font-m ms-fontColor-green">Successfully sent an email to {{actual_recipient}}!</p>
</div>
{{else}}
{{#if actual_recipient}}
<div>
<p class="ms-font-m ms-fontColor-green">Successfully sent an email to {{actual_recipient}}!</p>
</div>
{{/if}}
{{/if}}

</div>
Expand Down

0 comments on commit 3e5c1e5

Please sign in to comment.