@betterinternship/core 2.5.6 → 2.5.7

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.
@@ -1,95 +1,95 @@
1
- import { SendEmailCommand } from '@aws-sdk/client-ses';
2
- import { SESClient } from '@aws-sdk/client-ses';
3
- import * as nodemailer from 'nodemailer';
4
- import { v4 } from 'uuid';
5
- import { ENV } from '../env.js';
6
- const SMTP_EMAIL = ENV.SMTP_EMAIL;
7
- const SMTP_PASSWORD = ENV.SMTP_PASSWORD;
8
- if (!SMTP_EMAIL || !SMTP_PASSWORD)
9
- throw Error('[ERROR:ENV]: Missing SMTP setup.');
10
- const AWS_ACCESS_KEY_ID = ENV.AWS_ACCESS_KEY_ID;
11
- const AWS_SECRET_ACCESS_KEY = ENV.AWS_SECRET_ACCESS_KEY;
12
- const AWS_REGION = ENV.AWS_REGION;
13
- const AWS_DISABLED = !AWS_ACCESS_KEY_ID || !AWS_SECRET_ACCESS_KEY || !AWS_REGION;
14
- if (AWS_DISABLED)
15
- console.error('[ERROR:ENV]: Missing Amazon setup.');
16
- export const sesClient = new SESClient({
17
- region: ENV.AWS_REGION,
18
- });
19
- const transporter = nodemailer.createTransport({
20
- service: 'Gmail',
21
- host: 'smtp.gmail.com',
22
- port: 465,
23
- secure: true,
24
- auth: {
25
- user: SMTP_EMAIL,
26
- pass: SMTP_PASSWORD,
27
- },
28
- messageId: `${v4()}${SMTP_EMAIL}`,
29
- });
30
- const onEmailError = (recipient, resolve) => (error, info) => {
31
- if (error) {
32
- console.error('[ERROR:EMAIL] Could not send email.');
33
- console.error('[-----] ' + error.message);
34
- }
35
- else {
36
- resolve({
37
- messageId: info.messageId,
38
- response: info.response,
39
- });
40
- console.warn('[EMAIL] Email sent to ' + recipient);
41
- }
42
- };
43
- const traditionalEmailRoute = async ({ sender, subject, recipient, content, alias = 'hello', }) => {
44
- return new Promise((resolve) => {
45
- transporter.sendMail({
46
- from: `"${sender ?? 'BetterInternship'}" <${alias}@betterinternship.com>`,
47
- to: recipient,
48
- subject,
49
- html: content,
50
- }, onEmailError(recipient, resolve));
51
- });
52
- };
53
- const amazonEmailRoute = async ({ sender, subject, recipient, content, alias = 'hello', }) => {
54
- const params = {
55
- Destination: {
56
- ToAddresses: [recipient],
57
- },
58
- Message: {
59
- Subject: { Data: subject },
60
- Body: {
61
- Html: { Data: content },
62
- },
63
- },
64
- Source: `"${sender ?? 'BetterInternship'}" <${alias}@betterinternship.com>`,
65
- };
66
- try {
67
- const command = new SendEmailCommand(params);
68
- const result = await sesClient.send(command);
69
- console.warn('[AWSSES] Email sent to ' + recipient);
70
- return {
71
- messageId: result.MessageId,
72
- response: 'Successfully sent via Amazon SES.',
73
- };
74
- }
75
- catch (error) {
76
- console.error('[ERROR:AWSSES] Could not send email.');
77
- console.error('[-----] ' + error);
78
- }
79
- };
80
- export const sendSingleEmail = async (params) => {
81
- if (AWS_DISABLED) {
82
- return await traditionalEmailRoute(params);
83
- }
84
- else {
85
- const dice = Math.random();
86
- const threshold = 0;
87
- if (dice < threshold) {
88
- return await traditionalEmailRoute(params);
89
- }
90
- else {
91
- return await amazonEmailRoute(params);
92
- }
93
- }
94
- };
1
+ import { SendEmailCommand } from '@aws-sdk/client-ses';
2
+ import { SESClient } from '@aws-sdk/client-ses';
3
+ import * as nodemailer from 'nodemailer';
4
+ import { v4 } from 'uuid';
5
+ import { ENV } from '../env.js';
6
+ const SMTP_EMAIL = ENV.SMTP_EMAIL;
7
+ const SMTP_PASSWORD = ENV.SMTP_PASSWORD;
8
+ if (!SMTP_EMAIL || !SMTP_PASSWORD)
9
+ throw Error('[ERROR:ENV]: Missing SMTP setup.');
10
+ const AWS_ACCESS_KEY_ID = ENV.AWS_ACCESS_KEY_ID;
11
+ const AWS_SECRET_ACCESS_KEY = ENV.AWS_SECRET_ACCESS_KEY;
12
+ const AWS_REGION = ENV.AWS_REGION;
13
+ const AWS_DISABLED = !AWS_ACCESS_KEY_ID || !AWS_SECRET_ACCESS_KEY || !AWS_REGION;
14
+ if (AWS_DISABLED)
15
+ console.error('[ERROR:ENV]: Missing Amazon setup.');
16
+ export const sesClient = new SESClient({
17
+ region: ENV.AWS_REGION,
18
+ });
19
+ const transporter = nodemailer.createTransport({
20
+ service: 'Gmail',
21
+ host: 'smtp.gmail.com',
22
+ port: 465,
23
+ secure: true,
24
+ auth: {
25
+ user: SMTP_EMAIL,
26
+ pass: SMTP_PASSWORD,
27
+ },
28
+ messageId: `${v4()}${SMTP_EMAIL}`,
29
+ });
30
+ const onEmailError = (recipient, resolve) => (error, info) => {
31
+ if (error) {
32
+ console.error('[ERROR:EMAIL] Could not send email.');
33
+ console.error('[-----] ' + error.message);
34
+ }
35
+ else {
36
+ resolve({
37
+ messageId: info.messageId,
38
+ response: info.response,
39
+ });
40
+ console.warn('[EMAIL] Email sent to ' + recipient);
41
+ }
42
+ };
43
+ const traditionalEmailRoute = async ({ sender, subject, recipient, content, alias = 'hello', }) => {
44
+ return new Promise((resolve) => {
45
+ transporter.sendMail({
46
+ from: `"${sender ?? 'BetterInternship'}" <${alias}@betterinternship.com>`,
47
+ to: recipient,
48
+ subject,
49
+ html: content,
50
+ }, onEmailError(recipient, resolve));
51
+ });
52
+ };
53
+ const amazonEmailRoute = async ({ sender, subject, recipient, content, alias = 'hello', }) => {
54
+ const params = {
55
+ Destination: {
56
+ ToAddresses: [recipient],
57
+ },
58
+ Message: {
59
+ Subject: { Data: subject },
60
+ Body: {
61
+ Html: { Data: content },
62
+ },
63
+ },
64
+ Source: `"${sender ?? 'BetterInternship'}" <${alias}@betterinternship.com>`,
65
+ };
66
+ try {
67
+ const command = new SendEmailCommand(params);
68
+ const result = await sesClient.send(command);
69
+ console.warn('[AWSSES] Email sent to ' + recipient);
70
+ return {
71
+ messageId: result.MessageId,
72
+ response: 'Successfully sent via Amazon SES.',
73
+ };
74
+ }
75
+ catch (error) {
76
+ console.error('[ERROR:AWSSES] Could not send email.');
77
+ console.error('[-----] ' + error);
78
+ }
79
+ };
80
+ export const sendSingleEmail = async (params) => {
81
+ if (AWS_DISABLED) {
82
+ return await traditionalEmailRoute(params);
83
+ }
84
+ else {
85
+ const dice = Math.random();
86
+ const threshold = 0;
87
+ if (dice < threshold) {
88
+ return await traditionalEmailRoute(params);
89
+ }
90
+ else {
91
+ return await amazonEmailRoute(params);
92
+ }
93
+ }
94
+ };
95
95
  //# sourceMappingURL=email.js.map
package/dist/lib/env.js CHANGED
@@ -1,4 +1,4 @@
1
- import * as dotenv from "dotenv";
2
- dotenv.config();
3
- export const ENV = process.env;
1
+ import * as dotenv from "dotenv";
2
+ dotenv.config();
3
+ export const ENV = process.env;
4
4
  //# sourceMappingURL=env.js.map