From 07266b7e432489f757250b395ef52e3c93da4678 Mon Sep 17 00:00:00 2001 From: Bouncey Date: Wed, 6 Feb 2019 14:19:58 +0000 Subject: [PATCH] chore: commit lint fixes for the api --- api-server/common/models/User-Identity.js | 8 +-- api-server/common/utils/index.js | 17 +++-- api-server/server/boot/donate.js | 64 ++++++++--------- api-server/server/boot/randomAPIs.js | 8 +-- api-server/server/boot/user.js | 2 +- api-server/server/middlewares/csp.js | 8 +-- .../server/middlewares/jwt-authorization.js | 18 ++--- api-server/server/rss/index.js | 54 +++++++-------- api-server/server/rss/lybsyn.js | 12 ++-- api-server/server/rss/medium.js | 2 +- api-server/server/services/challenge.js | 12 ++-- api-server/server/services/user.js | 3 +- api-server/server/utils/date-utils.js | 2 +- .../server/utils/getDynamicPropsForUser.js | 69 ++++++++++--------- api-server/server/utils/index.js | 30 ++++---- api-server/server/utils/publicUserProps.js | 2 +- api-server/server/utils/url-utils.js | 2 +- 17 files changed, 159 insertions(+), 154 deletions(-) diff --git a/api-server/common/models/User-Identity.js b/api-server/common/models/User-Identity.js index bff8f3e6a57f9e..d271b78da4029a 100644 --- a/api-server/common/models/User-Identity.js +++ b/api-server/common/models/User-Identity.js @@ -115,10 +115,10 @@ export default function(UserIdent) { return identity ? Observable.of(identity.user()) : User.findOne$({ where: { email } }).flatMap(user => { - return user - ? Observable.of(user) - : User.create$({ email }).toPromise(); - }); + return user + ? Observable.of(user) + : User.create$({ email }).toPromise(); + }); }) .flatMap(user => { const createToken = observeQuery(AccessToken, 'create', { diff --git a/api-server/common/utils/index.js b/api-server/common/utils/index.js index 4bb667e3f9ffb1..16a18949044e99 100644 --- a/api-server/common/utils/index.js +++ b/api-server/common/utils/index.js @@ -4,14 +4,19 @@ export function dashify(str) { return ('' + str) .toLowerCase() .replace(/\s/g, '-') - .replace(/[^a-z0-9\-\.]/gi, '') - .replace(/\:/g, ''); + .replace(/[^a-z0-9\-.]/gi, '') + .replace(/:/g, ''); } // todo: unify with server/utils/index.js:dasherize const dasherize = dashify; export { dasherize }; -export const fixCompletedChallengeItem = obj => pick( - obj, - [ 'id', 'completedDate', 'solution', 'githubLink', 'challengeType', 'files' ] -); +export const fixCompletedChallengeItem = obj => + pick(obj, [ + 'id', + 'completedDate', + 'solution', + 'githubLink', + 'challengeType', + 'files', + ]); diff --git a/api-server/server/boot/donate.js b/api-server/server/boot/donate.js index c6fc7bad56b8a5..1feec901f65a26 100644 --- a/api-server/server/boot/donate.js +++ b/api-server/server/boot/donate.js @@ -21,9 +21,9 @@ export default function donateBoot(app, done) { name: 'Monthly Donation to freeCodeCamp.org - ' + `Thank you ($${current / 100})` - }, - currency: 'usd', - id: `monthly-donation-${current}` + }, + currency: 'usd', + id: `monthly-donation-${current}` } }), {} ); @@ -59,8 +59,8 @@ export default function donateBoot(app, done) { throw err; } console.log(`${plan.id} created`); - return; - }); + return; + }); } function createStripeDonation(req, res) { @@ -104,34 +104,34 @@ export default function donateBoot(app, done) { email, card: id }); - }) - .then(customer => { - donation.customerId = customer.id; - return stripe.subscriptions.create({ - customer: customer.id, - items: [ - { - plan: `monthly-donation-${amount}` - } - ] - }); - }) - .then(subscription => { - donation.subscriptionId = subscription.id; - return res.send(subscription); - }) - .then(() => { - donatingUser.createDonation(donation).toPromise() - .catch(err => { - throw new Error(err); + }) + .then(customer => { + donation.customerId = customer.id; + return stripe.subscriptions.create({ + customer: customer.id, + items: [ + { + plan: `monthly-donation-${amount}` + } + ] }); - }) - .catch(err => { - if (err.type === 'StripeCardError') { - return res.status(402).send({ error: err.message }); - } - return res.status(500).send({ error: 'Donation Failed' }); - }); + }) + .then(subscription => { + donation.subscriptionId = subscription.id; + return res.send(subscription); + }) + .then(() => { + donatingUser.createDonation(donation).toPromise() + .catch(err => { + throw new Error(err); + }); + }) + .catch(err => { + if (err.type === 'StripeCardError') { + return res.status(402).send({ error: err.message }); + } + return res.status(500).send({ error: 'Donation Failed' }); + }); } const pubKey = keys.stripe.public; diff --git a/api-server/server/boot/randomAPIs.js b/api-server/server/boot/randomAPIs.js index 4990728ff553b4..5d00439b327e68 100644 --- a/api-server/server/boot/randomAPIs.js +++ b/api-server/server/boot/randomAPIs.js @@ -88,7 +88,7 @@ module.exports = function(app) { .then(() => { req.flash( 'success', - "We've successfully updated your email preferences." + 'We\'ve successfully updated your email preferences.' ); return res.redirectWithFlash( `${homeLocation}/unsubscribed/${unsubscribeId}` @@ -144,7 +144,7 @@ module.exports = function(app) { .then(() => { req.flash( 'success', - "We've successfully updated your email preferences. Thank you " + + 'We\'ve successfully updated your email preferences. Thank you ' + 'for resubscribing.' ); return res.redirectWithFlash(homeLocation); @@ -175,7 +175,7 @@ module.exports = function(app) { } pulls = pulls ? Object.keys(JSON.parse(pulls)).length - : "Can't connect to github"; + : 'Can\'t connect to github'; return request( [ @@ -193,7 +193,7 @@ module.exports = function(app) { issues = pulls === parseInt(pulls, 10) && issues ? Object.keys(JSON.parse(issues)).length - pulls - : "Can't connect to GitHub"; + : 'Can\'t connect to GitHub'; return res.send({ issues: issues, pulls: pulls diff --git a/api-server/server/boot/user.js b/api-server/server/boot/user.js index c4ef2903dc8203..e1ee041be154de 100644 --- a/api-server/server/boot/user.js +++ b/api-server/server/boot/user.js @@ -229,7 +229,7 @@ function createPostReportUserProfile(app) { to: 'team@freecodecamp.org', cc: user.email, from: 'team@freecodecamp.org', - subject: 'Abuse Report : Reporting ' + username + "'s profile.", + subject: `Abuse Report : Reporting ${username}'s profile.`, text: dedent(` Hello Team,\n This is to report the profile of ${username}.\n diff --git a/api-server/server/middlewares/csp.js b/api-server/server/middlewares/csp.js index 335d9aa894475b..07b41137c9eb4a 100644 --- a/api-server/server/middlewares/csp.js +++ b/api-server/server/middlewares/csp.js @@ -3,7 +3,7 @@ import helmet from 'helmet'; import { homeLocation } from '../../../config/env'; let trusted = [ - "'self'", + '\'self\'', 'https://search.freecodecamp.org', homeLocation, 'https://' + process.env.AUTH0_DOMAIN @@ -31,8 +31,8 @@ export default function csp() { 'https://*.algolia.net' ]), scriptSrc: [ - "'unsafe-eval'", - "'unsafe-inline'", + '\'unsafe-eval\'', + '\'unsafe-inline\'', '*.google-analytics.com', '*.gstatic.com', 'https://*.cloudflare.com', @@ -48,7 +48,7 @@ export default function csp() { '*.ytimg.com' ].concat(trusted), styleSrc: [ - "'unsafe-inline'", + '\'unsafe-inline\'', '*.gstatic.com', '*.googleapis.com', '*.bootstrapcdn.com', diff --git a/api-server/server/middlewares/jwt-authorization.js b/api-server/server/middlewares/jwt-authorization.js index 2ba1864ecb4f70..5491cb68a20484 100644 --- a/api-server/server/middlewares/jwt-authorization.js +++ b/api-server/server/middlewares/jwt-authorization.js @@ -69,15 +69,15 @@ export default () => function authorizeByJWT(req, res, next) { if (!req.user) { const User = loopback.getModelByType('User'); return User.findById(userId) - .then(user => { - if (user) { - user.points = user.progressTimestamps.length; - req.user = user; - } - return; - }) - .then(next) - .catch(next); + .then(user => { + if (user) { + user.points = user.progressTimestamps.length; + req.user = user; + } + return; + }) + .then(next) + .catch(next); } else { return next(); } diff --git a/api-server/server/rss/index.js b/api-server/server/rss/index.js index 5fd8f2556c31ac..9d41ac776f6adb 100644 --- a/api-server/server/rss/index.js +++ b/api-server/server/rss/index.js @@ -33,35 +33,35 @@ class NewsFeed { const currentFeed = this.state.combinedFeed.slice(0); log('grabbing feeds'); return Promise.all([ - getMediumFeed(), - getLybsynFeed() - ]).then( - ([mediumFeed, lybsynFeed]) => this.setState( - state => ({ - ...state, - mediumFeed, - lybsynFeed + getMediumFeed(), + getLybsynFeed() + ]).then( + ([mediumFeed, lybsynFeed]) => this.setState( + state => ({ + ...state, + mediumFeed, + lybsynFeed + }) + )) + .then(() => { + log('crossing the streams'); + const { mediumFeed, lybsynFeed} = this.state; + const combinedFeed = [ ...mediumFeed, ...lybsynFeed ].sort((a, b) => { + return compareDesc(a.isoDate, b.isoDate); + }); + this.setState(state => ({ + ...state, + combinedFeed, + readyState: true + })); }) - )) - .then(() => { - log('crossing the streams'); - const { mediumFeed, lybsynFeed} = this.state; - const combinedFeed = [ ...mediumFeed, ...lybsynFeed ].sort((a, b) => { - return compareDesc(a.isoDate, b.isoDate); + .catch(err => { + console.log(err); + this.setState(state => ({ + ...state, + combinedFeed: currentFeed + })); }); - this.setState(state => ({ - ...state, - combinedFeed, - readyState: true - })); - }) - .catch(err => { - console.log(err); - this.setState(state => ({ - ...state, - combinedFeed: currentFeed - })); - }); } diff --git a/api-server/server/rss/lybsyn.js b/api-server/server/rss/lybsyn.js index f4797b74f86f11..701e2ea440361b 100644 --- a/api-server/server/rss/lybsyn.js +++ b/api-server/server/rss/lybsyn.js @@ -31,12 +31,12 @@ export function getLybsynFeed() { ]) ) /* eslint-disable camelcase */ - .map(({ full_item_url, item_title, release_date, item_body_short}) => ({ - title: item_title, - extract: item_body_short, - isoDate: new Date(release_date).toISOString(), - link: full_item_url - })); + .map(({ full_item_url, item_title, release_date, item_body_short}) => ({ + title: item_title, + extract: item_body_short, + isoDate: new Date(release_date).toISOString(), + link: full_item_url + })); /* eslint-enable camelcase */ return resolve(items); }); diff --git a/api-server/server/rss/medium.js b/api-server/server/rss/medium.js index 979b4c9fba36d0..cb3e083fdc91c6 100644 --- a/api-server/server/rss/medium.js +++ b/api-server/server/rss/medium.js @@ -11,7 +11,7 @@ function getExtract(str) { function addResponsiveClass(str) { - return str.replace(/\ cb(null, user), cb diff --git a/api-server/server/utils/date-utils.js b/api-server/server/utils/date-utils.js index a22de800153edc..fa5c7bd7baecf1 100644 --- a/api-server/server/utils/date-utils.js +++ b/api-server/server/utils/date-utils.js @@ -7,5 +7,5 @@ export function dayCount([head, tail], timezone = 'UTC') { moment(tail).tz(timezone).startOf('day'), 'days', true) - ); + ); } diff --git a/api-server/server/utils/getDynamicPropsForUser.js b/api-server/server/utils/getDynamicPropsForUser.js index 3036af61b669e2..38bd00e62f8087 100644 --- a/api-server/server/utils/getDynamicPropsForUser.js +++ b/api-server/server/utils/getDynamicPropsForUser.js @@ -1,4 +1,3 @@ - function getCompletedCertCount(user) { return [ 'isApisMicroservicesCert', @@ -7,46 +6,48 @@ function getCompletedCertCount(user) { 'isInfosecQaCert', 'isJsAlgoDataStructCert', 'isRespWebDesignCert' - ].reduce((sum, key) => user[key] ? sum + 1 : sum, 0); + ].reduce((sum, key) => (user[key] ? sum + 1 : sum), 0); } function getLegacyCertCount(user) { - return [ - 'isFrontEndCert', - 'isBackEndCert', - 'isDataVisCert' - ].reduce((sum, key) => user[key] ? sum + 1 : sum, 0); + return ['isFrontEndCert', 'isBackEndCert', 'isDataVisCert'].reduce( + (sum, key) => (user[key] ? sum + 1 : sum), + 0 + ); } export default function populateUser(db, user) { return new Promise((resolve, reject) => { - let populatedUser = {...user}; + let populatedUser = { ...user }; db.collection('user') - .aggregate([ - { $match: { _id: user.id } }, - { $project: { points: { $size: '$progressTimestamps' } } } - ]).get(function(err, [{ points = 1 } = {}]) { - if (err) { return reject(err); } - user.points = points; - let completedChallengeCount = 0; - let completedProjectCount = 0; - if ('completedChallenges' in user) { - completedChallengeCount = user.completedChallenges.length; - user.completedChallenges.forEach(item => { - if ( - 'challengeType' in item && - (item.challengeType === 3 || item.challengeType === 4) - ) { - completedProjectCount++; - } - }); - } - populatedUser.completedChallengeCount = completedChallengeCount; - populatedUser.completedProjectCount = completedProjectCount; - populatedUser.completedCertCount = getCompletedCertCount(user); - populatedUser.completedLegacyCertCount = getLegacyCertCount(user); - populatedUser.completedChallenges = []; - return resolve(populatedUser); - }); + .aggregate([ + { $match: { _id: user.id } }, + { $project: { points: { $size: '$progressTimestamps' } } }, + ]) + .get(function(err, [{ points = 1 } = {}]) { + if (err) { + return reject(err); + } + user.points = points; + let completedChallengeCount = 0; + let completedProjectCount = 0; + if ('completedChallenges' in user) { + completedChallengeCount = user.completedChallenges.length; + user.completedChallenges.forEach(item => { + if ( + 'challengeType' in item && + (item.challengeType === 3 || item.challengeType === 4) + ) { + completedProjectCount++; + } + }); + } + populatedUser.completedChallengeCount = completedChallengeCount; + populatedUser.completedProjectCount = completedProjectCount; + populatedUser.completedCertCount = getCompletedCertCount(user); + populatedUser.completedLegacyCertCount = getLegacyCertCount(user); + populatedUser.completedChallenges = []; + return resolve(populatedUser); + }); }); } diff --git a/api-server/server/utils/index.js b/api-server/server/utils/index.js index e95e2ca48648fb..7647af24772337 100644 --- a/api-server/server/utils/index.js +++ b/api-server/server/utils/index.js @@ -2,25 +2,25 @@ exports.dasherize = function dasherize(name) { return ('' + name) .toLowerCase() .replace(/\s/g, '-') - .replace(/[^a-z0-9\-\.]/gi, '') - .replace(/\:/g, ''); -} + .replace(/[^a-z0-9\-.]/gi, '') + .replace(/:/g, ''); +}; exports.nameify = function nameify(str) { - return ('' + str) - .replace(/[^a-zA-Z0-9\s]/g, '') - .replace(/\:/g, ''); -} + return ('' + str).replace(/[^a-zA-Z0-9\s]/g, '').replace(/:/g, ''); +}; exports.unDasherize = function unDasherize(name) { - return ('' + name) - // replace dash with space - .replace(/\-/g, ' ') - // strip nonalphanumarics chars except whitespace - .replace(/[^a-zA-Z\d\s]/g, '') - .trim(); -} + return ( + ('' + name) + // replace dash with space + .replace(/-/g, ' ') + // strip nonalphanumarics chars except whitespace + .replace(/[^a-zA-Z\d\s]/g, '') + .trim() + ); +}; exports.addPlaceholderImage = function addPlaceholderImage(name) { return `https://identicon.org?t=${name}&s=256`; -} +}; diff --git a/api-server/server/utils/publicUserProps.js b/api-server/server/utils/publicUserProps.js index 69ec276dab661d..9ef59e6546f357 100644 --- a/api-server/server/utils/publicUserProps.js +++ b/api-server/server/utils/publicUserProps.js @@ -69,7 +69,7 @@ export function getProgress(progressTimestamps, timezone = 'EST') { .reduce((data, timestamp) => { data[Math.floor(timestamp / 1000)] = 1; return data; - }, {}); + }, {}); const uniqueHours = prepUniqueDaysByHours(progressTimestamps, timezone); const streak = { longest: calcLongestStreak(uniqueHours, timezone), diff --git a/api-server/server/utils/url-utils.js b/api-server/server/utils/url-utils.js index d1403067333114..feb345f1f5dd6b 100644 --- a/api-server/server/utils/url-utils.js +++ b/api-server/server/utils/url-utils.js @@ -25,7 +25,7 @@ export function getHost() { export function getServerFullURL() { if (!isDev) { - return getProtocol() + return getProtocol() + '://' + getHost(); }