@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 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 | Type | Description |
24
- | ------------------ | -------------------- | --------------------------------- |
25
- | `notice_recipient` | m2o → directus_users | Recipient for error notifications |
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 recipient = recipientOverride;
64
- if (!recipient) {
65
- const globalSettings = await database.select("notice_recipient").from("global").first();
66
- recipient = globalSettings?.notice_recipient;
63
+ let recipients = [];
64
+ if (recipientOverride) {
65
+ recipients = [recipientOverride];
67
66
  }
68
- if (!recipient) {
69
- this.printLogs(this.extension, "No recipient defined (override or global settings)");
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
- await notificationService.createOne({
99
- recipient,
100
- sender: recipient,
101
- subject,
102
- message: fullMessage,
103
- collection: null,
104
- item: null,
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serhii.mazur/directus-gu-logs",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Helper class Logs for using in Directus extensions",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
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 recipient = recipientOverride;
92
- if (!recipient) {
93
- const globalSettings = await database.select("notice_recipient").from("global").first();
94
- recipient = globalSettings?.notice_recipient;
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 (!recipient) {
98
- this.printLogs(this.extension, "No recipient defined (override or global settings)");
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
- await notificationService.createOne({
133
- recipient,
134
- sender: recipient,
135
- subject,
136
- message: fullMessage,
137
- collection: null,
138
- item: null,
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
  }