Skip to content

Commit

Permalink
feat(inApp-notification-opt): Implement endpoint that control in-app opt
Browse files Browse the repository at this point in the history
- Write unit test
- Write documentation
- Modify user table
- Modify user in-app notification opt

[Finishes #167891584]
  • Loading branch information
Pomile committed Sep 16, 2019
1 parent 5791108 commit 0ff34df
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 14 deletions.
38 changes: 37 additions & 1 deletion src/controllers/NotificationOpt.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import Response from '../helpers/Response';
import modifyUserNotificationOpt from '../helpers/modifyUserNotificationOpt';

const { User } = db;
/** */
class NotificationOpt {
/**
* @param {req} req
* @param {res} res
* @returns {object} object
*/
static async modifyEmailNotificationOption(req, res) {
const { emailOpt } = req.body;
const { payload } = req.payload;
const { email } = payload;
const [ rowRetured, updatedUser] = await modifyUserNotificationOpt({ emailOpt }, email);
const [rowRetured, updatedUser] = await modifyUserNotificationOpt({ emailOpt }, email);
const opt = emailOpt ? 'on' : 'off';

const response = new Response(
Expand All @@ -24,6 +30,36 @@ class NotificationOpt {

return res.status(response.code).send(response);
}

/**
* @param {req} req
* @param {res} res
* @returns {object} object
*/
static async modifyInAppNotificationOption(req, res) {
const { inAppOpt } = req.body;
const { payload } = req.payload;
const { email } = payload;
const [rowRetured, updatedUser] = await modifyUserNotificationOpt(
{ inAppOpt },
email
);
const opt = inAppOpt ? 'on' : 'off';

const response = new Response(
true,
200,
`In-app notification turned ${opt}successfully`,
{
id: updatedUser.id,
email: updatedUser.email,
inAppOpt: updatedUser.inAppOpt
}
);

return res.status(response.code).send(response);

}
}

export default NotificationOpt;
16 changes: 11 additions & 5 deletions src/routes/notificationOpt.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ import { Router } from 'express';
import NotificationOpt from '../controllers/NotificationOpt';
import validator from '../middlewares/validator';
import TokenHelper from '../helpers/Token';
import { emailOptSchema, inAppOptSchema } from '../validation/notificationOptSchema'
import { emailOptSchema, inAppOptSchema } from '../validation/notificationOptSchema';

const notificationOptRoute = Router();

notificationOptRoute.patch(
'/email_opt',
TokenHelper.verifyToken,
validator(emailOptSchema),
NotificationOpt.modifyEmailNotificationOption,
'/email_opt',
TokenHelper.verifyToken,
validator(emailOptSchema),
NotificationOpt.modifyEmailNotificationOption,
);
notificationOptRoute.patch(
'/in_app_opt',
TokenHelper.verifyToken,
validator(inAppOptSchema),
NotificationOpt.modifyInAppNotificationOption
);

export default notificationOptRoute;
13 changes: 12 additions & 1 deletion src/validation/notificationOptSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@ const emailOptSchema = [
.withMessage('Email option must be a boolean')
];

export { emailOptSchema }
const inAppOptSchema = [
check('inAppOpt')
.exists()
.withMessage('In App option is required')
.isBoolean()
.withMessage('In App option must be a boolean')
];

export {
inAppOptSchema,
emailOptSchema
};
73 changes: 66 additions & 7 deletions test/notificationOpt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ describe("NOTIFICATION OPTION TEST", () => {
.send({ emailOpt: true })
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.an("object");
expect(res.body.data.email).to.equal("ogedengbe123@gmail.com");
expect(res.body).to.be.an('object');
expect(res.body.data.email).to.equal('ogedengbe123@gmail.com');
expect(res.body.data.emailOpt).to.equal(true);
done();
});
});
it("should turn off email notification for admin user", done => {
it('should turn off email notification for admin user', done => {
const { token } = adminAuth;
chai
.request(app)
Expand All @@ -33,13 +33,13 @@ describe("NOTIFICATION OPTION TEST", () => {
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.an("object");
expect(res.body.data.email).to.equal("ogedengbe123@gmail.com");
expect(res.body.data.email).to.equal('ogedengbe123@gmail.com');
expect(res.body.data.emailOpt).to.equal(false);
done();
});
});

it("should modify turn email notification without emailOpt", done => {
it('should modify turn email notification without emailOpt', done => {
const { token } = adminAuth;
chai
.request(app)
Expand All @@ -52,7 +52,7 @@ describe("NOTIFICATION OPTION TEST", () => {
done();
});
});
it("should modify turn email notification without emailOpt", done => {
it('should modify turn email notification without emailOpt', done => {
const { token } = adminAuth;
chai
.request(app)
Expand All @@ -62,7 +62,66 @@ describe("NOTIFICATION OPTION TEST", () => {
.end((err, res) => {
expect(res).to.have.status(400);
expect(res.body.data.emailOpt).to.equal(
"Email option must be a boolean"
'Email option must be a boolean'
);
done();
});
});
it("should turn on in-app notification for admin user", done => {
const { token } = adminAuth;
chai
.request(app)
.patch(`/api/v1/notifications/in_app_opt`)
.set({ authorization: `${token}` })
.send({ inAppOpt: true })
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.an("object");
expect(res.body.data.email).to.equal("ogedengbe123@gmail.com");
expect(res.body.data.inAppOpt).to.equal(true);
done();
});
});
it("should turn off in-app notification for admin user", done => {
const { token } = adminAuth;
chai
.request(app)
.patch(`/api/v1/notifications/in_app_opt`)
.set({ authorization: `${token}` })
.send({ inAppOpt: false })
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.an("object");
expect(res.body.data.email).to.equal("ogedengbe123@gmail.com");
expect(res.body.data.inAppOpt).to.equal(false);
done();
});
});

it("should not modify turn on notification without inAppOpt", done => {
const { token } = adminAuth;
chai
.request(app)
.patch(`/api/v1/notifications/in_app_opt`)
.set({ authorization: `${token}` })
.send({})
.end((err, res) => {
expect(res).to.have.status(400);
expect(res.body.data.inAppOpt).to.equal("In App option is required");
done();
});
});
it("should not modify turn email notification with invalid inAppOpt", done => {
const { token } = adminAuth;
chai
.request(app)
.patch(`/api/v1/notifications/in_app_opt`)
.set({ authorization: `${token}` })
.send({ inAppOpt: 'trueggfgfsgf' })
.end((err, res) => {
expect(res).to.have.status(400);
expect(res.body.data.inAppOpt).to.equal(
"In App option must be a boolean"
);
done();
});
Expand Down

0 comments on commit 0ff34df

Please sign in to comment.