-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
173 lines (148 loc) · 3.7 KB
/
server.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const express = require("express");
const cors = require("cors");
const mailchimp = require("@mailchimp/mailchimp_marketing");
const compression = require("compression");
var cookieParser = require("cookie-parser");
const register = require("./controllers/register");
const link = require("./controllers/link");
const admin = require("./controllers/admin");
const subscriber = require("./controllers/subscriber");
const user = require("./controllers/user");
const authentication = require("./controllers/authentication");
const helper = require("./controllers/helper");
var db = require("knex")({
client: "pg",
connection: process.env.POSTGRES_URI,
});
const app = express();
app.use(cors());
app.use(express.json());
app.use(cookieParser());
app.use(compression());
app.get("/", (req, res) => {
res.send(
"<h1>You are not supposed to be here, but welcome anyway. Dont do anything bad!</h1>"
);
});
// Path to fetch user data from the database
app.post("/getData", async (req, res) => {
try {
const { id } = req.body;
const userData = await helper.getData(id, db);
const token = authentication.generateToken(userData.user);
res.json({ userData, token });
} catch (error) {
console.log(error);
}
});
app.post("/toggleLink", async (req, res) => {
try {
link.toggleLink(req, res, db);
} catch (error) {
console.log(error);
}
});
app.post("/setQuota", async (req, res) => {
try {
admin.setQuota(req, res, db);
} catch (error) {
console.log(error);
}
});
// Path to register user and set new data in database
app.post("/register", async (req, res) => {
try {
register.handleRegistration(req, res, db);
} catch (error) {
console.log(error);
}
});
// Path to save new link's data in database
app.post("/newLink", async (req, res) => {
try {
link.newLink(req, res, db);
} catch (error) {
console.log(error);
}
});
// Path to delete link from Database
app.post("/deleteLink", async (req, res) => {
try {
link.deleteLink(req, res, db);
} catch (error) {
console.log(error);
}
});
// Path to delete subscriber
app.post("/deleteSubscriber", async (req, res) => {
try {
subscriber.deleteSubscriber(req, res, db);
} catch (error) {
console.log(error);
}
});
// Path to get link information
app.post("/getLink", async (req, res) => {
try {
link.getLink(req, res, db);
} catch (error) {
console.log(error);
}
});
// Path to register new subscriber to the database
app.post("/addSub", async (req, res) => {
try {
subscriber.addSubscriber(req, res, db, mailchimp);
} catch (error) {
console.log(error);
}
});
// Creating a new MailChimp list
app.post("/newMailchimpLink", async (req, res) => {
try {
link.addNewMailChimpLink(req, res, db, mailchimp);
} catch (error) {
console.log(error);
}
});
//retreives api key for subscription page
app.post("/getApiKey", async (req, res) => {
try {
user.getApiKey(req, res, db);
} catch (error) {
console.log(error);
}
});
//updates user's profile
app.post("/updateData", async (req, res) => {
try {
user.updateData(req, res, db);
} catch (error) {
console.log(error);
}
});
app.post("/confirmApiKey", async (req, res) => {
try {
user.confirmApiKey(req, res, mailchimp);
} catch (error) {
console.log(error);
}
});
app.post("/customBranding", async (req, res) => {
try {
link.customLink(req, res, db);
} catch (error) {
console.log(error);
}
});
app.post("/getCustomBranding", async (req, res) => {
try {
link.fetchCustomBranding(req, res, db);
} catch (error) {
console.log(error);
}
});
app.listen(process.env.PORT || 3001, () => {
console.log("Running the 22/08/2021 update");
});
//=================