@tomei/mailer 0.7.1 → 0.10.0-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/.gitlab-ci.yml +16 -16
  2. package/.husky/commit-msg +15 -15
  3. package/dist/__tests__/unit/infrastructure/repositories/file-system-mail-log.repository.spec.d.ts +1 -0
  4. package/dist/__tests__/unit/infrastructure/repositories/file-system-mail-log.repository.spec.js +47 -0
  5. package/dist/__tests__/unit/infrastructure/repositories/file-system-mail-log.repository.spec.js.map +1 -0
  6. package/dist/src/core/entity/email-log.entity.d.ts +35 -0
  7. package/dist/src/core/entity/email-log.entity.js +43 -0
  8. package/dist/src/core/entity/email-log.entity.js.map +1 -0
  9. package/dist/src/core/enum/email-status.enum.d.ts +5 -0
  10. package/dist/src/core/enum/email-status.enum.js +10 -0
  11. package/dist/src/core/enum/email-status.enum.js.map +1 -0
  12. package/dist/src/core/index.d.ts +4 -0
  13. package/dist/src/core/index.js +8 -0
  14. package/dist/src/core/index.js.map +1 -0
  15. package/dist/src/core/interface/email-log.interface.d.ts +18 -0
  16. package/dist/src/core/interface/email-log.interface.js +3 -0
  17. package/dist/src/core/interface/email-log.interface.js.map +1 -0
  18. package/dist/src/core/interface/i-email-repository.d.ts +16 -0
  19. package/dist/src/core/interface/i-email-repository.js +3 -0
  20. package/dist/src/core/interface/i-email-repository.js.map +1 -0
  21. package/dist/src/domain/repositories/mail-log.repository.d.ts +4 -0
  22. package/dist/src/domain/repositories/mail-log.repository.js +3 -0
  23. package/dist/src/domain/repositories/mail-log.repository.js.map +1 -0
  24. package/dist/src/infrastructure/repositories/file-system-mail-log.repository.d.ts +5 -0
  25. package/dist/src/infrastructure/repositories/file-system-mail-log.repository.js +26 -0
  26. package/dist/src/infrastructure/repositories/file-system-mail-log.repository.js.map +1 -0
  27. package/dist/src/interfaces/EmailLog.d.ts +39 -0
  28. package/dist/src/interfaces/EmailLog.js +45 -0
  29. package/dist/src/interfaces/EmailLog.js.map +1 -0
  30. package/dist/src/interfaces/IEmailRepository.d.ts +13 -0
  31. package/dist/src/interfaces/IEmailRepository.js +10 -0
  32. package/dist/src/interfaces/IEmailRepository.js.map +1 -0
  33. package/dist/src/interfaces/index.d.ts +2 -0
  34. package/dist/src/interfaces/index.js +5 -0
  35. package/dist/src/interfaces/index.js.map +1 -1
  36. package/dist/tsconfig.tsbuildinfo +1 -1
  37. package/eslint.config.mjs +58 -58
  38. package/package.json +47 -47
  39. package/sonar-project.properties +12 -12
  40. package/src/enum/email-status.enum.ts +4 -4
  41. package/src/enum/index.ts +1 -1
  42. package/src/interfaces/EmailLog.ts +91 -0
  43. package/src/interfaces/IEmailRepository.ts +18 -0
  44. package/src/interfaces/index.ts +2 -0
  45. package/src/interfaces/log-transaction-options.interface.ts +18 -18
  46. package/src/interfaces/mail-config.interface.ts +8 -8
  47. package/src/interfaces/mail-log.interface.ts +12 -12
  48. package/src/mailer/index.ts +2 -2
  49. package/src/mailer/mailer.base.ts +110 -110
  50. package/src/mailer/smtp-mailer.ts +121 -121
@@ -1,110 +1,110 @@
1
- import { IMailLog } from 'src/interfaces/mail-log.interface';
2
- import { ILogTransactionOption } from '../interfaces/log-transaction-options.interface';
3
- import { ISendMailConfig } from '../interfaces/mail-config.interface';
4
- import * as fs from 'fs';
5
- import * as path from 'path';
6
- import { EmailStatus } from '../enum/email-status.enum';
7
- import { ComponentConfig } from '@tomei/config';
8
-
9
- export abstract class MailerBase {
10
- abstract sendMail(options: any): Promise<any>;
11
-
12
- async send(options: ISendMailConfig): Promise<any> {
13
- // start timer
14
- const start = Date.now();
15
- let status: EmailStatus;
16
-
17
- // send email
18
- try {
19
- await this.sendMail(options);
20
- status = EmailStatus.Sent;
21
- } catch (error) {
22
- status = EmailStatus.Failed;
23
- } finally {
24
- const duration = Date.now() - start;
25
- const isLogEnabled = ComponentConfig.getComponentConfigValue(
26
- '@tomei/mailer',
27
- 'isLogEnabled',
28
- );
29
- if (isLogEnabled) {
30
- MailerBase.log({
31
- duration,
32
- from: options.from,
33
- to:
34
- typeof options.to === 'string' ? options.to : options.to.join(','),
35
- cc:
36
- typeof options.cc === 'string'
37
- ? options.cc
38
- : options.cc
39
- ? options.cc.join(',')
40
- : '',
41
- subject: options.subject,
42
- rawContent: options.html,
43
- date: new Date(),
44
- status: status,
45
- });
46
- }
47
- }
48
- }
49
-
50
- async logTransaction(logOptions: ILogTransactionOption): Promise<void> {
51
- const startTime = new Date().getTime();
52
- let endTime: number;
53
- const options: any = {
54
- from: logOptions.options.from,
55
- to: logOptions.options.to,
56
- subject: logOptions.options.subject,
57
- html: `<div>${JSON.stringify(logOptions.logData)}</div>`,
58
- cc: '',
59
- };
60
- let result: EmailStatus;
61
-
62
- try {
63
- await this.sendMail(options);
64
- result = EmailStatus.Sent;
65
- } catch (error) {
66
- //Retry once
67
- try {
68
- console.info('Retry to send mail');
69
- await this.sendMail(options);
70
- } catch (retryError) {
71
- result = EmailStatus.Failed;
72
- console.error(retryError);
73
- }
74
- } finally {
75
- endTime = new Date().getTime();
76
- const duration = endTime - startTime;
77
- const mailLogOptions: IMailLog = {
78
- duration,
79
- from: options.from,
80
- to: options.to,
81
- cc: '',
82
- subject: options.subject,
83
- rawContent: options.html,
84
- date: new Date(),
85
- status: result,
86
- };
87
- MailerBase.log(mailLogOptions);
88
- }
89
- }
90
-
91
- static log(mailLogOptions: IMailLog): void {
92
- try {
93
- const logFolderPath = path.join(process.cwd(), 'mail-log');
94
-
95
- if (!fs.existsSync(logFolderPath)) {
96
- fs.mkdirSync(logFolderPath);
97
- }
98
-
99
- const currentDate = new Date();
100
- const formattedDate = currentDate.toISOString().slice(0, 10);
101
- const logFileName = `${formattedDate}.log`;
102
- const logFilePath = path.join(logFolderPath, logFileName);
103
- const stringData = JSON.stringify(mailLogOptions);
104
-
105
- fs.appendFileSync(logFilePath, '\n' + stringData);
106
- } catch (error) {
107
- throw new Error('Error occurred while logging mail');
108
- }
109
- }
110
- }
1
+ import { IMailLog } from 'src/interfaces/mail-log.interface';
2
+ import { ILogTransactionOption } from '../interfaces/log-transaction-options.interface';
3
+ import { ISendMailConfig } from '../interfaces/mail-config.interface';
4
+ import * as fs from 'fs';
5
+ import * as path from 'path';
6
+ import { EmailStatus } from '../enum/email-status.enum';
7
+ import { ComponentConfig } from '@tomei/config';
8
+
9
+ export abstract class MailerBase {
10
+ abstract sendMail(options: any): Promise<any>;
11
+
12
+ async send(options: ISendMailConfig): Promise<any> {
13
+ // start timer
14
+ const start = Date.now();
15
+ let status: EmailStatus;
16
+
17
+ // send email
18
+ try {
19
+ await this.sendMail(options);
20
+ status = EmailStatus.Sent;
21
+ } catch (error) {
22
+ status = EmailStatus.Failed;
23
+ } finally {
24
+ const duration = Date.now() - start;
25
+ const isLogEnabled = ComponentConfig.getComponentConfigValue(
26
+ '@tomei/mailer',
27
+ 'isLogEnabled',
28
+ );
29
+ if (isLogEnabled) {
30
+ MailerBase.log({
31
+ duration,
32
+ from: options.from,
33
+ to:
34
+ typeof options.to === 'string' ? options.to : options.to.join(','),
35
+ cc:
36
+ typeof options.cc === 'string'
37
+ ? options.cc
38
+ : options.cc
39
+ ? options.cc.join(',')
40
+ : '',
41
+ subject: options.subject,
42
+ rawContent: options.html,
43
+ date: new Date(),
44
+ status: status,
45
+ });
46
+ }
47
+ }
48
+ }
49
+
50
+ async logTransaction(logOptions: ILogTransactionOption): Promise<void> {
51
+ const startTime = new Date().getTime();
52
+ let endTime: number;
53
+ const options: any = {
54
+ from: logOptions.options.from,
55
+ to: logOptions.options.to,
56
+ subject: logOptions.options.subject,
57
+ html: `<div>${JSON.stringify(logOptions.logData)}</div>`,
58
+ cc: '',
59
+ };
60
+ let result: EmailStatus;
61
+
62
+ try {
63
+ await this.sendMail(options);
64
+ result = EmailStatus.Sent;
65
+ } catch (error) {
66
+ //Retry once
67
+ try {
68
+ console.info('Retry to send mail');
69
+ await this.sendMail(options);
70
+ } catch (retryError) {
71
+ result = EmailStatus.Failed;
72
+ console.error(retryError);
73
+ }
74
+ } finally {
75
+ endTime = new Date().getTime();
76
+ const duration = endTime - startTime;
77
+ const mailLogOptions: IMailLog = {
78
+ duration,
79
+ from: options.from,
80
+ to: options.to,
81
+ cc: '',
82
+ subject: options.subject,
83
+ rawContent: options.html,
84
+ date: new Date(),
85
+ status: result,
86
+ };
87
+ MailerBase.log(mailLogOptions);
88
+ }
89
+ }
90
+
91
+ static log(mailLogOptions: IMailLog): void {
92
+ try {
93
+ const logFolderPath = path.join(process.cwd(), 'mail-log');
94
+
95
+ if (!fs.existsSync(logFolderPath)) {
96
+ fs.mkdirSync(logFolderPath);
97
+ }
98
+
99
+ const currentDate = new Date();
100
+ const formattedDate = currentDate.toISOString().slice(0, 10);
101
+ const logFileName = `${formattedDate}.log`;
102
+ const logFilePath = path.join(logFolderPath, logFileName);
103
+ const stringData = JSON.stringify(mailLogOptions);
104
+
105
+ fs.appendFileSync(logFilePath, '\n' + stringData);
106
+ } catch (error) {
107
+ throw new Error('Error occurred while logging mail');
108
+ }
109
+ }
110
+ }
@@ -1,121 +1,121 @@
1
- import { ComponentConfig } from '@tomei/config';
2
- import { MailerBase } from './mailer.base';
3
- import * as nodemailer from 'nodemailer';
4
- import { ISendMailConfig } from 'src/interfaces/mail-config.interface';
5
-
6
- interface TransportConfig {
7
- host: string;
8
- port: number;
9
- secure: boolean;
10
- auth: {
11
- user: string;
12
- pass: string;
13
- };
14
- tls?: any;
15
- debug?: boolean;
16
- logger?: boolean;
17
- }
18
-
19
- export class SMTPMailer extends MailerBase {
20
- async sendMail(options: ISendMailConfig): Promise<void> {
21
- try {
22
- const host = ComponentConfig.getComponentConfigValue(
23
- '@tomei/mailer',
24
- 'host',
25
- );
26
- const port = ComponentConfig.getComponentConfigValue(
27
- '@tomei/mailer',
28
- 'port',
29
- );
30
- const secure = ComponentConfig.getComponentConfigValue(
31
- '@tomei/mailer',
32
- 'secure',
33
- );
34
- const user = ComponentConfig.getComponentConfigValue(
35
- '@tomei/mailer',
36
- 'user',
37
- );
38
- const pass = ComponentConfig.getComponentConfigValue(
39
- '@tomei/mailer',
40
- 'pass',
41
- );
42
- const tlsOptions = ComponentConfig.getComponentConfigValue(
43
- '@tomei/mailer',
44
- 'tlsOptions',
45
- );
46
- const isMailerDebugEnabled = ComponentConfig.getComponentConfigValue(
47
- '@tomei/mailer',
48
- 'isMailerDebugEnabled',
49
- );
50
- const isMailerLogEnabled = ComponentConfig.getComponentConfigValue(
51
- '@tomei/mailer',
52
- 'isMailerLogEnabled',
53
- );
54
-
55
- const testReceipientEmail = ComponentConfig.getComponentConfigValue(
56
- '@tomei/mailer',
57
- 'testReceipientEmail',
58
- );
59
-
60
- const environment = ComponentConfig.getComponentConfigValue(
61
- '@tomei/mailer',
62
- 'environment',
63
- );
64
-
65
- if (
66
- !host ||
67
- !port ||
68
- !user ||
69
- !pass ||
70
- !environment ||
71
- !testReceipientEmail
72
- ) {
73
- throw new Error(
74
- 'Host, port, user, pass, environment, or test receipient email is not defined in the configuration.',
75
- );
76
- }
77
-
78
- if (environment !== 'production') {
79
- options.to = testReceipientEmail;
80
- }
81
-
82
- const transportConfig: TransportConfig = {
83
- host,
84
- port,
85
- secure,
86
- auth: {
87
- user,
88
- pass,
89
- },
90
- };
91
-
92
- if (tlsOptions) {
93
- transportConfig.tls = tlsOptions;
94
- }
95
-
96
- if (isMailerDebugEnabled) {
97
- transportConfig.debug = true;
98
- }
99
-
100
- if (isMailerLogEnabled) {
101
- transportConfig.logger = true;
102
- }
103
-
104
- const transporter = nodemailer.createTransport(transportConfig);
105
-
106
- await transporter.sendMail({
107
- from: options.from,
108
- to: options.to,
109
- subject: options.subject,
110
- html: options.html,
111
- cc: options.cc,
112
- attachments: options.attachments,
113
- });
114
- } catch (error) {
115
- console.log(error, '<<<<<<<<<< error caught from @tomei/mailer package');
116
- throw error;
117
- }
118
- }
119
- }
120
-
121
- export { nodemailer };
1
+ import { ComponentConfig } from '@tomei/config';
2
+ import { MailerBase } from './mailer.base';
3
+ import * as nodemailer from 'nodemailer';
4
+ import { ISendMailConfig } from 'src/interfaces/mail-config.interface';
5
+
6
+ interface TransportConfig {
7
+ host: string;
8
+ port: number;
9
+ secure: boolean;
10
+ auth: {
11
+ user: string;
12
+ pass: string;
13
+ };
14
+ tls?: any;
15
+ debug?: boolean;
16
+ logger?: boolean;
17
+ }
18
+
19
+ export class SMTPMailer extends MailerBase {
20
+ async sendMail(options: ISendMailConfig): Promise<void> {
21
+ try {
22
+ const host = ComponentConfig.getComponentConfigValue(
23
+ '@tomei/mailer',
24
+ 'host',
25
+ );
26
+ const port = ComponentConfig.getComponentConfigValue(
27
+ '@tomei/mailer',
28
+ 'port',
29
+ );
30
+ const secure = ComponentConfig.getComponentConfigValue(
31
+ '@tomei/mailer',
32
+ 'secure',
33
+ );
34
+ const user = ComponentConfig.getComponentConfigValue(
35
+ '@tomei/mailer',
36
+ 'user',
37
+ );
38
+ const pass = ComponentConfig.getComponentConfigValue(
39
+ '@tomei/mailer',
40
+ 'pass',
41
+ );
42
+ const tlsOptions = ComponentConfig.getComponentConfigValue(
43
+ '@tomei/mailer',
44
+ 'tlsOptions',
45
+ );
46
+ const isMailerDebugEnabled = ComponentConfig.getComponentConfigValue(
47
+ '@tomei/mailer',
48
+ 'isMailerDebugEnabled',
49
+ );
50
+ const isMailerLogEnabled = ComponentConfig.getComponentConfigValue(
51
+ '@tomei/mailer',
52
+ 'isMailerLogEnabled',
53
+ );
54
+
55
+ const testReceipientEmail = ComponentConfig.getComponentConfigValue(
56
+ '@tomei/mailer',
57
+ 'testReceipientEmail',
58
+ );
59
+
60
+ const environment = ComponentConfig.getComponentConfigValue(
61
+ '@tomei/mailer',
62
+ 'environment',
63
+ );
64
+
65
+ if (
66
+ !host ||
67
+ !port ||
68
+ !user ||
69
+ !pass ||
70
+ !environment ||
71
+ !testReceipientEmail
72
+ ) {
73
+ throw new Error(
74
+ 'Host, port, user, pass, environment, or test receipient email is not defined in the configuration.',
75
+ );
76
+ }
77
+
78
+ if (environment !== 'production') {
79
+ options.to = testReceipientEmail;
80
+ }
81
+
82
+ const transportConfig: TransportConfig = {
83
+ host,
84
+ port,
85
+ secure,
86
+ auth: {
87
+ user,
88
+ pass,
89
+ },
90
+ };
91
+
92
+ if (tlsOptions) {
93
+ transportConfig.tls = tlsOptions;
94
+ }
95
+
96
+ if (isMailerDebugEnabled) {
97
+ transportConfig.debug = true;
98
+ }
99
+
100
+ if (isMailerLogEnabled) {
101
+ transportConfig.logger = true;
102
+ }
103
+
104
+ const transporter = nodemailer.createTransport(transportConfig);
105
+
106
+ await transporter.sendMail({
107
+ from: options.from,
108
+ to: options.to,
109
+ subject: options.subject,
110
+ html: options.html,
111
+ cc: options.cc,
112
+ attachments: options.attachments,
113
+ });
114
+ } catch (error) {
115
+ console.log(error, '<<<<<<<<<< error caught from @tomei/mailer package');
116
+ throw error;
117
+ }
118
+ }
119
+ }
120
+
121
+ export { nodemailer };