-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
110 lines (97 loc) · 2.49 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
// Express for server side scripting
const express = require('express');
// jwt for auth token management
const jwt = require('jsonwebtoken');
// Helmet for http header management and security
const helmet = require('helmet');
// Cross origin resource sharing
const cors = require('cors');
const bodyParser = require('body-parser')
// Environment variable
require('dotenv').config();
// Connecting to database
const DB_HOST = process.env.DB_HOST
const db = require('./db');
db.connect(DB_HOST);
const models = require('./models')
// Setting up GraphQL server
// Apollo server for graph ql
const {ApolloServer} = require('apollo-server-express');
const typeDefs = require('./schema')
const resolvers = require('./resolvers')
const getUser = token=>{
if(token){
try{
return jwt.verify(token,process.env.JWT_SECRET);
}catch (e) {
console.log(e)
throw new Error("Session invalid")
}
}
}
async function startServer(app) {
const server = new ApolloServer({
typeDefs,
resolvers,
context:async ({req})=>{
const token = req.headers.authorization;
const user = getUser(token);
return {models,user};
}
})
await server.start();
server.applyMiddleware({app,path:'/api'});
}
//Express server setup
const port = process.env.PORT || 4000;
const app = express();
app.use(helmet());
app.use(cors());
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
// Todo move this
app.post("/payment/notify",async(req,res)=>{
if(req.body.status_code === '2'){
const bid = await models.JobBid.findByIdAndUpdate(req.body.order_id.substr(1),{
$set:{
state:"paid"
}
})
const jobPosting = await models.JobPosting.findByIdAndUpdate(bid.jobPosting,{
$set:{
state:"completed"
}
})
return res.status(200).json({
"message":"done",
"bid_id":bid._id
})
}
return res.status(400).json({
"message":"Failed"
})
})
app.post("/payment/notify_sr",async (req,res)=>{
if(req.body.status_code === '2'){
const id = req.body.order_id.substr(1)
const sr = await models.ServiceRequests.findByIdAndUpdate(id,{
$set:{
hasPaid:true
}
})
return res.status(200).json({
"message":"done",
"service_request_id":req.body.order_id.substr(1)
})
}
else res.status(400).json({
"message":"Failed"
})
})
startServer(app).then(()=>{
app.listen({port},()=>{
console.log(
`GraphQL Server is running successfully at port ${port}`
)
})
})