Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 5d59cc9

Browse files
committed
server api and hooks
1 parent 94f727c commit 5d59cc9

File tree

7 files changed

+143
-10
lines changed

7 files changed

+143
-10
lines changed

package-lock.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@topcoder/micro-frontends-community-admin-app",
33
"scripts": {
4-
"start": "node server.js",
4+
"start": "node --experimental-json-modules server.js",
55
"dev": "webpack-dev-server --port 8505 --host 0.0.0.0",
66
"dev-https": "webpack-dev-server --https --port 8505 --host 0.0.0.0",
77
"build": "webpack --mode=${APPMODE:-production} --env.config=${APPENV:-prod}",
@@ -65,6 +65,7 @@
6565
"express": "^4.17.1",
6666
"final-form": "^4.20.1",
6767
"immutability-helper": "^3.1.1",
68+
"isomorphic-fetch": "^3.0.0",
6869
"lodash": "^4.17.20",
6970
"moment": "^2.29.1",
7071
"moment-timezone": "^0.5.33",
@@ -94,5 +95,6 @@
9495
"last 1 version",
9596
"> 1%",
9697
"IE 11"
97-
]
98+
],
99+
"type": "module"
98100
}

server.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
/**
33
* Community Admin backend server
44
*/
5-
const express = require("express");
6-
// const betaTesters = require("./src/api/routes/betaTesters");
5+
import express from "express";
6+
import bodyParser from "body-parser";
7+
// import betaTesters from "./src/api/routes/betaTesters.js";
8+
import recruitHooks from "./src/api/routes/recruitHooks.js";
79

810
const app = express();
911

@@ -23,10 +25,11 @@ app.use(
2325
);
2426

2527
// plugins
26-
// app.use(bodyParser.json());
28+
app.use(bodyParser.json());
2729

2830
// API routes
29-
// app.use("/api/testers", betaTesters);
31+
// app.use("/api/testers", betaTesters); TODO for v2
32+
app.use("/api/hooks/", recruitHooks);
3033

3134
// ping route
3235
app.get("/", function (req, res) {

src/api/routes/betaTesters.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
*/
44

55
import express from "express";
6-
import { getMembers } from "../services/betaTesters";
7-
8-
const cors = require("cors");
6+
import { getMembers } from "../services/betaTesters.js";
7+
import cors from "cors";
98

109
const routes = express.Router();
1110

src/api/routes/recruitHooks.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* The routes related to beta testers integration
3+
*/
4+
5+
import express from "express";
6+
import cors from "cors";
7+
import { sendEmailDirect } from "../services/sendGrid.js";
8+
9+
const routes = express.Router();
10+
11+
// Enables CORS on those routes according config above
12+
// ToDo configure CORS for set of our trusted domains
13+
routes.use(cors());
14+
routes.options("*", cors());
15+
16+
routes.post("/recruit", (req, res) => {
17+
sendEmailDirect({
18+
personalizations: [
19+
{
20+
to: [{ email: "[email protected]" }],
21+
subject: "Recruit hook payload",
22+
},
23+
],
24+
from: {
25+
26+
},
27+
content: [
28+
{
29+
type: "text/plain",
30+
value: JSON.stringify(req.body),
31+
},
32+
],
33+
}).then((result) => {
34+
res.send({});
35+
});
36+
});
37+
38+
export default routes;

src/api/services/sendGrid.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Server-side functions necessary for sending emails via Sendgrid APIs
3+
*/
4+
/* global process */
5+
import fetch from "isomorphic-fetch";
6+
7+
/**
8+
* Sends emails via the Sendgrid API
9+
* https://sendgrid.com/docs/api-reference/
10+
* @param {Object} req the request
11+
* @param {Object} res the response
12+
*/
13+
export const sendEmail = async (req, res) => {
14+
try {
15+
const msg = req.body;
16+
const response = await fetch("https://api.sendgrid.com/v3/mail/send", {
17+
method: "POST",
18+
headers: {
19+
"Content-Type": "application/json",
20+
Authorization: `Bearer ${process.env.SENDGRID_API_KEY}`,
21+
},
22+
body: JSON.stringify(msg),
23+
});
24+
res.status(response.status);
25+
return {};
26+
} catch (error) {
27+
const { message, code, response } = error;
28+
res.status(code || 500);
29+
if (error.response) {
30+
const { headers, body } = response;
31+
return {
32+
message,
33+
headers,
34+
body,
35+
};
36+
}
37+
return { message };
38+
}
39+
};
40+
41+
/**
42+
* Send email directly via the SendGrid API
43+
* @param {Object} msg the payload
44+
* @returns Promise
45+
*/
46+
export const sendEmailDirect = async (msg) => {
47+
try {
48+
const response = await fetch("https://api.sendgrid.com/v3/mail/send", {
49+
method: "POST",
50+
headers: {
51+
"Content-Type": "application/json",
52+
Authorization: `Bearer ${process.env.SENDGRID_API_KEY}`,
53+
},
54+
body: JSON.stringify(msg),
55+
});
56+
return response;
57+
} catch (error) {
58+
const { message, code, response } = error;
59+
if (error.response) {
60+
const { headers, body } = response;
61+
return {
62+
code,
63+
message,
64+
headers,
65+
body,
66+
};
67+
}
68+
return { message };
69+
}
70+
};
71+
72+
export default undefined;

src/components/NoAccessPage/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const NoAccessPage = () => {
1212
<Page title="Forbidden">
1313
<PageHeader title="Forbidden Area" />
1414
<h4 styleName="msg">
15-
Sorry, looks that you not autorized to access this page :(
15+
Sorry, looks that you not authorized to access this page :(
1616
</h4>
1717
<p>
1818
If you feel this is an error, contact{" "}

0 commit comments

Comments
 (0)