Skip to content

Commit 89b6d11

Browse files
committed
Progress as of today#212
1 parent 8eab4ff commit 89b6d11

File tree

4 files changed

+63
-288
lines changed

4 files changed

+63
-288
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"@babel/plugin-transform-runtime": "7.17.0",
5151
"@babel/preset-env": "7.16.11",
5252
"@babel/register": "7.17.0",
53-
"eslint": "8.8.0",
5453
"eslint-config-airbnb-base": "15.0.0",
5554
"eslint-config-prettier": "8.3.0",
5655
"eslint-config-prettier-check": "1.0.0",
@@ -85,4 +84,4 @@
8584
"!/client/"
8685
]
8786
}
88-
}
87+
}

src/api/routes/newsLetterEmail.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { logger } from '@sentry/utils';
2+
import { Router } from 'express';
3+
import { check, validationResult } from 'express-validator';
4+
import { SEND_EMAIL } from '../../utils/config';
5+
import APICall from '../../utils/api/calls';
6+
7+
const router = Router();
8+
9+
const toEmail = SEND_EMAIL;
10+
11+
router.post(
12+
'/',
13+
[
14+
check('firstName', 'First Name is required').not().isEmpty(),
15+
check('lastName', 'LastName is required').not().isEmpty(),
16+
check('email', 'Email is required').isEmpty(),
17+
],
18+
async (req, res) => {
19+
const errors = validationResult(req);
20+
if (!errors.isEmpty()) {
21+
logger.info(errors);
22+
return res.status(400).json({ message: errors.array() });
23+
}
24+
const content = `Name: ${firstName} ${lastName} \nEmail: ${email}`;
25+
try {
26+
await APICall.sendEmail(
27+
toEmail,
28+
{
29+
name: 'CougarCS Website Newsletter',
30+
email,
31+
},
32+
'New Message from Newsletter',
33+
content
34+
);
35+
} catch (err) {
36+
logger.error(
37+
`${err.status || 500} - ${err.message} - ${req.originalUrl}
38+
- ${req.method} - ${req.ip}`
39+
);
40+
return res.status(500).json({ message: err.message });
41+
}
42+
logger.info('Email sent');
43+
return res.status(200).json({ message: 'Email sent.' });
44+
}
45+
);
46+
export default router;

src/config/app.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import events from '../api/routes/event';
1313
import tutors from '../api/routes/tutors';
1414
import youtube from '../api/routes/youtube';
1515
import payment from '../api/routes/payment';
16+
import newsLetterEmails from '../api/routes/newsLetterEmail';
1617
import { logger } from '../utils/logger/logger';
1718
import { httpLogger } from '../utils/logger/httpLogger';
1819
import { ENABLE_CORS, PROD, SENTRY_URL } from '../utils/config';
@@ -41,9 +42,9 @@ if (PROD) {
4142

4243
const corsOptions = ENABLE_CORS
4344
? {
44-
origin: ['https://cougarcs.com', 'http://localhost:45678'],
45-
methods: ['GET', 'POST'],
46-
}
45+
origin: ['https://cougarcs.com', 'http://localhost:45678'],
46+
methods: ['GET', 'POST'],
47+
}
4748
: '*';
4849

4950
app.use(compression());
@@ -68,6 +69,7 @@ app.use('/api/send', email);
6869
app.use('/api/events', events);
6970
app.use('/api/tutors', tutors);
7071
app.use('/api/youtube', youtube);
72+
app.use('/api/newsLetterEmails', newsLetterEmails);
7173

7274
app.use((req, res) => {
7375
throw new Error(`Invaild Request - Endpoint: ${req.originalUrl}`);
@@ -77,8 +79,7 @@ app.use(Sentry.Handlers.errorHandler());
7779

7880
app.use((err, req, res, next) => {
7981
logger.info(
80-
`${err.status || 500} - ${err.message} - ${req.originalUrl} - ${
81-
req.method
82+
`${err.status || 500} - ${err.message} - ${req.originalUrl} - ${req.method
8283
} - ${req.ip}`
8384
);
8485

0 commit comments

Comments
 (0)