2020-09-02 13:27:09 +00:00
|
|
|
const { Job } = require('flitter-jobs')
|
|
|
|
|
|
|
|
class PushNotifyJob extends Job {
|
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'models', 'jobs', 'output']
|
|
|
|
}
|
|
|
|
|
|
|
|
async execute(job) {
|
|
|
|
try {
|
|
|
|
const User = this.models.get('auth:User')
|
|
|
|
|
|
|
|
const { data } = job
|
|
|
|
let { title = '', message, priority = 5, user_id } = data
|
|
|
|
|
|
|
|
const user = await User.findById(user_id)
|
|
|
|
if ( !user ) throw new Error('Invalid user_id.')
|
|
|
|
|
|
|
|
const notify = user.notify_config
|
|
|
|
if ( !notify || !notify.active ) throw new Error('User does not have notifications configured.')
|
|
|
|
|
2020-12-05 02:40:27 +00:00
|
|
|
this.info(`Sending notification to ${user.uid}...`)
|
2020-09-02 13:27:09 +00:00
|
|
|
|
|
|
|
await notify.send({ title, message, priority })
|
|
|
|
} catch (e) {
|
2020-12-05 02:40:27 +00:00
|
|
|
this.error(e)
|
|
|
|
throw e
|
2020-09-02 13:27:09 +00:00
|
|
|
}
|
2020-12-05 02:40:27 +00:00
|
|
|
|
|
|
|
this.success(`Notification sent!`)
|
2020-09-02 13:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = PushNotifyJob
|