forked from cssgunc/react-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
153 lines (132 loc) · 4.59 KB
/
server.js
File metadata and controls
153 lines (132 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import dotenv from "dotenv";
import client from "@mailchimp/mailchimp_marketing";
dotenv.config(); //loading the .env variables
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const CI_IDS = {
binge: "100250717",
party: "100061976",
};
const app = express();
const port = 3000;
app.use(express.json());
client.setConfig({
apiKey: process.env.MAILCHIMP_API_KEY,
server: process.env.MAILCHIMP_SERVER_PREFIX,
});
//GET route to fetch each posted newsletter from mailchimp
app.get("/api/newsletters", async (req, res) => {
try {
//calling the mailchimp api to get a list of all the campaigns
//this will return an object with a 'campaigns' array
const response = await client.campaigns.list({
//setting the number of campaigns to retrieve from the api
count: 100,
//only getting sent campaigns, not drafts or scheduled ones
status: "sent",
//sorting by desc order so the newest comes first
sort_field: "send_time",
sort_dir: "DESC",
});
//extracting the campaigns array from the response
//empty array as safeguard
const campaigns = response.campaigns || [];
//transforming the data into the format needed for frontend
const newsletters = campaigns.map((campaign) => {
//the send time
const sendTime = campaign.send_time;
//the date of the send time
const date = new Date(sendTime);
//formatting the data as m/d/y
const formattedDate = `${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear().toString().slice(-2)}`;
//the new object
return {
id: campaign.id,
title: campaign.settings.title || "Untitled Newsletter",
date: formattedDate,
href: campaign.archive_url || "",
};
});
//success response w/ new newsletter array
res.status(200).json({
success: true,
data: newsletters,
});
} catch (error) {
console.error("Mailchimp Newsletter Fetch Error:", error);
//error response
res.status(500).json({
success: false,
error: error.response?.body || error.message,
});
}
});
//adding a subscriber to the mailchimp newsletter
app.post("/api/add-contact", async (req, res) => {
try {
const { email, firstName, lastName } = req.body;
const response = await client.lists.addListMember(
process.env.MAILCHIMP_AUDIENCE_ID,
{
email_address: email,
status: "subscribed",
merge_fields: {
FNAME: firstName || "",
LNAME: lastName || "",
},
}
);
res.status(200).json({ success: true, data: response });
} catch (error) {
console.error("Mailchimp Error:", error);
res.status(500).json({
success: false,
error: error.response?.body || error.message,
});
}
});
app.use(express.static(path.join(__dirname, "build")));
app.use(express.json());
if (process.env.NODE_ENV !== "production") {
app.listen(port, () => {
console.log(`Server is listening at http://localhost:${port}`);
});
}
//get request for clear impact data for data page
app.get("/api/ci/:which", async (req, res) => {
const which = req.params.which;
const id = CI_IDS[which];
if (!id) return res.status(400).json({ error: "unknown dataset" });
try {
const body = {
siteCode: process.env.CI_SITE_CODE,
apiKey: process.env.CI_API_KEY,
ID: id,
};
const r = await fetch(
"https://api.resultsscorecard.com/api/datavalues/export",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"User-Agent": "curl/8.7.1",
},
body: JSON.stringify(body),
}
);
const text = await r.text();
if (!r.ok)
return res
.status(r.status)
.type("application/json")
.send(text || "");
res.type("application/json").send(text || "{}");
} catch {
res.status(500).json({ error: "proxy error" });
}
});
export default app;