adminizer 4.2.0 → 4.3.0-build.73
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/add-BNb5A_qF.js +1 -0
- package/assets/{add-form-BjackXJe.js → add-form-CGi_qv3J.js} +1 -1
- package/assets/add-group-RRjHTu93.js +1 -0
- package/assets/{add-group-form-YI6KS37x.js → add-group-form-DSd4UrnU.js} +1 -1
- package/assets/add-user-DSuvcKp6.js +1 -0
- package/assets/{add-user-form-bv5BNX5A.js → add-user-form-BAssKDpI.js} +1 -1
- package/assets/{app-CVnUv8s_.css → app-GsYdx39W.css} +1 -1
- package/assets/{app-layout-DecN527i.js → app-layout-ContoN1K.js} +1 -1
- package/assets/{app-sidebar-4JFP8UdU.js → app-sidebar-D7l5BJPV.js} +1 -1
- package/assets/app-sidebar-header-CK8t32cm.js +1 -0
- package/assets/app.js +13 -13
- package/assets/{breadcrumbs-Bo2z-LNC.js → breadcrumbs-DnNOEnT4.js} +1 -1
- package/assets/{catalog-DVyrGTNR.js → catalog-BavcTVb4.js} +1 -1
- package/assets/{dashboard-Dy3pk9t7.js → dashboard-xr-OyVyG.js} +1 -1
- package/assets/{date-picker-B2z-aeuv.js → date-picker-DBHnomTY.js} +1 -1
- package/assets/{del-modal-l3LxJWCN.js → del-modal-BeF4r3gk.js} +1 -1
- package/assets/{field-renderer-BHo944Cn.js → field-renderer-Sd68OLyc.js} +3 -3
- package/assets/form-C9P9m_9q.js +1 -0
- package/assets/{form-hP8sjbND.js → form-DomTBI89.js} +1 -1
- package/assets/home-BWHeZm-1.js +2 -0
- package/assets/{icon-C_X2cQ6W.js → icon-ChQBPA5l.js} +1 -1
- package/assets/{init-user-DgCD4ZN1.js → init-user-DqMDju_E.js} +1 -1
- package/assets/input-error-g1h6ps7t.js +1 -0
- package/assets/list-EEgAn-nx.js +1 -0
- package/assets/{list-table-_OiQmr2A.js → list-table-Br0K3rXg.js} +1 -1
- package/assets/{login-jzvVXgAV.js → login-BR9cqsPn.js} +1 -1
- package/assets/manifest.json +97 -64
- package/assets/material-icon-BIcI8APG.js +1 -0
- package/assets/media-manager-CTstvyHm.js +14 -0
- package/assets/{module-Ceq6vuwa.js → module-CpZBs0fN.js} +1 -1
- package/assets/{multi-select-BbKSrMw2.js → multi-select-BFWbEjH-.js} +1 -1
- package/assets/{nav-footer-B8gMV6Wp.js → nav-footer-bO8D-Yir.js} +1 -1
- package/assets/{nav-main-BUp9qA8R.js → nav-main-DVgvko8g.js} +1 -1
- package/assets/{nav-user-DeF3SDRj.js → nav-user-vIwYShwE.js} +1 -1
- package/assets/notification-BTXEslqa.js +1 -0
- package/assets/{pagination-render-B9U_hVbB.js → pagination-render-rZ5_6g0n.js} +1 -1
- package/assets/{register-C6acWsLe.js → register-Kfu8zmmu.js} +1 -1
- package/assets/tabs-CwgzWt48.js +1 -0
- package/assets/{theme-switcher-D8T9NgWG.js → theme-switcher-fzVA8btX.js} +1 -1
- package/assets/{user-info-CAgkqARx.js → user-info-Db5Y8KeN.js} +1 -1
- package/assets/{user-menu-content-BxL0QMC9.js → user-menu-content-Bc8YNZPf.js} +1 -1
- package/assets/{welcome-Dk7GoS5R.js → welcome-MKPFhPtG.js} +1 -1
- package/controllers/add.js +2 -0
- package/controllers/edit.js +14 -0
- package/controllers/login.js +56 -21
- package/controllers/media-manager/mediaManagerApi.js +1 -1
- package/controllers/notifications/NotificationController.d.ts +11 -0
- package/controllers/notifications/NotificationController.js +203 -0
- package/controllers/remove.js +2 -0
- package/files/favicon.png +0 -0
- package/helpers/diffHelpers.d.ts +2 -0
- package/helpers/diffHelpers.js +24 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/interfaces/adminpanelConfig.d.ts +6 -0
- package/interfaces/types.d.ts +22 -0
- package/lib/Adminizer.d.ts +18 -2
- package/lib/Adminizer.js +61 -1
- package/lib/media-manager/AbstractMediaManager.js +2 -2
- package/lib/v4/model/adapter/sequelize.js +4 -0
- package/lib/v4/model/adapter/typeorm.d.ts +1 -0
- package/lib/v4/model/adapter/typeorm.js +21 -1
- package/lib/v4/notifications/AbstractNotificationService.d.ts +22 -0
- package/lib/v4/notifications/AbstractNotificationService.js +149 -0
- package/lib/v4/notifications/GeneralNotificationService.d.ts +8 -0
- package/lib/v4/notifications/GeneralNotificationService.js +56 -0
- package/lib/v4/notifications/NotificationHandler.d.ts +69 -0
- package/lib/v4/notifications/NotificationHandler.js +127 -0
- package/lib/v4/notifications/SystemNotificationService.d.ts +18 -0
- package/lib/v4/notifications/SystemNotificationService.js +123 -0
- package/models/NotificationAP.d.ts +36 -0
- package/models/NotificationAP.js +22 -0
- package/models/UserNotificationAP.d.ts +26 -0
- package/models/UserNotificationAP.js +18 -0
- package/package.json +13 -5
- package/system/Router.js +18 -0
- package/system/bindAssets.js +1 -0
- package/system/bindInertia.js +5 -0
- package/system/bindMediaManager.js +2 -1
- package/system/bindNavigation.js +2 -3
- package/system/bindNotifications.d.ts +2 -0
- package/system/bindNotifications.js +14 -0
- package/system/defaults.js +6 -0
- package/assets/add-DOaZgoTz.js +0 -1
- package/assets/add-group-Br44s2D6.js +0 -1
- package/assets/add-user-DWcH0s9-.js +0 -1
- package/assets/app-sidebar-header-CIKLfLsF.js +0 -1
- package/assets/form-BXfnz6h5.js +0 -1
- package/assets/home-BD4MWmm9.js +0 -2
- package/assets/input-error-Bab-HPbn.js +0 -1
- package/assets/list-C-27EH39.js +0 -1
- package/assets/material-icon-DsiW0iQG.js +0 -1
- package/assets/media-manager-CSB_XRcE.js +0 -14
|
@@ -170,6 +170,10 @@ export class SequelizeModel extends AbstractModel {
|
|
|
170
170
|
if (value === null) {
|
|
171
171
|
result[targetKey] = { [Op.is]: null };
|
|
172
172
|
}
|
|
173
|
+
else if (Array.isArray(value)) {
|
|
174
|
+
// ✅ Обработка массивов - используем оператор IN
|
|
175
|
+
result[targetKey] = { [Op.in]: value };
|
|
176
|
+
}
|
|
173
177
|
else if (typeof value === "object" && !Array.isArray(value)) {
|
|
174
178
|
const operatorEntries = Object.entries(value)
|
|
175
179
|
.filter(([_, v]) => v !== undefined && v !== null)
|
|
@@ -9,6 +9,7 @@ export declare class TypeORMModel<T> extends AbstractModel<T> {
|
|
|
9
9
|
protected _destroy(criteria: Partial<T>): Promise<T[]>;
|
|
10
10
|
protected _destroyOne(criteria: Partial<T>): Promise<T | null>;
|
|
11
11
|
protected _find(criteria: Partial<T>, options: FindOptions | undefined): Promise<T[]>;
|
|
12
|
+
private _convertCriteriaForTypeORM;
|
|
12
13
|
protected _findOne(criteria: Partial<T>): Promise<T | null>;
|
|
13
14
|
protected _update(criteria: Partial<T>, data: Partial<T>): Promise<T[]>;
|
|
14
15
|
protected _updateOne(criteria: Partial<T>, data: Partial<T>): Promise<T | null>;
|
|
@@ -27,7 +27,27 @@ export class TypeORMModel extends AbstractModel {
|
|
|
27
27
|
return null;
|
|
28
28
|
}
|
|
29
29
|
async _find(criteria, options) {
|
|
30
|
-
|
|
30
|
+
// Преобразуем критерии для поддержки массивов
|
|
31
|
+
const where = this._convertCriteriaForTypeORM(criteria);
|
|
32
|
+
return this.repository.find({ where, ...options });
|
|
33
|
+
}
|
|
34
|
+
_convertCriteriaForTypeORM(criteria) {
|
|
35
|
+
const result = {};
|
|
36
|
+
for (const key in criteria) {
|
|
37
|
+
const value = criteria[key];
|
|
38
|
+
if (Array.isArray(value)) {
|
|
39
|
+
// Для массивов используем оператор IN
|
|
40
|
+
result[key] = { $in: value };
|
|
41
|
+
}
|
|
42
|
+
else if (typeof value === 'object' && value !== null) {
|
|
43
|
+
// Обработка вложенных объектов с операторами
|
|
44
|
+
result[key] = value;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
result[key] = value;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
31
51
|
}
|
|
32
52
|
async _findOne(criteria) {
|
|
33
53
|
return this.repository.findOne({ where: criteria });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { Adminizer } from '../../Adminizer';
|
|
3
|
+
import { INotification, INotificationEvent } from '../../../interfaces/types';
|
|
4
|
+
export declare abstract class AbstractNotificationService extends EventEmitter {
|
|
5
|
+
protected clients: Map<string, (event: INotificationEvent) => void>;
|
|
6
|
+
protected adminizer: Adminizer;
|
|
7
|
+
abstract readonly notificationClass: string;
|
|
8
|
+
abstract readonly icon: string;
|
|
9
|
+
abstract readonly iconColor: string;
|
|
10
|
+
constructor(adminizer: Adminizer);
|
|
11
|
+
addClient(clientId: string, sendFn: (event: INotificationEvent) => void): void;
|
|
12
|
+
removeClient(clientId: string): void;
|
|
13
|
+
abstract dispatchNotification(notification: Omit<INotification, 'id' | 'createdAt' | 'notificationClass' | 'icon'>): Promise<boolean>;
|
|
14
|
+
protected broadcast(event: INotificationEvent): void;
|
|
15
|
+
sendHeartbeat(clientId: string): void;
|
|
16
|
+
protected generateId(): string;
|
|
17
|
+
getClientCount(): number;
|
|
18
|
+
protected createUserNotification(notificationId: string, userId?: number): Promise<void>;
|
|
19
|
+
protected getUserNotification(notificationId: string, userId: number): Promise<any>;
|
|
20
|
+
getNotifications(userId: number, limit?: number, unreadOnly?: boolean): Promise<INotification[]>;
|
|
21
|
+
markAsRead(userId: number, id: string): Promise<void>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { Adminizer } from '../../Adminizer';
|
|
3
|
+
export class AbstractNotificationService extends EventEmitter {
|
|
4
|
+
clients = new Map();
|
|
5
|
+
adminizer;
|
|
6
|
+
constructor(adminizer) {
|
|
7
|
+
super();
|
|
8
|
+
this.adminizer = adminizer;
|
|
9
|
+
}
|
|
10
|
+
// Добавление клиента
|
|
11
|
+
addClient(clientId, sendFn) {
|
|
12
|
+
this.clients.set(clientId, sendFn);
|
|
13
|
+
Adminizer.log.info(`[${this.notificationClass}] Client ${clientId} connected. Total: ${this.clients.size}`);
|
|
14
|
+
}
|
|
15
|
+
// Удаление клиента
|
|
16
|
+
removeClient(clientId) {
|
|
17
|
+
this.clients.delete(clientId);
|
|
18
|
+
Adminizer.log.info(`[${this.notificationClass}] Client ${clientId} disconnected. Total: ${this.clients.size}`);
|
|
19
|
+
}
|
|
20
|
+
// abstract getNotifications(userId?: number, limit?: number, unreadOnly?: boolean): Promise<INotification[]>;
|
|
21
|
+
// Рассылка события всем клиентам
|
|
22
|
+
broadcast(event) {
|
|
23
|
+
this.clients.forEach((sendFn, clientId) => {
|
|
24
|
+
try {
|
|
25
|
+
sendFn(event);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
Adminizer.log.error(`[${this.notificationClass}] Error sending to client ${clientId}:`, error);
|
|
29
|
+
this.removeClient(clientId);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
// Отправка heartbeat
|
|
34
|
+
sendHeartbeat(clientId) {
|
|
35
|
+
const sendFn = this.clients.get(clientId);
|
|
36
|
+
if (sendFn) {
|
|
37
|
+
const heartbeatEvent = {
|
|
38
|
+
type: 'heartbeat',
|
|
39
|
+
data: 'ping'
|
|
40
|
+
};
|
|
41
|
+
sendFn(heartbeatEvent);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// Генерация ID
|
|
45
|
+
generateId() {
|
|
46
|
+
return `${this.notificationClass}-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
47
|
+
}
|
|
48
|
+
// Получение количества подключенных клиентов
|
|
49
|
+
getClientCount() {
|
|
50
|
+
return this.clients.size;
|
|
51
|
+
}
|
|
52
|
+
// Метод для создания записи в UserNotificationAP
|
|
53
|
+
async createUserNotification(notificationId, userId) {
|
|
54
|
+
if (this.adminizer.modelHandler.model.has('usernotificationap') && userId) {
|
|
55
|
+
try {
|
|
56
|
+
await this.adminizer.modelHandler.model.get('usernotificationap')["_create"]({
|
|
57
|
+
userId: userId,
|
|
58
|
+
notificationId: notificationId,
|
|
59
|
+
read: false
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
Adminizer.log.error(`Error creating user notification record:`, error);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// Метод для получения UserNotificationAP по ID уведомления и пользователя
|
|
68
|
+
async getUserNotification(notificationId, userId) {
|
|
69
|
+
if (this.adminizer.modelHandler.model.has('usernotificationap')) {
|
|
70
|
+
try {
|
|
71
|
+
return await this.adminizer.modelHandler.model.get('usernotificationap')["_findOne"]({
|
|
72
|
+
where: { notificationId: notificationId, userId: userId }
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
Adminizer.log.error(`Error getting user notification:`, error);
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
async getNotifications(userId, limit = 50, unreadOnly = false) {
|
|
83
|
+
if (!this.adminizer.modelHandler.model.has('notificationap')) {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
let query = { notificationClass: this.notificationClass };
|
|
88
|
+
// Если запрашиваются уведомления для конкретного пользователя
|
|
89
|
+
// Получаем ID уведомлений пользователя
|
|
90
|
+
const userNotifications = await this.adminizer.modelHandler.model.get('usernotificationap')["_find"]({
|
|
91
|
+
where: {
|
|
92
|
+
userId: userId
|
|
93
|
+
},
|
|
94
|
+
include: ['notificationap']
|
|
95
|
+
});
|
|
96
|
+
const notificationIds = userNotifications.map((un) => un.notificationId.id);
|
|
97
|
+
if (unreadOnly) {
|
|
98
|
+
// Только непрочитанные
|
|
99
|
+
query.id = userNotifications
|
|
100
|
+
.filter((un) => !un.read)
|
|
101
|
+
.map((un) => un.notificationId.id);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
query.id = notificationIds;
|
|
105
|
+
}
|
|
106
|
+
let notificationsDB;
|
|
107
|
+
if (query.id.length === 0)
|
|
108
|
+
return [];
|
|
109
|
+
notificationsDB = await this.adminizer.modelHandler.model.get('notificationap')["_find"]({
|
|
110
|
+
where: query,
|
|
111
|
+
sort: 'createdAt DESC',
|
|
112
|
+
limit: limit
|
|
113
|
+
});
|
|
114
|
+
let notifications = [];
|
|
115
|
+
for (const notification of notificationsDB) {
|
|
116
|
+
let readStatus = false;
|
|
117
|
+
// Получаем статус прочтения из UserNotificationAP
|
|
118
|
+
const userNotification = await this.getUserNotification(notification.id, userId);
|
|
119
|
+
readStatus = userNotification ? userNotification.read : false;
|
|
120
|
+
notifications.push({
|
|
121
|
+
...notification,
|
|
122
|
+
read: readStatus,
|
|
123
|
+
icon: {
|
|
124
|
+
icon: this.icon,
|
|
125
|
+
iconColor: this.iconColor
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return notifications;
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
Adminizer.log.error('Error fetching notifications:', error);
|
|
133
|
+
return [];
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async markAsRead(userId, id) {
|
|
137
|
+
try {
|
|
138
|
+
const userNotification = await this.getUserNotification(id, userId);
|
|
139
|
+
if (userNotification) {
|
|
140
|
+
await this.adminizer.modelHandler.model.get('usernotificationap')["_update"]({
|
|
141
|
+
id: userNotification.id
|
|
142
|
+
}, { read: true });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
Adminizer.log.error('Error marking notification as read:', error);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AbstractNotificationService } from './AbstractNotificationService';
|
|
2
|
+
import { INotification } from '../../../interfaces/types';
|
|
3
|
+
export declare class GeneralNotificationService extends AbstractNotificationService {
|
|
4
|
+
readonly notificationClass = "general";
|
|
5
|
+
readonly icon: string;
|
|
6
|
+
readonly iconColor: string;
|
|
7
|
+
dispatchNotification(notification: Omit<INotification, 'id' | 'createdAt' | 'notificationClass' | 'icon'>): Promise<boolean>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { AbstractNotificationService } from './AbstractNotificationService';
|
|
2
|
+
import { Adminizer } from '../../Adminizer';
|
|
3
|
+
export class GeneralNotificationService extends AbstractNotificationService {
|
|
4
|
+
notificationClass = 'general';
|
|
5
|
+
icon = 'info';
|
|
6
|
+
iconColor = '#5987de';
|
|
7
|
+
async dispatchNotification(notification) {
|
|
8
|
+
const fullNotification = {
|
|
9
|
+
...notification,
|
|
10
|
+
notificationClass: this.notificationClass
|
|
11
|
+
};
|
|
12
|
+
let notificationDB;
|
|
13
|
+
// Сохраняем в базу
|
|
14
|
+
if (this.adminizer.modelHandler.model.has('notificationap')) {
|
|
15
|
+
try {
|
|
16
|
+
notificationDB = await this.adminizer.modelHandler.model.get('notificationap')["_create"](fullNotification);
|
|
17
|
+
if (notification.userId) {
|
|
18
|
+
// For specific user
|
|
19
|
+
await this.createUserNotification(notificationDB.id, notification.userId);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
// For all users
|
|
23
|
+
const users = await this.adminizer.modelHandler.model.get('userap')["_find"]({});
|
|
24
|
+
for (const user of users) {
|
|
25
|
+
try {
|
|
26
|
+
await this.createUserNotification(notificationDB.id, user.id);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
Adminizer.log.error('Error creating UserNotificationAP:', error);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const event = {
|
|
34
|
+
type: 'notification',
|
|
35
|
+
data: {
|
|
36
|
+
...notificationDB.toJSON(),
|
|
37
|
+
read: false,
|
|
38
|
+
icon: {
|
|
39
|
+
icon: this.icon,
|
|
40
|
+
iconColor: this.iconColor
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
notificationClass: this.notificationClass,
|
|
44
|
+
userId: notification.userId ?? null
|
|
45
|
+
};
|
|
46
|
+
this.broadcast(event);
|
|
47
|
+
Adminizer.log.info(`[General] Notification dispatched: ${fullNotification.title}`);
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
Adminizer.log.error('Error saving notification to database:', error);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { AbstractNotificationService } from "./AbstractNotificationService";
|
|
3
|
+
import { INotification } from '../../../interfaces/types';
|
|
4
|
+
export declare class NotificationHandler extends EventEmitter {
|
|
5
|
+
private services;
|
|
6
|
+
constructor();
|
|
7
|
+
/**
|
|
8
|
+
* Register a new notification service
|
|
9
|
+
* @param service
|
|
10
|
+
*/
|
|
11
|
+
registerService(service: AbstractNotificationService): void;
|
|
12
|
+
/**
|
|
13
|
+
* Remove a notification service
|
|
14
|
+
* @param notificationClass
|
|
15
|
+
*/
|
|
16
|
+
removeService(notificationClass: string): void;
|
|
17
|
+
/**
|
|
18
|
+
* Get a notification service by class
|
|
19
|
+
* @param notificationClass
|
|
20
|
+
*/
|
|
21
|
+
getService(notificationClass: string): AbstractNotificationService | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Get all registered services
|
|
24
|
+
*/
|
|
25
|
+
getAllServices(): AbstractNotificationService[];
|
|
26
|
+
/**
|
|
27
|
+
* Dispatch a notification to a service
|
|
28
|
+
* @param notificationClass
|
|
29
|
+
* @param notification
|
|
30
|
+
*/
|
|
31
|
+
dispatchNotification(notificationClass: string, notification: Omit<INotification, 'id' | 'createdAt' | 'notificationClass' | 'icon'>): Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Get notifications by class
|
|
34
|
+
* @param notificationClass
|
|
35
|
+
* @param userId
|
|
36
|
+
* @param limit
|
|
37
|
+
* @param unreadOnly
|
|
38
|
+
*/
|
|
39
|
+
getNotifications(notificationClass: string, userId?: number, limit?: number, unreadOnly?: boolean): Promise<INotification[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Get all notifications for a user
|
|
42
|
+
* @param user
|
|
43
|
+
* @param limit
|
|
44
|
+
* @param unreadOnly
|
|
45
|
+
*/
|
|
46
|
+
getUserNotifications(user: any, limit?: number, unreadOnly?: boolean): Promise<INotification[]>;
|
|
47
|
+
/**
|
|
48
|
+
* Mark a notification as read
|
|
49
|
+
* @param notificationClass
|
|
50
|
+
* @param id
|
|
51
|
+
* @param userId
|
|
52
|
+
*/
|
|
53
|
+
markAsRead(notificationClass: string, id: string, userId: number): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Check if user is admin
|
|
56
|
+
* @param user
|
|
57
|
+
* @private
|
|
58
|
+
*/
|
|
59
|
+
private isAdmin;
|
|
60
|
+
/**
|
|
61
|
+
* Get client count for a notification class
|
|
62
|
+
* @param notificationClass
|
|
63
|
+
*/
|
|
64
|
+
getClientCount(notificationClass: string): number;
|
|
65
|
+
/**
|
|
66
|
+
* Get total client count
|
|
67
|
+
*/
|
|
68
|
+
getTotalClientCount(): number;
|
|
69
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { Adminizer } from '../../Adminizer';
|
|
3
|
+
export class NotificationHandler extends EventEmitter {
|
|
4
|
+
services = new Map();
|
|
5
|
+
constructor() {
|
|
6
|
+
super();
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Register a new notification service
|
|
10
|
+
* @param service
|
|
11
|
+
*/
|
|
12
|
+
registerService(service) {
|
|
13
|
+
this.services.set(service.notificationClass, service);
|
|
14
|
+
Adminizer.log.info(`Notification service registered: ${service.notificationClass}`);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Remove a notification service
|
|
18
|
+
* @param notificationClass
|
|
19
|
+
*/
|
|
20
|
+
removeService(notificationClass) {
|
|
21
|
+
if (this.services.has(notificationClass)) {
|
|
22
|
+
this.services.delete(notificationClass);
|
|
23
|
+
Adminizer.log.info(`Notification service removed: ${notificationClass}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Get a notification service by class
|
|
28
|
+
* @param notificationClass
|
|
29
|
+
*/
|
|
30
|
+
getService(notificationClass) {
|
|
31
|
+
return this.services.get(notificationClass);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Get all registered services
|
|
35
|
+
*/
|
|
36
|
+
getAllServices() {
|
|
37
|
+
return Array.from(this.services.values());
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Dispatch a notification to a service
|
|
41
|
+
* @param notificationClass
|
|
42
|
+
* @param notification
|
|
43
|
+
*/
|
|
44
|
+
async dispatchNotification(notificationClass, notification) {
|
|
45
|
+
const service = this.getService(notificationClass);
|
|
46
|
+
if (!service) {
|
|
47
|
+
throw new Error(`Notification service not found: ${notificationClass}`);
|
|
48
|
+
}
|
|
49
|
+
return service.dispatchNotification(notification);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Get notifications by class
|
|
53
|
+
* @param notificationClass
|
|
54
|
+
* @param userId
|
|
55
|
+
* @param limit
|
|
56
|
+
* @param unreadOnly
|
|
57
|
+
*/
|
|
58
|
+
async getNotifications(notificationClass, userId, limit, unreadOnly) {
|
|
59
|
+
const service = this.getService(notificationClass);
|
|
60
|
+
if (!service) {
|
|
61
|
+
throw new Error(`Notification service not found: ${notificationClass}`);
|
|
62
|
+
}
|
|
63
|
+
return service.getNotifications(userId, limit, unreadOnly);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Get all notifications for a user
|
|
67
|
+
* @param user
|
|
68
|
+
* @param limit
|
|
69
|
+
* @param unreadOnly
|
|
70
|
+
*/
|
|
71
|
+
async getUserNotifications(user, limit = 50, unreadOnly = false) {
|
|
72
|
+
const allNotifications = [];
|
|
73
|
+
for (const service of this.getAllServices()) {
|
|
74
|
+
// Для системных уведомлений проверяем права админа
|
|
75
|
+
if (service.notificationClass === 'system') {
|
|
76
|
+
if (this.isAdmin(user)) {
|
|
77
|
+
const notifications = await service.getNotifications(user.id, limit, unreadOnly);
|
|
78
|
+
allNotifications.push(...notifications);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
// Для остальных уведомлений получаем по пользователю
|
|
83
|
+
const notifications = await service.getNotifications(user.id, limit, unreadOnly);
|
|
84
|
+
allNotifications.push(...notifications);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// Сортируем по дате создания
|
|
88
|
+
return allNotifications.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()).slice(0, limit);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Mark a notification as read
|
|
92
|
+
* @param notificationClass
|
|
93
|
+
* @param id
|
|
94
|
+
* @param userId
|
|
95
|
+
*/
|
|
96
|
+
async markAsRead(notificationClass, id, userId) {
|
|
97
|
+
const service = this.getService(notificationClass);
|
|
98
|
+
if (!service) {
|
|
99
|
+
throw new Error(`Notification service not found: ${notificationClass}`);
|
|
100
|
+
}
|
|
101
|
+
return service.markAsRead(userId, id);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Check if user is admin
|
|
105
|
+
* @param user
|
|
106
|
+
* @private
|
|
107
|
+
*/
|
|
108
|
+
isAdmin(user) {
|
|
109
|
+
if (!user)
|
|
110
|
+
return false;
|
|
111
|
+
return user.isAdministrator === true;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Get client count for a notification class
|
|
115
|
+
* @param notificationClass
|
|
116
|
+
*/
|
|
117
|
+
getClientCount(notificationClass) {
|
|
118
|
+
const service = this.getService(notificationClass);
|
|
119
|
+
return service ? service.getClientCount() : 0;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get total client count
|
|
123
|
+
*/
|
|
124
|
+
getTotalClientCount() {
|
|
125
|
+
return this.getAllServices().reduce((total, service) => total + service.getClientCount(), 0);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AbstractNotificationService } from './AbstractNotificationService';
|
|
2
|
+
import { INotification } from '../../../interfaces/types';
|
|
3
|
+
export declare class SystemNotificationService extends AbstractNotificationService {
|
|
4
|
+
readonly notificationClass = "system";
|
|
5
|
+
readonly icon: string;
|
|
6
|
+
readonly iconColor: string;
|
|
7
|
+
private crudChannels;
|
|
8
|
+
dispatchNotification(notification: Omit<INotification, 'id' | 'createdAt' | 'notificationClass' | 'icon'>): Promise<boolean>;
|
|
9
|
+
private broadcastToChannel;
|
|
10
|
+
addClientToChannel(clientId: string, channel: string): void;
|
|
11
|
+
removeClientFromChannel(clientId: string, channel: string): void;
|
|
12
|
+
removeClientFromAllChannels(clientId: string): void;
|
|
13
|
+
removeClient(clientId: string): void;
|
|
14
|
+
logSystemEvent(title: string, message: string, channel?: string, metadata?: Record<string | number, any>): Promise<boolean>;
|
|
15
|
+
logCreatedEvent(title: string, message: string, metadata?: Record<string | number, any>): Promise<boolean>;
|
|
16
|
+
logUpdatedEvent(title: string, message: string, metadata?: Record<string | number, any>): Promise<boolean>;
|
|
17
|
+
logDeletedEvent(title: string, message: string, metadata?: Record<string | number, any>): Promise<boolean>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { AbstractNotificationService } from './AbstractNotificationService';
|
|
2
|
+
import { Adminizer } from '../../Adminizer';
|
|
3
|
+
export class SystemNotificationService extends AbstractNotificationService {
|
|
4
|
+
notificationClass = 'system';
|
|
5
|
+
icon = 'settings';
|
|
6
|
+
iconColor = '#1eb707';
|
|
7
|
+
// Добавляем каналы для CRUD операций
|
|
8
|
+
crudChannels = new Map();
|
|
9
|
+
async dispatchNotification(notification) {
|
|
10
|
+
const fullNotification = {
|
|
11
|
+
...notification,
|
|
12
|
+
notificationClass: this.notificationClass,
|
|
13
|
+
channel: notification.channel ?? ''
|
|
14
|
+
};
|
|
15
|
+
let notificationDB;
|
|
16
|
+
// Сохраняем в базу
|
|
17
|
+
if (this.adminizer.modelHandler.model.has('notificationap')) {
|
|
18
|
+
try {
|
|
19
|
+
notificationDB = await this.adminizer.modelHandler.model.get('notificationap')["_create"](fullNotification);
|
|
20
|
+
const users = await this.adminizer.modelHandler.model.get('userap')["_find"]({ isAdministrator: 1 });
|
|
21
|
+
for (const user of users) {
|
|
22
|
+
try {
|
|
23
|
+
await this.createUserNotification(notificationDB.id, user.id);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
Adminizer.log.error('Error creating UserNotificationAP:', error);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const event = {
|
|
30
|
+
type: 'notification',
|
|
31
|
+
data: {
|
|
32
|
+
...notificationDB.toJSON(),
|
|
33
|
+
icon: {
|
|
34
|
+
icon: this.icon,
|
|
35
|
+
iconColor: this.iconColor
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
notificationClass: this.notificationClass,
|
|
39
|
+
userId: notification.userId ?? null,
|
|
40
|
+
channel: notification.channel // Добавляем информацию о канале
|
|
41
|
+
};
|
|
42
|
+
// Отправляем на все каналы или на конкретный канал
|
|
43
|
+
if (notification.channel) {
|
|
44
|
+
this.broadcastToChannel(notification.channel, event);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
this.broadcast(event);
|
|
48
|
+
}
|
|
49
|
+
Adminizer.log.info(`[System] Notification dispatched: ${fullNotification.title}`);
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
Adminizer.log.error('Error saving system notification to database:', error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
// Метод для отправки на конкретный канал
|
|
59
|
+
broadcastToChannel(channel, event) {
|
|
60
|
+
const channelClients = this.crudChannels.get(channel);
|
|
61
|
+
if (channelClients) {
|
|
62
|
+
channelClients.forEach(clientId => {
|
|
63
|
+
const sendFn = this.clients.get(clientId);
|
|
64
|
+
if (sendFn) {
|
|
65
|
+
try {
|
|
66
|
+
sendFn(event);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
Adminizer.log.error(`[${this.notificationClass}] Error sending to client ${clientId} on channel ${channel}:`, error);
|
|
70
|
+
this.removeClient(clientId);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// Добавляем клиента к каналу
|
|
77
|
+
addClientToChannel(clientId, channel) {
|
|
78
|
+
if (!this.crudChannels.has(channel)) {
|
|
79
|
+
this.crudChannels.set(channel, new Set());
|
|
80
|
+
}
|
|
81
|
+
this.crudChannels.get(channel).add(clientId);
|
|
82
|
+
Adminizer.log.info(`[${this.notificationClass}] Client ${clientId} added to channel ${channel}`);
|
|
83
|
+
}
|
|
84
|
+
// Удаляем клиента из канала
|
|
85
|
+
removeClientFromChannel(clientId, channel) {
|
|
86
|
+
const channelClients = this.crudChannels.get(channel);
|
|
87
|
+
if (channelClients) {
|
|
88
|
+
channelClients.delete(clientId);
|
|
89
|
+
Adminizer.log.info(`[${this.notificationClass}] Client ${clientId} removed from channel ${channel}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// Удаляем клиента из всех каналов
|
|
93
|
+
removeClientFromAllChannels(clientId) {
|
|
94
|
+
this.crudChannels.forEach((clients, channel) => {
|
|
95
|
+
clients.delete(clientId);
|
|
96
|
+
Adminizer.log.info(`[${this.notificationClass}] Client ${clientId} removed from channel ${channel}`);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
// Переопределяем removeClient для очистки каналов
|
|
100
|
+
removeClient(clientId) {
|
|
101
|
+
super.removeClient(clientId);
|
|
102
|
+
this.removeClientFromAllChannels(clientId);
|
|
103
|
+
}
|
|
104
|
+
// Специальный метод для системных событий с указанием канала
|
|
105
|
+
async logSystemEvent(title, message, channel, metadata) {
|
|
106
|
+
return this.dispatchNotification({
|
|
107
|
+
title: title,
|
|
108
|
+
message: message,
|
|
109
|
+
metadata: metadata,
|
|
110
|
+
channel: channel
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
// Методы для CRUD операций
|
|
114
|
+
async logCreatedEvent(title, message, metadata) {
|
|
115
|
+
return this.logSystemEvent(title, message, 'created', metadata);
|
|
116
|
+
}
|
|
117
|
+
async logUpdatedEvent(title, message, metadata) {
|
|
118
|
+
return this.logSystemEvent(title, message, 'updated', metadata);
|
|
119
|
+
}
|
|
120
|
+
async logDeletedEvent(title, message, metadata) {
|
|
121
|
+
return this.logSystemEvent(title, message, 'deleted', metadata);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Model } from "sequelize";
|
|
2
|
+
declare const _default: {
|
|
3
|
+
id: {
|
|
4
|
+
type: string;
|
|
5
|
+
autoIncrement: boolean;
|
|
6
|
+
primaryKey: boolean;
|
|
7
|
+
};
|
|
8
|
+
title: {
|
|
9
|
+
type: string;
|
|
10
|
+
};
|
|
11
|
+
message: {
|
|
12
|
+
type: string;
|
|
13
|
+
};
|
|
14
|
+
notificationClass: {
|
|
15
|
+
type: string;
|
|
16
|
+
};
|
|
17
|
+
channel: {
|
|
18
|
+
type: string;
|
|
19
|
+
};
|
|
20
|
+
metadata: {
|
|
21
|
+
type: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
export default _default;
|
|
25
|
+
export interface NotificationAP {
|
|
26
|
+
id: string;
|
|
27
|
+
title: string;
|
|
28
|
+
message: string;
|
|
29
|
+
channel: string;
|
|
30
|
+
notificationClass: string;
|
|
31
|
+
metadata?: Record<string, unknown>;
|
|
32
|
+
createdAt: Date;
|
|
33
|
+
}
|
|
34
|
+
export interface NotificationAPModel extends Model<NotificationAP>, NotificationAP {
|
|
35
|
+
toJSON(): NotificationAP;
|
|
36
|
+
}
|