0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-27 11:32:38 +00:00

Initial notificationAdd functionality

This commit is contained in:
G.Ambatte
2023-01-15 13:54:19 +13:00
parent 8adf5ce463
commit 04916d8931
4 changed files with 147 additions and 19 deletions

View File

@@ -100,13 +100,18 @@ router.get('/admin/stats', mw.adminOnly, (req, res)=>{
});
});
/* Searches for matching edit or share id, also attempts to partial match */
/* Searches for notification with matching key */
router.get('/admin/notification/lookup/:id', mw.adminOnly, (req, res, next)=>{
NotificationModel.findOne({ $or : [
{ dismissKey: { '$regex': req.params.id } },
] }).exec((err, notification)=>{
return res.json(notification);
});
NotificationModel.findOne({ dismissKey: req.params.id })
.exec((err, notification)=>{
return res.json(notification);
});
});
/* Add new notification */
router.post('/admin/notification/add', mw.adminOnly, async (req, res, next)=>{
const notification = await NotificationModel.addNotification(req.body);
return res.json(notification);
});
router.get('/admin', mw.adminOnly, (req, res)=>{

View File

@@ -2,9 +2,9 @@ const mongoose = require('mongoose');
const _ = require('lodash');
const NotificationSchema = mongoose.Schema({
dissmissKey : { type: String, index: { unique: true } },
title : { type: String, default: '' },
text : { type: String, default: '' },
dismissKey : { type: String, unique: true, required: true },
title : { type: String, default: '' },
text : { type: String, default: '' },
createdAt : { type: Date, default: Date.now },
startAt : { type: Date, default: Date.now },
@@ -30,19 +30,23 @@ NotificationSchema.statics.getByKey = function(key, fields=null){
});
};
NotificationSchema.statics.addNotification = async function(dismissKey, text, title=null, startAt=new Date, stopAt=new Date){
const data = {
dismissKey : dismissKey,
title : title,
text : text,
startAt : startAt,
stopAt : stopAt
NotificationSchema.statics.addNotification = async function(data){
// console.log('add notification');
if(!data.dismissKey) return 'Dismiss key is required!';
const defaults = {
title : '',
text : '',
startAt : new Date,
stopAt : new Date
};
_.mergeWith(data, defaults, (item)=>{ if(!item) return undefined; });
const newNotification = new Notification(data);
await newNotification.save()
.catch((err)=>{return err;});
const savedNotification = await newNotification.save()
.catch((err)=>{
return { err: err };
});
return newNotification;
return savedNotification;
};
NotificationSchema.statics.updateNotification = async function(dismissKey, title=null, text=null, startAt=null, stopAt=null){