-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
210 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import dotenv from 'dotenv'; | ||
import Mailer from '../helpers/mailer'; | ||
import models from '../models/index'; | ||
import mailLinkMaker from '../helpers/mailLinkMaker'; | ||
|
||
const { user: UserModel, followers: followersModel, notifications: notificationModel } = models; | ||
|
||
dotenv.config(); | ||
/** | ||
* this class distributed the notifications | ||
*/ | ||
class Notifications { | ||
/** | ||
* @author frank harerimana | ||
* @param {*} userid | ||
* @returns {*} welcome notification | ||
*/ | ||
static async userVerifiedAccount(userid) { | ||
const user = await UserModel.findUser(userid); | ||
Mailer.messageMaker(user.email, 'no-reply', 'Thanks for verifying account', `proceed and explore the web | ||
<a href='${process.env.APP_URL}'> here </a>`); | ||
return 'success'; | ||
} | ||
|
||
/** | ||
* @author frank harerimana | ||
* @param {*} id | ||
* @returns {*} reset password success nitification | ||
*/ | ||
static async resetpassword(id) { | ||
const user = await UserModel.findUser(id); | ||
Mailer.messageMaker(user.email, 'no-reply Authors haven', 'Your password has been changed successfully', ` | ||
<strong>click the link below to login again<strong> | ||
<a href='${process.env.APP_URL}'>Author's haven</a>`); | ||
return user; | ||
} | ||
|
||
/** | ||
* @author frank harerimana | ||
* @param {*} id | ||
* @returns {*} user | ||
*/ | ||
static async user(id) { | ||
const res = await UserModel.findUser(id); | ||
return res; | ||
} | ||
|
||
/** | ||
* @author frank harerimana | ||
* @param {*} id | ||
* @param {*} message | ||
* @param {*} link | ||
* @returns {*} user | ||
*/ | ||
static async NewNotification(id, message, link) { | ||
const res = await notificationModel.newRecord(id, 'article', message, link); | ||
return res; | ||
} | ||
|
||
/** | ||
* @author frank harerimana | ||
* @param {*} users | ||
* @param {*} author | ||
* @param {*} slug | ||
* @returns {*} user | ||
*/ | ||
static async sendNotifications(users, author, slug) { | ||
const notificationLink = await new mailLinkMaker(`${slug}`).article(); | ||
const message = `${author} published a new article`; | ||
for (let i = 0; i < users.length; i += 1) { | ||
this.NewNotification(users[i].id, message, notificationLink); | ||
if (users[i].email === true) Mailer.messageMaker(users[i].email, notificationLink, message, 'read the new article'); | ||
} | ||
return 'success'; | ||
} | ||
|
||
/** | ||
* @author frank harerimana | ||
* @param {*} authorId | ||
* @param {*} slug | ||
* @returns {*} success | ||
*/ | ||
static async createArticle(authorId, slug) { | ||
try { | ||
const userArray = []; | ||
const followers = await followersModel.followers(authorId); | ||
followers.map(element => userArray.push(element.dataValues.follower)); | ||
const result = []; | ||
// check user emails and notification settings | ||
for (let i = 0; i < userArray.length; i += 1) { | ||
result.push(this.user(userArray[i])); | ||
} | ||
const res = await Promise.all(result); | ||
const users = res.map((element) => { | ||
const respo = element.dataValues; | ||
return respo; | ||
}); | ||
const author = await UserModel.findUser(authorId); | ||
await this.sendNotifications(users, author.dataValues.username, slug); | ||
return 'success'; | ||
} catch (error) { | ||
return error; | ||
} | ||
} | ||
} | ||
|
||
export default Notifications; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* eslint-disable class-methods-use-this */ | ||
import { EventEmitter } from 'events'; | ||
import notification from '../controllers/notifications'; | ||
|
||
/** | ||
* notification handler | ||
*/ | ||
class ArticleEvents extends EventEmitter { | ||
/** | ||
* @author frank harerimana | ||
* @param {*} args | ||
* @param {*} slug | ||
* @returns {*} call notification controller | ||
*/ | ||
create(args) { | ||
notification.createArticle(args.authorid, args.slug); | ||
return 'success'; | ||
} | ||
} | ||
|
||
export default ArticleEvents; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import chai from 'chai'; | ||
import debug from 'debug'; | ||
import dotenv from 'dotenv'; | ||
import faker from 'faker'; | ||
import models from '../models/index'; | ||
import notification from '../controllers/notifications'; | ||
|
||
const { notifications: notificationModel } = models; | ||
|
||
dotenv.config(); | ||
process.env.NODE_ENV = 'test'; | ||
chai.should(); | ||
|
||
const logError = debug('app:*'); | ||
const users = [ | ||
{ | ||
id: 1, | ||
firstname: null, | ||
lastname: null, | ||
username: 'franklisnsv', | ||
email: 'harfrank3@gmailv.com', | ||
inapp_notifications: true, | ||
email_notifications: true | ||
} | ||
]; | ||
const data = { | ||
message: faker.lorem.words(), | ||
username: faker.name.findName(), | ||
link: faker.internet.url(), | ||
slug: faker.internet.domainWord() | ||
}; | ||
describe('test notification models method', () => { | ||
it('should be able insert notification', async () => { | ||
try { | ||
await notificationModel.newRecord(1, 'article', data.message, data.link); | ||
} catch (error) { | ||
logError(error); | ||
} | ||
}); | ||
}); | ||
|
||
describe('test notification controller mothods', () => { | ||
it('should be able insert notifications', async () => { | ||
try { | ||
await notification.createArticle(1, data.slug); | ||
} catch (error) { | ||
logError(error); | ||
} | ||
}); | ||
}); | ||
|
||
describe('test notification controller methods', () => { | ||
it('should be able send notifications', async () => { | ||
try { | ||
await notification.sendNotifications(users, data.username, data.slug); | ||
} catch (error) { | ||
logError(error); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters