Skip to content

Commit

Permalink
Merge f78b0ec into 22566b5
Browse files Browse the repository at this point in the history
  • Loading branch information
olusoladavid committed Aug 24, 2018
2 parents 22566b5 + f78b0ec commit cecd1b4
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 1 deletion.
83 changes: 83 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"pg": "^7.4.3",
"pg-connection-string": "^2.0.0",
"swagger-ui-express": "^3.0.10",
"web-push": "^3.3.2",
"yamljs": "^0.3.0"
},
"nyc": {
Expand Down
40 changes: 40 additions & 0 deletions server/workers/sendReminder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'babel-polyfill';
import webpush from 'web-push';
import dotenv from 'dotenv';
import { query } from '../db/index';

dotenv.config();

// general config
const pubKey = process.env.PUSH_PUB_KEY;
const privKey = process.env.PUSH_PRIV_KEY;

webpush.setVapidDetails('https://twitter.com/olusola_dev', pubKey, privKey);

// get all users from database
const getAllUsers = async () => {
const users = await query('SELECT * FROM users');
return users.rows;
};

// TODO: RAM optimization - get users in batches
const processNotifications = async () => {
const users = await getAllUsers().catch(() => {
console.error('Could not connect to database');
});
if (users) {
users.forEach(async (user) => {
if (user.reminderisset) {
const msg = '{ type: reminder }';
const options = { TTL: 86400 };
await webpush.sendNotification(user.push_sub, msg, options).catch(() => {
console.error(`Could not send reminder for user: ${user.email}`);
});
}
});
}
};

processNotifications();

export default processNotifications;
6 changes: 5 additions & 1 deletion test/sampleData.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ const sampleData = {
invalidEntryId: 'id',
nonExistentId: 5,
validProfile: {
push_sub: JSON.stringify({ channel: 'foo' }),
push_sub: JSON.stringify({ keys: 'foo' }),
reminder_set: true,
},
noReminderProfile: {
reminder_set: false,
},
invalidProfile: {
reminder_set: '',
Expand Down
41 changes: 41 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import chai from 'chai';
import chaiHttp from 'chai-http';
import { before } from 'mocha';
import sinon from 'sinon';
import webpush from 'web-push';
import app from '../server/index';
import sampleData from './sampleData';
import { pool, query } from '../server/db/index';
import createTables from '../server/db/createTables';
import processNotifications from '../server/workers/sendReminder';

const { expect } = chai;
chai.use(chaiHttp);
Expand Down Expand Up @@ -716,3 +718,42 @@ describe('/PUT profile error handling', () => {
done();
});
});

describe('Daily reminder feature', () => {
it('should not send a notification when a database error occurs', async () => {
pool.query.restore();
sinon.stub(pool, 'query').alwaysCalledWith('invalidQuery');
const webPushStub = sinon.spy(webpush, 'sendNotification');
await processNotifications();
expect(webPushStub.called).to.be.eql(false);
pool.query.restore();
webPushStub.restore();
});

it('should return a promise that resolves when notification is sent', async () => {
const webPushStub = sinon.stub(webpush, 'sendNotification');
webPushStub.resolves();
await processNotifications();
expect(webPushStub.called).to.be.eql(true);
webPushStub.restore();
});

it('should return a promise that rejects when notification is not sent', async () => {
const webPushStub = await sinon.stub(webpush, 'sendNotification').rejects();
await processNotifications();
expect(webPushStub.called).to.be.eql(true);
webPushStub.restore();
});

it('should not send a notification when the reminder is not set', async () => {
await chai
.request(app)
.put('/api/v1/profile')
.set('Authorization', makeAuthHeader(token))
.send(sampleData.noReminderProfile);
const webPushSpy = sinon.spy(webpush, 'sendNotification');
await processNotifications();
expect(webPushSpy.called).to.be.eql(false);
webPushSpy.restore();
});
});

0 comments on commit cecd1b4

Please sign in to comment.