@serhii.mazur/directus-gu-logs 1.0.5 → 1.0.7
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/README.MD +4 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +23 -15
- package/package.json +1 -1
- package/src/index.ts +30 -15
package/README.MD
CHANGED
|
@@ -20,9 +20,10 @@ You need to create a collection named `logs` with the following fields:
|
|
|
20
20
|
|
|
21
21
|
Add the following field to the `global` collection:
|
|
22
22
|
|
|
23
|
-
| Field Name
|
|
24
|
-
|
|
|
25
|
-
| `notice_recipient`
|
|
23
|
+
| Field Name | Type | Description |
|
|
24
|
+
| ---------------------------- | -------------------- | --------------------------------- |
|
|
25
|
+
| `notice_recipient` | m2o → directus_users | Recipient for error notifications |
|
|
26
|
+
| `developer_notice_recipient` | m2o → directus_users | Recipient for error notifications |
|
|
26
27
|
|
|
27
28
|
This recipient will be used to send internal notifications when errors occur.
|
|
28
29
|
|
package/dist/index.d.ts
CHANGED
|
@@ -9,5 +9,5 @@ export declare class Logs {
|
|
|
9
9
|
private createOne;
|
|
10
10
|
printLogs(functionName: string, error: string): Promise<void>;
|
|
11
11
|
createActivity(action: string, collection: string, id: PrimaryKey): Promise<void>;
|
|
12
|
-
createNotification(message: string, customSubject?: string | null, recipientOverride?: string | null): Promise<void>;
|
|
12
|
+
createNotification(message: string, customSubject?: string | null, recipientOverride?: string | null, collection?: string | null, item?: string | null): Promise<void>;
|
|
13
13
|
}
|
package/dist/index.js
CHANGED
|
@@ -55,18 +55,24 @@ export class Logs {
|
|
|
55
55
|
console.error("❌ Failed to create activity log:", error);
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
async createNotification(message, customSubject = null, recipientOverride = null) {
|
|
58
|
+
async createNotification(message, customSubject = null, recipientOverride = null, collection = null, item = null) {
|
|
59
59
|
try {
|
|
60
60
|
const schema = await this.getSchema();
|
|
61
61
|
const { database, services } = this.context;
|
|
62
62
|
// Check for passed recipient, fallback to global settings
|
|
63
|
-
let
|
|
64
|
-
if (
|
|
65
|
-
|
|
66
|
-
recipient = globalSettings?.notice_recipient;
|
|
63
|
+
let recipients = [];
|
|
64
|
+
if (recipientOverride) {
|
|
65
|
+
recipients = [recipientOverride];
|
|
67
66
|
}
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
else {
|
|
68
|
+
const globalSettings = await database
|
|
69
|
+
.select("notice_recipient", "developer_notice_recipient")
|
|
70
|
+
.from("global")
|
|
71
|
+
.first();
|
|
72
|
+
recipients = [globalSettings?.notice_recipient, globalSettings?.developer_notice_recipient].filter(Boolean);
|
|
73
|
+
}
|
|
74
|
+
if (recipients.length === 0) {
|
|
75
|
+
this.printLogs(this.extension, "No recipients defined (override or global settings)");
|
|
70
76
|
return;
|
|
71
77
|
}
|
|
72
78
|
const notificationService = new services.NotificationsService({ schema });
|
|
@@ -95,14 +101,16 @@ ${message}<br><br>
|
|
|
95
101
|
<strong>Backend URL:</strong> <a href="${backendUrl}" target="_blank">${backendUrl}</a><br>
|
|
96
102
|
<strong>Date/Time (UTC):</strong> ${timestamp}
|
|
97
103
|
`.trim();
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
for (const recipient of recipients) {
|
|
105
|
+
await notificationService.createOne({
|
|
106
|
+
recipient,
|
|
107
|
+
sender: recipient,
|
|
108
|
+
subject,
|
|
109
|
+
message: fullMessage,
|
|
110
|
+
collection,
|
|
111
|
+
item,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
106
114
|
}
|
|
107
115
|
catch (error) {
|
|
108
116
|
console.error("❌ Failed to create notification:", error);
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -81,21 +81,32 @@ export class Logs {
|
|
|
81
81
|
async createNotification(
|
|
82
82
|
message: string,
|
|
83
83
|
customSubject: string | null = null,
|
|
84
|
-
recipientOverride: string | null = null
|
|
84
|
+
recipientOverride: string | null = null,
|
|
85
|
+
collection: string | null = null,
|
|
86
|
+
item: string | null = null
|
|
85
87
|
) {
|
|
86
88
|
try {
|
|
87
89
|
const schema = await this.getSchema();
|
|
88
90
|
const { database, services } = this.context;
|
|
89
91
|
|
|
90
92
|
// Check for passed recipient, fallback to global settings
|
|
91
|
-
let
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
let recipients: string[] = [];
|
|
94
|
+
|
|
95
|
+
if (recipientOverride) {
|
|
96
|
+
recipients = [recipientOverride];
|
|
97
|
+
} else {
|
|
98
|
+
const globalSettings = await database
|
|
99
|
+
.select("notice_recipient", "developer_notice_recipient")
|
|
100
|
+
.from("global")
|
|
101
|
+
.first();
|
|
102
|
+
|
|
103
|
+
recipients = [globalSettings?.notice_recipient, globalSettings?.developer_notice_recipient].filter(
|
|
104
|
+
Boolean
|
|
105
|
+
);
|
|
95
106
|
}
|
|
96
107
|
|
|
97
|
-
if (
|
|
98
|
-
this.printLogs(this.extension, "No
|
|
108
|
+
if (recipients.length === 0) {
|
|
109
|
+
this.printLogs(this.extension, "No recipients defined (override or global settings)");
|
|
99
110
|
return;
|
|
100
111
|
}
|
|
101
112
|
|
|
@@ -103,9 +114,11 @@ export class Logs {
|
|
|
103
114
|
|
|
104
115
|
// Project Data
|
|
105
116
|
const settings = await database.select("project_name").from("directus_settings").first();
|
|
117
|
+
|
|
106
118
|
const projectName = settings?.project_name || "Unknown Project";
|
|
107
119
|
const backendUrl = process.env.BACKEND_URL || this.context.env?.PUBLIC_URL || "Unknown URL";
|
|
108
120
|
const environment = process.env.BRANCH || "dev";
|
|
121
|
+
|
|
109
122
|
const now = new Date();
|
|
110
123
|
const timestamp = new Intl.DateTimeFormat("en-US", {
|
|
111
124
|
month: "2-digit",
|
|
@@ -129,14 +142,16 @@ ${message}<br><br>
|
|
|
129
142
|
<strong>Date/Time (UTC):</strong> ${timestamp}
|
|
130
143
|
`.trim();
|
|
131
144
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
145
|
+
for (const recipient of recipients) {
|
|
146
|
+
await notificationService.createOne({
|
|
147
|
+
recipient,
|
|
148
|
+
sender: recipient,
|
|
149
|
+
subject,
|
|
150
|
+
message: fullMessage,
|
|
151
|
+
collection,
|
|
152
|
+
item,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
140
155
|
} catch (error) {
|
|
141
156
|
console.error("❌ Failed to create notification:", error);
|
|
142
157
|
}
|