-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
88 lines (78 loc) · 2.64 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
const express = require('express');
const path = require('path');
const gonebusy = require('gonebusy-nodejs-client');
const url = require('url');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');
const router = express.Router();
const moment = require('moment');
/* Loads variables from .env */
dotenv.load();
const { API_KEY, SERVICE_ID, USER_ID, PORT } = process.env;
gonebusy.Configuration.currentEnvironment = 'sandbox';
const authorization = `Token ${API_KEY}`;
const { ServicesController, ResourcesController, BookingsController } = gonebusy;
const app = express();
app.use(bodyParser.json());
app.use('/api', router);
if (process.env.NODE_ENV === 'production') {
const staticPath = path.join(__dirname, '/public');
app.use(express.static(staticPath));
}
router.get('/service', (req, res) => {
ServicesController.getServiceById(authorization, SERVICE_ID).then((success) =>
res.send(success.service)
).catch((error) =>
console.log(error.errorMessage)
);
});
router.get('/resources/:id', (req, res) => {
const { id } = req.params;
ResourcesController.getResourceById(authorization, id).then((success) =>
res.send(success.resource)
).catch((error) =>
console.log(error.errorMessage)
);
});
router.get('/slots', (req, res) => {
const urlParts = url.parse(req.url, true);
const query = urlParts.query;
const startDate = moment(query.startDate);
const endDate = moment(query.endDate);
const resourceId = query.resourceId;
ServicesController.getServiceAvailableSlotsById(
authorization, SERVICE_ID, null, startDate, endDate, resourceId)
.then(success =>
res.send(success.service.resources)
)
.catch((error) =>
console.log(error.errorMessage)
);
});
router.post('/bookings/new', (req, res) => {
const { date, time, duration, resourceId} = req.body;
const createBookingBody = {
date,
time,
duration,
resource_id: resourceId,
service_id: SERVICE_ID,
user_id: USER_ID
};
BookingsController.createBooking(authorization, createBookingBody).then((success) =>
res.send(success)
).catch((error) => {
console.log(error.errorResponse);
res.status(error.errorCode).send(error.errorResponse);
});
});
router.get('/bookings', (req, res) => {
BookingsController.getBookings(authorization, USER_ID).then((success) => {
res.send(success.bookings)
}).catch((error) => {
console.log(error.errorMessage, ' errors retrieving booking');
})
});
app.listen(PORT, () => {
console.log(`listening on port ${PORT}`);
});