-
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
5 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,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; |
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