@twin.org/messaging-service 0.0.2-next.5 → 0.0.3-next.1

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.
Files changed (29) hide show
  1. package/dist/es/entities/templateEntry.js +45 -0
  2. package/dist/es/entities/templateEntry.js.map +1 -0
  3. package/dist/es/index.js +10 -0
  4. package/dist/es/index.js.map +1 -0
  5. package/dist/es/messagingAdminService.js +106 -0
  6. package/dist/es/messagingAdminService.js.map +1 -0
  7. package/dist/es/messagingService.js +156 -0
  8. package/dist/es/messagingService.js.map +1 -0
  9. package/dist/es/models/IMessagingAdminServiceConfig.js +4 -0
  10. package/dist/es/models/IMessagingAdminServiceConfig.js.map +1 -0
  11. package/dist/es/models/IMessagingAdminServiceConstructorOptions.js +2 -0
  12. package/dist/es/models/IMessagingAdminServiceConstructorOptions.js.map +1 -0
  13. package/dist/es/models/IMessagingServiceConstructorOptions.js +4 -0
  14. package/dist/es/models/IMessagingServiceConstructorOptions.js.map +1 -0
  15. package/dist/es/schema.js +11 -0
  16. package/dist/es/schema.js.map +1 -0
  17. package/dist/types/index.d.ts +7 -7
  18. package/dist/types/messagingAdminService.d.ts +8 -10
  19. package/dist/types/messagingService.d.ts +7 -2
  20. package/dist/types/models/IMessagingAdminServiceConfig.d.ts +0 -10
  21. package/dist/types/models/IMessagingAdminServiceConstructorOptions.d.ts +1 -1
  22. package/docs/changelog.md +42 -0
  23. package/docs/open-api/spec.json +2 -2
  24. package/docs/reference/classes/MessagingAdminService.md +7 -25
  25. package/docs/reference/classes/MessagingService.md +17 -3
  26. package/docs/reference/interfaces/IMessagingAdminServiceConfig.md +0 -24
  27. package/package.json +12 -11
  28. package/dist/cjs/index.cjs +0 -323
  29. package/dist/esm/index.mjs +0 -319
@@ -1,319 +0,0 @@
1
- import { property, SortDirection, entity, EntitySchemaFactory, EntitySchemaHelper } from '@twin.org/entity';
2
- import { Is, Guards, GeneralError, ComponentFactory } from '@twin.org/core';
3
- import { EntityStorageConnectorFactory } from '@twin.org/entity-storage-models';
4
- import { MessagingEmailConnectorFactory, MessagingPushNotificationsConnectorFactory, MessagingSmsConnectorFactory } from '@twin.org/messaging-models';
5
-
6
- // Copyright 2024 IOTA Stiftung.
7
- // SPDX-License-Identifier: Apache-2.0.
8
- /**
9
- * Call defining a template message entry.
10
- */
11
- let TemplateEntry = class TemplateEntry {
12
- /**
13
- * The id.
14
- */
15
- id;
16
- /**
17
- * The title.
18
- */
19
- title;
20
- /**
21
- * The content.
22
- */
23
- content;
24
- /**
25
- * The timestamp of the template entry.
26
- */
27
- dateCreated;
28
- };
29
- __decorate([
30
- property({ type: "string", isPrimary: true }),
31
- __metadata("design:type", String)
32
- ], TemplateEntry.prototype, "id", void 0);
33
- __decorate([
34
- property({ type: "string" }),
35
- __metadata("design:type", String)
36
- ], TemplateEntry.prototype, "title", void 0);
37
- __decorate([
38
- property({ type: "string" }),
39
- __metadata("design:type", String)
40
- ], TemplateEntry.prototype, "content", void 0);
41
- __decorate([
42
- property({ type: "string", format: "date-time", sortDirection: SortDirection.Descending }),
43
- __metadata("design:type", String)
44
- ], TemplateEntry.prototype, "dateCreated", void 0);
45
- TemplateEntry = __decorate([
46
- entity()
47
- ], TemplateEntry);
48
-
49
- // Copyright 2024 IOTA Stiftung.
50
- // SPDX-License-Identifier: Apache-2.0.
51
- /**
52
- * Service for performing email messaging operations to a connector.
53
- */
54
- class MessagingAdminService {
55
- /**
56
- * Default locale for the messaging service.
57
- */
58
- static _DEFAULT_LOCALE = "en";
59
- /**
60
- * Runtime name for the class.
61
- */
62
- CLASS_NAME = "MessagingAdminService";
63
- /**
64
- * Entity storage connector used by the service.
65
- * @internal
66
- */
67
- _entityStorageConnector;
68
- /**
69
- * The default locale to use for the messaging service.
70
- * @internal
71
- */
72
- _defaultLocale;
73
- /**
74
- * Initial set of templates to create on startup.
75
- * @internal
76
- */
77
- _initialTemplates;
78
- /**
79
- * Create a new instance of MessagingAdminService.
80
- * @param options The options for the connector.
81
- */
82
- constructor(options) {
83
- this._entityStorageConnector = EntityStorageConnectorFactory.get(options?.templateEntryStorageConnectorType ?? "template-entry");
84
- this._defaultLocale = options?.config?.defaultLocale ?? MessagingAdminService._DEFAULT_LOCALE;
85
- this._initialTemplates = options?.config?.templates;
86
- }
87
- /**
88
- * The component needs to be started when the node is initialized.
89
- * @param nodeIdentity The identity of the node starting the component.
90
- * @param nodeLoggingComponentType The node logging component type.
91
- * @returns Nothing.
92
- */
93
- async start(nodeIdentity, nodeLoggingComponentType) {
94
- if (Is.arrayValue(this._initialTemplates)) {
95
- for (const template of this._initialTemplates) {
96
- for (const locale of Object.keys(template.content)) {
97
- await this.setTemplate(template.templateId, locale, template.title, template.content[locale]);
98
- }
99
- }
100
- }
101
- }
102
- /**
103
- * Create or update a template.
104
- * @param templateId The id of the template.
105
- * @param locale The locale of the template.
106
- * @param title The title of the template.
107
- * @param content The content of the template.
108
- * @returns Nothing.
109
- */
110
- async setTemplate(templateId, locale, title, content) {
111
- Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
112
- Guards.stringValue(this.CLASS_NAME, "locale", locale);
113
- Guards.stringValue(this.CLASS_NAME, "title", title);
114
- Guards.stringValue(this.CLASS_NAME, "content", content);
115
- const templateEntry = new TemplateEntry();
116
- templateEntry.id = `${templateId}:${locale}`;
117
- templateEntry.dateCreated = new Date(Date.now()).toISOString();
118
- templateEntry.title = title;
119
- templateEntry.content = content;
120
- await this._entityStorageConnector.set(templateEntry);
121
- }
122
- /**
123
- * Get the email template by id and locale.
124
- * @param templateId The id of the email template.
125
- * @param locale The locale of the email template.
126
- * @returns The email template.
127
- */
128
- async getTemplate(templateId, locale) {
129
- Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
130
- Guards.stringValue(this.CLASS_NAME, "locale", locale);
131
- let templateEntry;
132
- try {
133
- // First try to get the template for the requested locale
134
- templateEntry = await this._entityStorageConnector.get(`${templateId}:${locale}`);
135
- }
136
- catch { }
137
- // If the template is not found for the requested locale, try to get it for the default locale
138
- // only if the requested locale is different from the default locale
139
- if (Is.empty(templateEntry) && this._defaultLocale !== locale) {
140
- try {
141
- templateEntry = await this._entityStorageConnector.get(`${templateId}:${this._defaultLocale}`);
142
- }
143
- catch { }
144
- }
145
- if (Is.empty(templateEntry)) {
146
- throw new GeneralError(this.CLASS_NAME, "getTemplateFailed", { templateId, locale });
147
- }
148
- return templateEntry;
149
- }
150
- /**
151
- * Remove a template.
152
- * @param templateId The id of the template.
153
- * @param locale The locale of the template.
154
- * @returns Nothing
155
- */
156
- async removeTemplate(templateId, locale) {
157
- Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
158
- Guards.stringValue(this.CLASS_NAME, "locale", locale);
159
- return this._entityStorageConnector.remove(`${templateId}:${locale}`);
160
- }
161
- }
162
-
163
- // Copyright 2024 IOTA Stiftung.
164
- // SPDX-License-Identifier: Apache-2.0.
165
- /**
166
- * Service for performing email messaging operations to a connector.
167
- */
168
- class MessagingService {
169
- /**
170
- * Runtime name for the class.
171
- */
172
- CLASS_NAME = "MessagingService";
173
- /**
174
- * Emails messaging connector used by the service.
175
- * @internal
176
- */
177
- _emailMessagingConnector;
178
- /**
179
- * Push notifications messaging connector used by the service.
180
- * @internal
181
- */
182
- _pushNotificationMessagingConnector;
183
- /**
184
- * SMS messaging connector used by the service.
185
- * @internal
186
- */
187
- _smsMessagingConnector;
188
- /**
189
- * The admin component for the messaging.
190
- * @internal
191
- */
192
- _messagingAdminComponent;
193
- /**
194
- * Create a new instance of MessagingService.
195
- * @param options The options for the connector.
196
- */
197
- constructor(options) {
198
- if (Is.stringValue(options?.messagingEmailConnectorType)) {
199
- this._emailMessagingConnector = MessagingEmailConnectorFactory.get(options.messagingEmailConnectorType);
200
- }
201
- if (Is.stringValue(options?.messagingPushNotificationConnectorType)) {
202
- this._pushNotificationMessagingConnector = MessagingPushNotificationsConnectorFactory.get(options.messagingPushNotificationConnectorType);
203
- }
204
- if (Is.stringValue(options?.messagingSmsConnectorType)) {
205
- this._smsMessagingConnector = MessagingSmsConnectorFactory.get(options.messagingSmsConnectorType);
206
- }
207
- this._messagingAdminComponent = ComponentFactory.get(options?.messagingAdminComponentType ?? "messaging-admin");
208
- }
209
- /**
210
- * Send a custom email.
211
- * @param sender The sender email address.
212
- * @param recipients An array of recipients email addresses.
213
- * @param templateId The id of the email template.
214
- * @param data The data to populate the email template.
215
- * @param locale The locale of the email template.
216
- * @returns If the email was sent successfully.
217
- */
218
- async sendCustomEmail(sender, recipients, templateId, data, locale) {
219
- if (Is.empty(this._emailMessagingConnector)) {
220
- throw new GeneralError(this.CLASS_NAME, "notConfiguredEmailMessagingConnector");
221
- }
222
- Guards.stringValue(this.CLASS_NAME, "sender", sender);
223
- Guards.arrayValue(this.CLASS_NAME, "recipients", recipients);
224
- Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
225
- Guards.object(this.CLASS_NAME, "data", data);
226
- Guards.stringValue(this.CLASS_NAME, "locale", locale);
227
- const template = await this._messagingAdminComponent.getTemplate(templateId, locale);
228
- const populatedTemplate = this.populateTemplate(template, data);
229
- return this._emailMessagingConnector.sendCustomEmail(sender, recipients, populatedTemplate.title, populatedTemplate.content);
230
- }
231
- /**
232
- * Registers a device to an specific app in order to send notifications to it.
233
- * @param applicationId The application address.
234
- * @param deviceToken The device token.
235
- * @returns If the device was registered successfully.
236
- */
237
- async registerDevice(applicationId, deviceToken) {
238
- if (Is.empty(this._pushNotificationMessagingConnector)) {
239
- throw new GeneralError(this.CLASS_NAME, "notConfiguredPushNotificationMessagingConnector");
240
- }
241
- Guards.stringValue(this.CLASS_NAME, "applicationId", applicationId);
242
- Guards.stringValue(this.CLASS_NAME, "deviceToken", deviceToken);
243
- return this._pushNotificationMessagingConnector.registerDevice(applicationId, deviceToken);
244
- }
245
- /**
246
- * Send a push notification to a device.
247
- * @param deviceAddress The address of the device.
248
- * @param templateId The id of the push notification template.
249
- * @param data The data to populate the push notification template.
250
- * @param locale The locale of the push notification template.
251
- * @returns If the notification was sent successfully.
252
- */
253
- async sendSinglePushNotification(deviceAddress, templateId, data, locale) {
254
- if (Is.empty(this._pushNotificationMessagingConnector)) {
255
- throw new GeneralError(this.CLASS_NAME, "notConfiguredPushNotificationMessagingConnector");
256
- }
257
- Guards.stringValue(this.CLASS_NAME, "deviceAddress", deviceAddress);
258
- Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
259
- Guards.object(this.CLASS_NAME, "data", data);
260
- Guards.stringValue(this.CLASS_NAME, "locale", locale);
261
- const template = await this._messagingAdminComponent.getTemplate(templateId, locale);
262
- const populatedTemplate = this.populateTemplate(template, data);
263
- return this._pushNotificationMessagingConnector.sendSinglePushNotification(deviceAddress, populatedTemplate.title, populatedTemplate.content);
264
- }
265
- /**
266
- * Send a SMS message to a phone number.
267
- * @param phoneNumber The recipient phone number.
268
- * @param templateId The id of the SMS template.
269
- * @param data The data to populate the SMS template.
270
- * @param locale The locale of the SMS template.
271
- * @returns If the SMS was sent successfully.
272
- */
273
- async sendSMS(phoneNumber, templateId, data, locale) {
274
- if (Is.empty(this._smsMessagingConnector)) {
275
- throw new GeneralError(this.CLASS_NAME, "notConfiguredSmsMessagingConnector");
276
- }
277
- Guards.stringValue(this.CLASS_NAME, "phoneNumber", phoneNumber);
278
- Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
279
- Guards.object(this.CLASS_NAME, "data", data);
280
- Guards.stringValue(this.CLASS_NAME, "locale", locale);
281
- const template = await this._messagingAdminComponent.getTemplate(templateId, locale);
282
- const populatedTemplate = this.populateTemplate(template, data);
283
- return this._smsMessagingConnector.sendSMS(phoneNumber, populatedTemplate.content);
284
- }
285
- /**
286
- * Populate the template with data.
287
- * @param template The template.
288
- * @param template.title The title of the template.
289
- * @param template.content The content of the template.
290
- * @param data The data to populate the template.
291
- * @internal
292
- * @returns The populated template.
293
- */
294
- populateTemplate(template, data) {
295
- let populatedTitle = template.title;
296
- let populatedContent = template.content;
297
- for (const key in data) {
298
- const value = data[key];
299
- const placeholder = `{{${key}}}`;
300
- populatedTitle = populatedTitle.replace(new RegExp(placeholder, "g"), value);
301
- populatedContent = populatedContent.replace(new RegExp(placeholder, "g"), value);
302
- }
303
- return {
304
- title: populatedTitle,
305
- content: populatedContent
306
- };
307
- }
308
- }
309
-
310
- // Copyright 2024 IOTA Stiftung.
311
- // SPDX-License-Identifier: Apache-2.0.
312
- /**
313
- * Initialize the schema for the messaging service.
314
- */
315
- function initSchema() {
316
- EntitySchemaFactory.register("TemplateEntry", () => EntitySchemaHelper.getSchema(TemplateEntry));
317
- }
318
-
319
- export { MessagingAdminService, MessagingService, TemplateEntry, initSchema };