@twin.org/messaging-service 0.0.2-next.1 → 0.0.2-next.3
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 +100 -42
- package/dist/esm/index.mjs +101 -44
- package/dist/types/index.d.ts +3 -0
- package/dist/types/messagingAdminService.d.ts +46 -0
- package/dist/types/messagingService.d.ts +0 -9
- package/dist/types/models/IMessagingAdminServiceConfig.d.ts +10 -0
- package/dist/types/models/IMessagingAdminServiceConstructorOptions.d.ts +15 -0
- package/dist/types/models/IMessagingServiceConstructorOptions.d.ts +3 -3
- package/docs/changelog.md +28 -0
- package/docs/reference/classes/MessagingAdminService.md +147 -0
- package/docs/reference/classes/MessagingService.md +0 -40
- package/docs/reference/index.md +3 -0
- package/docs/reference/interfaces/IMessagingAdminServiceConfig.md +17 -0
- package/docs/reference/interfaces/IMessagingAdminServiceConstructorOptions.md +25 -0
- package/docs/reference/interfaces/IMessagingServiceConstructorOptions.md +4 -4
- package/locales/en.json +3 -1
- package/package.json +2 -2
package/dist/cjs/index.cjs
CHANGED
|
@@ -48,6 +48,99 @@ exports.TemplateEntry = __decorate([
|
|
|
48
48
|
entity.entity()
|
|
49
49
|
], exports.TemplateEntry);
|
|
50
50
|
|
|
51
|
+
// Copyright 2024 IOTA Stiftung.
|
|
52
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
53
|
+
/**
|
|
54
|
+
* Service for performing email messaging operations to a connector.
|
|
55
|
+
*/
|
|
56
|
+
class MessagingAdminService {
|
|
57
|
+
/**
|
|
58
|
+
* Default locale for the messaging service.
|
|
59
|
+
*/
|
|
60
|
+
static _DEFAULT_LOCALE = "en";
|
|
61
|
+
/**
|
|
62
|
+
* Runtime name for the class.
|
|
63
|
+
*/
|
|
64
|
+
CLASS_NAME = "MessagingAdminService";
|
|
65
|
+
/**
|
|
66
|
+
* Entity storage connector used by the service.
|
|
67
|
+
* @internal
|
|
68
|
+
*/
|
|
69
|
+
_entityStorageConnector;
|
|
70
|
+
/**
|
|
71
|
+
* The default locale to use for the messaging service.
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
_defaultLocale;
|
|
75
|
+
/**
|
|
76
|
+
* Create a new instance of MessagingAdminService.
|
|
77
|
+
* @param options The options for the connector.
|
|
78
|
+
*/
|
|
79
|
+
constructor(options) {
|
|
80
|
+
this._entityStorageConnector = entityStorageModels.EntityStorageConnectorFactory.get(options?.templateEntryStorageConnectorType ?? "template-entry");
|
|
81
|
+
this._defaultLocale = options?.config?.defaultLocale ?? MessagingAdminService._DEFAULT_LOCALE;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Create or update a template.
|
|
85
|
+
* @param templateId The id of the template.
|
|
86
|
+
* @param locale The locale of the template.
|
|
87
|
+
* @param title The title of the template.
|
|
88
|
+
* @param content The content of the template.
|
|
89
|
+
* @returns Nothing.
|
|
90
|
+
*/
|
|
91
|
+
async setTemplate(templateId, locale, title, content) {
|
|
92
|
+
core.Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
93
|
+
core.Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
94
|
+
core.Guards.stringValue(this.CLASS_NAME, "title", title);
|
|
95
|
+
core.Guards.stringValue(this.CLASS_NAME, "content", content);
|
|
96
|
+
const templateEntry = new exports.TemplateEntry();
|
|
97
|
+
templateEntry.id = `${templateId}:${locale}`;
|
|
98
|
+
templateEntry.dateCreated = new Date(Date.now()).toISOString();
|
|
99
|
+
templateEntry.title = title;
|
|
100
|
+
templateEntry.content = content;
|
|
101
|
+
await this._entityStorageConnector.set(templateEntry);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Get the email template by id and locale.
|
|
105
|
+
* @param templateId The id of the email template.
|
|
106
|
+
* @param locale The locale of the email template.
|
|
107
|
+
* @returns The email template.
|
|
108
|
+
*/
|
|
109
|
+
async getTemplate(templateId, locale) {
|
|
110
|
+
core.Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
111
|
+
core.Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
112
|
+
let templateEntry;
|
|
113
|
+
try {
|
|
114
|
+
// First try to get the template for the requested locale
|
|
115
|
+
templateEntry = await this._entityStorageConnector.get(`${templateId}:${locale}`);
|
|
116
|
+
}
|
|
117
|
+
catch { }
|
|
118
|
+
// If the template is not found for the requested locale, try to get it for the default locale
|
|
119
|
+
// only if the requested locale is different from the default locale
|
|
120
|
+
if (core.Is.empty(templateEntry) && this._defaultLocale !== locale) {
|
|
121
|
+
try {
|
|
122
|
+
templateEntry = await this._entityStorageConnector.get(`${templateId}:${this._defaultLocale}`);
|
|
123
|
+
}
|
|
124
|
+
catch { }
|
|
125
|
+
}
|
|
126
|
+
if (core.Is.empty(templateEntry)) {
|
|
127
|
+
throw new core.GeneralError(this.CLASS_NAME, "getTemplateFailed", { templateId, locale });
|
|
128
|
+
}
|
|
129
|
+
return templateEntry;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Remove a template.
|
|
133
|
+
* @param templateId The id of the template.
|
|
134
|
+
* @param locale The locale of the template.
|
|
135
|
+
* @returns Nothing
|
|
136
|
+
*/
|
|
137
|
+
async removeTemplate(templateId, locale) {
|
|
138
|
+
core.Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
139
|
+
core.Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
140
|
+
return this._entityStorageConnector.remove(`${templateId}:${locale}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
51
144
|
// Copyright 2024 IOTA Stiftung.
|
|
52
145
|
// SPDX-License-Identifier: Apache-2.0.
|
|
53
146
|
/**
|
|
@@ -74,10 +167,10 @@ class MessagingService {
|
|
|
74
167
|
*/
|
|
75
168
|
_smsMessagingConnector;
|
|
76
169
|
/**
|
|
77
|
-
*
|
|
170
|
+
* The admin component for the messaging.
|
|
78
171
|
* @internal
|
|
79
172
|
*/
|
|
80
|
-
|
|
173
|
+
_messagingAdminComponent;
|
|
81
174
|
/**
|
|
82
175
|
* Create a new instance of MessagingService.
|
|
83
176
|
* @param options The options for the connector.
|
|
@@ -92,7 +185,7 @@ class MessagingService {
|
|
|
92
185
|
if (core.Is.stringValue(options?.messagingSmsConnectorType)) {
|
|
93
186
|
this._smsMessagingConnector = messagingModels.MessagingSmsConnectorFactory.get(options.messagingSmsConnectorType);
|
|
94
187
|
}
|
|
95
|
-
this.
|
|
188
|
+
this._messagingAdminComponent = core.ComponentFactory.get(options?.messagingAdminComponentType ?? "messaging-admin");
|
|
96
189
|
}
|
|
97
190
|
/**
|
|
98
191
|
* Send a custom email.
|
|
@@ -112,7 +205,7 @@ class MessagingService {
|
|
|
112
205
|
core.Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
113
206
|
core.Guards.object(this.CLASS_NAME, "data", data);
|
|
114
207
|
core.Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
115
|
-
const template = await this.getTemplate(templateId, locale);
|
|
208
|
+
const template = await this._messagingAdminComponent.getTemplate(templateId, locale);
|
|
116
209
|
const populatedTemplate = this.populateTemplate(template, data);
|
|
117
210
|
return this._emailMessagingConnector.sendCustomEmail(sender, recipients, populatedTemplate.title, populatedTemplate.content);
|
|
118
211
|
}
|
|
@@ -146,7 +239,7 @@ class MessagingService {
|
|
|
146
239
|
core.Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
147
240
|
core.Guards.object(this.CLASS_NAME, "data", data);
|
|
148
241
|
core.Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
149
|
-
const template = await this.getTemplate(templateId, locale);
|
|
242
|
+
const template = await this._messagingAdminComponent.getTemplate(templateId, locale);
|
|
150
243
|
const populatedTemplate = this.populateTemplate(template, data);
|
|
151
244
|
return this._pushNotificationMessagingConnector.sendSinglePushNotification(deviceAddress, populatedTemplate.title, populatedTemplate.content);
|
|
152
245
|
}
|
|
@@ -166,46 +259,10 @@ class MessagingService {
|
|
|
166
259
|
core.Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
167
260
|
core.Guards.object(this.CLASS_NAME, "data", data);
|
|
168
261
|
core.Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
169
|
-
const template = await this.getTemplate(templateId, locale);
|
|
262
|
+
const template = await this._messagingAdminComponent.getTemplate(templateId, locale);
|
|
170
263
|
const populatedTemplate = this.populateTemplate(template, data);
|
|
171
264
|
return this._smsMessagingConnector.sendSMS(phoneNumber, populatedTemplate.content);
|
|
172
265
|
}
|
|
173
|
-
/**
|
|
174
|
-
* Create or update a template.
|
|
175
|
-
* @param templateId The id of the template.
|
|
176
|
-
* @param locale The locale of the template.
|
|
177
|
-
* @param title The title of the template.
|
|
178
|
-
* @param content The content of the template.
|
|
179
|
-
* @returns If the template was created or updated successfully.
|
|
180
|
-
*/
|
|
181
|
-
async createOrUpdateTemplate(templateId, locale, title, content) {
|
|
182
|
-
core.Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
183
|
-
core.Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
184
|
-
core.Guards.stringValue(this.CLASS_NAME, "title", title);
|
|
185
|
-
core.Guards.stringValue(this.CLASS_NAME, "content", content);
|
|
186
|
-
const templateEntry = new exports.TemplateEntry();
|
|
187
|
-
templateEntry.id = `${templateId}:${locale}`;
|
|
188
|
-
templateEntry.dateCreated = new Date(Date.now()).toISOString();
|
|
189
|
-
templateEntry.title = title;
|
|
190
|
-
templateEntry.content = content;
|
|
191
|
-
await this._entityStorageConnector.set(templateEntry);
|
|
192
|
-
return true;
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Get the email template by id and locale.
|
|
196
|
-
* @param templateId The id of the email template.
|
|
197
|
-
* @param locale The locale of the email template.
|
|
198
|
-
* @returns The email template.
|
|
199
|
-
* @internal
|
|
200
|
-
*/
|
|
201
|
-
async getTemplate(templateId, locale) {
|
|
202
|
-
const entityId = `${templateId}:${locale}`;
|
|
203
|
-
const templateInfo = await this._entityStorageConnector.get(entityId);
|
|
204
|
-
if (!templateInfo) {
|
|
205
|
-
throw new core.GeneralError(this.CLASS_NAME, "getTemplateFailed", { templateId, locale });
|
|
206
|
-
}
|
|
207
|
-
return templateInfo;
|
|
208
|
-
}
|
|
209
266
|
/**
|
|
210
267
|
* Populate the template with data.
|
|
211
268
|
* @param template The template.
|
|
@@ -240,5 +297,6 @@ function initSchema() {
|
|
|
240
297
|
entity.EntitySchemaFactory.register("TemplateEntry", () => entity.EntitySchemaHelper.getSchema(exports.TemplateEntry));
|
|
241
298
|
}
|
|
242
299
|
|
|
300
|
+
exports.MessagingAdminService = MessagingAdminService;
|
|
243
301
|
exports.MessagingService = MessagingService;
|
|
244
302
|
exports.initSchema = initSchema;
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { property, SortDirection, entity, EntitySchemaFactory, EntitySchemaHelper } from '@twin.org/entity';
|
|
2
|
-
import { Is, GeneralError,
|
|
2
|
+
import { Guards, Is, GeneralError, ComponentFactory } from '@twin.org/core';
|
|
3
3
|
import { EntityStorageConnectorFactory } from '@twin.org/entity-storage-models';
|
|
4
4
|
import { MessagingEmailConnectorFactory, MessagingPushNotificationsConnectorFactory, MessagingSmsConnectorFactory } from '@twin.org/messaging-models';
|
|
5
5
|
|
|
@@ -46,6 +46,99 @@ TemplateEntry = __decorate([
|
|
|
46
46
|
entity()
|
|
47
47
|
], TemplateEntry);
|
|
48
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
|
+
* Create a new instance of MessagingAdminService.
|
|
75
|
+
* @param options The options for the connector.
|
|
76
|
+
*/
|
|
77
|
+
constructor(options) {
|
|
78
|
+
this._entityStorageConnector = EntityStorageConnectorFactory.get(options?.templateEntryStorageConnectorType ?? "template-entry");
|
|
79
|
+
this._defaultLocale = options?.config?.defaultLocale ?? MessagingAdminService._DEFAULT_LOCALE;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Create or update a template.
|
|
83
|
+
* @param templateId The id of the template.
|
|
84
|
+
* @param locale The locale of the template.
|
|
85
|
+
* @param title The title of the template.
|
|
86
|
+
* @param content The content of the template.
|
|
87
|
+
* @returns Nothing.
|
|
88
|
+
*/
|
|
89
|
+
async setTemplate(templateId, locale, title, content) {
|
|
90
|
+
Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
91
|
+
Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
92
|
+
Guards.stringValue(this.CLASS_NAME, "title", title);
|
|
93
|
+
Guards.stringValue(this.CLASS_NAME, "content", content);
|
|
94
|
+
const templateEntry = new TemplateEntry();
|
|
95
|
+
templateEntry.id = `${templateId}:${locale}`;
|
|
96
|
+
templateEntry.dateCreated = new Date(Date.now()).toISOString();
|
|
97
|
+
templateEntry.title = title;
|
|
98
|
+
templateEntry.content = content;
|
|
99
|
+
await this._entityStorageConnector.set(templateEntry);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Get the email template by id and locale.
|
|
103
|
+
* @param templateId The id of the email template.
|
|
104
|
+
* @param locale The locale of the email template.
|
|
105
|
+
* @returns The email template.
|
|
106
|
+
*/
|
|
107
|
+
async getTemplate(templateId, locale) {
|
|
108
|
+
Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
109
|
+
Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
110
|
+
let templateEntry;
|
|
111
|
+
try {
|
|
112
|
+
// First try to get the template for the requested locale
|
|
113
|
+
templateEntry = await this._entityStorageConnector.get(`${templateId}:${locale}`);
|
|
114
|
+
}
|
|
115
|
+
catch { }
|
|
116
|
+
// If the template is not found for the requested locale, try to get it for the default locale
|
|
117
|
+
// only if the requested locale is different from the default locale
|
|
118
|
+
if (Is.empty(templateEntry) && this._defaultLocale !== locale) {
|
|
119
|
+
try {
|
|
120
|
+
templateEntry = await this._entityStorageConnector.get(`${templateId}:${this._defaultLocale}`);
|
|
121
|
+
}
|
|
122
|
+
catch { }
|
|
123
|
+
}
|
|
124
|
+
if (Is.empty(templateEntry)) {
|
|
125
|
+
throw new GeneralError(this.CLASS_NAME, "getTemplateFailed", { templateId, locale });
|
|
126
|
+
}
|
|
127
|
+
return templateEntry;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Remove a template.
|
|
131
|
+
* @param templateId The id of the template.
|
|
132
|
+
* @param locale The locale of the template.
|
|
133
|
+
* @returns Nothing
|
|
134
|
+
*/
|
|
135
|
+
async removeTemplate(templateId, locale) {
|
|
136
|
+
Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
137
|
+
Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
138
|
+
return this._entityStorageConnector.remove(`${templateId}:${locale}`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
49
142
|
// Copyright 2024 IOTA Stiftung.
|
|
50
143
|
// SPDX-License-Identifier: Apache-2.0.
|
|
51
144
|
/**
|
|
@@ -72,10 +165,10 @@ class MessagingService {
|
|
|
72
165
|
*/
|
|
73
166
|
_smsMessagingConnector;
|
|
74
167
|
/**
|
|
75
|
-
*
|
|
168
|
+
* The admin component for the messaging.
|
|
76
169
|
* @internal
|
|
77
170
|
*/
|
|
78
|
-
|
|
171
|
+
_messagingAdminComponent;
|
|
79
172
|
/**
|
|
80
173
|
* Create a new instance of MessagingService.
|
|
81
174
|
* @param options The options for the connector.
|
|
@@ -90,7 +183,7 @@ class MessagingService {
|
|
|
90
183
|
if (Is.stringValue(options?.messagingSmsConnectorType)) {
|
|
91
184
|
this._smsMessagingConnector = MessagingSmsConnectorFactory.get(options.messagingSmsConnectorType);
|
|
92
185
|
}
|
|
93
|
-
this.
|
|
186
|
+
this._messagingAdminComponent = ComponentFactory.get(options?.messagingAdminComponentType ?? "messaging-admin");
|
|
94
187
|
}
|
|
95
188
|
/**
|
|
96
189
|
* Send a custom email.
|
|
@@ -110,7 +203,7 @@ class MessagingService {
|
|
|
110
203
|
Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
111
204
|
Guards.object(this.CLASS_NAME, "data", data);
|
|
112
205
|
Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
113
|
-
const template = await this.getTemplate(templateId, locale);
|
|
206
|
+
const template = await this._messagingAdminComponent.getTemplate(templateId, locale);
|
|
114
207
|
const populatedTemplate = this.populateTemplate(template, data);
|
|
115
208
|
return this._emailMessagingConnector.sendCustomEmail(sender, recipients, populatedTemplate.title, populatedTemplate.content);
|
|
116
209
|
}
|
|
@@ -144,7 +237,7 @@ class MessagingService {
|
|
|
144
237
|
Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
145
238
|
Guards.object(this.CLASS_NAME, "data", data);
|
|
146
239
|
Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
147
|
-
const template = await this.getTemplate(templateId, locale);
|
|
240
|
+
const template = await this._messagingAdminComponent.getTemplate(templateId, locale);
|
|
148
241
|
const populatedTemplate = this.populateTemplate(template, data);
|
|
149
242
|
return this._pushNotificationMessagingConnector.sendSinglePushNotification(deviceAddress, populatedTemplate.title, populatedTemplate.content);
|
|
150
243
|
}
|
|
@@ -164,46 +257,10 @@ class MessagingService {
|
|
|
164
257
|
Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
165
258
|
Guards.object(this.CLASS_NAME, "data", data);
|
|
166
259
|
Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
167
|
-
const template = await this.getTemplate(templateId, locale);
|
|
260
|
+
const template = await this._messagingAdminComponent.getTemplate(templateId, locale);
|
|
168
261
|
const populatedTemplate = this.populateTemplate(template, data);
|
|
169
262
|
return this._smsMessagingConnector.sendSMS(phoneNumber, populatedTemplate.content);
|
|
170
263
|
}
|
|
171
|
-
/**
|
|
172
|
-
* Create or update a template.
|
|
173
|
-
* @param templateId The id of the template.
|
|
174
|
-
* @param locale The locale of the template.
|
|
175
|
-
* @param title The title of the template.
|
|
176
|
-
* @param content The content of the template.
|
|
177
|
-
* @returns If the template was created or updated successfully.
|
|
178
|
-
*/
|
|
179
|
-
async createOrUpdateTemplate(templateId, locale, title, content) {
|
|
180
|
-
Guards.stringValue(this.CLASS_NAME, "templateId", templateId);
|
|
181
|
-
Guards.stringValue(this.CLASS_NAME, "locale", locale);
|
|
182
|
-
Guards.stringValue(this.CLASS_NAME, "title", title);
|
|
183
|
-
Guards.stringValue(this.CLASS_NAME, "content", content);
|
|
184
|
-
const templateEntry = new TemplateEntry();
|
|
185
|
-
templateEntry.id = `${templateId}:${locale}`;
|
|
186
|
-
templateEntry.dateCreated = new Date(Date.now()).toISOString();
|
|
187
|
-
templateEntry.title = title;
|
|
188
|
-
templateEntry.content = content;
|
|
189
|
-
await this._entityStorageConnector.set(templateEntry);
|
|
190
|
-
return true;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Get the email template by id and locale.
|
|
194
|
-
* @param templateId The id of the email template.
|
|
195
|
-
* @param locale The locale of the email template.
|
|
196
|
-
* @returns The email template.
|
|
197
|
-
* @internal
|
|
198
|
-
*/
|
|
199
|
-
async getTemplate(templateId, locale) {
|
|
200
|
-
const entityId = `${templateId}:${locale}`;
|
|
201
|
-
const templateInfo = await this._entityStorageConnector.get(entityId);
|
|
202
|
-
if (!templateInfo) {
|
|
203
|
-
throw new GeneralError(this.CLASS_NAME, "getTemplateFailed", { templateId, locale });
|
|
204
|
-
}
|
|
205
|
-
return templateInfo;
|
|
206
|
-
}
|
|
207
264
|
/**
|
|
208
265
|
* Populate the template with data.
|
|
209
266
|
* @param template The template.
|
|
@@ -238,4 +295,4 @@ function initSchema() {
|
|
|
238
295
|
EntitySchemaFactory.register("TemplateEntry", () => EntitySchemaHelper.getSchema(TemplateEntry));
|
|
239
296
|
}
|
|
240
297
|
|
|
241
|
-
export { MessagingService, TemplateEntry, initSchema };
|
|
298
|
+
export { MessagingAdminService, MessagingService, TemplateEntry, initSchema };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export * from "./entities/templateEntry";
|
|
2
|
+
export * from "./messagingAdminService";
|
|
2
3
|
export * from "./messagingService";
|
|
4
|
+
export * from "./models/IMessagingAdminServiceConfig";
|
|
5
|
+
export * from "./models/IMessagingAdminServiceConstructorOptions";
|
|
3
6
|
export * from "./models/IMessagingServiceConstructorOptions";
|
|
4
7
|
export * from "./schema";
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { IMessagingAdminComponent } from "@twin.org/messaging-models";
|
|
2
|
+
import type { IMessagingAdminServiceConstructorOptions } from "./models/IMessagingAdminServiceConstructorOptions";
|
|
3
|
+
/**
|
|
4
|
+
* Service for performing email messaging operations to a connector.
|
|
5
|
+
*/
|
|
6
|
+
export declare class MessagingAdminService implements IMessagingAdminComponent {
|
|
7
|
+
/**
|
|
8
|
+
* Default locale for the messaging service.
|
|
9
|
+
*/
|
|
10
|
+
private static readonly _DEFAULT_LOCALE;
|
|
11
|
+
/**
|
|
12
|
+
* Runtime name for the class.
|
|
13
|
+
*/
|
|
14
|
+
readonly CLASS_NAME: string;
|
|
15
|
+
/**
|
|
16
|
+
* Create a new instance of MessagingAdminService.
|
|
17
|
+
* @param options The options for the connector.
|
|
18
|
+
*/
|
|
19
|
+
constructor(options?: IMessagingAdminServiceConstructorOptions);
|
|
20
|
+
/**
|
|
21
|
+
* Create or update a template.
|
|
22
|
+
* @param templateId The id of the template.
|
|
23
|
+
* @param locale The locale of the template.
|
|
24
|
+
* @param title The title of the template.
|
|
25
|
+
* @param content The content of the template.
|
|
26
|
+
* @returns Nothing.
|
|
27
|
+
*/
|
|
28
|
+
setTemplate(templateId: string, locale: string, title: string, content: string): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Get the email template by id and locale.
|
|
31
|
+
* @param templateId The id of the email template.
|
|
32
|
+
* @param locale The locale of the email template.
|
|
33
|
+
* @returns The email template.
|
|
34
|
+
*/
|
|
35
|
+
getTemplate(templateId: string, locale: string): Promise<{
|
|
36
|
+
title: string;
|
|
37
|
+
content: string;
|
|
38
|
+
}>;
|
|
39
|
+
/**
|
|
40
|
+
* Remove a template.
|
|
41
|
+
* @param templateId The id of the template.
|
|
42
|
+
* @param locale The locale of the template.
|
|
43
|
+
* @returns Nothing
|
|
44
|
+
*/
|
|
45
|
+
removeTemplate(templateId: string, locale: string): Promise<void>;
|
|
46
|
+
}
|
|
@@ -54,13 +54,4 @@ export declare class MessagingService implements IMessagingComponent {
|
|
|
54
54
|
sendSMS(phoneNumber: string, templateId: string, data: {
|
|
55
55
|
[key: string]: string;
|
|
56
56
|
}, locale: string): Promise<boolean>;
|
|
57
|
-
/**
|
|
58
|
-
* Create or update a template.
|
|
59
|
-
* @param templateId The id of the template.
|
|
60
|
-
* @param locale The locale of the template.
|
|
61
|
-
* @param title The title of the template.
|
|
62
|
-
* @param content The content of the template.
|
|
63
|
-
* @returns If the template was created or updated successfully.
|
|
64
|
-
*/
|
|
65
|
-
createOrUpdateTemplate(templateId: string, locale: string, title: string, content: string): Promise<boolean>;
|
|
66
57
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { IMessagingAdminServiceConfig } from "./IMessagingAdminServiceConfig";
|
|
2
|
+
/**
|
|
3
|
+
* Options for the messaging admin service.
|
|
4
|
+
*/
|
|
5
|
+
export interface IMessagingAdminServiceConstructorOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The type of the entity connector to use.
|
|
8
|
+
* @default template-entry
|
|
9
|
+
*/
|
|
10
|
+
templateEntryStorageConnectorType?: string;
|
|
11
|
+
/**
|
|
12
|
+
* The configuration for the messaging admin service.
|
|
13
|
+
*/
|
|
14
|
+
config?: IMessagingAdminServiceConfig;
|
|
15
|
+
}
|
|
@@ -15,8 +15,8 @@ export interface IMessagingServiceConstructorOptions {
|
|
|
15
15
|
*/
|
|
16
16
|
messagingSmsConnectorType?: string;
|
|
17
17
|
/**
|
|
18
|
-
* The type of the
|
|
19
|
-
* @default
|
|
18
|
+
* The type of the messaging admin component to use.
|
|
19
|
+
* @default messaging-admin
|
|
20
20
|
*/
|
|
21
|
-
|
|
21
|
+
messagingAdminComponentType?: string;
|
|
22
22
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# @twin.org/messaging-service - Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.2-next.3](https://github.com/twinfoundation/messaging/compare/messaging-service-v0.0.2-next.2...messaging-service-v0.0.2-next.3) (2025-09-29)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add messaging admin component ([cbaaca3](https://github.com/twinfoundation/messaging/commit/cbaaca34db6a9f5c51438c201535b4b43a1aea1f))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/messaging-models bumped from 0.0.2-next.2 to 0.0.2-next.3
|
|
16
|
+
|
|
17
|
+
## [0.0.2-next.2](https://github.com/twinfoundation/messaging/compare/messaging-service-v0.0.2-next.1...messaging-service-v0.0.2-next.2) (2025-08-29)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* eslint migration to flat config ([faa02ec](https://github.com/twinfoundation/messaging/commit/faa02ec0ef450db88b08e938415e40cf13625d15))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Dependencies
|
|
26
|
+
|
|
27
|
+
* The following workspace dependencies were updated
|
|
28
|
+
* dependencies
|
|
29
|
+
* @twin.org/messaging-models bumped from 0.0.2-next.1 to 0.0.2-next.2
|
|
30
|
+
|
|
3
31
|
## [0.0.2-next.1](https://github.com/twinfoundation/messaging/compare/messaging-service-v0.0.2-next.0...messaging-service-v0.0.2-next.1) (2025-08-20)
|
|
4
32
|
|
|
5
33
|
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# Class: MessagingAdminService
|
|
2
|
+
|
|
3
|
+
Service for performing email messaging operations to a connector.
|
|
4
|
+
|
|
5
|
+
## Implements
|
|
6
|
+
|
|
7
|
+
- `IMessagingAdminComponent`
|
|
8
|
+
|
|
9
|
+
## Constructors
|
|
10
|
+
|
|
11
|
+
### Constructor
|
|
12
|
+
|
|
13
|
+
> **new MessagingAdminService**(`options?`): `MessagingAdminService`
|
|
14
|
+
|
|
15
|
+
Create a new instance of MessagingAdminService.
|
|
16
|
+
|
|
17
|
+
#### Parameters
|
|
18
|
+
|
|
19
|
+
##### options?
|
|
20
|
+
|
|
21
|
+
[`IMessagingAdminServiceConstructorOptions`](../interfaces/IMessagingAdminServiceConstructorOptions.md)
|
|
22
|
+
|
|
23
|
+
The options for the connector.
|
|
24
|
+
|
|
25
|
+
#### Returns
|
|
26
|
+
|
|
27
|
+
`MessagingAdminService`
|
|
28
|
+
|
|
29
|
+
## Properties
|
|
30
|
+
|
|
31
|
+
### CLASS\_NAME
|
|
32
|
+
|
|
33
|
+
> `readonly` **CLASS\_NAME**: `string`
|
|
34
|
+
|
|
35
|
+
Runtime name for the class.
|
|
36
|
+
|
|
37
|
+
#### Implementation of
|
|
38
|
+
|
|
39
|
+
`IMessagingAdminComponent.CLASS_NAME`
|
|
40
|
+
|
|
41
|
+
## Methods
|
|
42
|
+
|
|
43
|
+
### setTemplate()
|
|
44
|
+
|
|
45
|
+
> **setTemplate**(`templateId`, `locale`, `title`, `content`): `Promise`\<`void`\>
|
|
46
|
+
|
|
47
|
+
Create or update a template.
|
|
48
|
+
|
|
49
|
+
#### Parameters
|
|
50
|
+
|
|
51
|
+
##### templateId
|
|
52
|
+
|
|
53
|
+
`string`
|
|
54
|
+
|
|
55
|
+
The id of the template.
|
|
56
|
+
|
|
57
|
+
##### locale
|
|
58
|
+
|
|
59
|
+
`string`
|
|
60
|
+
|
|
61
|
+
The locale of the template.
|
|
62
|
+
|
|
63
|
+
##### title
|
|
64
|
+
|
|
65
|
+
`string`
|
|
66
|
+
|
|
67
|
+
The title of the template.
|
|
68
|
+
|
|
69
|
+
##### content
|
|
70
|
+
|
|
71
|
+
`string`
|
|
72
|
+
|
|
73
|
+
The content of the template.
|
|
74
|
+
|
|
75
|
+
#### Returns
|
|
76
|
+
|
|
77
|
+
`Promise`\<`void`\>
|
|
78
|
+
|
|
79
|
+
Nothing.
|
|
80
|
+
|
|
81
|
+
#### Implementation of
|
|
82
|
+
|
|
83
|
+
`IMessagingAdminComponent.setTemplate`
|
|
84
|
+
|
|
85
|
+
***
|
|
86
|
+
|
|
87
|
+
### getTemplate()
|
|
88
|
+
|
|
89
|
+
> **getTemplate**(`templateId`, `locale`): `Promise`\<\{ `title`: `string`; `content`: `string`; \}\>
|
|
90
|
+
|
|
91
|
+
Get the email template by id and locale.
|
|
92
|
+
|
|
93
|
+
#### Parameters
|
|
94
|
+
|
|
95
|
+
##### templateId
|
|
96
|
+
|
|
97
|
+
`string`
|
|
98
|
+
|
|
99
|
+
The id of the email template.
|
|
100
|
+
|
|
101
|
+
##### locale
|
|
102
|
+
|
|
103
|
+
`string`
|
|
104
|
+
|
|
105
|
+
The locale of the email template.
|
|
106
|
+
|
|
107
|
+
#### Returns
|
|
108
|
+
|
|
109
|
+
`Promise`\<\{ `title`: `string`; `content`: `string`; \}\>
|
|
110
|
+
|
|
111
|
+
The email template.
|
|
112
|
+
|
|
113
|
+
#### Implementation of
|
|
114
|
+
|
|
115
|
+
`IMessagingAdminComponent.getTemplate`
|
|
116
|
+
|
|
117
|
+
***
|
|
118
|
+
|
|
119
|
+
### removeTemplate()
|
|
120
|
+
|
|
121
|
+
> **removeTemplate**(`templateId`, `locale`): `Promise`\<`void`\>
|
|
122
|
+
|
|
123
|
+
Remove a template.
|
|
124
|
+
|
|
125
|
+
#### Parameters
|
|
126
|
+
|
|
127
|
+
##### templateId
|
|
128
|
+
|
|
129
|
+
`string`
|
|
130
|
+
|
|
131
|
+
The id of the template.
|
|
132
|
+
|
|
133
|
+
##### locale
|
|
134
|
+
|
|
135
|
+
`string`
|
|
136
|
+
|
|
137
|
+
The locale of the template.
|
|
138
|
+
|
|
139
|
+
#### Returns
|
|
140
|
+
|
|
141
|
+
`Promise`\<`void`\>
|
|
142
|
+
|
|
143
|
+
Nothing
|
|
144
|
+
|
|
145
|
+
#### Implementation of
|
|
146
|
+
|
|
147
|
+
`IMessagingAdminComponent.removeTemplate`
|
|
@@ -201,43 +201,3 @@ If the SMS was sent successfully.
|
|
|
201
201
|
#### Implementation of
|
|
202
202
|
|
|
203
203
|
`IMessagingComponent.sendSMS`
|
|
204
|
-
|
|
205
|
-
***
|
|
206
|
-
|
|
207
|
-
### createOrUpdateTemplate()
|
|
208
|
-
|
|
209
|
-
> **createOrUpdateTemplate**(`templateId`, `locale`, `title`, `content`): `Promise`\<`boolean`\>
|
|
210
|
-
|
|
211
|
-
Create or update a template.
|
|
212
|
-
|
|
213
|
-
#### Parameters
|
|
214
|
-
|
|
215
|
-
##### templateId
|
|
216
|
-
|
|
217
|
-
`string`
|
|
218
|
-
|
|
219
|
-
The id of the template.
|
|
220
|
-
|
|
221
|
-
##### locale
|
|
222
|
-
|
|
223
|
-
`string`
|
|
224
|
-
|
|
225
|
-
The locale of the template.
|
|
226
|
-
|
|
227
|
-
##### title
|
|
228
|
-
|
|
229
|
-
`string`
|
|
230
|
-
|
|
231
|
-
The title of the template.
|
|
232
|
-
|
|
233
|
-
##### content
|
|
234
|
-
|
|
235
|
-
`string`
|
|
236
|
-
|
|
237
|
-
The content of the template.
|
|
238
|
-
|
|
239
|
-
#### Returns
|
|
240
|
-
|
|
241
|
-
`Promise`\<`boolean`\>
|
|
242
|
-
|
|
243
|
-
If the template was created or updated successfully.
|
package/docs/reference/index.md
CHANGED
|
@@ -3,10 +3,13 @@
|
|
|
3
3
|
## Classes
|
|
4
4
|
|
|
5
5
|
- [TemplateEntry](classes/TemplateEntry.md)
|
|
6
|
+
- [MessagingAdminService](classes/MessagingAdminService.md)
|
|
6
7
|
- [MessagingService](classes/MessagingService.md)
|
|
7
8
|
|
|
8
9
|
## Interfaces
|
|
9
10
|
|
|
11
|
+
- [IMessagingAdminServiceConfig](interfaces/IMessagingAdminServiceConfig.md)
|
|
12
|
+
- [IMessagingAdminServiceConstructorOptions](interfaces/IMessagingAdminServiceConstructorOptions.md)
|
|
10
13
|
- [IMessagingServiceConstructorOptions](interfaces/IMessagingServiceConstructorOptions.md)
|
|
11
14
|
|
|
12
15
|
## Functions
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Interface: IMessagingAdminServiceConfig
|
|
2
|
+
|
|
3
|
+
Options for the messaging service.
|
|
4
|
+
|
|
5
|
+
## Properties
|
|
6
|
+
|
|
7
|
+
### defaultLocale?
|
|
8
|
+
|
|
9
|
+
> `optional` **defaultLocale**: `string`
|
|
10
|
+
|
|
11
|
+
The default locale to use for the messaging service.
|
|
12
|
+
|
|
13
|
+
#### Default
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
en
|
|
17
|
+
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Interface: IMessagingAdminServiceConstructorOptions
|
|
2
|
+
|
|
3
|
+
Options for the messaging admin service.
|
|
4
|
+
|
|
5
|
+
## Properties
|
|
6
|
+
|
|
7
|
+
### templateEntryStorageConnectorType?
|
|
8
|
+
|
|
9
|
+
> `optional` **templateEntryStorageConnectorType**: `string`
|
|
10
|
+
|
|
11
|
+
The type of the entity connector to use.
|
|
12
|
+
|
|
13
|
+
#### Default
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
template-entry
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
***
|
|
20
|
+
|
|
21
|
+
### config?
|
|
22
|
+
|
|
23
|
+
> `optional` **config**: [`IMessagingAdminServiceConfig`](IMessagingAdminServiceConfig.md)
|
|
24
|
+
|
|
25
|
+
The configuration for the messaging admin service.
|
|
@@ -28,14 +28,14 @@ The type of the sms messaging connector to use, defaults to not configured.
|
|
|
28
28
|
|
|
29
29
|
***
|
|
30
30
|
|
|
31
|
-
###
|
|
31
|
+
### messagingAdminComponentType?
|
|
32
32
|
|
|
33
|
-
> `optional` **
|
|
33
|
+
> `optional` **messagingAdminComponentType**: `string`
|
|
34
34
|
|
|
35
|
-
The type of the
|
|
35
|
+
The type of the messaging admin component to use.
|
|
36
36
|
|
|
37
37
|
#### Default
|
|
38
38
|
|
|
39
39
|
```ts
|
|
40
|
-
|
|
40
|
+
messaging-admin
|
|
41
41
|
```
|
package/locales/en.json
CHANGED
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
},
|
|
5
5
|
"error": {
|
|
6
6
|
"messagingService": {
|
|
7
|
-
"getTemplateFailed": "Failed to get template with id: \"{templateId}\" and locale: \"{locale}\"",
|
|
8
7
|
"notConfiguredEmailMessagingConnector": "Email messaging connector was not configured",
|
|
9
8
|
"notConfiguredPushNotificationMessagingConnector": "Push notification messaging connector was not configured",
|
|
10
9
|
"notConfiguredSmsMessagingConnector": "SMS messaging connector was not configured"
|
|
10
|
+
},
|
|
11
|
+
"messagingAdminService": {
|
|
12
|
+
"getTemplateFailed": "Failed to get template with id: \"{templateId}\" and locale: \"{locale}\""
|
|
11
13
|
}
|
|
12
14
|
}
|
|
13
15
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/messaging-service",
|
|
3
|
-
"version": "0.0.2-next.
|
|
3
|
+
"version": "0.0.2-next.3",
|
|
4
4
|
"description": "Messaging service implementation",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"@twin.org/entity": "next",
|
|
19
19
|
"@twin.org/entity-storage-models": "next",
|
|
20
20
|
"@twin.org/logging-models": "next",
|
|
21
|
-
"@twin.org/messaging-models": "0.0.2-next.
|
|
21
|
+
"@twin.org/messaging-models": "0.0.2-next.3",
|
|
22
22
|
"@twin.org/nameof": "next"
|
|
23
23
|
},
|
|
24
24
|
"main": "./dist/cjs/index.cjs",
|