Skip to content

Commit 0804bf1

Browse files
committed
Merge branch 'add-computer'
2 parents 7517536 + 6867216 commit 0804bf1

16 files changed

+842
-8
lines changed

config/routes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports.
44
mount = app => {
55
const usersRoutes = require('../models/users/user.routes')
66
const authRoutes = require('../models/auth/auth.routes')
7+
const computerRoutes = require('../models/computers/computer.routes')
78
const restoreDatabaseRoutes = require('../models/restore-database/restore-database.routes')
89

910
// tomake the travis builds succeed
@@ -14,5 +15,6 @@ mount = app => {
1415

1516
app.use('/api/v1/users', usersRoutes)
1617
app.use('/api/v1/auth', authRoutes)
18+
app.use('/api/v1/computers', computerRoutes)
1719
app.use('/restore-database', restoreDatabaseRoutes)
1820
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
exports.up = function(knex, Promise) {
3+
return knex.schema.createTable('computers', table => {
4+
table.string('name').primary()
5+
table.boolean('active').defaultTo(true)
6+
table.timestamp('created_at').defaultTo(knex.fn.now())
7+
})
8+
}
9+
10+
exports.down = function(knex, Promise) {
11+
return knex.schema.dropTable('computers')
12+
}

database/seeds/computers.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
exports.seed = function(knex, Promise) {
3+
// Deletes ALL existing entries
4+
return knex('computers').del()
5+
.then(function () {
6+
// Inserts seed entries
7+
return knex('computers').insert([
8+
{ name: 'computer1', created_at: new Date },
9+
{ name: 'computer2', created_at: new Date },
10+
{ name: 'computer3', created_at: new Date },
11+
{ name: 'computer4', created_at: new Date },
12+
{ name: 'computer5', active: false, created_at: new Date }
13+
])
14+
})
15+
}

models/auth/auth.controller.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ module.exports.logout = async (req, res) => {
3838
if (error) return buildResponse(res, 400, { message: 'bad request', reason: error.details[0].message})
3939

4040
try {
41-
await userModel.logout(value)
41+
const userApi = new userModel()
42+
await userApi.logout(value)
4243
buildResponse(res, 200, { message: 'Successfully logged out'})
4344
} catch (error) {
4445
buildResponse(res, 500, { message: 'Something aweful happend and we couldn\'t log out', error })

models/auth/auth.spec.e2e.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('POST /api/v1/auth/login', () => {
8181
.send(user)
8282
.query({
8383
app: 'desktop',
84-
computer_name: 'Computer1'
84+
computer_name: 'computer1'
8585
})
8686
.end((err, res) => {
8787
should.not.exist(err)
@@ -379,7 +379,7 @@ describe('POST /api/v1/auth/login', () => {
379379
chai.request(server)
380380
.post(loginUrl)
381381
.send(user)
382-
.query({ computer_name: 'Computer1' })
382+
.query({ computer_name: 'computer1' })
383383
.end((err, res) => {
384384
should.exist(err)
385385
res.redirects.length.should.eql(0)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const ComputerModel = require('./')
2+
const { buildResponse } = require('../../utils/responseService')
3+
4+
async function create (req, res) {
5+
try {
6+
let newComputer = req.body
7+
const computerApi = new ComputerModel()
8+
const createdComputer = await computerApi.create(newComputer)
9+
buildResponse(res, 200, { message: 'successfully created computer.', computer: createdComputer })
10+
} catch (error) {
11+
if (error.status) return buildResponse(res, error.status, { message: error.message, error })
12+
buildResponse(res, 500, { message: 'something happened', error })
13+
}
14+
}
15+
16+
async function getAllComputers (req, res) {
17+
try {
18+
const computerApi = new ComputerModel()
19+
const computers = await computerApi.getAllComputers()
20+
buildResponse(res, 200, { computers })
21+
} catch (err) {
22+
buildResponse(res, 500, err)
23+
}
24+
}
25+
26+
async function deactivate (req, res) {
27+
try {
28+
let name = req.body.name
29+
const computerApi = new ComputerModel()
30+
const response = await computerApi.deactivate({ name })
31+
buildResponse(res, 200, { message: response.message })
32+
} catch (err) {
33+
buildResponse(res, 500, err)
34+
}
35+
}
36+
37+
38+
async function reactivate (req, res) {
39+
try {
40+
let name = req.body.name
41+
const computerApi = new ComputerModel()
42+
const response = await computerApi.reactivate({ name })
43+
buildResponse(res, 200, { message: response.message })
44+
} catch (err) {
45+
buildResponse(res, 500, err)
46+
}
47+
}
48+
49+
async function unregister (req, res) {
50+
try {
51+
let name = req.body.name
52+
const computerApi = new ComputerModel()
53+
const response = await computerApi.unregister({ name })
54+
buildResponse(res, 200, { message: response.message })
55+
} catch (err) {
56+
buildResponse(res, 500, err)
57+
}
58+
}
59+
60+
module.exports = {
61+
create,
62+
getAllComputers,
63+
deactivate,
64+
reactivate,
65+
unregister
66+
}
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
## User.js
2+
3+
## Routes available
4+
5+
The following routes are available for the ```Computer``` resource. They are all prefixed by ```/api/v1/computer```.
6+
7+
| Methods | Path | Authentication | Description |
8+
| --- | --- | --- | --- |
9+
| `POST` | '/new' | Admin Required | Add a new computer to the system |
10+
| `GET` | '/' | Required | Gets all the computers registered in the system |
11+
| `PUT` | '/deactivate' | Admin Required | Deactivate the computer so that it is not accessible |
12+
| `PUT` | '/reactivate' | Admin Required | Reactivate the computer so that it can be accessible |
13+
| `Delete` | '/unregister' | Admin Required | Remove the computer from the system |
14+
15+
## #create()
16+
17+
This method creates a new computer in the database
18+
19+
**Usage**
20+
```javascript
21+
const axios = require('axios')
22+
23+
const res = await axios.post('/api/v1/computers/new', {
24+
name: 'computer3'
25+
})
26+
27+
console.log(res.data)
28+
```
29+
30+
> The responses below are just a demo to show the keys that the response object may have
31+
### Success response
32+
33+
```json
34+
{
35+
"message": "successfully created computer.",
36+
"computer": {
37+
"name": "computer3",
38+
"active": true,
39+
"created_at": "2018-01-12T16:25:11.893Z"
40+
}
41+
}
42+
```
43+
44+
### Error response
45+
46+
**If no token is provided**
47+
```json
48+
{
49+
"message": "No token provided."
50+
}
51+
```
52+
53+
**validation errors**
54+
```json
55+
{
56+
"message": "Validation errors occured",
57+
"error": {
58+
"message": "Validation errors occured",
59+
"errors": [
60+
{
61+
"message": "\"name\" is required",
62+
"path": "name",
63+
"type": "any.required",
64+
"context": {
65+
"key": "name"
66+
}
67+
}
68+
],
69+
"status": 400
70+
}
71+
}
72+
```
73+
---
74+
## #getAllComputers()
75+
76+
Returns all the computers in the databse
77+
78+
**Usage**
79+
```javascript
80+
const axios = require('axios')
81+
82+
const res = await axios.post('/api/v1/computers')
83+
84+
console.log(res)
85+
```
86+
87+
> The responses below are just a demo to show the keys that the response object may have
88+
### Success response
89+
90+
```js
91+
{
92+
"computers": [
93+
{
94+
"name": "computer1",
95+
"active": true,
96+
"created_at": "2018-01-12T15:56:42.181Z",
97+
"username": "garikai",
98+
"login_time": "18:15:41",
99+
"login_date": "2018-01-12T18:15:41.522+02:00",
100+
"computer_name": "computer1",
101+
"status": "in_use"
102+
},
103+
{
104+
"name": "computer3",
105+
"active": true,
106+
"created_at": "2018-01-12T16:25:11.893Z",
107+
"username": null,
108+
"login_time": null,
109+
"login_date": null,
110+
"computer_name": null,
111+
"status": "available"
112+
}
113+
]
114+
}
115+
```
116+
117+
### Error response
118+
119+
**If no token is provided**
120+
```json
121+
{
122+
"message": "No token provided."
123+
}
124+
```
125+
---
126+
127+
## #deactivate()
128+
129+
Deactivate the computer so that it is not accessible
130+
131+
**Usage**
132+
```javascript
133+
const axios = require('axios')
134+
135+
const res = await axios.put('/api/v1/computers/deactivate', {
136+
name: 'computer3'
137+
})
138+
139+
console.log(res.data)
140+
```
141+
142+
> The responses below are just a demo to show the keys that the response object may have
143+
### Success response
144+
145+
```json
146+
{
147+
"message": "successfully deactivated computer."
148+
}
149+
```
150+
151+
### Error response
152+
153+
**If no token is provided**
154+
```json
155+
{
156+
"message": "No token provided."
157+
}
158+
```
159+
---
160+
161+
162+
## #reactivate()
163+
164+
Deactivate the computer so that it is not accessible
165+
166+
**Usage**
167+
```javascript
168+
const axios = require('axios')
169+
170+
const res = await axios.put('/api/v1/computers/reactivate', {
171+
name: 'computer3'
172+
})
173+
174+
console.log(res.data)
175+
```
176+
177+
> The responses below are just a demo to show the keys that the response object may have
178+
### Success response
179+
180+
```json
181+
{
182+
"message": "successfully reactivated computer."
183+
}
184+
```
185+
186+
### Error response
187+
188+
**If no token is provided**
189+
```json
190+
{
191+
"message": "No token provided."
192+
}
193+
```
194+
---
195+
196+
197+
## #unregister()
198+
199+
Deactivate the computer so that it is not accessible
200+
201+
**Usage**
202+
```javascript
203+
const axios = require('axios')
204+
205+
const res = await axios.delete('/api/v1/computers/unregister', {
206+
name: 'computer3'
207+
})
208+
209+
console.log(res.data)
210+
```
211+
212+
> The responses below are just a demo to show the keys that the response object may have
213+
### Success response
214+
215+
```json
216+
{
217+
"message": "successfully unregistered computer."
218+
}
219+
```
220+
221+
### Error response
222+
223+
**If no token is provided**
224+
```json
225+
{
226+
"message": "No token provided."
227+
}
228+
```
229+
---
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const router = require('express').Router()
2+
const controller = require('./computer.controller')
3+
4+
const { authenticateAdmin, authenticate } = require('../../utils/middlewareService')
5+
6+
router.post('/new', authenticateAdmin, controller.create)
7+
router.get('/', authenticate, controller.getAllComputers)
8+
router.put('/deactivate', authenticateAdmin, controller.deactivate)
9+
router.put('/reactivate', authenticateAdmin, controller.reactivate)
10+
router.delete('/unregister', authenticateAdmin, controller.unregister)
11+
12+
module.exports = router

0 commit comments

Comments
 (0)