|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | -import moment from 'moment'; |
17 | | - |
18 | 16 | import sbaConfig from '@/sba-config'; |
19 | 17 | import axios from '@/utils/axios'; |
20 | 18 | import uri from '@/utils/uri'; |
| 19 | +import Application from "@/services/application"; |
| 20 | +import Instance from "@/services/instance"; |
| 21 | + |
| 22 | +export type NotificationFilterProps = { |
| 23 | + id: string, |
| 24 | + applicationName: string, |
| 25 | + instanceId: string, |
| 26 | + expiry: string, |
| 27 | + expired: boolean |
| 28 | +} |
21 | 29 |
|
22 | 30 | class NotificationFilter { |
23 | | - private id: string; |
24 | | - private applicationName: string; |
25 | | - private instanceId: string; |
26 | | - private expiry: moment.Moment | null; |
27 | | - |
28 | | - constructor({ expiry, ...filter }) { |
29 | | - Object.assign(this, filter); |
30 | | - this.expiry = expiry ? moment(expiry) : null; |
31 | | - } |
32 | | - |
33 | | - affects(obj) { |
34 | | - if (!obj) { |
35 | | - return false; |
| 31 | + public readonly expired: boolean; |
| 32 | + private readonly id: string; |
| 33 | + private readonly applicationName: string; |
| 34 | + private readonly instanceId: string; |
| 35 | + |
| 36 | + constructor({id, applicationName, instanceId, expiry, expired, ...filter}: NotificationFilterProps) { |
| 37 | + Object.assign(this, filter); |
| 38 | + this.id = id; |
| 39 | + this.applicationName = applicationName; |
| 40 | + this.instanceId = instanceId; |
| 41 | + this.expired = expired; |
36 | 42 | } |
37 | 43 |
|
38 | | - if (this.isApplicationFilter) { |
39 | | - return this.applicationName === obj.name; |
| 44 | + static isSupported() { |
| 45 | + return Boolean(sbaConfig.uiSettings.notificationFilterEnabled); |
40 | 46 | } |
41 | 47 |
|
42 | | - if (this.isInstanceFilter) { |
43 | | - return this.instanceId === obj.id; |
| 48 | + static async getFilters() { |
| 49 | + return axios.get('notifications/filters', { |
| 50 | + transformResponse: NotificationFilter._transformResponse, |
| 51 | + }); |
44 | 52 | } |
45 | 53 |
|
46 | | - return false; |
47 | | - } |
48 | | - |
49 | | - get isApplicationFilter() { |
50 | | - return this.applicationName != null; |
51 | | - } |
52 | | - |
53 | | - get isInstanceFilter() { |
54 | | - return this.instanceId != null; |
55 | | - } |
| 54 | + static async addFilter(object: Instance | Application, ttl: number) { |
| 55 | + const params = {ttl} as { ttl: number, applicationName?: string; instanceId?: string }; |
| 56 | + if (object instanceof Application) { |
| 57 | + params.applicationName = object.name; |
| 58 | + } else if ('id' in object) { |
| 59 | + params.instanceId = object.id; |
| 60 | + } |
| 61 | + return axios.post('notifications/filters', null, { |
| 62 | + params, |
| 63 | + transformResponse: NotificationFilter._transformResponse, |
| 64 | + }); |
| 65 | + } |
56 | 66 |
|
57 | | - async delete() { |
58 | | - return axios.delete(uri`notifications/filters/${this.id}`); |
59 | | - } |
| 67 | + static _transformResponse(data: any) { |
| 68 | + if (!data) { |
| 69 | + return data; |
| 70 | + } |
| 71 | + const json = JSON.parse(data); |
| 72 | + if (json instanceof Array) { |
| 73 | + return json |
| 74 | + .map((notificationFilter) => new NotificationFilter(notificationFilter)) |
| 75 | + .filter((f) => !f.expired); |
| 76 | + } |
| 77 | + return new NotificationFilter(json); |
| 78 | + } |
60 | 79 |
|
61 | | - static isSupported() { |
62 | | - return Boolean(sbaConfig.uiSettings.notificationFilterEnabled); |
63 | | - } |
| 80 | + affects(obj: Instance | Application) { |
| 81 | + if (!obj) { |
| 82 | + return false; |
| 83 | + } |
64 | 84 |
|
65 | | - static async getFilters() { |
66 | | - return axios.get('notifications/filters', { |
67 | | - transformResponse: NotificationFilter._transformResponse, |
68 | | - }); |
69 | | - } |
| 85 | + if (obj instanceof Application) { |
| 86 | + return this.applicationName === obj.name; |
| 87 | + } |
70 | 88 |
|
71 | | - static async addFilter(object, ttl) { |
72 | | - const params = { ttl }; |
73 | | - if ('name' in object) { |
74 | | - params.applicationName = object.name; |
75 | | - } else if ('id' in object) { |
76 | | - params.instanceId = object.id; |
| 89 | + return this.instanceId === obj.id; |
77 | 90 | } |
78 | | - return axios.post('notifications/filters', null, { |
79 | | - params, |
80 | | - transformResponse: NotificationFilter._transformResponse, |
81 | | - }); |
82 | | - } |
83 | 91 |
|
84 | | - static _transformResponse(data) { |
85 | | - if (!data) { |
86 | | - return data; |
| 92 | + async delete() { |
| 93 | + return axios.delete(uri`notifications/filters/${this.id}`); |
87 | 94 | } |
88 | | - const json = JSON.parse(data); |
89 | | - if (json instanceof Array) { |
90 | | - return json |
91 | | - .map(NotificationFilter._toNotificationFilters) |
92 | | - .filter((f) => !f.expired); |
93 | | - } |
94 | | - return NotificationFilter._toNotificationFilters(json); |
95 | | - } |
96 | | - |
97 | | - static _toNotificationFilters(notificationFilter) { |
98 | | - return new NotificationFilter(notificationFilter); |
99 | | - } |
100 | 95 | } |
101 | 96 |
|
102 | 97 | export default NotificationFilter; |
0 commit comments