Skip to content

Commit

Permalink
Merge e35479f into 30629b6
Browse files Browse the repository at this point in the history
  • Loading branch information
kevoese authored Jul 9, 2019
2 parents 30629b6 + e35479f commit 40e7330
Show file tree
Hide file tree
Showing 24 changed files with 741 additions and 87 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"one-var": 0,
"one-var-declaration-per-line": 0,
"new-cap": 0,
"consistent-return": 0,
"consistent-return": 0,
"no-unused-expressions": 0,
"array-callback-return": 0,
"no-console": "off",
"no-param-reassign": 0,
"class-methods-use-this":"off",
Expand Down
1 change: 1 addition & 0 deletions controllers/profiles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default {
profile: data
});
} catch (e) {
/* istanbul ignore next */
return res.status(500).json({
message: 'Something went wrong'
});
Expand Down
65 changes: 64 additions & 1 deletion controllers/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export default {
user: user.response()
});
} catch (e) {
/* istanbul ignore next */
console.log(e);
/* istanbul ignore next */
return res.status(500).json({
message: 'Something went wrong'
});
Expand Down Expand Up @@ -80,7 +82,9 @@ export default {
user: user.response()
});
} catch (e) {
/* istanbul ignore next */
console.log(e);
/* istanbul ignore next */
return res.status(500).json({
message: 'Something went wrong'
});
Expand Down Expand Up @@ -187,5 +191,64 @@ export default {

home: async (req, res) => res.status(200).send({
user: req.user
})
}),

changeEmailNotification: async (req, res) => {
const { user } = req;
const notifyMe = user.emailNotify;
user.update({
emailNotify: !notifyMe,
});

return res.status(200).send({
message: 'updated'
});
},

changeInAppNotification: async (req, res) => {
const { user } = req;
const notifyMe = user.inAppNotify;
user.update({
inAppNotify: !notifyMe,
});

return res.status(200).send({
message: 'updated'
});
},

getNotifications: async (req, res) => {
const { user } = req;
const { id } = user;

const notifications = await db.Notification.findAll({
where: {
receiverId: id,
},
include: ['notifyMessage'],
});

return res.status(200).send({
notifications
});
},

readNotification: async (req, res) => {
const { user } = req;
const { id } = user;
const notifyId = req.params.id;
const notification = await db.Notification.findOne({
where: {
receiverId: id,
id: notifyId,
}
});
const result = await notification.update({
seen: !notification.seen,
});

return res.status(200).send({
result,
});
}
};
1 change: 0 additions & 1 deletion db/migrations/20190703160309-add_social_to_users_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ module.exports = {
}),
down: queryInterface => queryInterface
.removeColumn('Users', 'social')
.then(() => queryInterface.removeColumn('Users', 'social'))
};
14 changes: 14 additions & 0 deletions db/migrations/20190708143436-alter_user_table_for_notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface
.addColumn('Users', 'emailNotify', {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: true
})
.then(() => queryInterface.addColumn('Users', 'inAppNotify', {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: true
})),
down: queryInterface => queryInterface.removeColumn('Users', 'emailNotify').then(() => queryInterface.removeColumn('Users', 'inAppNotify')),
};
25 changes: 25 additions & 0 deletions db/migrations/20190708150656-create_notification_message_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('NotifyMessages', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
message: {
type: Sequelize.TEXT
},
link: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('NotifyMessages')
};
47 changes: 47 additions & 0 deletions db/migrations/20190708161211-create-notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Notifications', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
receiverId: {
type: Sequelize.INTEGER,
references: {
model: 'Users',
key: 'id',
as: 'receiver',
},
},
senderId: {
type: Sequelize.INTEGER,
references: {
model: 'Users',
key: 'id',
as: 'sender',
},
},
seen: {
type: Sequelize.BOOLEAN,
defaultValue: false
},
notifyMessageId: {
type: Sequelize.INTEGER,
references: {
model: 'NotifyMessages',
key: 'id',
as: 'notifyMessage',
},
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('Notifications')
};
4 changes: 2 additions & 2 deletions db/models/Article.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ module.exports = (sequelize, DataTypes) => {
});
Article.associate = (models) => {
Article.belongsTo(models.User, {
foreginKey: 'userId',
as: 'user',
foreignKey: 'userId',
as: 'author',
});
};
return Article;
Expand Down
25 changes: 25 additions & 0 deletions db/models/Notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = (sequelize, DataTypes) => {
const Notification = sequelize.define(
'Notification',
{
receiverId: DataTypes.INTEGER,
senderId: DataTypes.INTEGER,
seen: DataTypes.BOOLEAN,
notifyMessageId: DataTypes.INTEGER,
},
{}
);

Notification.associate = (models) => {
Notification.belongsTo(models.NotifyMessage, {
foreignKey: 'notifyMessageId',
as: 'notifyMessage',
});

Notification.belongsTo(models.User, {
foreignKey: 'receiverId',
as: 'receiver',
});
};
return Notification;
};
18 changes: 18 additions & 0 deletions db/models/NotifyMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = (sequelize, DataTypes) => {
const NotifyMessage = sequelize.define(
'NotifyMessage',
{
message: DataTypes.TEXT,
link: DataTypes.STRING,
},
{}
);

NotifyMessage.associate = (models) => {
NotifyMessage.hasMany(models.Notification, {
foreignKey: 'notifyMessageId',
as: 'notifications',
});
};
return NotifyMessage;
};
27 changes: 22 additions & 5 deletions db/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ module.exports = (sequelize, DataTypes) => {
passwordResetToken: DataTypes.STRING,
passwordResetExpire: DataTypes.DATE,
emailVerificationToken: DataTypes.STRING,
emailNotify: {
type: DataTypes.BOOLEAN,
default: true,
},
inAppNotify: {
type: DataTypes.BOOLEAN,
default: true,
},
activated: {
type: DataTypes.BOOLEAN,
default: false,
Expand Down Expand Up @@ -46,11 +54,20 @@ module.exports = (sequelize, DataTypes) => {
},
}
);
User.associate = models => User.hasMany(models.Article, {
foreignKey: 'userId',
as: 'article',
cascade: true,
});
User.associate = (models) => {
User.hasMany(models.Article, {
foreignKey: 'userId',
as: 'article',
cascade: true,
});

User.hasMany(models.Notification, {
foreignKey: 'receiverId',
as: 'notifications',
cascade: true,
});
};

User.prototype.passwordsMatch = function match(password) {
return bcrypt.compare(password, this.password);
};
Expand Down
4 changes: 4 additions & 0 deletions env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ BASE_URL=
GOOGLE_CALLBACK=
MAIL_USERNAME=
MAIL_PASSWORD=
PUSHER_APP_ID=
PUSHER_KEY=
PUSHER_SECRET=
PUSHER_CLUSTER=
Loading

0 comments on commit 40e7330

Please sign in to comment.