Skip to content

Commit

Permalink
Merge 99390f4 into 11a0a04
Browse files Browse the repository at this point in the history
  • Loading branch information
rwajon committed Jul 24, 2019
2 parents 11a0a04 + 99390f4 commit 867db23
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 51 deletions.
9 changes: 6 additions & 3 deletions src/config/dbConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@ const config = {
password: process.env.DB_PASSWORD_DEV,
database: process.env.DB_NAME_DEV,
host: process.env.DB_HOST_DEV,
dialect: 'postgres'
dialect: 'postgres',
seederStorage: 'sequelize'
},
test: {
use_env_variable: 'DATABASE_URL_TEST',
username: process.env.DB_USER_TEST,
password: process.env.DB_PASSWORD_TEST,
database: process.env.DB_NAME_TEST,
host: process.env.DB_HOST_TEST,
dialect: 'postgres'
dialect: 'postgres',
seederStorage: 'sequelize'
},
production: {
use_env_variable: 'DATABASE_URL',
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
dialect: 'postgres'
dialect: 'postgres',
seederStorage: 'sequelize'
}
};

Expand Down
30 changes: 15 additions & 15 deletions src/controllers/HighlightController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ export default class Highlights {
* @returns {Object} response
*/
static async create(req, res) {
const userId = req.user.id;
const { articleSlug } = req.params;
const {
highlightedText, startIndex, stopIndex, comment
} = req.body;

const [{ articleSlug }, userId] = [req.params, req.user.id];
const { startIndex, stopIndex } = req.body;
const { highlightedText, comment, anchorKey } = req.body;
const contentLength = highlightedText.split('').length;
const indexesLength = stopIndex - startIndex;
if (contentLength !== indexesLength + 1) {
if (contentLength !== indexesLength) {
return res.status(status.BAD_REQUEST).json({
message: 'Sorry the length of your highlightedText does not match with start and end index'
message: 'Sorry the length of your highlighted text does not match with start and end index'
});
}

const created = await findOrCreate({
articleSlug,
userId,
highlightedText,
startIndex,
stopIndex,
comment
comment,
anchorKey
});

return res.status(status.CREATED).json({ message: 'You have highlighted this text', created });
return (
(created.errors
&& res.status(status.SERVER_ERROR).json({ message: 'Oops, something went wrong' }))
|| res.status(status.CREATED).json({ message: 'You have highlighted this text', created })
);
}

/** ,
Expand Down Expand Up @@ -76,11 +76,11 @@ export default class Highlights {
return (
(!highlight
&& res.status(status.NOT_FOUND).json({
message: `the highlight with id ${id} does not exist`
message: 'Sorry, this highlight does not exist'
}))
|| res.status(status.OK).json({
message: 'You have successfully remove your highlight',
highlight
message: 'You have successfully removed your highlight',
highlightId: id
})
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/queryHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ const dbFindSingle = async (model, whereCondition = {}) => model.findOne({
logging: false
});

const dbFindAll = async (model, whereCondition, offset = 0, limit = 20) => model.findAll({
const dbFindAll = async (model, whereCondition, offset = 0, limit = 20, include) => model.findAll({
offset,
limit,
where: whereCondition,
logging: false
logging: false,
include
});

const dbCreate = async (model, condition) => model.create(condition, { logging: false });
Expand Down
9 changes: 5 additions & 4 deletions src/helpers/validation/createHighlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ export default (input) => {
const schema = Joi.object().keys({
highlightedText: Joi.string()
.min(5)
.max(255)
.required(),
startIndex: Joi.number().required(),
stopIndex: Joi.number().required(),
comment: Joi.string()
.min(5)
.max(255)
.required()
.allow(null, '')
.optional(),
anchorKey: Joi.string()
.allow(null, '')
.optional()
});

return Joi.validate(input, schema, { abortEarly: false });
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/checkArticle.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const checkArticle = async (req, res, next) => {
});
if (!findArticle) {
return res.status(status.NOT_FOUND).send({
message: 'That article does not exist'
message: 'This article does not exist'
});
}
next();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
up: (queryInterface, Sequelize) => queryInterface.changeColumn('Highlights', 'highlightedText', {
type: Sequelize.TEXT,
allowNull: false
}),
down: (queryInterface, Sequelize) => queryInterface.changeColumn('Highlights', 'highlightedText', {
type: Sequelize.TEXT,
allowNull: false
})
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
up: (queryInterface, Sequelize) => queryInterface.addColumn('Highlights', 'anchorKey', {
type: Sequelize.STRING,
allowNull: true
}),
down: queryInterface => queryInterface.removeColumn('Highlights', 'anchorKey')
};
18 changes: 10 additions & 8 deletions src/models/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ module.exports = (sequelize, DataTypes) => {
},
userId: {
type: DataTypes.INTEGER,
allowNull: true,
allowNull: false,
references: {
model: 'Users',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
onDelete: 'CASCADE'
},
highlightedText: {
type: DataTypes.STRING,
type: DataTypes.TEXT,
allowNull: false,
field: 'highlightedText'
},
Expand All @@ -38,7 +38,12 @@ module.exports = (sequelize, DataTypes) => {
field: 'stopIndex'
},
comment: {
type: DataTypes.TEXT
type: DataTypes.TEXT,
allowNull: true
},
anchorKey: {
type: DataTypes.STRING,
allowNull: true
},
createdAt: {
allowNull: false,
Expand All @@ -52,10 +57,7 @@ module.exports = (sequelize, DataTypes) => {
{}
);
Highlight.associate = (models) => {
Highlight.belongsTo(models.User, {
foreignKey: 'userId',
onDelete: 'CASCADE'
});
Highlight.belongsTo(models.User, { foreignKey: 'userId', as: 'commentAuthor' });
};
return Highlight;
};
8 changes: 7 additions & 1 deletion src/queries/highlights/getAll.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import db from '../../models';
import { dbFindAll } from '../../helpers/queryHelper';

const getAll = async (condition = {}) => dbFindAll(db.Highlight, condition);
const getAll = async (condition = {}) => dbFindAll(db.Highlight, condition, null, null, [
{
model: db.User,
as: 'commentAuthor',
attributes: ['firstName', 'lastName', 'username', 'email', 'image']
}
]);

export default getAll;
1 change: 0 additions & 1 deletion src/routes/api/highlights.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ router.post(

router.get(
'/:articleSlug/highlights',
verifyToken,
checkArticle,
asyncHandler(HighlightController.getHighlights)
);
Expand Down
8 changes: 4 additions & 4 deletions src/seeders/20190605075225-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import * as Factory from '../helpers/factory';

const permissionsNormal = Factory.permissionsNormal.build();
const permissionsAdmin = Factory.permissionsAdmin.build();
permissionsNormal.createdAt = '2019-05-12T22:00:00';
permissionsNormal.updatedAt = '2019-05-12T22:00:00';
permissionsAdmin.createdAt = '2019-05-12T22:00:00';
permissionsAdmin.updatedAt = '2019-05-12T22:00:00';
permissionsNormal.createdAt = new Date();
permissionsNormal.updatedAt = new Date();
permissionsAdmin.createdAt = new Date();
permissionsAdmin.updatedAt = new Date();

export default {
up: queryInterface => queryInterface.bulkInsert('Permissions', [permissionsNormal, permissionsAdmin], {}),
Expand Down
20 changes: 20 additions & 0 deletions src/seeders/20190715123639-admin-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as Factory from '../helpers/factory';
import { password } from '../helpers';

const userAdmin = {
firstName: 'admin',
lastName: 'admin',
username: 'admin',
email: 'admin@admin.com',
password: password.hash('admin'),
role: 'admin',
permissions: Factory.permissionsAdmin.build().permissions,
createdAt: new Date(),
updatedAt: new Date()
};

export default {
up: queryInterface => queryInterface.bulkInsert('Users', [userAdmin], {}),

down: queryInterface => queryInterface.bulkDelete('Users', null, {})
};
17 changes: 5 additions & 12 deletions src/tests/routes/highlight.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ describe('Highlight on Article', () => {
cascade: true,
logging: false
});
await db.Article.destroy({
truncate: true,
cascade: true,
logging: false
});
await db.Highlight.destroy({
truncate: true,
cascade: true,
logging: false
});

createdUser = (await db.User.create(newUser, { logging: false })).dataValues;
newArticle.userId = createdUser.id;
Expand All @@ -52,6 +42,7 @@ describe('Highlight on Article', () => {
{ expiresIn: '1d' }
);
createdArticle = (await db.Article.create(newArticle, { logging: false })).dataValues;
newHighlight.userId = createdArticle.userId;
newHighlight.articleSlug = createdArticle.slug;
createdHighlight = await db.Highlight.create(newHighlight, { logging: false });
newArticleSlug = createdHighlight.articleSlug;
Expand All @@ -67,9 +58,10 @@ describe('Highlight on Article', () => {
.post(`/api/v1/${newArticleSlug}/highlights`)
.set('access-token', accessToken)
.send({
anchorKey: 'anchorKey',
highlightedText: 'on sera ensemble bientotssssss',
startIndex: 0,
stopIndex: 29,
stopIndex: 30,
comment: 'welcomme to the party'
})
.end((err, res) => {
Expand Down Expand Up @@ -97,9 +89,10 @@ describe('Highlight on Article', () => {
.post(`/api/v1/${newArticleSlug}/highlights`)
.set('access-token', accessToken)
.send({
anchorKey: 'anchorKey',
highlightedText: 'on sera ensemble bientotssssss',
startIndex: 0,
stopIndex: 30,
stopIndex: 39,
comment: 'welcomme to the party'
})
.end((err, res) => {
Expand Down

0 comments on commit 867db23

Please sign in to comment.