-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
167 lines (143 loc) · 4.45 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
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
require("dotenv").config();
const express = require("express");
const axios = require("axios");
const knex = require("./database");
const dayjs = require("dayjs");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.json());
app.use(cors());
// Validate the request
const _validate = (requestData) => {
// Check key card
if (!requestData?.keyCard || requestData?.keyCard === "") {
throw new Error("key card id was not provided.");
}
// Check if visitor token
if (!requestData?.visitorToken || requestData?.visitorToken === "") {
throw new Error("Visitor token was not provided.");
}
// Check if access point
if (!requestData?.accessPoints) {
throw new Error("Access point was not provided.");
}
// Check if access point is empty
if (requestData?.accessPoints.length === 0) {
throw new Error("Access points is empty");
}
};
// Make request to the access control server
const _makeRequest = (url) => {
console.log(url);
return axios.get(url, {
headers: {
Authorization: `Basic ${Buffer.from(
`${process.env.CLOUD_EXCHANGE_ACCESS_CONTROL_SERVER_USERNAME}:${process.env.CLOUD_EXCHANGE_ACCESS_CONTROL_SERVER_PASSWORD}`
).toString("base64")}`,
},
});
};
// Grant the user access to the device
const grantAccess = async (requestData) => {
_validate(requestData);
const { visitorToken, accessPoints, keyCard } = requestData;
const url = `${
process.env.CLOUD_EXCHANGE_ACCESS_CONTROL_SERVER
}action=assign;device=${accessPoints.toString()};id=${keyCard}`;
// Make request to cloud exchange access control
try {
const requestAccess = await _makeRequest(url);
const successStatus = [200, 201];
if (!successStatus.includes(requestAccess.status)) {
throw new Error("Access control servers not available, try again.");
}
// log the activity
await knex("access_control_log").insert({
visitor_token: visitorToken,
key_card: keyCard,
access_points: JSON.stringify(accessPoints),
time_checked_in: dayjs().format("YYYY-MM-DD HH:mm:ss"),
});
return {
status: requestAccess.status,
data: requestAccess.data,
};
} catch (error) {
console.log(error.message);
throw new Error("An error occured while process..");
}
};
// Revoke user access from the device
const revokeAccess = async (requestData) => {
_validate(requestData);
const { visitorToken, accessPoints, keyCard } = requestData;
const url = `${
process.env.CLOUD_EXCHANGE_ACCESS_CONTROL_SERVER
}action=revoke;device=${accessPoints.toString()};id=${keyCard}`;
// Make request to cloud exchange access control
try {
const revokedAccess = await _makeRequest(url);
const successStatus = [200, 201];
console.log(revokedAccess.status);
if (!successStatus.includes(revokedAccess.status)) {
throw new Error("Access control servers not available, try again.");
}
// Check if the visit key
const visitor_access_log = await knex
.select("id")
.from("access_control_log")
.where({
visitor_token: visitorToken,
key_card: keyCard,
});
if (visitor_access_log.length !== 0) {
// log the activity
await knex("access_control_log")
.update({
is_active: false,
time_checked_out: dayjs().format("YYYY-MM-DD HH:mm:ss"),
})
.where({
visitor_token: visitorToken,
key_card: keyCard,
});
}
return {
status: revokedAccess.status,
data: revokedAccess.data,
};
} catch (error) {
console.log(error.message);
throw new Error("An error occured while process.");
}
};
/**
* Grant access to access control
*/
app.post("/access_control/grant-access", async (req, res) => {
try {
const response = await grantAccess(req.body);
return res.status(200).json(response);
} catch (error) {
return res
.status(400)
.json({ message: "Request failed", error: error.message });
}
});
/**
* Revoke access to access control
*/
app.post("/access_control/revoke-access", async (req, res) => {
try {
const response = await revokeAccess(req.body);
return res.status(200).json(response);
} catch (error) {
return res
.status(400)
.json({ message: "Request failed", error: error.message });
}
});
app.listen(4000, () => console.log("Server started"));