-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (110 loc) · 3.5 KB
/
index.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
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const app = express();
const socketio = require("socket.io");
const axios = require("axios");
const cookieParser = require("cookie-parser");
const cookieSession = require("cookie-session");
//Routes
const alertsRoute = require("./routes/alerts");
const donationRoute = require("./routes/donation");
const logOutRoute = require("./routes/logout");
const googleLogin = require("./routes/loginWithGoogle");
const userRoute = require("./routes/UserRoute");
const passportSetup = require("./passportSetup/passport-setup");
const passport = require("passport");
const port = process.env.PORT || "8000";
const mongoose = require("mongoose");
const UserData = require("./models/UserData");
const { AlertsModel, AlertsHistoryModel } = require("./models/AlertsData");
const moment = require('moment');
//Mongoose Connect
mongoose
.connect(
process.env.MONGO_URL,
{ useUnifiedTopology: true, useNewUrlParser: true,useFindAndModify: false}
)
.then(() => {
console.log("Database successfully connected");
});
app.use(passport.initialize());
app.use(passport.session());
app.use(cookieParser());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(
cors({
origin: ["http://localhost:3000", "http://your-production-website.com"],
credentials: true,
})
);
app.get("/", (req, res) => {
res.send("Here is home page");
});
//Routes
app.use("/auth", googleLogin);
app.use("/alerts", alertsRoute);
app.use("/donation", donationRoute);
app.use("/user", userRoute);
const server = app.listen(port, "0.0.0.0", () => {
console.log(port);
});
const io = socketio(server);
//Socketio logics
io.on("connect", (socket) => {
socket.on("room", (roomCode) => {
console.log(roomCode,"Id");
socket.join(roomCode);
io.to(roomCode).emit("check", `Hello ${roomCode}`);
socket.on("testing",()=>{
console.log("Testing Connected");
io.to(roomCode).emit("Alert", {
donatorName:"Test",
donationAmount:69,
donationMessage:"This is a test",
});
})
socket.on("Donated", (msg) => {
console.log(msg);
let data = {
token: msg.token,
amount: msg.amount,
};
let config = {
headers: {
Authorization:
process.env.KhaltiSecret,
},
};
axios
.post("https://khalti.com/api/v2/payment/verify/", data, config)
.then(async (response) => {
if (response.data.state.name === "Completed") {
//Saving donation in history
const user = await UserData.findOneAndUpdate({ token: roomCode },{$inc:{amount:msg.donationAmount,totalAmount:msg.donationAmount}});
//^ updating user earned amounts
console.log(moment().format());
new AlertsHistoryModel({
googleId: user.googleId,
donatorName: msg.donatorName,
donationAmount: msg.donationAmount,
donationMessage: msg.donationMessage,
})
.save()
.then(() => {
io.to(roomCode).emit("Alert", msg);
io.to(msg.donationPageId).emit("thanksForDonation", "true,Thanks yrr");
console.log("Completed");
});
}
})
.catch((error) => {
console.log(error, "ERR");
});
});
socket.on("disconnect", () => {
console.log("Disconnected from " + roomCode);
});
});
});