-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
177 lines (141 loc) · 4.09 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
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
import express from 'express';
//# TO-DO : import these too? why am i doing this different?
const mongo = require('./mongoCommunication');
const notifications = require('./notifications');
const sms = require('./sms');
const app = express();
app.use(express.json());
//#TO-DO : only allow api access from my app.
//GET CALLS
app.get('/', (req, res) => {
res.send('Server Running');
});
//uuid refers to users uuid
app.get('/getContacts/:uuid', (req, res) => {
mongo.getContacts(req.params.uuid)
.then((_response) => {
console.log('Contacts retrieved');
res.json({success: true, contacts: _response});
})
.catch((_error) => {
console.log('Contact retrieval failed.');
res.status(400).send({
success: false,
message: 'Getting contacts failed: ' + _error
});
});
});
//uuid refers to users uuid, contactUuid refers to... you get it
app.get('/getContacts/:uuid/specificContact/:contactUuid', (req, res) => {
mongo.getContact(req.params.uuid, req.params.contactUuid)
.then((_response) => {
console.log('Contact retrieved');
res.json({success: true, contact: _response});
})
.catch((_error) => {
console.log('Contact retrieval failed.');
res.status(400).send({
success: false,
message: 'Getting contacts failed: ' + _error
});
});
});
//uuid refers to users uuid
app.get('/getHookupDetails/:uuid', (req, res) => {
mongo.getHookupDetails(req.params.uuid)
.then((_response) => {
console.log('Hookup Details Claimed: ', _response);
res.json({
success: true,
isHookingUp: _response.isHookingUp,
hookUpDetails: _response.hookUpDetails,
});
})
.catch((_error) => {
console.log('Hookup details could not be gotten.');
res.status(400).send({
success: false,
message: 'Getting hookup failed: ' + _error
});
});
});
//POST CALLS
app.post('/endHookup', (req, res) => {
mongo.endHookup(req.body.uuid)
.then((_response) => {
console.log('Hookup Ended');
res.json({success: true});
})
.catch((_error) => {
console.log('User registration failed.');
res.status(400).send({
success: false,
message: 'User registration failed: ' + _error
});
});
});
app.post('/register', (req, res) => {
mongo.register(req.body.user.username, req.body.user.password, req.body.token.value)
.then((_response) => {
console.log('User Registered');
res.json({success: true, uuid: _response});
})
.catch((_error) => {
console.log('User registration failed.');
res.status(400).send({
success: false,
message: 'User registration failed: ' + _error
});
});
});
app.post('/login', (req, res) => {
mongo.login(req.body.user.username, req.body.user.password, req.body.token.value)
.then((_response) => {
console.log('User logged in');
res.json({success: true, uuid: _response});
})
.catch((_error) => {
console.log('User login failed.');
res.status(400).send({
success: false,
message: 'User login failed: ' + _error
});
});
});
app.post('/addEditContact', (req, res) => {
mongo.addEditContact(req.body.uuid, req.body.contactDetails)
.then((_response) => {
console.log('Contacts updated');
res.json({success: true});
})
.catch((_error) => {
console.log('Contact could not be added');
res.status(400).send({
success: false,
message: 'Contact could not be added: ' + _error
});
});
});
app.post('/sendMessage', (req, res) => {
console.log('sending sms');
sms.sendSMS('NUMBER', 'hello');
});
app.post('/startHookup', (req, res) => {
mongo.startHookup(req.body.uuid, req.body.hookupDetails)
.then((_response) => {
console.log('hookupHasBegun');
notifications.startTimer(req.body.uuid, req.body.hookupDetails.checkInTime);
res.json({success: true});
})
.catch((_error) => {
console.log('Hookup could not be started');
res.status(400).send({
success: false,
message: 'Hookup could not be started: ' + _error
});
});
});
const PORT_NUMBER = 80;
app.listen(PORT_NUMBER, () => {
console.log(`Server online! @ Port: ${PORT_NUMBER}`);
});