mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-08 18:22:40 +00:00
"Updated mock implementation for deleting a notification in admin API test"
This commit is contained in:
@@ -11,40 +11,50 @@ const NotificationSchema = new mongoose.Schema({
|
|||||||
}, { versionKey: false });
|
}, { versionKey: false });
|
||||||
|
|
||||||
NotificationSchema.statics.addNotification = async function(data) {
|
NotificationSchema.statics.addNotification = async function(data) {
|
||||||
if(!data.dismissKey) return 'Dismiss key is required!';
|
if(!data.dismissKey) return { success: false, message: '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);
|
|
||||||
const newNotification = new this(data);
|
const notificationData = _.defaults(data, defaults);
|
||||||
|
|
||||||
|
if(!(notificationData.startAt instanceof Date) || !(notificationData.stopAt instanceof Date)) {
|
||||||
|
return { success: false, message: 'Invalid date format for startAt or stopAt' };
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const newNotification = new this(notificationData);
|
||||||
const savedNotification = await newNotification.save();
|
const savedNotification = await newNotification.save();
|
||||||
return savedNotification;
|
return { success: true, notification: savedNotification };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(err.message || 'Error saving notification');
|
return { success: false, message: err.message || 'Error saving notification' };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
NotificationSchema.statics.deleteNotification = async function(dismissKey) {
|
NotificationSchema.statics.deleteNotification = async function(dismissKey) {
|
||||||
if(!dismissKey) return 'No key provided!';
|
if(!dismissKey) return { success: false, message: '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) {
|
||||||
return deletedNotification;
|
return { success: false, message: 'Notification not found' };
|
||||||
|
}
|
||||||
|
return { success: true, notification: deletedNotification };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(err.message || 'Error deleting notification');
|
return { success: false, message: err.message || 'Error deleting notification' };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
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 { success: true, notifications };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(err.message || 'Error retrieving notifications');
|
return { success: false, message: err.message || 'Error retrieving notifications' };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user