Skip to content

Commit ac1fa31

Browse files
authored
Update management routes for admin users. (#675)
* Dont run expensive populate and loop functions * Update tests * Use stream in management route * Add projection * Update tests
1 parent 7607208 commit ac1fa31

File tree

2 files changed

+14
-39
lines changed

2 files changed

+14
-39
lines changed

packages/api/lib/controllers/managementController.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,22 @@ const { Box, User } = require('@sensebox/opensensemap-api-models'),
66
handleError = require('../helpers/errorHandler'),
77
{
88
retrieveParameters,
9-
} = require('../helpers/userParamHelpers');
9+
} = require('../helpers/userParamHelpers'),
10+
jsonstringify = require('stringify-stream');
1011

1112

1213
const listBoxes = async function listBoxes (req, res, next) {
13-
try {
14-
let boxes = await Box.find().exec();
15-
const users = await User.find().exec();
16-
17-
boxes = boxes
18-
.map(b => b.toJSON({ includeSecrets: true }));
19-
20-
for (const user of users) {
21-
for (const userbox of user.boxes) {
22-
const foundbox = boxes.find(box => box._id.equals(userbox));
23-
if (foundbox) {
24-
foundbox.owner = user.toJSON({ includeSecrets: true });
25-
}
26-
}
27-
}
14+
// default format
15+
const stringifier = jsonstringify({ open: '[', close: ']' });
2816

29-
res.send({ code: 'Ok', boxes });
17+
try {
18+
const stream = await Box.find({}, { _id: 1, name: 1, exposure: 1, model: 1, createdAt: 1, updatedAt: 1 }).cursor({ lean: true });
19+
stream
20+
.pipe(stringifier)
21+
.on('error', function (err) {
22+
res.end(`Error: ${err.message}`);
23+
})
24+
.pipe(res);
3025
} catch (err) {
3126
handleError(err, next);
3227
}
@@ -35,15 +30,10 @@ const listBoxes = async function listBoxes (req, res, next) {
3530
const listUsers = async function listUsers (req, res, next) {
3631
try {
3732
const users = await User.find()
38-
.populate('boxes')
3933
.then(function (users) {
4034
return users.map(function (user) {
41-
const boxes = user.boxes.map(b => b.toJSON({ includeSecrets: true }));
42-
4335
user = user.toJSON({ includeSecrets: true });
4436

45-
user.boxes = boxes;
46-
4737
return user;
4838
});
4939
});

tests/tests/015-admin-test.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ describe('Management Tests', function () {
8282
expect(user._id).exist;
8383
expect(user.boxes).exist;
8484

85-
for (const box of user.boxes) {
86-
expect(box.integrations).exist;
87-
}
88-
8985
return chakram.wait();
9086
});
9187

@@ -100,15 +96,8 @@ describe('Management Tests', function () {
10096

10197
describe('boxes management', function () {
10298
it('should allow to request a list of boxes with secrets', async function () {
103-
const { body: { boxes } } = await requestWithAuth('get', `${MANAGEMENT_URL}/boxes`);
104-
expect(Array.isArray(boxes)).true;
105-
for (const box of boxes) {
106-
expect(box.integrations).exist;
107-
expect(box.owner).exist;
108-
expect(box.owner.name).exist;
109-
expect(box.owner.email).exist;
110-
expect(box.owner._id).exist;
111-
}
99+
const { body } = await requestWithAuth('get', `${MANAGEMENT_URL}/boxes`);
100+
expect(Array.isArray(body)).true;
112101

113102
return chakram.wait();
114103
});
@@ -117,10 +106,6 @@ describe('Management Tests', function () {
117106
const { body } = await requestWithAuth('get', `${MANAGEMENT_URL}/boxes/${normalUserBox._id}`);
118107
expect(body.name).equal(valid_sensebox().name);
119108
expect(body.integrations).exist;
120-
expect(body.owner).exist;
121-
expect(body.owner.name).exist;
122-
expect(body.owner.email).exist;
123-
expect(body.owner._id).exist;
124109

125110
return chakram.wait();
126111
});

0 commit comments

Comments
 (0)