0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-04 21:12:41 +00:00

More Linting

This commit is contained in:
Trevor Buckner
2024-09-18 15:50:46 -04:00
parent a7c892c1bb
commit 5c2ad7dfee
5 changed files with 103 additions and 103 deletions

View File

@@ -26,7 +26,7 @@ const NotificationAdd = ()=>{
setError('All fields are required'); setError('All fields are required');
return; return;
} }
if (startAt >= stopAt) { if(startAt >= stopAt) {
setError('End date must be after the start date!'); setError('End date must be after the start date!');
return; return;
} }

View File

@@ -45,7 +45,7 @@ const NotificationLookup = ()=>{
setNotifications(res.body || []); setNotifications(res.body || []);
} catch (err) { } catch (err) {
console.log(err); console.log(err);
setError(`Error looking up notifications: ${err.response.body.message}`) setError(`Error looking up notifications: ${err.response.body.message}`);
} finally { } finally {
setSearching(false); setSearching(false);
} }
@@ -71,9 +71,9 @@ const NotificationLookup = ()=>{
lookupAll(); lookupAll();
} catch (err) { } catch (err) {
console.log(err); console.log(err);
setError(`Error deleting notification: ${err.response.body.message}`) setError(`Error deleting notification: ${err.response.body.message}`);
}; };
} };
const renderNotificationsList = ()=>{ const renderNotificationsList = ()=>{
if(error) if(error)

View File

@@ -3,13 +3,13 @@ const React = require('react');
const NotificationLookup = require('./notificationLookup/notificationLookup.jsx'); const NotificationLookup = require('./notificationLookup/notificationLookup.jsx');
const NotificationAdd = require('./notificationAdd/notificationAdd.jsx'); const NotificationAdd = require('./notificationAdd/notificationAdd.jsx');
const NotificationUtils = () => { const NotificationUtils = ()=>{
return ( return (
<section className='notificationUtils'> <section className='notificationUtils'>
<NotificationAdd /> <NotificationAdd />
<NotificationLookup /> <NotificationLookup />
</section> </section>
); );
}; };
module.exports = NotificationUtils; module.exports = NotificationUtils;

View File

@@ -141,35 +141,35 @@ router.get('/admin/stats', mw.adminOnly, async (req, res)=>{
// ####################### NOTIFICATIONS // ####################### NOTIFICATIONS
router.get('/admin/notification/all', async (req, res, next) => { router.get('/admin/notification/all', async (req, res, next)=>{
try { try {
const notifications = await NotificationModel.getAll(); const notifications = await NotificationModel.getAll();
return res.json(notifications); return res.json(notifications);
} catch (error) { } catch (error) {
console.log('Error getting all notifications: ', error.message); console.log('Error getting all notifications: ', error.message);
return res.status(500).json({message: error.message}); return res.status(500).json({ message: error.message });
} }
}); });
router.post('/admin/notification/add', mw.adminOnly, async (req, res, next) => { router.post('/admin/notification/add', mw.adminOnly, async (req, res, next)=>{
console.table(req.body); console.table(req.body);
try { try {
const notification = await NotificationModel.addNotification(req.body); const notification = await NotificationModel.addNotification(req.body);
return res.status(201).json(notification); return res.status(201).json(notification);
} catch (error) { } catch (error) {
console.log('Error adding notification: ', error.message); console.log('Error adding notification: ', error.message);
return res.status(500).json({message: error.message}); return res.status(500).json({ message: error.message });
} }
}); });
router.delete('/admin/notification/delete/:id', mw.adminOnly, async (req, res, next) => { router.delete('/admin/notification/delete/:id', mw.adminOnly, async (req, res, next)=>{
try { try {
const notification = await NotificationModel.deleteNotification(req.params.id); const notification = await NotificationModel.deleteNotification(req.params.id);
return res.json(notification); return res.json(notification);
} catch (error) { } catch (error) {
console.error('Error deleting notification: { key: ', req.params.id , ' error: ', error.message ,' }'); console.error('Error deleting notification: { key: ', req.params.id, ' error: ', error.message, ' }');
return res.status(500).json({message: error.message}); return res.status(500).json({ message: error.message });
} }
}); });
router.get('/admin', mw.adminOnly, (req, res)=>{ router.get('/admin', mw.adminOnly, (req, res)=>{
@@ -177,10 +177,10 @@ router.get('/admin', mw.adminOnly, (req, res)=>{
url : req.originalUrl url : req.originalUrl
}) })
.then((page)=>res.send(page)) .then((page)=>res.send(page))
.catch((err)=> { .catch((err)=>{
console.log(err) console.log(err);
res.sendStatus(500) res.sendStatus(500);
}) });
}); });
module.exports = router; module.exports = router;

View File

@@ -3,102 +3,102 @@ const _ = require('lodash');
// Define the schema for the notification // Define the schema for the notification
const NotificationSchema = new mongoose.Schema({ const NotificationSchema = new mongoose.Schema({
dismissKey: { type: String, unique: true, required: true }, dismissKey : { type: String, unique: true, required: true },
title: { type: String, default: '' }, title : { type: String, default: '' },
text: { type: String, default: '' }, text : { type: String, default: '' },
createdAt: { type: Date, default: Date.now }, createdAt : { type: Date, default: Date.now },
startAt: { type: Date, default: Date.now }, startAt : { type: Date, default: Date.now },
stopAt: { type: Date, default: Date.now }, stopAt : { type: Date, default: Date.now },
}, { versionKey: false }); }, { versionKey: false });
// Static method to get a notification based on a query // Static method to get a notification based on a query
NotificationSchema.statics.get = async function(query, fields = null) { NotificationSchema.statics.get = async function(query, fields = null) {
try { try {
const notifications = await this.find(query, fields).exec(); const notifications = await this.find(query, fields).exec();
if (!notifications.length) throw new Error('Cannot find notification'); if(!notifications.length) throw new Error('Cannot find notification');
return notifications[0]; return notifications[0];
} catch (err) { } catch (err) {
throw new Error(err.message || 'Error finding notification'); throw new Error(err.message || 'Error finding notification');
} }
}; };
// Static method to get a notification by its dismiss key // Static method to get a notification by its dismiss key
NotificationSchema.statics.getByKey = async function(key, fields = null) { NotificationSchema.statics.getByKey = async function(key, fields = null) {
try { try {
const notification = await this.findOne({ dismissKey: key }, fields).lean().exec(); const notification = await this.findOne({ dismissKey: key }, fields).lean().exec();
if (!notification) throw new Error('Cannot find notification'); if(!notification) throw new Error('Cannot find notification');
return notification; return notification;
} catch (err) { } catch (err) {
throw new Error(err.message || 'Error finding notification'); throw new Error(err.message || 'Error finding notification');
} }
}; };
// Static method to add a new notification // Static method to add a new notification
NotificationSchema.statics.addNotification = async function(data) { NotificationSchema.statics.addNotification = async function(data) {
if (!data.dismissKey) return 'Dismiss key is required!'; if(!data.dismissKey) return 'Dismiss key is required!';
const defaults = { const defaults = {
title: '', title : '',
text: '', text : '',
startAt: new Date(), startAt : new Date(),
stopAt: new Date() stopAt : new Date()
}; };
_.defaults(data, defaults); _.defaults(data, defaults);
const newNotification = new this(data); const newNotification = new this(data);
try { try {
const savedNotification = await newNotification.save(); const savedNotification = await newNotification.save();
return savedNotification; return savedNotification;
} catch (err) { } catch (err) {
throw new Error(err.message || 'Error saving notification'); throw new Error(err.message || 'Error saving notification');
} }
}; };
// Static method to update a notification // Static method to update a notification
NotificationSchema.statics.updateNotification = async function(dismissKey, title = null, text = null, startAt = null, stopAt = null) { NotificationSchema.statics.updateNotification = async function(dismissKey, title = null, text = null, startAt = null, stopAt = null) {
if (!dismissKey) return 'No key!'; if(!dismissKey) return 'No key!';
if (!title && !text && !startAt && !stopAt) return 'No data!'; if(!title && !text && !startAt && !stopAt) return 'No data!';
const filter = { dismissKey: dismissKey }; const filter = { dismissKey: dismissKey };
const data = { title, text, startAt, stopAt }; const data = { title, text, startAt, stopAt };
// Remove null values from the data object // Remove null values from the data object
for (const [key, value] of Object.entries(data)) { for (const [key, value] of Object.entries(data)) {
if (value === null) delete data[key]; if(value === null) delete data[key];
} }
try { try {
const updatedNotification = await this.findOneAndUpdate(filter, data, { new: true }).exec(); const updatedNotification = await this.findOneAndUpdate(filter, data, { new: true }).exec();
if (!updatedNotification) throw new Error('Cannot find notification'); if(!updatedNotification) throw new Error('Cannot find notification');
return updatedNotification; return updatedNotification;
} catch (err) { } catch (err) {
throw new Error(err.message || 'Error updating notification'); throw new Error(err.message || 'Error updating notification');
} }
}; };
// Static method to delete a notification // Static method to delete a notification
NotificationSchema.statics.deleteNotification = async function(dismissKey) { NotificationSchema.statics.deleteNotification = async function(dismissKey) {
if (!dismissKey) return 'No key provided!'; if(!dismissKey) return 'No key provided!';
try { try {
const deletedNotification = await this.findOneAndDelete({ dismissKey }).exec(); const deletedNotification = await this.findOneAndDelete({ dismissKey }).exec();
if (!deletedNotification) throw new Error('Notification not found'); if(!deletedNotification) throw new Error('Notification not found');
return deletedNotification; return deletedNotification;
} catch (err) { } catch (err) {
throw new Error(err.message || 'Error deleting notification'); throw new Error(err.message || 'Error deleting notification');
} }
}; };
// Static method to get all notifications // Static method to get all notifications
NotificationSchema.statics.getAll = async function() { NotificationSchema.statics.getAll = async function() {
try { try {
const notifications = await this.find().exec(); const notifications = await this.find().exec();
return notifications; return notifications;
} catch (err) { } catch (err) {
throw new Error(err.message || 'Error retrieving notifications'); throw new Error(err.message || 'Error retrieving notifications');
} }
}; };
// Create and export the model // Create and export the model
const Notification = mongoose.model('Notification', NotificationSchema); const Notification = mongoose.model('Notification', NotificationSchema);
module.exports = { module.exports = {
schema: NotificationSchema, schema : NotificationSchema,
model: Notification, model : Notification,
}; };