Notifications UI update and delete endpoint

This commit is contained in:
advplyr 2022-09-23 18:10:03 -05:00
parent b08ad8785e
commit 37a3fdb606
6 changed files with 95 additions and 21 deletions

View File

@ -210,7 +210,7 @@ export default {
} }
console.log('Sending create notification', payload) console.log('Sending create notification', payload)
this.$axios this.$axios
.$post('/api/notifications/event', payload) .$post('/api/notifications', payload)
.then(() => { .then(() => {
this.$toast.success('Notification created') this.$toast.success('Notification created')
}) })

View File

@ -23,7 +23,17 @@
</div> </div>
<template v-for="notification in notifications"> <template v-for="notification in notifications">
<div :key="notification.id" class="w-full bg-primary rounded-xl p-4"> <div :key="notification.id" class="w-full bg-primary rounded-xl p-4">
<p>{{ notification.eventName }}</p> <div class="flex items-center">
<p class="text-lg font-semibold">{{ notification.eventName }}</p>
<div class="flex-grow" />
<ui-btn :loading="sendingTest" small class="mr-2" @click="sendTest(notification)">Test</ui-btn>
<ui-icon-btn bg-color="error" :size="7" icon-font-size="1.2rem" icon="delete" @click="deleteNotification(notification)" />
</div>
<div class="pt-4">
<p class="text-gray-300">{{ notification.urls.join(', ') }}</p>
</div>
</div> </div>
</template> </template>
</div> </div>
@ -42,11 +52,40 @@ export default {
notificationSettings: null, notificationSettings: null,
notificationData: null, notificationData: null,
showEditModal: false, showEditModal: false,
selectedNotification: null selectedNotification: null,
sendingTest: false
} }
}, },
computed: {}, computed: {},
methods: { methods: {
deleteNotification(notification) {
this.$axios
.$delete(`/api/notifications/${notification.id}`)
.then(() => {
this.$toast.success('Deleted notification')
this.notificationSettings.notifications = this.notificationSettings.notifications.filter((n) => n.id !== notification.id)
this.notifications = this.notificationSettings.notifications
})
.catch((error) => {
console.error('Failed', error)
this.$toast.error('Failed to delete notification')
})
},
sendTest(notification) {
this.sendingTest = true
this.$axios
.$get(`/api/notifications/${notification.id}/test`)
.then(() => {
this.$toast.success('Triggered test notification')
})
.catch((error) => {
console.error('Failed', error)
this.$toast.error('Failed to trigger test notification')
})
.finally(() => {
this.sendingTest = false
})
},
clickCreate() { clickCreate() {
this.selectedNotification = null this.selectedNotification = null
this.showEditModal = true this.showEditModal = true

View File

@ -18,17 +18,8 @@ class NotificationController {
res.sendStatus(200) res.sendStatus(200)
} }
async createEvent(req, res) { async createNotification(req, res) {
const success = this.db.notificationSettings.addNewEvent(req.body) const success = this.db.notificationSettings.createNotification(req.body)
if (success) {
await this.db.updateEntity('settings', this.db.notificationSettings)
}
res.sendStatus(200)
}
async updateEvent(req, res) {
const success = this.db.notificationSettings.updateEvent(req.body)
if (success) { if (success) {
await this.db.updateEntity('settings', this.db.notificationSettings) await this.db.updateEntity('settings', this.db.notificationSettings)
@ -40,10 +31,40 @@ class NotificationController {
res.json(this.notificationManager.getData()) res.json(this.notificationManager.getData())
} }
async deleteNotification(req, res) {
if (this.db.notificationSettings.removeNotification(req.notification.id)) {
await this.db.updateEntity('settings', this.db.notificationSettings)
}
res.sendStatus(200)
}
async updateNotification(req, res) {
const success = this.db.notificationSettings.updateNotification(req.body)
if (success) {
await this.db.updateEntity('settings', this.db.notificationSettings)
}
res.sendStatus(200)
}
sendNotificationTest(req, res) {
this.notificationManager.onTest()
res.sendStatus(200)
}
middleware(req, res, next) { middleware(req, res, next) {
if (!req.user.isAdminOrUp) { if (!req.user.isAdminOrUp) {
return res.sendStatus(404) return res.sendStatus(404)
} }
if (req.params.id) {
const notification = this.db.notificationSettings.getNotification(req.params.id)
if (!notification) {
return res.sendStatus(404)
}
req.notification = notification
}
next() next()
} }
} }

View File

@ -48,8 +48,8 @@ class NotificationManager {
sendNotification(notification, eventData) { sendNotification(notification, eventData) {
const payload = notification.getApprisePayload(eventData) const payload = notification.getApprisePayload(eventData)
return axios.post(`${this.db.notificationSettings.appriseApiUrl}/notify`, payload, { timeout: 6000 }).then((data) => { return axios.post(`${this.db.notificationSettings.appriseApiUrl}/notify`, payload, { timeout: 6000 }).then((response) => {
Logger.debug(`[NotificationManager] sendNotification: ${notification.eventName}/${notification.id} response=${data}`) Logger.debug(`[NotificationManager] sendNotification: ${notification.eventName}/${notification.id} response=`, response.data)
return true return true
}).catch((error) => { }).catch((error) => {
Logger.error(`[NotificationManager] sendNotification: ${notification.eventName}/${notification.id} error=`, error) Logger.error(`[NotificationManager] sendNotification: ${notification.eventName}/${notification.id} error=`, error)

View File

@ -15,7 +15,7 @@ class NotificationSettings {
construct(settings) { construct(settings) {
this.appriseType = settings.appriseType this.appriseType = settings.appriseType
this.appriseApiUrl = settings.appriseApiUrl || null this.appriseApiUrl = settings.appriseApiUrl || null
this.notifications = (settings.notifications || []).map(n => ({ ...n })) this.notifications = (settings.notifications || []).map(n => new Notification(n))
} }
toJSON() { toJSON() {
@ -35,6 +35,18 @@ class NotificationSettings {
return this.notifications.filter(n => n.eventName === eventName) return this.notifications.filter(n => n.eventName === eventName)
} }
getNotification(id) {
return this.notifications.find(n => n.id === id)
}
removeNotification(id) {
if (this.notifications.some(n => n.id === id)) {
this.notifications = this.notifications.filter(n => n.id !== id)
return true
}
return false
}
update(payload) { update(payload) {
if (!payload) return false if (!payload) return false
if (payload.appriseApiUrl !== this.appriseApiUrl) { if (payload.appriseApiUrl !== this.appriseApiUrl) {
@ -44,7 +56,7 @@ class NotificationSettings {
return false return false
} }
addNewEvent(payload) { createNotification(payload) {
if (!payload) return false if (!payload) return false
// TODO: validate // TODO: validate
@ -54,7 +66,7 @@ class NotificationSettings {
return true return true
} }
updateEvent(payload) { updateNotification(payload) {
if (!payload) return false if (!payload) return false
const notification = this.notifications.find(n => n.id === payload.id) const notification = this.notifications.find(n => n.id === payload.id)
if (!notification) return false if (!notification) return false

View File

@ -206,9 +206,11 @@ class ApiRouter {
// //
this.router.get('/notifications', NotificationController.middleware.bind(this), NotificationController.get.bind(this)) this.router.get('/notifications', NotificationController.middleware.bind(this), NotificationController.get.bind(this))
this.router.patch('/notifications', NotificationController.middleware.bind(this), NotificationController.update.bind(this)) this.router.patch('/notifications', NotificationController.middleware.bind(this), NotificationController.update.bind(this))
this.router.post('/notifications/event', NotificationController.middleware.bind(this), NotificationController.createEvent.bind(this))
this.router.patch('/notifications/event', NotificationController.middleware.bind(this), NotificationController.updateEvent.bind(this))
this.router.get('/notificationdata', NotificationController.middleware.bind(this), NotificationController.getData.bind(this)) this.router.get('/notificationdata', NotificationController.middleware.bind(this), NotificationController.getData.bind(this))
this.router.post('/notifications', NotificationController.middleware.bind(this), NotificationController.createNotification.bind(this))
this.router.delete('/notifications/:id', NotificationController.middleware.bind(this), NotificationController.deleteNotification.bind(this))
this.router.patch('/notifications/:id', NotificationController.middleware.bind(this), NotificationController.updateNotification.bind(this))
this.router.get('/notifications/:id/test', NotificationController.middleware.bind(this), NotificationController.sendNotificationTest.bind(this))
// //
// Misc Routes // Misc Routes