Skip to content

Commit 3efeac7

Browse files
committed
dashboard evals
1 parent 78ee5c0 commit 3efeac7

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
const extrovert = require('extrovert');
4+
5+
module.exports = extrovert.toNetlifyFunction(require('../../src/actions/getDashboardResults'));

src/actions/completeDashboardEvaluate.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
const Archetype = require('archetype');
44
const connect = require('../../src/db');
5+
const mongoose = require('mongoose');
56

67
const CompleteDashboardEvaluateParams = new Archetype({
78
authorization: {
89
$type: 'string'
910
},
1011
dashboardResultId: {
11-
$type: 'string'
12+
$type: mongoose.Types.ObjectId
1213
},
1314
workspaceId: {
14-
$type: 'string'
15+
$type: mongoose.Types.ObjectId
1516
},
1617
finishedEvaluatingAt: {
1718
$type: Date

src/actions/getDashboardResults.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const Archetype = require('archetype');
4+
const connect = require('../../src/db');
5+
const mongoose = require('mongoose');
6+
7+
const GetDashboardResultsParams = new Archetype({
8+
authorization: {
9+
$type: 'string'
10+
},
11+
dashboardId: {
12+
$type: mongoose.Types.ObjectId
13+
},
14+
workspaceId: {
15+
$type: mongoose.Types.ObjectId
16+
}
17+
}).compile('GetDashboardResultsParams');
18+
19+
module.exports = async function getDashboardResults(params) {
20+
const { authorization, dashboardId, workspaceId } = new GetDashboardResultsParams(params);
21+
22+
const db = await connect();
23+
const { AccessToken, DashboardResult, Workspace } = db.models;
24+
25+
let userId = null;
26+
if (authorization) {
27+
const accessToken = await AccessToken.findById(authorization).orFail(new Error('Invalid access token'));
28+
userId = accessToken.userId;
29+
}
30+
31+
// Find the workspace and check user permissions
32+
const workspace = await Workspace.findById(workspaceId).orFail(new Error('Workspace not found'));
33+
34+
const isAuthorized = workspace.members.some(member =>
35+
member.userId.toString() === userId.toString() && member.roles.find(role => ['admin', 'owner', 'member', 'readonly', 'dashboards'].includes(role))
36+
);
37+
if (!isAuthorized) {
38+
throw new Error('Unauthorized');
39+
}
40+
41+
const dashboardResults = await DashboardResult.find({ dashboardId }).sort({ _id: -1 }).limit(10);
42+
43+
return { dashboardResults };
44+
};

0 commit comments

Comments
 (0)