This repository has been archived by the owner on Jul 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerts.js
114 lines (102 loc) · 2.3 KB
/
alerts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import {Meteor} from 'meteor/meteor';
import {Mongo} from 'meteor/mongo';
import {_} from 'meteor/underscore';
/**
* The collection of alerts
* @type {Mongo.Collection}
*/
const Alerts = new Mongo.Collection(null);
/**
* The alert helper
*/
class AlertHelper {
constructor() {
this.id = Date.now() + Math.random();
}
/**
* Adds an alert
* @return {*}
* @param type
* @param message
* @param extra
* @returns {*}
*/
add(type, message, extra) {
if (typeof message === 'object' && !message) {
if (message.reason) {
message = message.reason;
}
else if (message.message) {
message = message.message;
}
}
return Alerts.insert(_.extend({
type: type,
createdAt: new Date(),
helperId: this.id,
message: message
}, extra));
}
/**
* Adds an error
* @return {*}
* @param message
* @param extra
* @returns {*}
*/
addError(message, extra) {
return this.add('error', message, extra);
}
/**
* Adds an info alert
* @return {*}
* @param message
* @param extra
* @returns {*}
*/
addInfo(message, extra) {
return this.add('info', message, extra);
}
/**
* Adds a success alert
* @return {*}
* @param message
* @param extra
* @returns {*}
*/
addSuccess(message, extra) {
return this.add('success', message, extra);
}
/**
* Adds a warning alert
* @return {*}
* @param message
* @param extra
* @returns {*}
*/
addWarning(message, extra) {
return this.add('warning', message, extra);
}
/**
* Removes alerts matching filters
* @param filters
*/
clear(filters) {
return Alerts.remove(_.extend({}, filters, {helperId: this.id}));
}
/**
* Returns alerts matching filters
* @param filters
* @param options
* @return {Mongo.Cursor}
*/
find(filters, options) {
filters = _.extend({}, filters, {helperId: this.id});
options = _.extend({sort: {createdAt: -1}}, options);
return Alerts.find(filters, options);
}
}
export {
AlertHelper,
Alerts
};