freeCodeCamp/api-server/server/boot/randomAPIs.js

207 lines
5.6 KiB
JavaScript
Raw Normal View History

import request from 'request';
import { homeLocation } from '../../../config/env';
import constantStrings from '../utils/constantStrings.json';
const githubClient = process.env.GITHUB_ID;
const githubSecret = process.env.GITHUB_SECRET;
module.exports = function(app) {
const router = app.loopback.Router();
const api = app.loopback.Router();
const User = app.models.User;
2018-05-15 05:12:05 +00:00
router.get('/api/github', githubCalls);
router.get('/u/:email', unsubscribeDeprecated);
router.get('/unsubscribe/:email', unsubscribeDeprecated);
2018-08-02 10:34:35 +00:00
router.get('/ue/:unsubscribeId', unsubscribeById);
2018-05-15 05:12:05 +00:00
router.get(
2016-06-17 19:35:10 +00:00
'/the-fastest-web-page-on-the-internet',
theFastestWebPageOnTheInternet
);
2018-08-02 10:34:35 +00:00
router.get('/unsubscribed/:unsubscribeId', unsubscribedWithId);
2016-06-17 19:35:10 +00:00
router.get('/unsubscribed', unsubscribed);
api.get('/resubscribe/:unsubscribeId', resubscribe);
router.get('/nonprofits', nonprofits);
2016-06-17 19:35:10 +00:00
router.get('/coding-bootcamp-cost-calculator', bootcampCalculator);
2018-05-15 05:12:05 +00:00
app.use(router);
app.use('/internal', api);
function theFastestWebPageOnTheInternet(req, res) {
res.render('resources/the-fastest-web-page-on-the-internet', {
title: 'This is the fastest web page on the internet'
});
}
function bootcampCalculator(req, res) {
res.render('resources/calculator', {
title: 'Coding Bootcamp Cost Calculator'
});
}
function nonprofits(req, res) {
res.render('resources/nonprofits', {
2015-10-31 09:59:09 +00:00
title: 'Your Nonprofit Can Get Pro Bono Code'
});
}
function unsubscribeDeprecated(req, res) {
req.flash(
'info',
'We are no longer able to process this unsubscription request. ' +
'Please go to your settings to update your email preferences'
);
res.redirectWithFlash(homeLocation);
}
2018-08-02 10:34:35 +00:00
function unsubscribeById(req, res, next) {
const { unsubscribeId } = req.params;
2018-08-07 13:31:26 +00:00
if (!unsubscribeId) {
req.flash('info', 'We could not find an account to unsubscribe');
return res.redirectWithFlash(homeLocation);
2018-08-07 13:31:26 +00:00
}
2018-08-02 10:34:35 +00:00
return User.find({ where: { unsubscribeId } }, (err, users) => {
if (err || !users.length) {
req.flash('info', 'We could not find an account to unsubscribe');
return res.redirectWithFlash(homeLocation);
2018-08-02 10:34:35 +00:00
}
2018-08-07 13:31:26 +00:00
const updates = users.map(user => {
return new Promise((resolve, reject) =>
user.updateAttributes(
{
sendQuincyEmail: false
},
err => {
if (err) {
reject(err);
} else {
resolve();
}
2018-08-07 13:31:26 +00:00
}
)
2018-08-07 13:31:26 +00:00
);
});
return Promise.all(updates)
.then(() => {
req.flash(
'success',
"We've successfully updated your email preferences."
);
return res.redirectWithFlash(
`${homeLocation}/unsubscribed/${unsubscribeId}`
);
2018-08-07 13:31:26 +00:00
})
.catch(next);
2018-08-02 10:34:35 +00:00
});
}
function unsubscribed(req, res) {
res.render('resources/unsubscribed', {
title: 'You have been unsubscribed'
});
}
2018-08-02 10:34:35 +00:00
function unsubscribedWithId(req, res) {
const { unsubscribeId } = req.params;
return res.render('resources/unsubscribed', {
title: 'You have been unsubscribed',
unsubscribeId
});
}
function resubscribe(req, res, next) {
const { unsubscribeId } = req.params;
if (!unsubscribeId) {
req.flash(
'info',
'We we unable to process this request, please check and try againÍ'
);
res.redirect(homeLocation);
}
return User.find({ where: { unsubscribeId } }, (err, users) => {
if (err || !users.length) {
req.flash('info', 'We could not find an account to resubscribe');
return res.redirectWithFlash(homeLocation);
}
const [user] = users;
return new Promise((resolve, reject) =>
user.updateAttributes(
{
2018-08-02 10:34:35 +00:00
sendQuincyEmail: true
},
err => {
2018-08-02 10:34:35 +00:00
if (err) {
reject(err);
} else {
resolve();
}
}
2018-08-02 10:34:35 +00:00
)
)
2018-08-02 10:34:35 +00:00
.then(() => {
req.flash(
'success',
"We've successfully updated your email preferences. Thank you " +
'for resubscribing.'
);
return res.redirectWithFlash(homeLocation);
2018-08-02 10:34:35 +00:00
})
.catch(next);
});
}
function githubCalls(req, res, next) {
var githubHeaders = {
headers: {
'User-Agent': constantStrings.gitHubUserAgent
},
port: 80
};
request(
[
'https://api.github.com/repos/freecodecamp/',
'freecodecamp/pulls?client_id=',
githubClient,
'&client_secret=',
githubSecret
].join(''),
githubHeaders,
function(err, status1, pulls) {
if (err) {
return next(err);
}
pulls = pulls
? Object.keys(JSON.parse(pulls)).length
: "Can't connect to github";
2016-03-03 04:54:14 +00:00
return request(
[
'https://api.github.com/repos/freecodecamp/',
'freecodecamp/issues?client_id=',
githubClient,
'&client_secret=',
githubSecret
].join(''),
githubHeaders,
2015-10-07 07:28:42 +00:00
function(err, status2, issues) {
if (err) {
return next(err);
}
issues =
pulls === parseInt(pulls, 10) && issues
? Object.keys(JSON.parse(issues)).length - pulls
: "Can't connect to GitHub";
2016-03-03 04:54:14 +00:00
return res.send({
issues: issues,
pulls: pulls
});
}
);
}
);
}
};