@twin.org/messaging-service 0.0.2-next.6 → 0.0.3-next.2

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.
@@ -0,0 +1,45 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ import { entity, property, SortDirection } from "@twin.org/entity";
4
+ /**
5
+ * Call defining a template message entry.
6
+ */
7
+ let TemplateEntry = class TemplateEntry {
8
+ /**
9
+ * The id.
10
+ */
11
+ id;
12
+ /**
13
+ * The title.
14
+ */
15
+ title;
16
+ /**
17
+ * The content.
18
+ */
19
+ content;
20
+ /**
21
+ * The timestamp of the template entry.
22
+ */
23
+ dateCreated;
24
+ };
25
+ __decorate([
26
+ property({ type: "string", isPrimary: true }),
27
+ __metadata("design:type", String)
28
+ ], TemplateEntry.prototype, "id", void 0);
29
+ __decorate([
30
+ property({ type: "string" }),
31
+ __metadata("design:type", String)
32
+ ], TemplateEntry.prototype, "title", void 0);
33
+ __decorate([
34
+ property({ type: "string" }),
35
+ __metadata("design:type", String)
36
+ ], TemplateEntry.prototype, "content", void 0);
37
+ __decorate([
38
+ property({ type: "string", format: "date-time", sortDirection: SortDirection.Descending }),
39
+ __metadata("design:type", String)
40
+ ], TemplateEntry.prototype, "dateCreated", void 0);
41
+ TemplateEntry = __decorate([
42
+ entity()
43
+ ], TemplateEntry);
44
+ export { TemplateEntry };
45
+ //# sourceMappingURL=templateEntry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"templateEntry.js","sourceRoot":"","sources":["../../../src/entities/templateEntry.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEnE;;GAEG;AAEI,IAAM,aAAa,GAAnB,MAAM,aAAa;IACzB;;OAEG;IAEI,EAAE,CAAU;IAEnB;;OAEG;IAEI,KAAK,CAAU;IAEtB;;OAEG;IAEI,OAAO,CAAU;IAExB;;OAEG;IAEI,WAAW,CAAU;CAC5B,CAAA;AAnBO;IADN,QAAQ,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;;yCAC3B;AAMZ;IADN,QAAQ,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;;4CACP;AAMf;IADN,QAAQ,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;;8CACL;AAMjB;IADN,QAAQ,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE,aAAa,EAAE,aAAa,CAAC,UAAU,EAAE,CAAC;;kDAC/D;AAvBhB,aAAa;IADzB,MAAM,EAAE;GACI,aAAa,CAwBzB","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { entity, property, SortDirection } from \"@twin.org/entity\";\n\n/**\n * Call defining a template message entry.\n */\n@entity()\nexport class TemplateEntry {\n\t/**\n\t * The id.\n\t */\n\t@property({ type: \"string\", isPrimary: true })\n\tpublic id!: string;\n\n\t/**\n\t * The title.\n\t */\n\t@property({ type: \"string\" })\n\tpublic title!: string;\n\n\t/**\n\t * The content.\n\t */\n\t@property({ type: \"string\" })\n\tpublic content!: string;\n\n\t/**\n\t * The timestamp of the template entry.\n\t */\n\t@property({ type: \"string\", format: \"date-time\", sortDirection: SortDirection.Descending })\n\tpublic dateCreated!: string;\n}\n"]}
@@ -0,0 +1,10 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ export * from "./entities/templateEntry.js";
4
+ export * from "./messagingAdminService.js";
5
+ export * from "./messagingService.js";
6
+ export * from "./models/IMessagingAdminServiceConfig.js";
7
+ export * from "./models/IMessagingAdminServiceConstructorOptions.js";
8
+ export * from "./models/IMessagingServiceConstructorOptions.js";
9
+ export * from "./schema.js";
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,uBAAuB,CAAC;AACtC,cAAc,0CAA0C,CAAC;AACzD,cAAc,sDAAsD,CAAC;AACrE,cAAc,iDAAiD,CAAC;AAChE,cAAc,aAAa,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./entities/templateEntry.js\";\nexport * from \"./messagingAdminService.js\";\nexport * from \"./messagingService.js\";\nexport * from \"./models/IMessagingAdminServiceConfig.js\";\nexport * from \"./models/IMessagingAdminServiceConstructorOptions.js\";\nexport * from \"./models/IMessagingServiceConstructorOptions.js\";\nexport * from \"./schema.js\";\n"]}
@@ -0,0 +1,106 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ import { GeneralError, Guards, Is } from "@twin.org/core";
4
+ import { EntityStorageConnectorFactory } from "@twin.org/entity-storage-models";
5
+ import { TemplateEntry } from "./entities/templateEntry.js";
6
+ /**
7
+ * Service for performing email messaging operations to a connector.
8
+ */
9
+ export class MessagingAdminService {
10
+ /**
11
+ * Runtime name for the class.
12
+ */
13
+ static CLASS_NAME = "MessagingAdminService";
14
+ /**
15
+ * Default locale for the messaging service.
16
+ */
17
+ static _DEFAULT_LOCALE = "en";
18
+ /**
19
+ * Entity storage connector used by the service.
20
+ * @internal
21
+ */
22
+ _entityStorageConnector;
23
+ /**
24
+ * The default locale to use for the messaging service.
25
+ * @internal
26
+ */
27
+ _defaultLocale;
28
+ /**
29
+ * Create a new instance of MessagingAdminService.
30
+ * @param options The options for the connector.
31
+ */
32
+ constructor(options) {
33
+ this._entityStorageConnector = EntityStorageConnectorFactory.get(options?.templateEntryStorageConnectorType ?? "template-entry");
34
+ this._defaultLocale = options?.config?.defaultLocale ?? MessagingAdminService._DEFAULT_LOCALE;
35
+ }
36
+ /**
37
+ * Returns the class name of the component.
38
+ * @returns The class name of the component.
39
+ */
40
+ className() {
41
+ return MessagingAdminService.CLASS_NAME;
42
+ }
43
+ /**
44
+ * Create or update a template.
45
+ * @param templateId The id of the template.
46
+ * @param locale The locale of the template.
47
+ * @param title The title of the template.
48
+ * @param content The content of the template.
49
+ * @returns Nothing.
50
+ */
51
+ async setTemplate(templateId, locale, title, content) {
52
+ Guards.stringValue(MessagingAdminService.CLASS_NAME, "templateId", templateId);
53
+ Guards.stringValue(MessagingAdminService.CLASS_NAME, "locale", locale);
54
+ Guards.stringValue(MessagingAdminService.CLASS_NAME, "title", title);
55
+ Guards.stringValue(MessagingAdminService.CLASS_NAME, "content", content);
56
+ const templateEntry = new TemplateEntry();
57
+ templateEntry.id = `${templateId}:${locale}`;
58
+ templateEntry.dateCreated = new Date(Date.now()).toISOString();
59
+ templateEntry.title = title;
60
+ templateEntry.content = content;
61
+ await this._entityStorageConnector.set(templateEntry);
62
+ }
63
+ /**
64
+ * Get the email template by id and locale.
65
+ * @param templateId The id of the email template.
66
+ * @param locale The locale of the email template.
67
+ * @returns The email template.
68
+ */
69
+ async getTemplate(templateId, locale) {
70
+ Guards.stringValue(MessagingAdminService.CLASS_NAME, "templateId", templateId);
71
+ Guards.stringValue(MessagingAdminService.CLASS_NAME, "locale", locale);
72
+ let templateEntry;
73
+ try {
74
+ // First try to get the template for the requested locale
75
+ templateEntry = await this._entityStorageConnector.get(`${templateId}:${locale}`);
76
+ }
77
+ catch { }
78
+ // If the template is not found for the requested locale, try to get it for the default locale
79
+ // only if the requested locale is different from the default locale
80
+ if (Is.empty(templateEntry) && this._defaultLocale !== locale) {
81
+ try {
82
+ templateEntry = await this._entityStorageConnector.get(`${templateId}:${this._defaultLocale}`);
83
+ }
84
+ catch { }
85
+ }
86
+ if (Is.empty(templateEntry)) {
87
+ throw new GeneralError(MessagingAdminService.CLASS_NAME, "getTemplateFailed", {
88
+ templateId,
89
+ locale
90
+ });
91
+ }
92
+ return templateEntry;
93
+ }
94
+ /**
95
+ * Remove a template.
96
+ * @param templateId The id of the template.
97
+ * @param locale The locale of the template.
98
+ * @returns Nothing
99
+ */
100
+ async removeTemplate(templateId, locale) {
101
+ Guards.stringValue(MessagingAdminService.CLASS_NAME, "templateId", templateId);
102
+ Guards.stringValue(MessagingAdminService.CLASS_NAME, "locale", locale);
103
+ return this._entityStorageConnector.remove(`${templateId}:${locale}`);
104
+ }
105
+ }
106
+ //# sourceMappingURL=messagingAdminService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messagingAdminService.js","sourceRoot":"","sources":["../../src/messagingAdminService.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EACN,6BAA6B,EAE7B,MAAM,iCAAiC,CAAC;AAGzC,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAG5D;;GAEG;AACH,MAAM,OAAO,qBAAqB;IACjC;;OAEG;IACI,MAAM,CAAU,UAAU,2BAA2C;IAE5E;;OAEG;IACK,MAAM,CAAU,eAAe,GAAW,IAAI,CAAC;IAEvD;;;OAGG;IACc,uBAAuB,CAAyC;IAEjF;;;OAGG;IACc,cAAc,CAAS;IAExC;;;OAGG;IACH,YAAY,OAAkD;QAC7D,IAAI,CAAC,uBAAuB,GAAG,6BAA6B,CAAC,GAAG,CAC/D,OAAO,EAAE,iCAAiC,IAAI,gBAAgB,CAC9D,CAAC;QAEF,IAAI,CAAC,cAAc,GAAG,OAAO,EAAE,MAAM,EAAE,aAAa,IAAI,qBAAqB,CAAC,eAAe,CAAC;IAC/F,CAAC;IAED;;;OAGG;IACI,SAAS;QACf,OAAO,qBAAqB,CAAC,UAAU,CAAC;IACzC,CAAC;IAED;;;;;;;OAOG;IACI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AAC,KAAK,CAAC,aAAa,CAAC,IAAI,IAAI,CAAC,cAAc,KAAK,MAAM,EAAE,CAAC;YAC/D,IAAI,CAAC;gBACJ,aAAa,GAAG,MAAM,IAAI,CAAC,uBAAuB,CAAC,GAAG,CACrD,GAAG,UAAU,IAAI,IAAI,CAAC,cAAc,EAAE,CACtC,CAAC;YACH,CAAC;YAAC,MAAM,CAAC,CAAA,CAAC;QACX,CAAC;QAED,IAAI,EAAE,CAAC,KAAK,CAAC,aAAa,CAAC,EAAE,CAAC;YAC7B,MAAM,IAAI,YAAY,CAAC,qBAAqB,CAAC,UAAU,EAAE,mBAAmB,EAAE;gBAC7E,UAAU;gBACV,MAAM;aACN,CAAC,CAAC;QACJ,CAAC;QAED,OAAO,aAAa,CAAC;IACtB,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,cAAc,CAAC,UAAkB,EAAE,MAAc;QAC7D,MAAM,CAAC,WAAW,CAAC,qBAAqB,CAAC,UAAU,gBAAsB,UAAU,CAAC,CAAC;QACrF,MAAM,CAAC,WAAW,CAAC,qBAAqB,CAAC,UAAU,YAAkB,MAAM,CAAC,CAAC;QAE7E,OAAO,IAAI,CAAC,uBAAuB,CAAC,MAAM,CAAC,GAAG,UAAU,IAAI,MAAM,EAAE,CAAC,CAAC;IACvE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { GeneralError, Guards, Is } from \"@twin.org/core\";\nimport {\n\tEntityStorageConnectorFactory,\n\ttype IEntityStorageConnector\n} from \"@twin.org/entity-storage-models\";\nimport type { IMessagingAdminComponent } from \"@twin.org/messaging-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { TemplateEntry } from \"./entities/templateEntry.js\";\nimport type { IMessagingAdminServiceConstructorOptions } from \"./models/IMessagingAdminServiceConstructorOptions.js\";\n\n/**\n * Service for performing email messaging operations to a connector.\n */\nexport class MessagingAdminService implements IMessagingAdminComponent {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<MessagingAdminService>();\n\n\t/**\n\t * Default locale for the messaging service.\n\t */\n\tprivate static readonly _DEFAULT_LOCALE: string = \"en\";\n\n\t/**\n\t * Entity storage connector used by the service.\n\t * @internal\n\t */\n\tprivate readonly _entityStorageConnector: IEntityStorageConnector<TemplateEntry>;\n\n\t/**\n\t * The default locale to use for the messaging service.\n\t * @internal\n\t */\n\tprivate readonly _defaultLocale: string;\n\n\t/**\n\t * Create a new instance of MessagingAdminService.\n\t * @param options The options for the connector.\n\t */\n\tconstructor(options?: IMessagingAdminServiceConstructorOptions) {\n\t\tthis._entityStorageConnector = EntityStorageConnectorFactory.get(\n\t\t\toptions?.templateEntryStorageConnectorType ?? \"template-entry\"\n\t\t);\n\n\t\tthis._defaultLocale = options?.config?.defaultLocale ?? MessagingAdminService._DEFAULT_LOCALE;\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn MessagingAdminService.CLASS_NAME;\n\t}\n\n\t/**\n\t * Create or update a template.\n\t * @param templateId The id of the template.\n\t * @param locale The locale of the template.\n\t * @param title The title of the template.\n\t * @param content The content of the template.\n\t * @returns Nothing.\n\t */\n\tpublic async setTemplate(\n\t\ttemplateId: string,\n\t\tlocale: string,\n\t\ttitle: string,\n\t\tcontent: string\n\t): Promise<void> {\n\t\tGuards.stringValue(MessagingAdminService.CLASS_NAME, nameof(templateId), templateId);\n\t\tGuards.stringValue(MessagingAdminService.CLASS_NAME, nameof(locale), locale);\n\t\tGuards.stringValue(MessagingAdminService.CLASS_NAME, nameof(title), title);\n\t\tGuards.stringValue(MessagingAdminService.CLASS_NAME, nameof(content), content);\n\n\t\tconst templateEntry = new TemplateEntry();\n\t\ttemplateEntry.id = `${templateId}:${locale}`;\n\t\ttemplateEntry.dateCreated = new Date(Date.now()).toISOString();\n\t\ttemplateEntry.title = title;\n\t\ttemplateEntry.content = content;\n\n\t\tawait this._entityStorageConnector.set(templateEntry);\n\t}\n\n\t/**\n\t * Get the email template by id and locale.\n\t * @param templateId The id of the email template.\n\t * @param locale The locale of the email template.\n\t * @returns The email template.\n\t */\n\tpublic async getTemplate(\n\t\ttemplateId: string,\n\t\tlocale: string\n\t): Promise<{ title: string; content: string }> {\n\t\tGuards.stringValue(MessagingAdminService.CLASS_NAME, nameof(templateId), templateId);\n\t\tGuards.stringValue(MessagingAdminService.CLASS_NAME, nameof(locale), locale);\n\n\t\tlet templateEntry;\n\n\t\ttry {\n\t\t\t// First try to get the template for the requested locale\n\t\t\ttemplateEntry = await this._entityStorageConnector.get(`${templateId}:${locale}`);\n\t\t} catch {}\n\n\t\t// If the template is not found for the requested locale, try to get it for the default locale\n\t\t// only if the requested locale is different from the default locale\n\t\tif (Is.empty(templateEntry) && this._defaultLocale !== locale) {\n\t\t\ttry {\n\t\t\t\ttemplateEntry = await this._entityStorageConnector.get(\n\t\t\t\t\t`${templateId}:${this._defaultLocale}`\n\t\t\t\t);\n\t\t\t} catch {}\n\t\t}\n\n\t\tif (Is.empty(templateEntry)) {\n\t\t\tthrow new GeneralError(MessagingAdminService.CLASS_NAME, \"getTemplateFailed\", {\n\t\t\t\ttemplateId,\n\t\t\t\tlocale\n\t\t\t});\n\t\t}\n\n\t\treturn templateEntry;\n\t}\n\n\t/**\n\t * Remove a template.\n\t * @param templateId The id of the template.\n\t * @param locale The locale of the template.\n\t * @returns Nothing\n\t */\n\tpublic async removeTemplate(templateId: string, locale: string): Promise<void> {\n\t\tGuards.stringValue(MessagingAdminService.CLASS_NAME, nameof(templateId), templateId);\n\t\tGuards.stringValue(MessagingAdminService.CLASS_NAME, nameof(locale), locale);\n\n\t\treturn this._entityStorageConnector.remove(`${templateId}:${locale}`);\n\t}\n}\n"]}
@@ -1,174 +1,11 @@
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
1
  // Copyright 2024 IOTA Stiftung.
50
2
  // SPDX-License-Identifier: Apache-2.0.
3
+ import { ComponentFactory, GeneralError, Guards, Is } from "@twin.org/core";
4
+ import { MessagingEmailConnectorFactory, MessagingPushNotificationsConnectorFactory, MessagingSmsConnectorFactory } from "@twin.org/messaging-models";
51
5
  /**
52
6
  * Service for performing email messaging operations to a connector.
53
7
  */
54
- class MessagingAdminService {
55
- /**
56
- * Runtime name for the class.
57
- */
58
- static CLASS_NAME = "MessagingAdminService";
59
- /**
60
- * Default locale for the messaging service.
61
- */
62
- static _DEFAULT_LOCALE = "en";
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(MessagingAdminService.CLASS_NAME, "templateId", templateId);
112
- Guards.stringValue(MessagingAdminService.CLASS_NAME, "locale", locale);
113
- Guards.stringValue(MessagingAdminService.CLASS_NAME, "title", title);
114
- Guards.stringValue(MessagingAdminService.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(MessagingAdminService.CLASS_NAME, "templateId", templateId);
130
- Guards.stringValue(MessagingAdminService.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(MessagingAdminService.CLASS_NAME, "getTemplateFailed", {
147
- templateId,
148
- locale
149
- });
150
- }
151
- return templateEntry;
152
- }
153
- /**
154
- * Remove a template.
155
- * @param templateId The id of the template.
156
- * @param locale The locale of the template.
157
- * @returns Nothing
158
- */
159
- async removeTemplate(templateId, locale) {
160
- Guards.stringValue(MessagingAdminService.CLASS_NAME, "templateId", templateId);
161
- Guards.stringValue(MessagingAdminService.CLASS_NAME, "locale", locale);
162
- return this._entityStorageConnector.remove(`${templateId}:${locale}`);
163
- }
164
- }
165
-
166
- // Copyright 2024 IOTA Stiftung.
167
- // SPDX-License-Identifier: Apache-2.0.
168
- /**
169
- * Service for performing email messaging operations to a connector.
170
- */
171
- class MessagingService {
8
+ export class MessagingService {
172
9
  /**
173
10
  * Runtime name for the class.
174
11
  */
@@ -209,6 +46,13 @@ class MessagingService {
209
46
  }
210
47
  this._messagingAdminComponent = ComponentFactory.get(options?.messagingAdminComponentType ?? "messaging-admin");
211
48
  }
49
+ /**
50
+ * Returns the class name of the component.
51
+ * @returns The class name of the component.
52
+ */
53
+ className() {
54
+ return MessagingService.CLASS_NAME;
55
+ }
212
56
  /**
213
57
  * Send a custom email.
214
58
  * @param sender The sender email address.
@@ -309,14 +153,4 @@ class MessagingService {
309
153
  };
310
154
  }
311
155
  }
312
-
313
- // Copyright 2024 IOTA Stiftung.
314
- // SPDX-License-Identifier: Apache-2.0.
315
- /**
316
- * Initialize the schema for the messaging service.
317
- */
318
- function initSchema() {
319
- EntitySchemaFactory.register("TemplateEntry", () => EntitySchemaHelper.getSchema(TemplateEntry));
320
- }
321
-
322
- export { MessagingAdminService, MessagingService, TemplateEntry, initSchema };
156
+ //# sourceMappingURL=messagingService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messagingService.js","sourceRoot":"","sources":["../../src/messagingService.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAC5E,OAAO,EAEN,8BAA8B,EAC9B,0CAA0C,EAC1C,4BAA4B,EAK5B,MAAM,4BAA4B,CAAC;AAIpC;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAC5B;;OAEG;IACI,MAAM,CAAU,UAAU,sBAAsC;IAEvE;;;OAGG;IACc,wBAAwB,CAA4B;IAErE;;;OAGG;IACc,mCAAmC,CAAwC;IAE5F;;;OAGG;IACc,sBAAsB,CAA0B;IAEjE;;;OAGG;IACc,wBAAwB,CAA2B;IAEpE;;;OAGG;IACH,YAAY,OAA6C;QACxD,IAAI,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,2BAA2B,CAAC,EAAE,CAAC;YAC1D,IAAI,CAAC,wBAAwB,GAAG,8BAA8B,CAAC,GAAG,CACjE,OAAO,CAAC,2BAA2B,CACnC,CAAC;QACH,CAAC;QAED,IAAI,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,sCAAsC,CAAC,EAAE,CAAC;YACrE,IAAI,CAAC,mCAAmC,GAAG,0CAA0C,CAAC,GAAG,CACxF,OAAO,CAAC,sCAAsC,CAC9C,CAAC;QACH,CAAC;QAED,IAAI,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,yBAAyB,CAAC,EAAE,CAAC;YACxD,IAAI,CAAC,sBAAsB,GAAG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IAAI,CAAC,GAAG,CAAC,CAAC;YACxB,MAAM,WAAW,GAAG,KAAK,GAAG,IAAI,CAAC;YACjC,cAAc,GAAG,cAAc,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;YAC7E,gBAAgB,GAAG,gBAAgB,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;QAClF,CAAC;QAED,OAAO;YACN,KAAK,EAAE,cAAc;YACrB,OAAO,EAAE,gBAAgB;SACzB,CAAC;IACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { ComponentFactory, GeneralError, Guards, Is } from \"@twin.org/core\";\nimport {\n\ttype IMessagingAdminComponent,\n\tMessagingEmailConnectorFactory,\n\tMessagingPushNotificationsConnectorFactory,\n\tMessagingSmsConnectorFactory,\n\ttype IMessagingComponent,\n\ttype IMessagingEmailConnector,\n\ttype IMessagingPushNotificationsConnector,\n\ttype IMessagingSmsConnector\n} from \"@twin.org/messaging-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IMessagingServiceConstructorOptions } from \"./models/IMessagingServiceConstructorOptions.js\";\n\n/**\n * Service for performing email messaging operations to a connector.\n */\nexport class MessagingService implements IMessagingComponent {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<MessagingService>();\n\n\t/**\n\t * Emails messaging connector used by the service.\n\t * @internal\n\t */\n\tprivate readonly _emailMessagingConnector?: IMessagingEmailConnector;\n\n\t/**\n\t * Push notifications messaging connector used by the service.\n\t * @internal\n\t */\n\tprivate readonly _pushNotificationMessagingConnector?: IMessagingPushNotificationsConnector;\n\n\t/**\n\t * SMS messaging connector used by the service.\n\t * @internal\n\t */\n\tprivate readonly _smsMessagingConnector?: IMessagingSmsConnector;\n\n\t/**\n\t * The admin component for the messaging.\n\t * @internal\n\t */\n\tprivate readonly _messagingAdminComponent: IMessagingAdminComponent;\n\n\t/**\n\t * Create a new instance of MessagingService.\n\t * @param options The options for the connector.\n\t */\n\tconstructor(options?: IMessagingServiceConstructorOptions) {\n\t\tif (Is.stringValue(options?.messagingEmailConnectorType)) {\n\t\t\tthis._emailMessagingConnector = MessagingEmailConnectorFactory.get(\n\t\t\t\toptions.messagingEmailConnectorType\n\t\t\t);\n\t\t}\n\n\t\tif (Is.stringValue(options?.messagingPushNotificationConnectorType)) {\n\t\t\tthis._pushNotificationMessagingConnector = MessagingPushNotificationsConnectorFactory.get(\n\t\t\t\toptions.messagingPushNotificationConnectorType\n\t\t\t);\n\t\t}\n\n\t\tif (Is.stringValue(options?.messagingSmsConnectorType)) {\n\t\t\tthis._smsMessagingConnector = MessagingSmsConnectorFactory.get(\n\t\t\t\toptions.messagingSmsConnectorType\n\t\t\t);\n\t\t}\n\n\t\tthis._messagingAdminComponent = ComponentFactory.get(\n\t\t\toptions?.messagingAdminComponentType ?? \"messaging-admin\"\n\t\t);\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn MessagingService.CLASS_NAME;\n\t}\n\n\t/**\n\t * Send a custom email.\n\t * @param sender The sender email address.\n\t * @param recipients An array of recipients email addresses.\n\t * @param templateId The id of the email template.\n\t * @param data The data to populate the email template.\n\t * @param locale The locale of the email template.\n\t * @returns If the email was sent successfully.\n\t */\n\tpublic async sendCustomEmail(\n\t\tsender: string,\n\t\trecipients: string[],\n\t\ttemplateId: string,\n\t\tdata: { [key: string]: string },\n\t\tlocale: string\n\t): Promise<boolean> {\n\t\tif (Is.empty(this._emailMessagingConnector)) {\n\t\t\tthrow new GeneralError(MessagingService.CLASS_NAME, \"notConfiguredEmailMessagingConnector\");\n\t\t}\n\n\t\tGuards.stringValue(MessagingService.CLASS_NAME, nameof(sender), sender);\n\t\tGuards.arrayValue(MessagingService.CLASS_NAME, nameof(recipients), recipients);\n\t\tGuards.stringValue(MessagingService.CLASS_NAME, nameof(templateId), templateId);\n\t\tGuards.object(MessagingService.CLASS_NAME, nameof(data), data);\n\t\tGuards.stringValue(MessagingService.CLASS_NAME, nameof(locale), locale);\n\n\t\tconst template = await this._messagingAdminComponent.getTemplate(templateId, locale);\n\t\tconst populatedTemplate = this.populateTemplate(template, data);\n\n\t\treturn this._emailMessagingConnector.sendCustomEmail(\n\t\t\tsender,\n\t\t\trecipients,\n\t\t\tpopulatedTemplate.title,\n\t\t\tpopulatedTemplate.content\n\t\t);\n\t}\n\n\t/**\n\t * Registers a device to an specific app in order to send notifications to it.\n\t * @param applicationId The application address.\n\t * @param deviceToken The device token.\n\t * @returns If the device was registered successfully.\n\t */\n\tpublic async registerDevice(applicationId: string, deviceToken: string): Promise<string> {\n\t\tif (Is.empty(this._pushNotificationMessagingConnector)) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tMessagingService.CLASS_NAME,\n\t\t\t\t\"notConfiguredPushNotificationMessagingConnector\"\n\t\t\t);\n\t\t}\n\n\t\tGuards.stringValue(MessagingService.CLASS_NAME, nameof(applicationId), applicationId);\n\t\tGuards.stringValue(MessagingService.CLASS_NAME, nameof(deviceToken), deviceToken);\n\n\t\treturn this._pushNotificationMessagingConnector.registerDevice(applicationId, deviceToken);\n\t}\n\n\t/**\n\t * Send a push notification to a device.\n\t * @param deviceAddress The address of the device.\n\t * @param templateId The id of the push notification template.\n\t * @param data The data to populate the push notification template.\n\t * @param locale The locale of the push notification template.\n\t * @returns If the notification was sent successfully.\n\t */\n\tpublic async sendSinglePushNotification(\n\t\tdeviceAddress: string,\n\t\ttemplateId: string,\n\t\tdata: { [key: string]: string },\n\t\tlocale: string\n\t): Promise<boolean> {\n\t\tif (Is.empty(this._pushNotificationMessagingConnector)) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tMessagingService.CLASS_NAME,\n\t\t\t\t\"notConfiguredPushNotificationMessagingConnector\"\n\t\t\t);\n\t\t}\n\n\t\tGuards.stringValue(MessagingService.CLASS_NAME, nameof(deviceAddress), deviceAddress);\n\t\tGuards.stringValue(MessagingService.CLASS_NAME, nameof(templateId), templateId);\n\t\tGuards.object(MessagingService.CLASS_NAME, nameof(data), data);\n\t\tGuards.stringValue(MessagingService.CLASS_NAME, nameof(locale), locale);\n\n\t\tconst template = await this._messagingAdminComponent.getTemplate(templateId, locale);\n\t\tconst populatedTemplate = this.populateTemplate(template, data);\n\n\t\treturn this._pushNotificationMessagingConnector.sendSinglePushNotification(\n\t\t\tdeviceAddress,\n\t\t\tpopulatedTemplate.title,\n\t\t\tpopulatedTemplate.content\n\t\t);\n\t}\n\n\t/**\n\t * Send a SMS message to a phone number.\n\t * @param phoneNumber The recipient phone number.\n\t * @param templateId The id of the SMS template.\n\t * @param data The data to populate the SMS template.\n\t * @param locale The locale of the SMS template.\n\t * @returns If the SMS was sent successfully.\n\t */\n\tpublic async sendSMS(\n\t\tphoneNumber: string,\n\t\ttemplateId: string,\n\t\tdata: { [key: string]: string },\n\t\tlocale: string\n\t): Promise<boolean> {\n\t\tif (Is.empty(this._smsMessagingConnector)) {\n\t\t\tthrow new GeneralError(MessagingService.CLASS_NAME, \"notConfiguredSmsMessagingConnector\");\n\t\t}\n\n\t\tGuards.stringValue(MessagingService.CLASS_NAME, nameof(phoneNumber), phoneNumber);\n\t\tGuards.stringValue(MessagingService.CLASS_NAME, nameof(templateId), templateId);\n\t\tGuards.object(MessagingService.CLASS_NAME, nameof(data), data);\n\t\tGuards.stringValue(MessagingService.CLASS_NAME, nameof(locale), locale);\n\n\t\tconst template = await this._messagingAdminComponent.getTemplate(templateId, locale);\n\t\tconst populatedTemplate = this.populateTemplate(template, data);\n\n\t\treturn this._smsMessagingConnector.sendSMS(phoneNumber, populatedTemplate.content);\n\t}\n\n\t/**\n\t * Populate the template with data.\n\t * @param template The template.\n\t * @param template.title The title of the template.\n\t * @param template.content The content of the template.\n\t * @param data The data to populate the template.\n\t * @internal\n\t * @returns The populated template.\n\t */\n\tprivate populateTemplate(\n\t\ttemplate: { title: string; content: string },\n\t\tdata: { [key: string]: string }\n\t): { title: string; content: string } {\n\t\tlet populatedTitle = template.title;\n\t\tlet populatedContent = template.content;\n\n\t\tfor (const key in data) {\n\t\t\tconst value = data[key];\n\t\t\tconst placeholder = `{{${key}}}`;\n\t\t\tpopulatedTitle = populatedTitle.replace(new RegExp(placeholder, \"g\"), value);\n\t\t\tpopulatedContent = populatedContent.replace(new RegExp(placeholder, \"g\"), value);\n\t\t}\n\n\t\treturn {\n\t\t\ttitle: populatedTitle,\n\t\t\tcontent: populatedContent\n\t\t};\n\t}\n}\n"]}
@@ -0,0 +1,4 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ export {};
4
+ //# sourceMappingURL=IMessagingAdminServiceConfig.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IMessagingAdminServiceConfig.js","sourceRoot":"","sources":["../../../src/models/IMessagingAdminServiceConfig.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Options for the messaging service.\n */\nexport interface IMessagingAdminServiceConfig {\n\t/**\n\t * The default locale to use for the messaging service.\n\t * @default en\n\t */\n\tdefaultLocale?: string;\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=IMessagingAdminServiceConstructorOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IMessagingAdminServiceConstructorOptions.js","sourceRoot":"","sources":["../../../src/models/IMessagingAdminServiceConstructorOptions.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IMessagingAdminServiceConfig } from \"./IMessagingAdminServiceConfig.js\";\n\n/**\n * Options for the messaging admin service.\n */\nexport interface IMessagingAdminServiceConstructorOptions {\n\t/**\n\t * The type of the entity connector to use.\n\t * @default template-entry\n\t */\n\ttemplateEntryStorageConnectorType?: string;\n\n\t/**\n\t * The configuration for the messaging admin service.\n\t */\n\tconfig?: IMessagingAdminServiceConfig;\n}\n"]}
@@ -0,0 +1,4 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ export {};
4
+ //# sourceMappingURL=IMessagingServiceConstructorOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IMessagingServiceConstructorOptions.js","sourceRoot":"","sources":["../../../src/models/IMessagingServiceConstructorOptions.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Options for the messaging service.\n */\nexport interface IMessagingServiceConstructorOptions {\n\t/**\n\t * The type of the email messaging connector to use, defaults to not configured.\n\t */\n\tmessagingEmailConnectorType?: string;\n\n\t/**\n\t * The type of the push notifications messaging connector to use, defaults to not configured.\n\t */\n\tmessagingPushNotificationConnectorType?: string;\n\n\t/**\n\t * The type of the sms messaging connector to use, defaults to not configured.\n\t */\n\tmessagingSmsConnectorType?: string;\n\n\t/**\n\t * The type of the messaging admin component to use.\n\t * @default messaging-admin\n\t */\n\tmessagingAdminComponentType?: string;\n}\n"]}
@@ -0,0 +1,11 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ import { EntitySchemaFactory, EntitySchemaHelper } from "@twin.org/entity";
4
+ import { TemplateEntry } from "./entities/templateEntry.js";
5
+ /**
6
+ * Initialize the schema for the messaging service.
7
+ */
8
+ export function initSchema() {
9
+ EntitySchemaFactory.register("TemplateEntry", () => EntitySchemaHelper.getSchema(TemplateEntry));
10
+ }
11
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/schema.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAE3E,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D;;GAEG;AACH,MAAM,UAAU,UAAU;IACzB,mBAAmB,CAAC,QAAQ,kBAA0B,GAAG,EAAE,CAC1D,kBAAkB,CAAC,SAAS,CAAC,aAAa,CAAC,CAC3C,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { EntitySchemaFactory, EntitySchemaHelper } from \"@twin.org/entity\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { TemplateEntry } from \"./entities/templateEntry.js\";\n\n/**\n * Initialize the schema for the messaging service.\n */\nexport function initSchema(): void {\n\tEntitySchemaFactory.register(nameof<TemplateEntry>(), () =>\n\t\tEntitySchemaHelper.getSchema(TemplateEntry)\n\t);\n}\n"]}
@@ -1,7 +1,7 @@
1
- export * from "./entities/templateEntry";
2
- export * from "./messagingAdminService";
3
- export * from "./messagingService";
4
- export * from "./models/IMessagingAdminServiceConfig";
5
- export * from "./models/IMessagingAdminServiceConstructorOptions";
6
- export * from "./models/IMessagingServiceConstructorOptions";
7
- export * from "./schema";
1
+ export * from "./entities/templateEntry.js";
2
+ export * from "./messagingAdminService.js";
3
+ export * from "./messagingService.js";
4
+ export * from "./models/IMessagingAdminServiceConfig.js";
5
+ export * from "./models/IMessagingAdminServiceConstructorOptions.js";
6
+ export * from "./models/IMessagingServiceConstructorOptions.js";
7
+ export * from "./schema.js";
@@ -1,5 +1,5 @@
1
1
  import type { IMessagingAdminComponent } from "@twin.org/messaging-models";
2
- import type { IMessagingAdminServiceConstructorOptions } from "./models/IMessagingAdminServiceConstructorOptions";
2
+ import type { IMessagingAdminServiceConstructorOptions } from "./models/IMessagingAdminServiceConstructorOptions.js";
3
3
  /**
4
4
  * Service for performing email messaging operations to a connector.
5
5
  */
@@ -18,12 +18,10 @@ export declare class MessagingAdminService implements IMessagingAdminComponent {
18
18
  */
19
19
  constructor(options?: IMessagingAdminServiceConstructorOptions);
20
20
  /**
21
- * The component needs to be started when the node is initialized.
22
- * @param nodeIdentity The identity of the node starting the component.
23
- * @param nodeLoggingComponentType The node logging component type.
24
- * @returns Nothing.
21
+ * Returns the class name of the component.
22
+ * @returns The class name of the component.
25
23
  */
26
- start(nodeIdentity?: string, nodeLoggingComponentType?: string): Promise<void>;
24
+ className(): string;
27
25
  /**
28
26
  * Create or update a template.
29
27
  * @param templateId The id of the template.
@@ -1,5 +1,5 @@
1
1
  import { type IMessagingComponent } from "@twin.org/messaging-models";
2
- import type { IMessagingServiceConstructorOptions } from "./models/IMessagingServiceConstructorOptions";
2
+ import type { IMessagingServiceConstructorOptions } from "./models/IMessagingServiceConstructorOptions.js";
3
3
  /**
4
4
  * Service for performing email messaging operations to a connector.
5
5
  */
@@ -13,6 +13,11 @@ export declare class MessagingService implements IMessagingComponent {
13
13
  * @param options The options for the connector.
14
14
  */
15
15
  constructor(options?: IMessagingServiceConstructorOptions);
16
+ /**
17
+ * Returns the class name of the component.
18
+ * @returns The class name of the component.
19
+ */
20
+ className(): string;
16
21
  /**
17
22
  * Send a custom email.
18
23
  * @param sender The sender email address.
@@ -7,14 +7,4 @@ export interface IMessagingAdminServiceConfig {
7
7
  * @default en
8
8
  */
9
9
  defaultLocale?: string;
10
- /**
11
- * Initial set of templates to create on startup.
12
- */
13
- templates?: {
14
- templateId: string;
15
- title: string;
16
- content: {
17
- [locale: string]: string;
18
- };
19
- }[];
20
10
  }
@@ -1,4 +1,4 @@
1
- import type { IMessagingAdminServiceConfig } from "./IMessagingAdminServiceConfig";
1
+ import type { IMessagingAdminServiceConfig } from "./IMessagingAdminServiceConfig.js";
2
2
  /**
3
3
  * Options for the messaging admin service.
4
4
  */
package/docs/changelog.md CHANGED
@@ -1,5 +1,47 @@
1
1
  # @twin.org/messaging-service - Changelog
2
2
 
3
+ ## [0.0.3-next.2](https://github.com/twinfoundation/messaging/compare/messaging-service-v0.0.3-next.1...messaging-service-v0.0.3-next.2) (2026-01-20)
4
+
5
+
6
+ ### Miscellaneous Chores
7
+
8
+ * **messaging-service:** Synchronize repo versions
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/messaging-models bumped from 0.0.3-next.1 to 0.0.3-next.2
16
+
17
+ ## [0.0.3-next.1](https://github.com/twinfoundation/messaging/compare/messaging-service-v0.0.3-next.0...messaging-service-v0.0.3-next.1) (2025-11-11)
18
+
19
+
20
+ ### Features
21
+
22
+ * add context id features ([#24](https://github.com/twinfoundation/messaging/issues/24)) ([42b94e8](https://github.com/twinfoundation/messaging/commit/42b94e8be32c86db6afc5ccf5e9ff2c93d15d05d))
23
+ * add initial templates config ([66b40eb](https://github.com/twinfoundation/messaging/commit/66b40eb57a478d9f79de540eb847a4fb37894235))
24
+ * add messaging admin component ([cbaaca3](https://github.com/twinfoundation/messaging/commit/cbaaca34db6a9f5c51438c201535b4b43a1aea1f))
25
+ * add validate-locales ([5eeff9b](https://github.com/twinfoundation/messaging/commit/5eeff9b9bf6cecf93b249b56c4bb74b4c5bf86c9))
26
+ * eslint migration to flat config ([faa02ec](https://github.com/twinfoundation/messaging/commit/faa02ec0ef450db88b08e938415e40cf13625d15))
27
+ * Messaging services ([#3](https://github.com/twinfoundation/messaging/issues/3)) ([32571b5](https://github.com/twinfoundation/messaging/commit/32571b5abf5d3fc3b168074c23507e926c5d00b0))
28
+ * remove unused namespace ([2201898](https://github.com/twinfoundation/messaging/commit/22018988b84e4b3e76da1a1e16f29bfde7e6f0e6))
29
+ * update dependencies ([53dcde6](https://github.com/twinfoundation/messaging/commit/53dcde60d6efaee5957296f6d097cb97c0fb2f9d))
30
+ * update framework core ([fc476b4](https://github.com/twinfoundation/messaging/commit/fc476b48e6075de0ae871e3cec1fa179aa04b5eb))
31
+ * use shared store mechanism ([#8](https://github.com/twinfoundation/messaging/issues/8)) ([9ad65c2](https://github.com/twinfoundation/messaging/commit/9ad65c239ba77bb75604a1f6e51b975357f3228d))
32
+
33
+
34
+ ### Bug Fixes
35
+
36
+ * Adding format to the MessagingEntries ts number ([8b99d4f](https://github.com/twinfoundation/messaging/commit/8b99d4f01c4f2b08da8d2affc1b9554fcb0d3690))
37
+
38
+
39
+ ### Dependencies
40
+
41
+ * The following workspace dependencies were updated
42
+ * dependencies
43
+ * @twin.org/messaging-models bumped from 0.0.3-next.0 to 0.0.3-next.1
44
+
3
45
  ## [0.0.2-next.6](https://github.com/twinfoundation/messaging/compare/messaging-service-v0.0.2-next.5...messaging-service-v0.0.2-next.6) (2025-10-09)
4
46
 
5
47
 
@@ -36,35 +36,21 @@ Runtime name for the class.
36
36
 
37
37
  ## Methods
38
38
 
39
- ### start()
39
+ ### className()
40
40
 
41
- > **start**(`nodeIdentity?`, `nodeLoggingComponentType?`): `Promise`\<`void`\>
41
+ > **className**(): `string`
42
42
 
43
- The component needs to be started when the node is initialized.
44
-
45
- #### Parameters
46
-
47
- ##### nodeIdentity?
48
-
49
- `string`
50
-
51
- The identity of the node starting the component.
52
-
53
- ##### nodeLoggingComponentType?
54
-
55
- `string`
56
-
57
- The node logging component type.
43
+ Returns the class name of the component.
58
44
 
59
45
  #### Returns
60
46
 
61
- `Promise`\<`void`\>
47
+ `string`
62
48
 
63
- Nothing.
49
+ The class name of the component.
64
50
 
65
51
  #### Implementation of
66
52
 
67
- `IMessagingAdminComponent.start`
53
+ `IMessagingAdminComponent.className`
68
54
 
69
55
  ***
70
56
 
@@ -36,6 +36,24 @@ Runtime name for the class.
36
36
 
37
37
  ## Methods
38
38
 
39
+ ### className()
40
+
41
+ > **className**(): `string`
42
+
43
+ Returns the class name of the component.
44
+
45
+ #### Returns
46
+
47
+ `string`
48
+
49
+ The class name of the component.
50
+
51
+ #### Implementation of
52
+
53
+ `IMessagingComponent.className`
54
+
55
+ ***
56
+
39
57
  ### sendCustomEmail()
40
58
 
41
59
  > **sendCustomEmail**(`sender`, `recipients`, `templateId`, `data`, `locale`): `Promise`\<`boolean`\>
@@ -15,27 +15,3 @@ The default locale to use for the messaging service.
15
15
  ```ts
16
16
  en
17
17
  ```
18
-
19
- ***
20
-
21
- ### templates?
22
-
23
- > `optional` **templates**: `object`[]
24
-
25
- Initial set of templates to create on startup.
26
-
27
- #### templateId
28
-
29
- > **templateId**: `string`
30
-
31
- #### title
32
-
33
- > **title**: `string`
34
-
35
- #### content
36
-
37
- > **content**: `object`
38
-
39
- ##### Index Signature
40
-
41
- \[`locale`: `string`\]: `string`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/messaging-service",
3
- "version": "0.0.2-next.6",
3
+ "version": "0.0.3-next.2",
4
4
  "description": "Messaging service implementation",
5
5
  "repository": {
6
6
  "type": "git",
@@ -18,23 +18,20 @@
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.6",
21
+ "@twin.org/messaging-models": "0.0.3-next.2",
22
22
  "@twin.org/nameof": "next"
23
23
  },
24
- "main": "./dist/cjs/index.cjs",
25
- "module": "./dist/esm/index.mjs",
24
+ "main": "./dist/es/index.js",
26
25
  "types": "./dist/types/index.d.ts",
27
26
  "exports": {
28
27
  ".": {
29
28
  "types": "./dist/types/index.d.ts",
30
- "require": "./dist/cjs/index.cjs",
31
- "import": "./dist/esm/index.mjs"
32
- },
33
- "./locales": "./locales"
29
+ "import": "./dist/es/index.js",
30
+ "default": "./dist/es/index.js"
31
+ }
34
32
  },
35
33
  "files": [
36
- "dist/cjs",
37
- "dist/esm",
34
+ "dist/es",
38
35
  "dist/types",
39
36
  "locales",
40
37
  "docs"
@@ -1,326 +0,0 @@
1
- 'use strict';
2
-
3
- var entity = require('@twin.org/entity');
4
- var core = require('@twin.org/core');
5
- var entityStorageModels = require('@twin.org/entity-storage-models');
6
- var messagingModels = require('@twin.org/messaging-models');
7
-
8
- // Copyright 2024 IOTA Stiftung.
9
- // SPDX-License-Identifier: Apache-2.0.
10
- /**
11
- * Call defining a template message entry.
12
- */
13
- exports.TemplateEntry = class TemplateEntry {
14
- /**
15
- * The id.
16
- */
17
- id;
18
- /**
19
- * The title.
20
- */
21
- title;
22
- /**
23
- * The content.
24
- */
25
- content;
26
- /**
27
- * The timestamp of the template entry.
28
- */
29
- dateCreated;
30
- };
31
- __decorate([
32
- entity.property({ type: "string", isPrimary: true }),
33
- __metadata("design:type", String)
34
- ], exports.TemplateEntry.prototype, "id", void 0);
35
- __decorate([
36
- entity.property({ type: "string" }),
37
- __metadata("design:type", String)
38
- ], exports.TemplateEntry.prototype, "title", void 0);
39
- __decorate([
40
- entity.property({ type: "string" }),
41
- __metadata("design:type", String)
42
- ], exports.TemplateEntry.prototype, "content", void 0);
43
- __decorate([
44
- entity.property({ type: "string", format: "date-time", sortDirection: entity.SortDirection.Descending }),
45
- __metadata("design:type", String)
46
- ], exports.TemplateEntry.prototype, "dateCreated", void 0);
47
- exports.TemplateEntry = __decorate([
48
- entity.entity()
49
- ], exports.TemplateEntry);
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
- * Runtime name for the class.
59
- */
60
- static CLASS_NAME = "MessagingAdminService";
61
- /**
62
- * Default locale for the messaging service.
63
- */
64
- static _DEFAULT_LOCALE = "en";
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
- * Initial set of templates to create on startup.
77
- * @internal
78
- */
79
- _initialTemplates;
80
- /**
81
- * Create a new instance of MessagingAdminService.
82
- * @param options The options for the connector.
83
- */
84
- constructor(options) {
85
- this._entityStorageConnector = entityStorageModels.EntityStorageConnectorFactory.get(options?.templateEntryStorageConnectorType ?? "template-entry");
86
- this._defaultLocale = options?.config?.defaultLocale ?? MessagingAdminService._DEFAULT_LOCALE;
87
- this._initialTemplates = options?.config?.templates;
88
- }
89
- /**
90
- * The component needs to be started when the node is initialized.
91
- * @param nodeIdentity The identity of the node starting the component.
92
- * @param nodeLoggingComponentType The node logging component type.
93
- * @returns Nothing.
94
- */
95
- async start(nodeIdentity, nodeLoggingComponentType) {
96
- if (core.Is.arrayValue(this._initialTemplates)) {
97
- for (const template of this._initialTemplates) {
98
- for (const locale of Object.keys(template.content)) {
99
- await this.setTemplate(template.templateId, locale, template.title, template.content[locale]);
100
- }
101
- }
102
- }
103
- }
104
- /**
105
- * Create or update a template.
106
- * @param templateId The id of the template.
107
- * @param locale The locale of the template.
108
- * @param title The title of the template.
109
- * @param content The content of the template.
110
- * @returns Nothing.
111
- */
112
- async setTemplate(templateId, locale, title, content) {
113
- core.Guards.stringValue(MessagingAdminService.CLASS_NAME, "templateId", templateId);
114
- core.Guards.stringValue(MessagingAdminService.CLASS_NAME, "locale", locale);
115
- core.Guards.stringValue(MessagingAdminService.CLASS_NAME, "title", title);
116
- core.Guards.stringValue(MessagingAdminService.CLASS_NAME, "content", content);
117
- const templateEntry = new exports.TemplateEntry();
118
- templateEntry.id = `${templateId}:${locale}`;
119
- templateEntry.dateCreated = new Date(Date.now()).toISOString();
120
- templateEntry.title = title;
121
- templateEntry.content = content;
122
- await this._entityStorageConnector.set(templateEntry);
123
- }
124
- /**
125
- * Get the email template by id and locale.
126
- * @param templateId The id of the email template.
127
- * @param locale The locale of the email template.
128
- * @returns The email template.
129
- */
130
- async getTemplate(templateId, locale) {
131
- core.Guards.stringValue(MessagingAdminService.CLASS_NAME, "templateId", templateId);
132
- core.Guards.stringValue(MessagingAdminService.CLASS_NAME, "locale", locale);
133
- let templateEntry;
134
- try {
135
- // First try to get the template for the requested locale
136
- templateEntry = await this._entityStorageConnector.get(`${templateId}:${locale}`);
137
- }
138
- catch { }
139
- // If the template is not found for the requested locale, try to get it for the default locale
140
- // only if the requested locale is different from the default locale
141
- if (core.Is.empty(templateEntry) && this._defaultLocale !== locale) {
142
- try {
143
- templateEntry = await this._entityStorageConnector.get(`${templateId}:${this._defaultLocale}`);
144
- }
145
- catch { }
146
- }
147
- if (core.Is.empty(templateEntry)) {
148
- throw new core.GeneralError(MessagingAdminService.CLASS_NAME, "getTemplateFailed", {
149
- templateId,
150
- locale
151
- });
152
- }
153
- return templateEntry;
154
- }
155
- /**
156
- * Remove a template.
157
- * @param templateId The id of the template.
158
- * @param locale The locale of the template.
159
- * @returns Nothing
160
- */
161
- async removeTemplate(templateId, locale) {
162
- core.Guards.stringValue(MessagingAdminService.CLASS_NAME, "templateId", templateId);
163
- core.Guards.stringValue(MessagingAdminService.CLASS_NAME, "locale", locale);
164
- return this._entityStorageConnector.remove(`${templateId}:${locale}`);
165
- }
166
- }
167
-
168
- // Copyright 2024 IOTA Stiftung.
169
- // SPDX-License-Identifier: Apache-2.0.
170
- /**
171
- * Service for performing email messaging operations to a connector.
172
- */
173
- class MessagingService {
174
- /**
175
- * Runtime name for the class.
176
- */
177
- static CLASS_NAME = "MessagingService";
178
- /**
179
- * Emails messaging connector used by the service.
180
- * @internal
181
- */
182
- _emailMessagingConnector;
183
- /**
184
- * Push notifications messaging connector used by the service.
185
- * @internal
186
- */
187
- _pushNotificationMessagingConnector;
188
- /**
189
- * SMS messaging connector used by the service.
190
- * @internal
191
- */
192
- _smsMessagingConnector;
193
- /**
194
- * The admin component for the messaging.
195
- * @internal
196
- */
197
- _messagingAdminComponent;
198
- /**
199
- * Create a new instance of MessagingService.
200
- * @param options The options for the connector.
201
- */
202
- constructor(options) {
203
- if (core.Is.stringValue(options?.messagingEmailConnectorType)) {
204
- this._emailMessagingConnector = messagingModels.MessagingEmailConnectorFactory.get(options.messagingEmailConnectorType);
205
- }
206
- if (core.Is.stringValue(options?.messagingPushNotificationConnectorType)) {
207
- this._pushNotificationMessagingConnector = messagingModels.MessagingPushNotificationsConnectorFactory.get(options.messagingPushNotificationConnectorType);
208
- }
209
- if (core.Is.stringValue(options?.messagingSmsConnectorType)) {
210
- this._smsMessagingConnector = messagingModels.MessagingSmsConnectorFactory.get(options.messagingSmsConnectorType);
211
- }
212
- this._messagingAdminComponent = core.ComponentFactory.get(options?.messagingAdminComponentType ?? "messaging-admin");
213
- }
214
- /**
215
- * Send a custom email.
216
- * @param sender The sender email address.
217
- * @param recipients An array of recipients email addresses.
218
- * @param templateId The id of the email template.
219
- * @param data The data to populate the email template.
220
- * @param locale The locale of the email template.
221
- * @returns If the email was sent successfully.
222
- */
223
- async sendCustomEmail(sender, recipients, templateId, data, locale) {
224
- if (core.Is.empty(this._emailMessagingConnector)) {
225
- throw new core.GeneralError(MessagingService.CLASS_NAME, "notConfiguredEmailMessagingConnector");
226
- }
227
- core.Guards.stringValue(MessagingService.CLASS_NAME, "sender", sender);
228
- core.Guards.arrayValue(MessagingService.CLASS_NAME, "recipients", recipients);
229
- core.Guards.stringValue(MessagingService.CLASS_NAME, "templateId", templateId);
230
- core.Guards.object(MessagingService.CLASS_NAME, "data", data);
231
- core.Guards.stringValue(MessagingService.CLASS_NAME, "locale", locale);
232
- const template = await this._messagingAdminComponent.getTemplate(templateId, locale);
233
- const populatedTemplate = this.populateTemplate(template, data);
234
- return this._emailMessagingConnector.sendCustomEmail(sender, recipients, populatedTemplate.title, populatedTemplate.content);
235
- }
236
- /**
237
- * Registers a device to an specific app in order to send notifications to it.
238
- * @param applicationId The application address.
239
- * @param deviceToken The device token.
240
- * @returns If the device was registered successfully.
241
- */
242
- async registerDevice(applicationId, deviceToken) {
243
- if (core.Is.empty(this._pushNotificationMessagingConnector)) {
244
- throw new core.GeneralError(MessagingService.CLASS_NAME, "notConfiguredPushNotificationMessagingConnector");
245
- }
246
- core.Guards.stringValue(MessagingService.CLASS_NAME, "applicationId", applicationId);
247
- core.Guards.stringValue(MessagingService.CLASS_NAME, "deviceToken", deviceToken);
248
- return this._pushNotificationMessagingConnector.registerDevice(applicationId, deviceToken);
249
- }
250
- /**
251
- * Send a push notification to a device.
252
- * @param deviceAddress The address of the device.
253
- * @param templateId The id of the push notification template.
254
- * @param data The data to populate the push notification template.
255
- * @param locale The locale of the push notification template.
256
- * @returns If the notification was sent successfully.
257
- */
258
- async sendSinglePushNotification(deviceAddress, templateId, data, locale) {
259
- if (core.Is.empty(this._pushNotificationMessagingConnector)) {
260
- throw new core.GeneralError(MessagingService.CLASS_NAME, "notConfiguredPushNotificationMessagingConnector");
261
- }
262
- core.Guards.stringValue(MessagingService.CLASS_NAME, "deviceAddress", deviceAddress);
263
- core.Guards.stringValue(MessagingService.CLASS_NAME, "templateId", templateId);
264
- core.Guards.object(MessagingService.CLASS_NAME, "data", data);
265
- core.Guards.stringValue(MessagingService.CLASS_NAME, "locale", locale);
266
- const template = await this._messagingAdminComponent.getTemplate(templateId, locale);
267
- const populatedTemplate = this.populateTemplate(template, data);
268
- return this._pushNotificationMessagingConnector.sendSinglePushNotification(deviceAddress, populatedTemplate.title, populatedTemplate.content);
269
- }
270
- /**
271
- * Send a SMS message to a phone number.
272
- * @param phoneNumber The recipient phone number.
273
- * @param templateId The id of the SMS template.
274
- * @param data The data to populate the SMS template.
275
- * @param locale The locale of the SMS template.
276
- * @returns If the SMS was sent successfully.
277
- */
278
- async sendSMS(phoneNumber, templateId, data, locale) {
279
- if (core.Is.empty(this._smsMessagingConnector)) {
280
- throw new core.GeneralError(MessagingService.CLASS_NAME, "notConfiguredSmsMessagingConnector");
281
- }
282
- core.Guards.stringValue(MessagingService.CLASS_NAME, "phoneNumber", phoneNumber);
283
- core.Guards.stringValue(MessagingService.CLASS_NAME, "templateId", templateId);
284
- core.Guards.object(MessagingService.CLASS_NAME, "data", data);
285
- core.Guards.stringValue(MessagingService.CLASS_NAME, "locale", locale);
286
- const template = await this._messagingAdminComponent.getTemplate(templateId, locale);
287
- const populatedTemplate = this.populateTemplate(template, data);
288
- return this._smsMessagingConnector.sendSMS(phoneNumber, populatedTemplate.content);
289
- }
290
- /**
291
- * Populate the template with data.
292
- * @param template The template.
293
- * @param template.title The title of the template.
294
- * @param template.content The content of the template.
295
- * @param data The data to populate the template.
296
- * @internal
297
- * @returns The populated template.
298
- */
299
- populateTemplate(template, data) {
300
- let populatedTitle = template.title;
301
- let populatedContent = template.content;
302
- for (const key in data) {
303
- const value = data[key];
304
- const placeholder = `{{${key}}}`;
305
- populatedTitle = populatedTitle.replace(new RegExp(placeholder, "g"), value);
306
- populatedContent = populatedContent.replace(new RegExp(placeholder, "g"), value);
307
- }
308
- return {
309
- title: populatedTitle,
310
- content: populatedContent
311
- };
312
- }
313
- }
314
-
315
- // Copyright 2024 IOTA Stiftung.
316
- // SPDX-License-Identifier: Apache-2.0.
317
- /**
318
- * Initialize the schema for the messaging service.
319
- */
320
- function initSchema() {
321
- entity.EntitySchemaFactory.register("TemplateEntry", () => entity.EntitySchemaHelper.getSchema(exports.TemplateEntry));
322
- }
323
-
324
- exports.MessagingAdminService = MessagingAdminService;
325
- exports.MessagingService = MessagingService;
326
- exports.initSchema = initSchema;