@serhii.mazur/directus-gu-logs 1.0.3 → 1.0.5
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 +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +36 -7
- package/docs/model_global.png +0 -0
- package/docs/model_logs.png +0 -0
- package/package.json +1 -1
- package/src/index.ts +43 -7
- package/tsconfig.json +0 -0
package/README.MD
CHANGED
|
@@ -50,4 +50,5 @@ const logs = new Logs(context, "my-extension", "logs");
|
|
|
50
50
|
await logs.printLogs("myFunction", "message");
|
|
51
51
|
await logs.createActivity("create", "collection", "id");
|
|
52
52
|
await logs.createNotification("An error occurred");
|
|
53
|
+
await logs.createNotification("An error occurred", "Custom subject", "recipient_id");
|
|
53
54
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -4,10 +4,10 @@ export declare class Logs {
|
|
|
4
4
|
protected context: ApiExtensionContext;
|
|
5
5
|
protected extension: string;
|
|
6
6
|
protected collectionName: string;
|
|
7
|
-
constructor(context: ApiExtensionContext, extension
|
|
7
|
+
constructor(context: ApiExtensionContext, extension: string, collectionName?: string);
|
|
8
8
|
private getSchema;
|
|
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): Promise<void>;
|
|
12
|
+
createNotification(message: string, customSubject?: string | null, recipientOverride?: string | null): Promise<void>;
|
|
13
13
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export class Logs {
|
|
2
|
-
constructor(context, extension
|
|
2
|
+
constructor(context, extension, collectionName = "logs") {
|
|
3
3
|
this.context = context;
|
|
4
4
|
this.extension = extension;
|
|
5
5
|
this.collectionName = collectionName;
|
|
@@ -55,22 +55,51 @@ export class Logs {
|
|
|
55
55
|
console.error("❌ Failed to create activity log:", error);
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
async createNotification(message =
|
|
58
|
+
async createNotification(message, customSubject = null, recipientOverride = null) {
|
|
59
59
|
try {
|
|
60
60
|
const schema = await this.getSchema();
|
|
61
61
|
const { database, services } = this.context;
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
// Check for passed recipient, fallback to global settings
|
|
63
|
+
let recipient = recipientOverride;
|
|
64
64
|
if (!recipient) {
|
|
65
|
-
|
|
65
|
+
const globalSettings = await database.select("notice_recipient").from("global").first();
|
|
66
|
+
recipient = globalSettings?.notice_recipient;
|
|
67
|
+
}
|
|
68
|
+
if (!recipient) {
|
|
69
|
+
this.printLogs(this.extension, "No recipient defined (override or global settings)");
|
|
66
70
|
return;
|
|
67
71
|
}
|
|
68
72
|
const notificationService = new services.NotificationsService({ schema });
|
|
73
|
+
// Project Data
|
|
74
|
+
const settings = await database.select("project_name").from("directus_settings").first();
|
|
75
|
+
const projectName = settings?.project_name || "Unknown Project";
|
|
76
|
+
const backendUrl = process.env.BACKEND_URL || this.context.env?.PUBLIC_URL || "Unknown URL";
|
|
77
|
+
const environment = process.env.BRANCH || "dev";
|
|
78
|
+
const now = new Date();
|
|
79
|
+
const timestamp = new Intl.DateTimeFormat("en-US", {
|
|
80
|
+
month: "2-digit",
|
|
81
|
+
day: "2-digit",
|
|
82
|
+
year: "numeric",
|
|
83
|
+
hour: "2-digit",
|
|
84
|
+
minute: "2-digit",
|
|
85
|
+
hour12: false,
|
|
86
|
+
timeZone: "UTC",
|
|
87
|
+
}).format(now);
|
|
88
|
+
// Compose subject & message
|
|
89
|
+
const subject = customSubject
|
|
90
|
+
? `${customSubject} - ${projectName}`
|
|
91
|
+
: `Directus Error Notification - ${projectName}`;
|
|
92
|
+
const fullMessage = `
|
|
93
|
+
${message}<br><br>
|
|
94
|
+
<strong>Environment:</strong> ${environment}<br>
|
|
95
|
+
<strong>Backend URL:</strong> <a href="${backendUrl}" target="_blank">${backendUrl}</a><br>
|
|
96
|
+
<strong>Date/Time (UTC):</strong> ${timestamp}
|
|
97
|
+
`.trim();
|
|
69
98
|
await notificationService.createOne({
|
|
70
99
|
recipient,
|
|
71
100
|
sender: recipient,
|
|
72
|
-
subject
|
|
73
|
-
message,
|
|
101
|
+
subject,
|
|
102
|
+
message: fullMessage,
|
|
74
103
|
collection: null,
|
|
75
104
|
item: null,
|
|
76
105
|
});
|
package/docs/model_global.png
CHANGED
|
File without changes
|
package/docs/model_logs.png
CHANGED
|
File without changes
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -14,7 +14,7 @@ export class Logs {
|
|
|
14
14
|
protected extension: string;
|
|
15
15
|
protected collectionName: string;
|
|
16
16
|
|
|
17
|
-
constructor(context: ApiExtensionContext, extension
|
|
17
|
+
constructor(context: ApiExtensionContext, extension: string, collectionName: string = "logs") {
|
|
18
18
|
this.context = context;
|
|
19
19
|
this.extension = extension;
|
|
20
20
|
this.collectionName = collectionName;
|
|
@@ -78,26 +78,62 @@ export class Logs {
|
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
async createNotification(
|
|
81
|
+
async createNotification(
|
|
82
|
+
message: string,
|
|
83
|
+
customSubject: string | null = null,
|
|
84
|
+
recipientOverride: string | null = null
|
|
85
|
+
) {
|
|
82
86
|
try {
|
|
83
87
|
const schema = await this.getSchema();
|
|
84
88
|
const { database, services } = this.context;
|
|
85
89
|
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
// Check for passed recipient, fallback to global settings
|
|
91
|
+
let recipient = recipientOverride;
|
|
92
|
+
if (!recipient) {
|
|
93
|
+
const globalSettings = await database.select("notice_recipient").from("global").first();
|
|
94
|
+
recipient = globalSettings?.notice_recipient;
|
|
95
|
+
}
|
|
88
96
|
|
|
89
97
|
if (!recipient) {
|
|
90
|
-
this.printLogs(this.extension, "No recipient defined
|
|
98
|
+
this.printLogs(this.extension, "No recipient defined (override or global settings)");
|
|
91
99
|
return;
|
|
92
100
|
}
|
|
93
101
|
|
|
94
102
|
const notificationService = new services.NotificationsService({ schema });
|
|
95
103
|
|
|
104
|
+
// Project Data
|
|
105
|
+
const settings = await database.select("project_name").from("directus_settings").first();
|
|
106
|
+
const projectName = settings?.project_name || "Unknown Project";
|
|
107
|
+
const backendUrl = process.env.BACKEND_URL || this.context.env?.PUBLIC_URL || "Unknown URL";
|
|
108
|
+
const environment = process.env.BRANCH || "dev";
|
|
109
|
+
const now = new Date();
|
|
110
|
+
const timestamp = new Intl.DateTimeFormat("en-US", {
|
|
111
|
+
month: "2-digit",
|
|
112
|
+
day: "2-digit",
|
|
113
|
+
year: "numeric",
|
|
114
|
+
hour: "2-digit",
|
|
115
|
+
minute: "2-digit",
|
|
116
|
+
hour12: false,
|
|
117
|
+
timeZone: "UTC",
|
|
118
|
+
}).format(now);
|
|
119
|
+
|
|
120
|
+
// Compose subject & message
|
|
121
|
+
const subject = customSubject
|
|
122
|
+
? `${customSubject} - ${projectName}`
|
|
123
|
+
: `Directus Error Notification - ${projectName}`;
|
|
124
|
+
|
|
125
|
+
const fullMessage = `
|
|
126
|
+
${message}<br><br>
|
|
127
|
+
<strong>Environment:</strong> ${environment}<br>
|
|
128
|
+
<strong>Backend URL:</strong> <a href="${backendUrl}" target="_blank">${backendUrl}</a><br>
|
|
129
|
+
<strong>Date/Time (UTC):</strong> ${timestamp}
|
|
130
|
+
`.trim();
|
|
131
|
+
|
|
96
132
|
await notificationService.createOne({
|
|
97
133
|
recipient,
|
|
98
134
|
sender: recipient,
|
|
99
|
-
subject
|
|
100
|
-
message,
|
|
135
|
+
subject,
|
|
136
|
+
message: fullMessage,
|
|
101
137
|
collection: null,
|
|
102
138
|
item: null,
|
|
103
139
|
});
|
package/tsconfig.json
CHANGED
|
File without changes
|