Skip to content

Commit

Permalink
Merge 968d626 into 9b39eea
Browse files Browse the repository at this point in the history
  • Loading branch information
eliemugenzi committed Jun 17, 2019
2 parents 9b39eea + 968d626 commit 16cedfd
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 33 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"coverage": "nyc report --reporter=text-lcov | coveralls",
"test:local": "yarn undo && yarn migrate && yarn seed && yarn test",
"migrate": "sequelize db:migrate",
"undo": "sequelize db:migrate:undo",
"undo": "sequelize db:migrate:undo:all",
"seed": "sequelize db:seed:all"
},
"author": "Andela Simulations Programme",
Expand Down Expand Up @@ -63,5 +63,6 @@
"mocha": "^6.1.4",
"nodemon": "^1.19.1",
"nyc": "^14.1.1"
}
},
"repository": "https://github.com/andela/tesla-ah.git"
}
3 changes: 3 additions & 0 deletions src/api/controllers/articlesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/* eslint-disable no-console */
import articles from '../../helpers/articlesHelper';
import models from '../../sequelize/models';
import readTime from '../../helpers/ReadTime.helper';

const { article, User } = models;

Expand Down Expand Up @@ -54,6 +55,8 @@ class articlesController {

const oneArticle = await articles.getOneSlug(slug);
res.status(200).send({
status: 200,
readtime: readTime(oneArticle.body),
article: oneArticle
});
}
Expand Down
25 changes: 25 additions & 0 deletions src/config/environments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import dotenv from 'dotenv';

dotenv.config();

const environments = {
development: {
dbUrl: process.env.DATABASE_URL,
},
staging: {
dbUrl: process.env.DATABASE_URL,
},
test: {
dbUrl: process.env.DATABASE_URL,
},
};

// Determine which environment we are in
const currentEnvironment = typeof (process.env.NODE_ENV) === 'string' ? process.env.NODE_ENV.toLowerCase() : '';

// Check that the current environment is one the envs defined above, if not default to development
const environment = typeof (environments[currentEnvironment]) === 'object'
? environments[currentEnvironment] : environments.development;

// Export the selected environment configuration object
export default environment;
35 changes: 35 additions & 0 deletions src/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Sequelize from 'sequelize';
import dbUrlParser from '../helpers/dbUrlParser';
import environment from '../config/environments';

const {
dbUser,
dbPassword,
dbName,
dbHost
} = dbUrlParser(environment.dbUrl);

const sequelize = new Sequelize(
dbName,
dbUser,
dbPassword,
{
dialect: 'postgres',
host: dbHost,
logging: false,
},
);

const models = {
// User: sequelize.import('../models/user'),
};

Object.keys(models).forEach((key) => {
if ('associate' in models[key]) {
models[key].associate(models);
}
});

export { sequelize };

export default models;
14 changes: 14 additions & 0 deletions src/helpers/ReadTime.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const converter = (seconds) => {
if (seconds > 60) {
return `${Math.ceil(seconds / 60)} min`;
}
return 'Less than a minute';
};
const readTime = (body) => {
const numWords = w => w.split(' ').length;
const WPS = 4;
const words = numWords(body);
const sec = words / WPS;
return converter(sec);
};
export default readTime;
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const port = process.env.PORT || 3000;
const app = express();

globalMiddleware(app);

app.use('/api', api);
app.use('/', swaggerUi.serve, swaggerUi.setup(swaggerDoc));

Expand Down
8 changes: 2 additions & 6 deletions src/middleware/droppedToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ const dropToken = async (req, res, next) => {
}
});
if (user.length) {
const deleted = await Blacklist.destroy({
await Blacklist.destroy({
where: {
userId: id
}
});
if (deleted) {
next();
}
} else {
next();
}
next();
};

export default dropToken;
2 changes: 1 addition & 1 deletion test/passwordreset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('Password reset', () => {
chai
.request(app)
.post('/api/auth/reset')
.send({ email: 'gprestein055@gmail.com' })
.send({ email: 'elie@gmail.com' })
.end(async (err, res) => {
expect(res.body.status).to.eql(201);
({ token } = res.body.data);
Expand Down
31 changes: 31 additions & 0 deletions test/readtime.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect } from 'chai';
import readTime from '../src/helpers/ReadTime.helper';


describe('Read time tests', () => {
it('should return a beautiful read time', () => {
const body = `Since joining Andela’s Bootcamp,
that was the first time to know how to test my codes,
because there are some situations where the software( like a website)
is being broken in production(means when the users are using that product and face the technical bugs).
The way this was a challenge to me is that I had to learn it fast and implement them immediately.
I found how important it is, the way you target every block of code as input and
expect each possible output in order to catch some errors and correct them before the product
is going to get deployed. The main thing I learned from this challenge is that
I have to make sure my codes are bug-free before getting deployed and make sure my tests are covering every
block of codes. How I adapted to this challenge is, I spent a lot of sleepless nights figuring out how
to write my tests, I didn’t know anything about Travis CI and I got several emails that my builds were failing.
The main key is working hard, ask around and do more research to get your work done.\nGit workflow was another challenge I faced.
I tried it before, but I never tried the feature-branch workflow.
I found that workflow was awesome because it helps you manage the project tasks and work them into branches.
The reason it was a challenge is that we had to work in several branches and merge our work into the main branch and sometimes you face some merge conflicts.
I didn’t know how to resolve conflicts, but I tried to make some research about it, ask my colleagues how to resolve them and luckily
I got my work really organized on Github.`;
expect(readTime(body)).to.equal('2 min');
});

it('should get a less than a minute read time', () => {
const body = 'This is an amazing project we are working on, Authors Haven';
expect(readTime(body)).to.equal('Less than a minute');
});
});
48 changes: 24 additions & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ browser-stdout@1.3.1:
resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.1.tgz#baa559ee14ced73452229bad7326467c61fabd60"
integrity sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==

browserslist@^4.6.0:
browserslist@^4.6.0, browserslist@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.6.2.tgz#574c665950915c2ac73a4594b8537a9eba26203f"
integrity sha512-2neU/V0giQy9h3XMPwLhEY3+Ao0uHSwHvU8Q1Ea6AgLVL1sXbX3dzPrJ8NWe5Hi4PoTkCYXOtVR9rfRLI0J/8Q==
Expand Down Expand Up @@ -1360,28 +1360,28 @@ copy-descriptor@^0.1.0:
integrity sha1-Z29us8OZl8LuGsOpJP1hJHSPV40=

core-js-compat@^3.1.1:
version "3.1.3"
resolved "https://registry.yarnpkg.com/core-js-compat/-/core-js-compat-3.1.3.tgz#0cc3ba4c7f62928c2837e1cffbe8dc78b4f1ae14"
integrity sha512-EP018pVhgwsKHz3YoN1hTq49aRe+h017Kjz0NQz3nXV0cCRMvH3fLQl+vEPGr4r4J5sk4sU3tUC7U1aqTCeJeA==
version "3.1.4"
resolved "https://registry.yarnpkg.com/core-js-compat/-/core-js-compat-3.1.4.tgz#e4d0c40fbd01e65b1d457980fe4112d4358a7408"
integrity sha512-Z5zbO9f1d0YrJdoaQhphVAnKPimX92D6z8lCGphH89MNRxlL1prI9ExJPqVwP0/kgkQCv8c4GJGT8X16yUncOg==
dependencies:
browserslist "^4.6.0"
core-js-pure "3.1.3"
semver "^6.1.0"
browserslist "^4.6.2"
core-js-pure "3.1.4"
semver "^6.1.1"

core-js-pure@3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.1.3.tgz#4c90752d5b9471f641514f3728f51c1e0783d0b5"
integrity sha512-k3JWTrcQBKqjkjI0bkfXS0lbpWPxYuHWfMMjC1VDmzU4Q58IwSbuXSo99YO/hUHlw/EB4AlfA2PVxOGkrIq6dA==
core-js-pure@3.1.4:
version "3.1.4"
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.1.4.tgz#5fa17dc77002a169a3566cc48dc774d2e13e3769"
integrity sha512-uJ4Z7iPNwiu1foygbcZYJsJs1jiXrTTCvxfLDXNhI/I+NHbSIEyr548y4fcsCEyWY0XgfAG/qqaunJ1SThHenA==

core-js@^2.6.5:
version "2.6.9"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.9.tgz#6b4b214620c834152e179323727fc19741b084f2"
integrity sha512-HOpZf6eXmnl7la+cUdMnLvUxKNqLUzJvgIziQ0DiF3JwSImNphIqdGqzj6hIKyX04MmV0poclQ7+wjWvxQyR2A==

core-js@^3.0.0:
version "3.1.3"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.1.3.tgz#95700bca5f248f5f78c0ec63e784eca663ec4138"
integrity sha512-PWZ+ZfuaKf178BIAg+CRsljwjIMRV8MY00CbZczkR6Zk5LfkSkjGoaab3+bqRQWVITNZxQB7TFYz+CFcyuamvA==
version "3.1.4"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.1.4.tgz#3a2837fc48e582e1ae25907afcd6cf03b0cc7a07"
integrity sha512-YNZN8lt82XIMLnLirj9MhKDFZHalwzzrL9YLt6eb0T5D0EDl4IQ90IGkua8mHbnxNrkj1d8hbdizMc0Qmg1WnQ==

core-util-is@1.0.2, core-util-is@~1.0.0:
version "1.0.2"
Expand Down Expand Up @@ -1643,14 +1643,14 @@ ee-first@1.1.1:
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=

ejs@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.6.1.tgz#498ec0d495655abc6f23cd61868d926464071aa0"
integrity sha512-0xy4A/twfrRCnkhfk8ErDi5DqdAsAqeGxht4xkCUrsvhhbQNs7E+4jV0CN7+NKIY0aHE72+XvqtBIXzD31ZbXQ==
version "2.6.2"
resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.6.2.tgz#3a32c63d1cd16d11266cd4703b14fec4e74ab4f6"
integrity sha512-PcW2a0tyTuPHz3tWyYqtK6r1fZ3gp+3Sop8Ph+ZYN81Ob5rwmbHEzaqs10N3BEsaGTkh/ooniXK+WwszGlc2+Q==

electron-to-chromium@^1.3.150:
version "1.3.159"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.159.tgz#4292643c5cb77678821ce01557ba6dd0f562db5e"
integrity sha512-bhiEr8/A97GUBcUzNb9MFNhzQOjakbKmEKBEAa6UMY45zG2e8PM63LOgAPXEJE9bQiaQH6nOdYiYf8X821tZjQ==
version "1.3.164"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.164.tgz#8680b875577882c1572c42218d53fa9ba5f71d5d"
integrity sha512-VLlalqUeduN4+fayVtRZvGP2Hl1WrRxlwzh2XVVMJym3IFrQUS29BFQ1GP/BxOJXJI1OFCrJ5BnFEsAe8NHtOg==

emoji-regex@^7.0.1:
version "7.0.3"
Expand Down Expand Up @@ -4495,7 +4495,7 @@ semver@4.3.2:
resolved "https://registry.yarnpkg.com/semver/-/semver-4.3.2.tgz#c7a07158a80bedd052355b770d82d6640f803be7"
integrity sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c=

semver@^6.0.0, semver@^6.1.0:
semver@^6.0.0, semver@^6.1.1:
version "6.1.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.1.1.tgz#53f53da9b30b2103cd4f15eab3a18ecbcb210c9b"
integrity sha512-rWYq2e5iYW+fFe/oPPtYJxYgjBm8sC4rmoGdUOgBB7VnwKt6HrL793l2voH1UlsyYZpJ4g0wfjnTEO1s1NP2eQ==
Expand Down Expand Up @@ -4896,9 +4896,9 @@ swagger-ui-express@^4.0.6:
swagger-ui-dist "^3.18.1"

table@^5.2.3:
version "5.4.0"
resolved "https://registry.yarnpkg.com/table/-/table-5.4.0.tgz#d772a3216e68829920a41a32c18eda286c95d780"
integrity sha512-nHFDrxmbrkU7JAFKqKbDJXfzrX2UBsWmrieXFTGxiI5e4ncg3VqsZeI4EzNmX0ncp4XNGVeoxIWJXfCIXwrsvw==
version "5.4.1"
resolved "https://registry.yarnpkg.com/table/-/table-5.4.1.tgz#0691ae2ebe8259858efb63e550b6d5f9300171e8"
integrity sha512-E6CK1/pZe2N75rGZQotFOdmzWQ1AILtgYbMAbAjvms0S1l5IDB47zG3nCnFGB/w+7nB3vKofbLXCH7HPBo864w==
dependencies:
ajv "^6.9.1"
lodash "^4.17.11"
Expand Down

0 comments on commit 16cedfd

Please sign in to comment.