-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
439 lines (389 loc) · 14.5 KB
/
app.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
const axios = require("axios");
const qs = require("query-string");
const morgan = require("morgan");
const express = require("express");
const rateLimit = require("express-rate-limit");
const helmet = require("helmet");
const mongosanitize = require("express-mongo-sanitize");
const bodyParser = require("body-parser");
const WorkOS = require("@workos-inc/node");
const catchAsync = require("./utils/catchAsync");
const jsforce = require("jsforce");
const passport = require("passport");
const xss = require("xss-clean");
const globalErrorHandler = require("./controllers/errController");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const jwt = require("jsonwebtoken");
const session = require("cookie-session");
const Event = require("./models/eventModel");
var request = require("superagent");
const querystring = require("querystring");
const uploadRoutes = require("./routes/uploadRoutes");
const userRoutes = require("./routes/userRoutes");
const registrationRoutes = require("./routes/registrationRoutes");
const communityRoutes = require("./routes/communityRoutes");
const globalRoutes = require("./routes/globalRoutes");
const feedbackRoutes = require("./routes/feedbackRoutes");
const authRoutes = require("./routes/authRoutes");
const salesDepartmentRoutes = require("./routes/salesDepartmentRoutes");
const customPlanRoutes = require("./routes/customPlanRoutes");
const eventRoutes = require("./routes/eventRoutes");
const sessionRoutes = require("./routes/sessionRoutes");
const speakerRoutes = require("./routes/speakerRoutes");
const boothRoutes = require("./routes/boothRoutes");
const sponsorRoutes = require("./routes/sponsorRoutes");
const ticketRoutes = require("./routes/ticketRoutes");
const stripeRoutes = require("./routes/stripeRoutes");
const razorpayRoutes = require("./routes/razorpayRoutes.js");
const teamInvite = require("./routes/teamInviteRoutes");
const demoRoutes = require("./routes/demoRoutes");
const newsletterRoutes = require("./routes/newsletterRoutes");
const contactUsRoutes = require("./routes/contactUsRoutes");
const twillioRoutes = require("./routes/twillioRoutes");
const fundTransferRoutes = require("./routes/fundTransferRoutes");
const communityPlanRoutes = require("./routes/communityPlanRoutes");
const affiliateRoutes = require("./routes/affiliateRoutes");
const interestedPeopleRoutes = require("./routes/interestedPeopleRoutes");
const paypalRoutes = require("./routes/payPalRoutes");
const zapierRoutes = require("./routes/zapierRoutes");
const roleRoutes = require("./routes/roleRoutes");
const mailRoutes = require("./routes/mailRoutes");
const trackRoutes = require("./routes/trackRoutes");
const reviewRoutes = require("./routes/reviewRoutes");
const recordingRoutes = require("./routes/recordingRoutes");
const authController = require("./controllers/authController.js");
const MailChimp = require("./models/mailChimpModel");
const SalesForce = require("./models/salesForceModel");
const Hubspot = require("./models/hubspotModel");
const Community = require("./models/communityModel");
const { promisify } = require("util");
require("./services/passport");
const app = express();
const urlToGetLinkedInAccessToken =
"https://www.linkedin.com/oauth/v2/accessToken";
const urlToGetUserProfile =
"https://api.linkedin.com/v2/me?projection=(id,localizedFirstName,localizedLastName,profilePicture(displayImage~digitalmediaAsset:playableStreams))";
const urlToGetUserEmail =
"https://api.linkedin.com/v2/clientAwareMemberHandles?q=members&projection=(elements*(primary,type,handle~))";
const MAILCHIMP_CLIENT_ID = "919814706970";
const MAILCHIMP_CLIENT_SECRET =
"3837302297576b7845b5ced8bd4691bb69ac7b8c5f90645887";
const BASE_URL =
process.env.NODE_ENV === "development"
? "http://127.0.0.1:3000"
: "https://api.bluemeet.in";
const OAUTH_CALLBACK = `http://127.0.0.1:3001/bluemeet/redirect`;
app.use(
cors({
origin: [
"http://127.0.0.1:3001",
"http://localhost:3001",
"https://www.bluemeet.in",
"https://bluemeet.in",
"https://zapier.com",
"https://www.zapier.com",
],
methods: ["GET", "PATCH", "POST", "DELETE", "PUT"],
credentials: true,
})
);
app.use(cookieParser());
// Use JSON parser for all non-webhook routes
app.use(
express.json({
verify: (req, res, buf) => {
const url = req.originalUrl;
if (
url.startsWith("/api-eureka/eureka/v1/stripe/eventTicketPurchased") ||
url.startsWith("/api-eureka/eureka/v1/stripe/eventPurchaseFailed")
) {
req.rawBody = buf.toString();
}
},
})
);
// Setup express response and body parser configurations
app.use(express.json());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
session({
secret: "keyboard cat",
proxy: true,
resave: true,
saveUnintialized: true,
cookie: {
secure: false,
},
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(helmet());
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
const limiter = rateLimit({
max: 100000,
windowMs: 60 * 60 * 1000, // In one hour
message: "Too many Requests from this IP, please try again in an hour!",
});
app.use("/eureka", limiter);
app.use(express.json({ limit: "10kb" }));
app.use(
express.urlencoded({
extended: true,
})
);
app.use(mongosanitize());
app.use(xss());
app.get("/api-eureka/getUserCredentials", (req, res, next) => {
const code = req.query.code;
console.log(code);
let userProfile = {};
let accessToken = null;
const config = {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
};
const parameters = {
grant_type: "authorization_code",
code: code,
redirect_uri: "https://bluemeet.in/signin",
client_id: "7761igbj4li3f2",
client_secret: "aitQmTG7uYvSkaRZ",
};
console.log(urlToGetLinkedInAccessToken, qs.stringify(parameters), config);
axios
.post(urlToGetLinkedInAccessToken, qs.stringify(parameters), config)
.then((response) => {
accessToken = response.data.access_token;
const config = {
headers: {
Authorization: `Bearer ${accessToken}`,
},
};
axios
.get(urlToGetUserProfile, config)
.then((response) => {
userProfile.firstName = response.data["localizedFirstName"];
userProfile.lastName = response.data["localizedLastName"];
userProfile.image =
response.data.profilePicture[
"displayImage~"
].elements[0].identifiers[0].identifier;
userProfile.linkedinId = response.data.id;
axios
.get(urlToGetUserEmail, config)
.then((response) => {
userProfile.email =
response.data.elements[0]["handle~"].emailAddress;
res.status(200).json({ userProfile });
})
.catch((error) => next(error));
})
.catch((error) => next(error));
})
.catch((err) => {
console.log(err);
next(err);
});
});
app.use("/api-eureka/eureka/v1/auth", authRoutes);
app.use("/api-eureka/eureka/v1/upload", uploadRoutes);
app.use("/api-eureka/eureka/v1/users", userRoutes);
app.use("/api-eureka/eureka/v1/registrations", registrationRoutes);
app.use("/api-eureka/eureka/v1/community", communityRoutes);
app.use("/api-eureka/eureka/v1/feedback", feedbackRoutes);
app.use("/api-eureka/eureka/v1/sales", salesDepartmentRoutes);
app.use("/api-eureka/eureka/v1/customPlan", customPlanRoutes);
app.use("/api-eureka/eureka/v1/events", eventRoutes);
app.use("/api-eureka/eureka/v1/sessions", sessionRoutes);
app.use("/api-eureka/eureka/v1/speakers", speakerRoutes);
app.use("/api-eureka/eureka/v1/booths", boothRoutes);
app.use("/api-eureka/eureka/v1/sponsors", sponsorRoutes);
app.use("/api-eureka/eureka/v1/stripe", stripeRoutes);
app.use("/api-eureka/eureka/v1/razorpay", razorpayRoutes);
app.use("/api-eureka/eureka/v1/tickets", ticketRoutes);
app.use("/api-eureka/eureka/v1/team-invites", teamInvite);
app.use("/api-eureka/eureka/v1/demo", demoRoutes);
app.use("/api-eureka/eureka/v1/newsletter", newsletterRoutes);
app.use("/api-eureka/eureka/v1/contactUs", contactUsRoutes);
app.use("/api-eureka/eureka/v1/twillio", twillioRoutes);
app.use("/api-eureka/eureka/v1/fund", fundTransferRoutes);
app.use("/api-eureka/eureka/v1/communityPlan", communityPlanRoutes);
app.use("/api-eureka/eureka/v1/affiliate", affiliateRoutes);
app.use("/api-eureka/eureka/v1/interestedPeople", interestedPeopleRoutes);
app.use("/api-eureka/eureka/v1/paypal", paypalRoutes);
app.use("/api-eureka/eureka/v1/zapier", zapierRoutes);
app.use("/api-eureka/eureka/v1/role", roleRoutes);
app.use("/api-eureka/eureka/v1/mail", mailRoutes);
app.use("/api-eureka/eureka/v1/track", trackRoutes);
app.use("/api-eureka/eureka/v1/review", reviewRoutes);
app.use("/api-eureka/eureka/v1/recording", recordingRoutes);
const signToken = (id) => jwt.sign({ id }, process.env.JWT_SECRET);
app.get("/api-eureka/eureka/v1/current_user", (req, res) => {
const token = signToken(req.user._id);
res.send({ user: req.user, token: token });
});
app.get("/api-eureka/eureka/v1/auth/mailchimp", (req, res, next) => {
res.redirect(
`https://login.mailchimp.com/oauth2/authorize?${querystring.stringify({
response_type: "code",
client_id: MAILCHIMP_CLIENT_ID,
redirect_uri: OAUTH_CALLBACK,
})}`
);
});
app.get("/api-eureka/eureka/v1/oauth/mailchimp/callback", (req, res, next) => {
const communityId = req.query.communityId;
let accessToken = null;
const config = {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
};
const parameters = {
grant_type: "authorization_code",
code: req.query.code,
client_id: MAILCHIMP_CLIENT_ID,
client_secret: MAILCHIMP_CLIENT_SECRET,
redirect_uri: OAUTH_CALLBACK,
};
axios
.post(
"https://login.mailchimp.com/oauth2/token",
qs.stringify(parameters),
config
)
.then((response) => {
accessToken = response.data.access_token;
axios
.get("https://login.mailchimp.com/oauth2/metadata", {
headers: {
Authorization: `OAuth ${accessToken}`,
},
})
.then((metadataResponse) => {
Community.findById(communityId)
.then((community) => {
const isConnectedMailChimp = community.isConnectedMailChimp;
if (isConnectedMailChimp) {
//
res.status(200).json({
status: "success",
data: community,
});
} else {
MailChimp.create({
communityId: community._id,
accessToken,
server: metadataResponse.data.dc,
apiEndPoint: metadataResponse.data.api_endpoint,
})
.then(async () => {
community.isConnectedMailChimp = true;
// const [a] = community;
const updatedCommunity = await community.save({
new: true,
validateModifiedOnly: true,
});
res.status(200).json({
status: "success",
data: updatedCommunity,
});
})
.catch((error) => next(error));
}
})
.catch((error) => next(error));
})
.catch((error) => next(error));
});
});
app.get(
"/api-eureka/eureka/v1/fetchMailChimpAudiences",
async (req, res, next) => {
// 1. find by community id by event id
console.log(req.query, "i am counting on you eventid");
const eventData = await Event.findById(req.query.eventId);
//2. find the mailchimp account with community id
const communityId = eventData.communityId;
console.log(communityId, "i am counting on you communityId");
const mailChimpData = await MailChimp.findOne({ communityId });
//3. dynamically form request for fetching list
// fetch(`https://${mailChimpData.server}.api.mailchimp.com/3.0/lists`)
// https://us5.api.mailchimp.com
request
.get(mailChimpData.apiEndPoint + "/3.0/lists")
.set("Accept", "application/json")
.set("Authorization", "OAuth " + mailChimpData.accessToken)
.end((err, result) => {
if (err) {
res.status(500).json(err);
} else {
res.json(result.body.lists);
}
});
}
);
app.get("/api-eureka/eureka/v1/logout", (req, res) => {
req.logout();
res.redirect("/");
});
app.use("/api-eureka/eureka/v1", globalRoutes);
app.use(globalErrorHandler);
app.get("/api-eureka/eureka/v1/auth/salesforce", function (req, res, next) {
const oauth2 = new jsforce.OAuth2({
clientId: process.env.SALESFORCE_CLIENT_ID,
clientSecret: process.env.SALESFORCE_CLIENT_SECRET_ID,
redirectUri: "http://localhost:3001/bluemeet/salesforce/redirect",
});
res.redirect(oauth2.getAuthorizationUrl({}));
});
app.get(
"/api-eureka/eureka/v1/oauth/salesforce/callback",
(req, response, next) => {
const oauth2 = new jsforce.OAuth2({
clientId: process.env.SALESFORCE_CLIENT_ID,
clientSecret: process.env.SALESFORCE_CLIENT_SECRET_ID,
redirectUri: "http://localhost:3001/bluemeet/salesforce/redirect",
});
const conn = new jsforce.Connection({ oauth2: oauth2 });
conn.authorize(req.query.code, function (err, userInfo) {
if (err) {
return console.error(err);
}
const conn2 = new jsforce.Connection({
instanceUrl: conn.instanceUrl,
accessToken: conn.accessToken,
});
conn2.identity(function (err, res) {
if (err) {
return console.error(err);
}
Community.findById(req.query.communityId)
.then((community) => {
SalesForce.create({
communityId: req.query.communityId,
accessToken: conn.accessToken,
instanceUrl: conn.instanceUrl,
refreshToken: conn.refreshToken,
})
.then(async () => {
community.isConnectedSalesforce = true;
const updatedCommunity = await community.save({
new: true,
validateModifiedOnly: true,
});
response.status(200).json({
status: "success",
data: updatedCommunity,
});
})
.catch((err) => next(err));
})
.catch((err) => next(err));
});
});
}
);
module.exports = app;