Skip to content

Commit

Permalink
[chore #166841614] Configure migrations with the current codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nkpremices committed Jun 21, 2019
1 parent f5a11d0 commit d91edb2
Show file tree
Hide file tree
Showing 21 changed files with 356 additions and 153 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ API_BASE_URL=theApiBaseUrlToInsertInTheLinksSharedInEmails
TOKEN_KEY=TheTokenKey

BCRYPT_HASH_ROUNDS=theBcryptSaltingRounds

USERNAME=thePostgresUsername
PASSWORD=TheDatabasePassword
DEV_DATABASE=theDevelopementDbName
TEST_DATABASE=theTestDbName
HOST=TheDatabaseHost
DB_PORT =theDatabasePort
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ node_modules

# Coverage directory used by tools like istanbul
coverage

# Migrations
src/api/migrations

# Seeders
src/api/seeders
10 changes: 10 additions & 0 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require('@babel/register')

const path = require('path');

module.exports = {
"config": path.resolve('./src/configs', 'environments.js'),
"models-path": path.resolve('./src/api/models'),
"seeders-path": path.resolve('./src/api/seeders'),
"migrations-path": path.resolve('./src/api/migrations')
};
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[![Maintainability](https://api.codeclimate.com/v1/badges/c05960d83e2ec9822111/maintainability)](https://codeclimate.com/github/andela/savage_rangers-ah/maintainability)
=======


# Authors Haven - A Social platform for the creative at heart.

## Vision
Expand Down Expand Up @@ -412,3 +411,26 @@ No additional parameters required
### Get Tags

`GET /api/tags`

## Getting started

### 1. Installation steps

- Clone the repo using

`https://github.com/andela/savage_rangers-ah.git`

- Install dependecies by runing

`npm install`

- Create a `.env` file at the root of the project and fill out the variables you can find in `.env.example` file

- Run `npm run setup:dev` to create the tables in the database

- You can now run `npm start` to start the server

### 2. Steps for running tests

- Run tests
`npm test` or `npm ts`
143 changes: 74 additions & 69 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,71 +1,76 @@
{
"name": "express-authorshaven",
"version": "1.0.0",
"description": "A Social platform for the creative at heart",
"main": "index.js",
"scripts": {
"start": "babel-node src/index.js",
"test": "NODE_ENV=test nyc --reporter=html --reporter=text --reporter=lcov ./node_modules/.bin/mocha ./tests/* --require @babel/register --timeout 4000 --exit",
"dev": "NODE_ENV=development nodemon --exec babel-node src/index.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"eslint": "eslint . --cache --fix",
"precommit": "lint-staged"
},
"lint-staged": {
"*.js": [
"eslint --cache --fix",
"git add"
]
},
"devDependencies": {
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"coveralls": "^3.0.3",
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.17.3",
"husky": "^2.4.1",
"lint-staged": "^8.2.1",
"mocha": "^6.1.4",
"nodemon": "^1.19.1",
"nyc": "^14.1.1"
},
"dependencies": {
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.5",
"@babel/node": "^7.4.5",
"@babel/polyfill": "^7.4.4",
"@babel/preset-env": "^7.4.5",
"@babel/register": "^7.4.4",
"@hapi/joi": "^15.0.3",
"@sendgrid/mail": "^6.4.0",
"babel-plugin-istanbul": "^5.1.2",
"bcrypt": "^3.0.6",
"body-parser": "^1.18.3",
"cors": "^2.8.5",
"dotenv": "^8.0.0",
"expect": "^24.8.0",
"express": "^4.16.4",
"joi": "^14.3.1",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.11",
"morgan": "^1.9.1",
"pg": "^7.11.0",
"sequelize": "^5.8.7",
"swagger-node-express": "^2.1.3",
"swagger-ui-express": "^4.0.6"
},
"nyc": {
"per-file": true,
"check-coverage": true,
"branches": 80,
"lines": 80,
"functions": 100,
"statements": 80,
"exclude": [
"src/index.js"
]
},
"author": "Andela Simulations Programme",
"license": "MIT"
"name": "express-authorshaven",
"version": "1.0.0",
"description": "A Social platform for the creative at heart",
"main": "index.js",
"scripts": {
"start": "babel-node src/index.js",
"test": "npm run setup:test && NODE_ENV=test nyc --reporter=html --reporter=text --reporter=lcov ./node_modules/.bin/mocha ./tests/* --require @babel/register --timeout 4000 --exit",
"dev": "NODE_ENV=development nodemon --exec babel-node src/index.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"eslint": "eslint . --cache --fix",
"setup:test": "NODE_ENV=test npm run migrate:undo && NODE_ENV=test npm run migrate && NODE_ENV=test npm run seed",
"setup:dev": "NODE_ENV=development npm run migrate:undo && NODE_ENV=development npm run migrate && NODE_ENV=development npm run seed",
"precommit": "lint-staged",
"migrate": "node_modules/.bin/sequelize db:migrate",
"seed": "node_modules/.bin/sequelize db:seed:all",
"migrate:undo": "node_modules/.bin/sequelize db:migrate:undo:all"
},
"lint-staged": {
"*.js": [
"eslint --cache --fix",
"git add"
]
},
"devDependencies": {
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"coveralls": "^3.0.3",
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.17.3",
"husky": "^2.4.1",
"lint-staged": "^8.2.1",
"mocha": "^6.1.4",
"nodemon": "^1.19.1",
"nyc": "^14.1.1"
},
"dependencies": {
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.5",
"@babel/node": "^7.4.5",
"@babel/polyfill": "^7.4.4",
"@babel/preset-env": "^7.4.5",
"@babel/register": "^7.4.4",
"@hapi/joi": "^15.0.3",
"@sendgrid/mail": "^6.4.0",
"babel-plugin-istanbul": "^5.1.2",
"bcrypt": "^3.0.6",
"body-parser": "^1.18.3",
"cors": "^2.8.5",
"dotenv": "^8.0.0",
"expect": "^24.8.0",
"express": "^4.16.4",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.11",
"morgan": "^1.9.1",
"pg": "^7.11.0",
"sequelize": "^5.8.7",
"sequelize-cli": "^5.5.0",
"swagger-node-express": "^2.1.3",
"swagger-ui-express": "^4.0.6"
},
"nyc": {
"per-file": true,
"check-coverage": true,
"branches": 80,
"lines": 80,
"functions": 100,
"statements": 80,
"exclude": [
"src/index.js"
]
},
"author": "Andela Simulations Programme",
"license": "MIT"
}
7 changes: 3 additions & 4 deletions src/api/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import models from '../models/index';
import generateToken from '../../helpers/tokens/generate.token';
import sendResult from '../../helpers/results/send.auth';
import status from '../../helpers/constants/status.codes';
import environments from '../../configs/environments';
import env from '../../configs/environments';
import sendError from '../../helpers/error.sender';
import errors from '../../helpers/constants/error.messages';

const { User } = models;
const env = environments.currentEnv;
/**
* containing all user's model controllers (signup, login)
*
Expand All @@ -34,7 +33,7 @@ export default class Auth {
email,
password: hashedPassword
});
const tokenData = { username, email };
const tokenData = { id: user.dataValues.id, username, email };
const token = generateToken(tokenData, env.secret);
return sendResult(res, status.CREATED, 'user created successfully', user, token);
}
Expand All @@ -54,7 +53,7 @@ export default class Auth {
if (user) {
const isPasswordValid = bcrypt.compareSync(password, user.dataValues.password);
if (isPasswordValid) {
const tokenData = { username: user.dataValues.username, email };
const tokenData = { id: user.dataValues.id, username: user.dataValues.username, email };
const token = generateToken(tokenData, env.secret);
return sendResult(res, status.OK, 'user logged in successfully', user, token);
}
Expand Down
5 changes: 2 additions & 3 deletions src/api/controllers/password.reset.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { hashSync, genSaltSync } from 'bcrypt';
import models from '../models/index';
import mailer from '../../helpers/Mailer';
import environment from '../../configs/environments';
import env from '../../configs/environments';
import status from '../../helpers/constants/status.codes';

const { User } = models;
const env = environment.currentEnv;

/**
* containing aal controllers of the signup process
Expand Down Expand Up @@ -33,7 +32,7 @@ export default class PasswordReset {
userName: username,
buttonText: 'RESET',
message:
"You are receiving this email beacause you've requested the recovery "
"You are receiving this email because you've requested the recovery "
+ 'of your Authors Heaven password. Kindly click the button below.'
});

Expand Down
70 changes: 70 additions & 0 deletions src/api/migrations/20190620192200-create-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
export default {
up: (queryInterface, Sequelize) => queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
username: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
},
country: {
type: Sequelize.STRING
},
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
bio: {
type: Sequelize.STRING
},
address: {
type: Sequelize.STRING
},
gender: {
type: Sequelize.STRING
},
profileImage: {
type: Sequelize.STRING
},
phoneNumber: {
type: Sequelize.STRING
},
provider: {
type: Sequelize.STRING,
allowNull: true
},
uniqueId: {
type: Sequelize.STRING,
allowNull: true
},
verified: {
type: Sequelize.BOOLEAN,
defaultValue: false
},
facebook: {
type: Sequelize.STRING
},
twitter: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: (queryInterface, Sequelize) => queryInterface.dropTable('Users')
};
22 changes: 22 additions & 0 deletions src/api/migrations/20190620193548-create-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default {
up: (queryInterface, Sequelize) => queryInterface.createTable('Tokens', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
data: {
type: Sequelize.TEXT
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: (queryInterface, Sequelize) => queryInterface.dropTable('Tokens')
};
7 changes: 3 additions & 4 deletions src/api/models/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Sequelize from 'sequelize';
import environments from '../../configs/environments';

const env = environments.currentEnv;
import env from '../../configs/environments';

const sequelize = new Sequelize(env.dbUrl, {
logging: false
logging: false,
dialect: 'postgres'
});

const models = {
Expand Down
14 changes: 6 additions & 8 deletions src/api/models/token.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const token = (sequelize, DataTypes) => {
const Token = sequelize.define('Tokens', {
data: {
type: DataTypes.TEXT
}
});
export default (sequelize, DataTypes) => {
const Token = sequelize.define('Token',
{
data: DataTypes.TEXT
},
{});
return Token;
};

export default token;
Loading

0 comments on commit d91edb2

Please sign in to comment.