@twin.org/messaging-connector-entity-storage 0.0.1-next.3 → 0.0.1-next.4
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/dist/cjs/index.cjs +268 -258
- package/dist/esm/index.mjs +265 -255
- package/dist/types/index.d.ts +4 -0
- package/dist/types/schema.d.ts +9 -1
- package/docs/changelog.md +1 -1
- package/docs/reference/classes/EmailEntry.md +77 -0
- package/docs/reference/classes/PushNotificationDeviceEntry.md +61 -0
- package/docs/reference/classes/PushNotificationMessageEntry.md +69 -0
- package/docs/reference/classes/SmsEntry.md +61 -0
- package/docs/reference/functions/initSchema.md +19 -1
- package/docs/reference/index.md +4 -0
- package/package.json +2 -2
package/dist/esm/index.mjs
CHANGED
|
@@ -1,255 +1,7 @@
|
|
|
1
|
+
import { property, entity, EntitySchemaFactory, EntitySchemaHelper } from '@twin.org/entity';
|
|
1
2
|
import { Is, StringHelper, Guards, Converter, RandomHelper, GeneralError } from '@twin.org/core';
|
|
2
3
|
import { EntityStorageConnectorFactory } from '@twin.org/entity-storage-models';
|
|
3
4
|
import { LoggingConnectorFactory } from '@twin.org/logging-models';
|
|
4
|
-
import { property, entity, EntitySchemaFactory, EntitySchemaHelper } from '@twin.org/entity';
|
|
5
|
-
|
|
6
|
-
// Copyright 2024 IOTA Stiftung.
|
|
7
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
8
|
-
/**
|
|
9
|
-
* Class for connecting to the email messaging operations of the Entity Storage.
|
|
10
|
-
*/
|
|
11
|
-
class EntityStorageMessagingEmailConnector {
|
|
12
|
-
/**
|
|
13
|
-
* Runtime name for the class.
|
|
14
|
-
*/
|
|
15
|
-
CLASS_NAME = "EntityStorageMessagingEmailConnector";
|
|
16
|
-
/**
|
|
17
|
-
* The logging connector.
|
|
18
|
-
* @internal
|
|
19
|
-
*/
|
|
20
|
-
_logging;
|
|
21
|
-
/**
|
|
22
|
-
* The entity storage for the emails entries.
|
|
23
|
-
* @internal
|
|
24
|
-
*/
|
|
25
|
-
_messagingEmailEntryStorage;
|
|
26
|
-
/**
|
|
27
|
-
* Create a new instance of EntityStorageMessagingEmailConnector.
|
|
28
|
-
* @param options The options for the connector.
|
|
29
|
-
* @param options.loggingConnectorType The type of logging connector to use, defaults to no logging.
|
|
30
|
-
* @param options.messagingEmailEntryStorageConnectorType The type of entity storage connector to use for the email entries, defaults to "email-entry".
|
|
31
|
-
*/
|
|
32
|
-
constructor(options) {
|
|
33
|
-
if (Is.stringValue(options?.loggingConnectorType)) {
|
|
34
|
-
this._logging = LoggingConnectorFactory.get(options.loggingConnectorType);
|
|
35
|
-
}
|
|
36
|
-
this._messagingEmailEntryStorage = EntityStorageConnectorFactory.get(options?.messagingEmailEntryStorageConnectorType ??
|
|
37
|
-
StringHelper.kebabCase("EmailEntry"));
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Store a custom email using Entity Storage.
|
|
41
|
-
* @param sender The sender email address.
|
|
42
|
-
* @param recipients An array of recipients email addresses.
|
|
43
|
-
* @param subject The subject of the email.
|
|
44
|
-
* @param content The html content of the email.
|
|
45
|
-
* @returns True if the email was send successfully, otherwise undefined.
|
|
46
|
-
*/
|
|
47
|
-
async sendCustomEmail(sender, recipients, subject, content) {
|
|
48
|
-
Guards.stringValue(this.CLASS_NAME, "sender", sender);
|
|
49
|
-
Guards.arrayValue(this.CLASS_NAME, "recipients", recipients);
|
|
50
|
-
Guards.stringValue(this.CLASS_NAME, "subject", subject);
|
|
51
|
-
Guards.stringValue(this.CLASS_NAME, "content", content);
|
|
52
|
-
try {
|
|
53
|
-
await this._logging?.log({
|
|
54
|
-
level: "info",
|
|
55
|
-
source: this.CLASS_NAME,
|
|
56
|
-
ts: Date.now(),
|
|
57
|
-
message: "emailSending",
|
|
58
|
-
data: {
|
|
59
|
-
type: "Custom Email"
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
const id = Converter.bytesToHex(RandomHelper.generate(32));
|
|
63
|
-
const entity = {
|
|
64
|
-
id,
|
|
65
|
-
sender,
|
|
66
|
-
recipients,
|
|
67
|
-
ts: Date.now(),
|
|
68
|
-
message: content,
|
|
69
|
-
subject,
|
|
70
|
-
status: "pending"
|
|
71
|
-
};
|
|
72
|
-
await this._messagingEmailEntryStorage.set(entity);
|
|
73
|
-
return true;
|
|
74
|
-
}
|
|
75
|
-
catch (err) {
|
|
76
|
-
throw new GeneralError(this.CLASS_NAME, "sendCustomEmailFailed", undefined, err);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Copyright 2024 IOTA Stiftung.
|
|
82
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
83
|
-
/**
|
|
84
|
-
* Class for connecting to the push notifications messaging operations of the Entity Storage.
|
|
85
|
-
*/
|
|
86
|
-
class EntityStorageMessagingPushNotificationConnector {
|
|
87
|
-
/**
|
|
88
|
-
* Runtime name for the class.
|
|
89
|
-
*/
|
|
90
|
-
CLASS_NAME = "EntityStorageMessagingPushNotificationConnector";
|
|
91
|
-
/**
|
|
92
|
-
* The logging connector.
|
|
93
|
-
* @internal
|
|
94
|
-
*/
|
|
95
|
-
_logging;
|
|
96
|
-
/**
|
|
97
|
-
* The entity storage for the push notifications device entries.
|
|
98
|
-
* @internal
|
|
99
|
-
*/
|
|
100
|
-
_messagingDeviceEntryStorage;
|
|
101
|
-
/**
|
|
102
|
-
* The entity storage for the push notifications message entries.
|
|
103
|
-
* @internal
|
|
104
|
-
*/
|
|
105
|
-
_messagingMessageEntryStorage;
|
|
106
|
-
/**
|
|
107
|
-
* Create a new instance of EntityStorageMessagingPushNotificationConnector.
|
|
108
|
-
* @param options The options for the connector.
|
|
109
|
-
* @param options.loggingConnectorType The type of logging connector to use, defaults to no logging.
|
|
110
|
-
* @param options.messagingDeviceEntryStorageConnectorType The type of entity storage connector to use for the push notifications entries, defaults to "push-notification-device-entry".
|
|
111
|
-
* @param options.messagingMessageEntryStorageConnectorType The type of entity storage connector to use for the push notifications entries, defaults to "push-notification-message-entry".
|
|
112
|
-
*/
|
|
113
|
-
constructor(options) {
|
|
114
|
-
if (Is.stringValue(options?.loggingConnectorType)) {
|
|
115
|
-
this._logging = LoggingConnectorFactory.get(options.loggingConnectorType);
|
|
116
|
-
}
|
|
117
|
-
this._messagingDeviceEntryStorage = EntityStorageConnectorFactory.get(options?.messagingDeviceEntryStorageConnectorType ??
|
|
118
|
-
StringHelper.kebabCase("PushNotificationDeviceEntry"));
|
|
119
|
-
this._messagingMessageEntryStorage = EntityStorageConnectorFactory.get(options?.messagingMessageEntryStorageConnectorType ??
|
|
120
|
-
StringHelper.kebabCase("PushNotificationMessageEntry"));
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Registers a device to an specific app in order to send notifications to it.
|
|
124
|
-
* @param applicationId The application address.
|
|
125
|
-
* @param deviceToken The device token.
|
|
126
|
-
* @returns If the device was registered successfully.
|
|
127
|
-
*/
|
|
128
|
-
async registerDevice(applicationId, deviceToken) {
|
|
129
|
-
Guards.stringValue(this.CLASS_NAME, "applicationId", applicationId);
|
|
130
|
-
Guards.stringValue(this.CLASS_NAME, "deviceToken", deviceToken);
|
|
131
|
-
try {
|
|
132
|
-
await this._logging?.log({
|
|
133
|
-
level: "info",
|
|
134
|
-
source: this.CLASS_NAME,
|
|
135
|
-
ts: Date.now(),
|
|
136
|
-
message: "deviceRegistering"
|
|
137
|
-
});
|
|
138
|
-
const id = Converter.bytesToHex(RandomHelper.generate(32));
|
|
139
|
-
const entity = {
|
|
140
|
-
id,
|
|
141
|
-
applicationId,
|
|
142
|
-
deviceToken,
|
|
143
|
-
ts: Date.now(),
|
|
144
|
-
status: "pending"
|
|
145
|
-
};
|
|
146
|
-
await this._messagingDeviceEntryStorage.set(entity);
|
|
147
|
-
return id;
|
|
148
|
-
}
|
|
149
|
-
catch (err) {
|
|
150
|
-
throw new GeneralError(this.CLASS_NAME, "deviceTokenRegisterFailed", { property: "applicationId", value: applicationId }, err);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Send a push notification to a device.
|
|
155
|
-
* @param deviceAddress The address of the device.
|
|
156
|
-
* @param title The title of the notification.
|
|
157
|
-
* @param message The message to send.
|
|
158
|
-
* @returns If the notification was sent successfully.
|
|
159
|
-
*/
|
|
160
|
-
async sendSinglePushNotification(deviceAddress, title, message) {
|
|
161
|
-
Guards.stringValue(this.CLASS_NAME, "deviceAddress", deviceAddress);
|
|
162
|
-
Guards.stringValue(this.CLASS_NAME, "title", title);
|
|
163
|
-
Guards.stringValue(this.CLASS_NAME, "message", message);
|
|
164
|
-
try {
|
|
165
|
-
await this._logging?.log({
|
|
166
|
-
level: "info",
|
|
167
|
-
source: this.CLASS_NAME,
|
|
168
|
-
ts: Date.now(),
|
|
169
|
-
message: "pushNotificationSending"
|
|
170
|
-
});
|
|
171
|
-
const id = Converter.bytesToHex(RandomHelper.generate(32));
|
|
172
|
-
const entity = {
|
|
173
|
-
id,
|
|
174
|
-
deviceAddress,
|
|
175
|
-
title,
|
|
176
|
-
message,
|
|
177
|
-
ts: Date.now(),
|
|
178
|
-
status: "pending"
|
|
179
|
-
};
|
|
180
|
-
await this._messagingMessageEntryStorage.set(entity);
|
|
181
|
-
return true;
|
|
182
|
-
}
|
|
183
|
-
catch (err) {
|
|
184
|
-
throw new GeneralError(this.CLASS_NAME, "sendPushNotificationFailed", { value: deviceAddress }, err);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Copyright 2024 IOTA Stiftung.
|
|
190
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
191
|
-
/**
|
|
192
|
-
* Class for connecting to the SMS messaging operations of the Entity Storage.
|
|
193
|
-
*/
|
|
194
|
-
class EntityStorageMessagingSmsConnector {
|
|
195
|
-
/**
|
|
196
|
-
* Runtime name for the class.
|
|
197
|
-
*/
|
|
198
|
-
CLASS_NAME = "EntityStorageMessagingSmsConnector";
|
|
199
|
-
/**
|
|
200
|
-
* The logging connector.
|
|
201
|
-
* @internal
|
|
202
|
-
*/
|
|
203
|
-
_logging;
|
|
204
|
-
/**
|
|
205
|
-
* The entity storage for the sms entries.
|
|
206
|
-
* @internal
|
|
207
|
-
*/
|
|
208
|
-
_messagingSmsEntryStorage;
|
|
209
|
-
/**
|
|
210
|
-
* Create a new instance of EntityStorageMessagingSmsConnector.
|
|
211
|
-
* @param options The options for the connector.
|
|
212
|
-
* @param options.loggingConnectorType The type of logging connector to use, defaults to no logging.
|
|
213
|
-
* @param options.messagingSmsEntryStorageConnectorType The type of entity storage connector to use for the sms entries, defaults to "sms-entry".
|
|
214
|
-
*/
|
|
215
|
-
constructor(options) {
|
|
216
|
-
if (Is.stringValue(options?.loggingConnectorType)) {
|
|
217
|
-
this._logging = LoggingConnectorFactory.get(options.loggingConnectorType);
|
|
218
|
-
}
|
|
219
|
-
this._messagingSmsEntryStorage = EntityStorageConnectorFactory.get(options?.messagingSmsEntryStorageConnectorType ?? StringHelper.kebabCase("SmsEntry"));
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Send a SMS message to a phone number.
|
|
223
|
-
* @param phoneNumber The recipient phone number.
|
|
224
|
-
* @param message The message to send.
|
|
225
|
-
* @returns If the SMS was sent successfully.
|
|
226
|
-
*/
|
|
227
|
-
async sendSMS(phoneNumber, message) {
|
|
228
|
-
Guards.stringValue(this.CLASS_NAME, "phoneNumber", phoneNumber);
|
|
229
|
-
Guards.stringValue(this.CLASS_NAME, "message", message);
|
|
230
|
-
try {
|
|
231
|
-
await this._logging?.log({
|
|
232
|
-
level: "info",
|
|
233
|
-
source: this.CLASS_NAME,
|
|
234
|
-
ts: Date.now(),
|
|
235
|
-
message: "smsSending"
|
|
236
|
-
});
|
|
237
|
-
const id = Converter.bytesToHex(RandomHelper.generate(32));
|
|
238
|
-
const entity = {
|
|
239
|
-
id,
|
|
240
|
-
phoneNumber,
|
|
241
|
-
message,
|
|
242
|
-
ts: Date.now(),
|
|
243
|
-
status: "sent"
|
|
244
|
-
};
|
|
245
|
-
await this._messagingSmsEntryStorage.set(entity);
|
|
246
|
-
return true;
|
|
247
|
-
}
|
|
248
|
-
catch (err) {
|
|
249
|
-
throw new GeneralError(this.CLASS_NAME, "sendSMSFailed", undefined, err);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
5
|
|
|
254
6
|
/**
|
|
255
7
|
* Call defining an email entry.
|
|
@@ -503,16 +255,274 @@ SmsEntry = __decorate([
|
|
|
503
255
|
entity()
|
|
504
256
|
], SmsEntry);
|
|
505
257
|
|
|
258
|
+
// Copyright 2024 IOTA Stiftung.
|
|
259
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
260
|
+
/**
|
|
261
|
+
* Class for connecting to the email messaging operations of the Entity Storage.
|
|
262
|
+
*/
|
|
263
|
+
class EntityStorageMessagingEmailConnector {
|
|
264
|
+
/**
|
|
265
|
+
* Runtime name for the class.
|
|
266
|
+
*/
|
|
267
|
+
CLASS_NAME = "EntityStorageMessagingEmailConnector";
|
|
268
|
+
/**
|
|
269
|
+
* The logging connector.
|
|
270
|
+
* @internal
|
|
271
|
+
*/
|
|
272
|
+
_logging;
|
|
273
|
+
/**
|
|
274
|
+
* The entity storage for the emails entries.
|
|
275
|
+
* @internal
|
|
276
|
+
*/
|
|
277
|
+
_messagingEmailEntryStorage;
|
|
278
|
+
/**
|
|
279
|
+
* Create a new instance of EntityStorageMessagingEmailConnector.
|
|
280
|
+
* @param options The options for the connector.
|
|
281
|
+
* @param options.loggingConnectorType The type of logging connector to use, defaults to no logging.
|
|
282
|
+
* @param options.messagingEmailEntryStorageConnectorType The type of entity storage connector to use for the email entries, defaults to "email-entry".
|
|
283
|
+
*/
|
|
284
|
+
constructor(options) {
|
|
285
|
+
if (Is.stringValue(options?.loggingConnectorType)) {
|
|
286
|
+
this._logging = LoggingConnectorFactory.get(options.loggingConnectorType);
|
|
287
|
+
}
|
|
288
|
+
this._messagingEmailEntryStorage = EntityStorageConnectorFactory.get(options?.messagingEmailEntryStorageConnectorType ??
|
|
289
|
+
StringHelper.kebabCase("EmailEntry"));
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Store a custom email using Entity Storage.
|
|
293
|
+
* @param sender The sender email address.
|
|
294
|
+
* @param recipients An array of recipients email addresses.
|
|
295
|
+
* @param subject The subject of the email.
|
|
296
|
+
* @param content The html content of the email.
|
|
297
|
+
* @returns True if the email was send successfully, otherwise undefined.
|
|
298
|
+
*/
|
|
299
|
+
async sendCustomEmail(sender, recipients, subject, content) {
|
|
300
|
+
Guards.stringValue(this.CLASS_NAME, "sender", sender);
|
|
301
|
+
Guards.arrayValue(this.CLASS_NAME, "recipients", recipients);
|
|
302
|
+
Guards.stringValue(this.CLASS_NAME, "subject", subject);
|
|
303
|
+
Guards.stringValue(this.CLASS_NAME, "content", content);
|
|
304
|
+
try {
|
|
305
|
+
await this._logging?.log({
|
|
306
|
+
level: "info",
|
|
307
|
+
source: this.CLASS_NAME,
|
|
308
|
+
ts: Date.now(),
|
|
309
|
+
message: "emailSending",
|
|
310
|
+
data: {
|
|
311
|
+
type: "Custom Email"
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
const id = Converter.bytesToHex(RandomHelper.generate(32));
|
|
315
|
+
const entity = {
|
|
316
|
+
id,
|
|
317
|
+
sender,
|
|
318
|
+
recipients,
|
|
319
|
+
ts: Date.now(),
|
|
320
|
+
message: content,
|
|
321
|
+
subject,
|
|
322
|
+
status: "pending"
|
|
323
|
+
};
|
|
324
|
+
await this._messagingEmailEntryStorage.set(entity);
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
catch (err) {
|
|
328
|
+
throw new GeneralError(this.CLASS_NAME, "sendCustomEmailFailed", undefined, err);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Copyright 2024 IOTA Stiftung.
|
|
334
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
335
|
+
/**
|
|
336
|
+
* Class for connecting to the push notifications messaging operations of the Entity Storage.
|
|
337
|
+
*/
|
|
338
|
+
class EntityStorageMessagingPushNotificationConnector {
|
|
339
|
+
/**
|
|
340
|
+
* Runtime name for the class.
|
|
341
|
+
*/
|
|
342
|
+
CLASS_NAME = "EntityStorageMessagingPushNotificationConnector";
|
|
343
|
+
/**
|
|
344
|
+
* The logging connector.
|
|
345
|
+
* @internal
|
|
346
|
+
*/
|
|
347
|
+
_logging;
|
|
348
|
+
/**
|
|
349
|
+
* The entity storage for the push notifications device entries.
|
|
350
|
+
* @internal
|
|
351
|
+
*/
|
|
352
|
+
_messagingDeviceEntryStorage;
|
|
353
|
+
/**
|
|
354
|
+
* The entity storage for the push notifications message entries.
|
|
355
|
+
* @internal
|
|
356
|
+
*/
|
|
357
|
+
_messagingMessageEntryStorage;
|
|
358
|
+
/**
|
|
359
|
+
* Create a new instance of EntityStorageMessagingPushNotificationConnector.
|
|
360
|
+
* @param options The options for the connector.
|
|
361
|
+
* @param options.loggingConnectorType The type of logging connector to use, defaults to no logging.
|
|
362
|
+
* @param options.messagingDeviceEntryStorageConnectorType The type of entity storage connector to use for the push notifications entries, defaults to "push-notification-device-entry".
|
|
363
|
+
* @param options.messagingMessageEntryStorageConnectorType The type of entity storage connector to use for the push notifications entries, defaults to "push-notification-message-entry".
|
|
364
|
+
*/
|
|
365
|
+
constructor(options) {
|
|
366
|
+
if (Is.stringValue(options?.loggingConnectorType)) {
|
|
367
|
+
this._logging = LoggingConnectorFactory.get(options.loggingConnectorType);
|
|
368
|
+
}
|
|
369
|
+
this._messagingDeviceEntryStorage = EntityStorageConnectorFactory.get(options?.messagingDeviceEntryStorageConnectorType ??
|
|
370
|
+
StringHelper.kebabCase("PushNotificationDeviceEntry"));
|
|
371
|
+
this._messagingMessageEntryStorage = EntityStorageConnectorFactory.get(options?.messagingMessageEntryStorageConnectorType ??
|
|
372
|
+
StringHelper.kebabCase("PushNotificationMessageEntry"));
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Registers a device to an specific app in order to send notifications to it.
|
|
376
|
+
* @param applicationId The application address.
|
|
377
|
+
* @param deviceToken The device token.
|
|
378
|
+
* @returns If the device was registered successfully.
|
|
379
|
+
*/
|
|
380
|
+
async registerDevice(applicationId, deviceToken) {
|
|
381
|
+
Guards.stringValue(this.CLASS_NAME, "applicationId", applicationId);
|
|
382
|
+
Guards.stringValue(this.CLASS_NAME, "deviceToken", deviceToken);
|
|
383
|
+
try {
|
|
384
|
+
await this._logging?.log({
|
|
385
|
+
level: "info",
|
|
386
|
+
source: this.CLASS_NAME,
|
|
387
|
+
ts: Date.now(),
|
|
388
|
+
message: "deviceRegistering"
|
|
389
|
+
});
|
|
390
|
+
const id = Converter.bytesToHex(RandomHelper.generate(32));
|
|
391
|
+
const entity = {
|
|
392
|
+
id,
|
|
393
|
+
applicationId,
|
|
394
|
+
deviceToken,
|
|
395
|
+
ts: Date.now(),
|
|
396
|
+
status: "pending"
|
|
397
|
+
};
|
|
398
|
+
await this._messagingDeviceEntryStorage.set(entity);
|
|
399
|
+
return id;
|
|
400
|
+
}
|
|
401
|
+
catch (err) {
|
|
402
|
+
throw new GeneralError(this.CLASS_NAME, "deviceTokenRegisterFailed", { property: "applicationId", value: applicationId }, err);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Send a push notification to a device.
|
|
407
|
+
* @param deviceAddress The address of the device.
|
|
408
|
+
* @param title The title of the notification.
|
|
409
|
+
* @param message The message to send.
|
|
410
|
+
* @returns If the notification was sent successfully.
|
|
411
|
+
*/
|
|
412
|
+
async sendSinglePushNotification(deviceAddress, title, message) {
|
|
413
|
+
Guards.stringValue(this.CLASS_NAME, "deviceAddress", deviceAddress);
|
|
414
|
+
Guards.stringValue(this.CLASS_NAME, "title", title);
|
|
415
|
+
Guards.stringValue(this.CLASS_NAME, "message", message);
|
|
416
|
+
try {
|
|
417
|
+
await this._logging?.log({
|
|
418
|
+
level: "info",
|
|
419
|
+
source: this.CLASS_NAME,
|
|
420
|
+
ts: Date.now(),
|
|
421
|
+
message: "pushNotificationSending"
|
|
422
|
+
});
|
|
423
|
+
const id = Converter.bytesToHex(RandomHelper.generate(32));
|
|
424
|
+
const entity = {
|
|
425
|
+
id,
|
|
426
|
+
deviceAddress,
|
|
427
|
+
title,
|
|
428
|
+
message,
|
|
429
|
+
ts: Date.now(),
|
|
430
|
+
status: "pending"
|
|
431
|
+
};
|
|
432
|
+
await this._messagingMessageEntryStorage.set(entity);
|
|
433
|
+
return true;
|
|
434
|
+
}
|
|
435
|
+
catch (err) {
|
|
436
|
+
throw new GeneralError(this.CLASS_NAME, "sendPushNotificationFailed", { value: deviceAddress }, err);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// Copyright 2024 IOTA Stiftung.
|
|
442
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
443
|
+
/**
|
|
444
|
+
* Class for connecting to the SMS messaging operations of the Entity Storage.
|
|
445
|
+
*/
|
|
446
|
+
class EntityStorageMessagingSmsConnector {
|
|
447
|
+
/**
|
|
448
|
+
* Runtime name for the class.
|
|
449
|
+
*/
|
|
450
|
+
CLASS_NAME = "EntityStorageMessagingSmsConnector";
|
|
451
|
+
/**
|
|
452
|
+
* The logging connector.
|
|
453
|
+
* @internal
|
|
454
|
+
*/
|
|
455
|
+
_logging;
|
|
456
|
+
/**
|
|
457
|
+
* The entity storage for the sms entries.
|
|
458
|
+
* @internal
|
|
459
|
+
*/
|
|
460
|
+
_messagingSmsEntryStorage;
|
|
461
|
+
/**
|
|
462
|
+
* Create a new instance of EntityStorageMessagingSmsConnector.
|
|
463
|
+
* @param options The options for the connector.
|
|
464
|
+
* @param options.loggingConnectorType The type of logging connector to use, defaults to no logging.
|
|
465
|
+
* @param options.messagingSmsEntryStorageConnectorType The type of entity storage connector to use for the sms entries, defaults to "sms-entry".
|
|
466
|
+
*/
|
|
467
|
+
constructor(options) {
|
|
468
|
+
if (Is.stringValue(options?.loggingConnectorType)) {
|
|
469
|
+
this._logging = LoggingConnectorFactory.get(options.loggingConnectorType);
|
|
470
|
+
}
|
|
471
|
+
this._messagingSmsEntryStorage = EntityStorageConnectorFactory.get(options?.messagingSmsEntryStorageConnectorType ?? StringHelper.kebabCase("SmsEntry"));
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Send a SMS message to a phone number.
|
|
475
|
+
* @param phoneNumber The recipient phone number.
|
|
476
|
+
* @param message The message to send.
|
|
477
|
+
* @returns If the SMS was sent successfully.
|
|
478
|
+
*/
|
|
479
|
+
async sendSMS(phoneNumber, message) {
|
|
480
|
+
Guards.stringValue(this.CLASS_NAME, "phoneNumber", phoneNumber);
|
|
481
|
+
Guards.stringValue(this.CLASS_NAME, "message", message);
|
|
482
|
+
try {
|
|
483
|
+
await this._logging?.log({
|
|
484
|
+
level: "info",
|
|
485
|
+
source: this.CLASS_NAME,
|
|
486
|
+
ts: Date.now(),
|
|
487
|
+
message: "smsSending"
|
|
488
|
+
});
|
|
489
|
+
const id = Converter.bytesToHex(RandomHelper.generate(32));
|
|
490
|
+
const entity = {
|
|
491
|
+
id,
|
|
492
|
+
phoneNumber,
|
|
493
|
+
message,
|
|
494
|
+
ts: Date.now(),
|
|
495
|
+
status: "sent"
|
|
496
|
+
};
|
|
497
|
+
await this._messagingSmsEntryStorage.set(entity);
|
|
498
|
+
return true;
|
|
499
|
+
}
|
|
500
|
+
catch (err) {
|
|
501
|
+
throw new GeneralError(this.CLASS_NAME, "sendSMSFailed", undefined, err);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
506
|
// Copyright 2024 IOTA Stiftung.
|
|
507
507
|
// SPDX-License-Identifier: Apache-2.0.
|
|
508
508
|
/**
|
|
509
509
|
* Initialize the schema for the messaging connector entity storage.
|
|
510
|
+
* @param options The options for the initialisation.
|
|
511
|
+
* @param options.email Should we register email schemas.
|
|
512
|
+
* @param options.sms Should we register sms schemas.
|
|
513
|
+
* @param options.pushNotification Should we register push notification schemas.
|
|
510
514
|
*/
|
|
511
|
-
function initSchema() {
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
515
|
+
function initSchema(options) {
|
|
516
|
+
if (options?.email ?? true) {
|
|
517
|
+
EntitySchemaFactory.register("EmailEntry", () => EntitySchemaHelper.getSchema(EmailEntry));
|
|
518
|
+
}
|
|
519
|
+
if (options?.pushNotification ?? true) {
|
|
520
|
+
EntitySchemaFactory.register("PushNotificationDeviceEntry", () => EntitySchemaHelper.getSchema(PushNotificationDeviceEntry));
|
|
521
|
+
EntitySchemaFactory.register("PushNotificationMessageEntry", () => EntitySchemaHelper.getSchema(PushNotificationMessageEntry));
|
|
522
|
+
}
|
|
523
|
+
if (options?.sms ?? true) {
|
|
524
|
+
EntitySchemaFactory.register("SmsEntry", () => EntitySchemaHelper.getSchema(SmsEntry));
|
|
525
|
+
}
|
|
516
526
|
}
|
|
517
527
|
|
|
518
|
-
export { EntityStorageMessagingEmailConnector, EntityStorageMessagingPushNotificationConnector, EntityStorageMessagingSmsConnector, initSchema };
|
|
528
|
+
export { EmailEntry, EntityStorageMessagingEmailConnector, EntityStorageMessagingPushNotificationConnector, EntityStorageMessagingSmsConnector, PushNotificationDeviceEntry, PushNotificationMessageEntry, SmsEntry, initSchema };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
export * from "./entities/emailEntry";
|
|
2
|
+
export * from "./entities/pushNotificationDeviceEntry";
|
|
3
|
+
export * from "./entities/pushNotificationMessageEntry";
|
|
4
|
+
export * from "./entities/smsEntry";
|
|
1
5
|
export * from "./entityStorageMessagingEmailConnector";
|
|
2
6
|
export * from "./entityStorageMessagingPushNotificationConnector";
|
|
3
7
|
export * from "./entityStorageMessagingSmsConnector";
|
package/dist/types/schema.d.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Initialize the schema for the messaging connector entity storage.
|
|
3
|
+
* @param options The options for the initialisation.
|
|
4
|
+
* @param options.email Should we register email schemas.
|
|
5
|
+
* @param options.sms Should we register sms schemas.
|
|
6
|
+
* @param options.pushNotification Should we register push notification schemas.
|
|
3
7
|
*/
|
|
4
|
-
export declare function initSchema(
|
|
8
|
+
export declare function initSchema(options?: {
|
|
9
|
+
email?: boolean;
|
|
10
|
+
sms?: boolean;
|
|
11
|
+
pushNotification?: boolean;
|
|
12
|
+
}): void;
|
package/docs/changelog.md
CHANGED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Class: EmailEntry
|
|
2
|
+
|
|
3
|
+
Call defining an email entry.
|
|
4
|
+
|
|
5
|
+
## Constructors
|
|
6
|
+
|
|
7
|
+
### new EmailEntry()
|
|
8
|
+
|
|
9
|
+
> **new EmailEntry**(): [`EmailEntry`](EmailEntry.md)
|
|
10
|
+
|
|
11
|
+
#### Returns
|
|
12
|
+
|
|
13
|
+
[`EmailEntry`](EmailEntry.md)
|
|
14
|
+
|
|
15
|
+
## Properties
|
|
16
|
+
|
|
17
|
+
### id
|
|
18
|
+
|
|
19
|
+
> **id**: `string`
|
|
20
|
+
|
|
21
|
+
The id.
|
|
22
|
+
|
|
23
|
+
***
|
|
24
|
+
|
|
25
|
+
### sender
|
|
26
|
+
|
|
27
|
+
> **sender**: `string`
|
|
28
|
+
|
|
29
|
+
The sender email address.
|
|
30
|
+
|
|
31
|
+
***
|
|
32
|
+
|
|
33
|
+
### recipients
|
|
34
|
+
|
|
35
|
+
> **recipients**: `string`[]
|
|
36
|
+
|
|
37
|
+
The recipient email addresses.
|
|
38
|
+
|
|
39
|
+
***
|
|
40
|
+
|
|
41
|
+
### ts
|
|
42
|
+
|
|
43
|
+
> **ts**: `number`
|
|
44
|
+
|
|
45
|
+
The timestamp of the email entry.
|
|
46
|
+
|
|
47
|
+
***
|
|
48
|
+
|
|
49
|
+
### message
|
|
50
|
+
|
|
51
|
+
> **message**: `string`
|
|
52
|
+
|
|
53
|
+
The message.
|
|
54
|
+
|
|
55
|
+
***
|
|
56
|
+
|
|
57
|
+
### subject
|
|
58
|
+
|
|
59
|
+
> **subject**: `string`
|
|
60
|
+
|
|
61
|
+
The subject.
|
|
62
|
+
|
|
63
|
+
***
|
|
64
|
+
|
|
65
|
+
### status
|
|
66
|
+
|
|
67
|
+
> **status**: `string`
|
|
68
|
+
|
|
69
|
+
The status.
|
|
70
|
+
|
|
71
|
+
***
|
|
72
|
+
|
|
73
|
+
### error?
|
|
74
|
+
|
|
75
|
+
> `optional` **error**: `IError`
|
|
76
|
+
|
|
77
|
+
The error.
|