Skip to content

Commit 78ee5c0

Browse files
committed
store dashboard evaluations
1 parent 125c528 commit 78ee5c0

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
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/completeDashboardEvaluate'));
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/startDashboardEvaluate'));
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const Archetype = require('archetype');
4+
const connect = require('../../src/db');
5+
6+
const CompleteDashboardEvaluateParams = new Archetype({
7+
authorization: {
8+
$type: 'string'
9+
},
10+
dashboardResultId: {
11+
$type: 'string'
12+
},
13+
workspaceId: {
14+
$type: 'string'
15+
},
16+
finishedEvaluatingAt: {
17+
$type: Date
18+
},
19+
result: {
20+
$type: Archetype.Any
21+
}
22+
}).compile('CompleteDashboardEvaluateParams');
23+
24+
module.exports = async function completeDashboardEvaluate(params) {
25+
const { authorization, dashboardResultId, workspaceId, finishedEvaluatingAt, result } = new CompleteDashboardEvaluateParams(params);
26+
27+
const db = await connect();
28+
const { AccessToken, DashboardResult, Workspace } = db.models;
29+
30+
let userId = null;
31+
if (authorization) {
32+
const accessToken = await AccessToken.findById(authorization).orFail(new Error('Invalid access token'));
33+
userId = accessToken.userId;
34+
}
35+
36+
// Find the workspace and check user permissions
37+
const workspace = await Workspace.findById(workspaceId).orFail(new Error('Workspace not found'));
38+
39+
const isAuthorized = workspace.members.some(member =>
40+
member.userId.toString() === userId.toString() && member.roles.find(role => ['admin', 'owner', 'member'].includes(role))
41+
);
42+
if (!isAuthorized) {
43+
throw new Error('Unauthorized');
44+
}
45+
46+
const dashboardResult = await DashboardResult.findById(dashboardResultId).orFail();
47+
dashboardResult.finishedEvaluatingAt = finishedEvaluatingAt;
48+
dashboardResult.result = result;
49+
dashboardResult.status = 'completed';
50+
await dashboardResult.save();
51+
52+
return { dashboardResult };
53+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
const Archetype = require('archetype');
4+
const connect = require('../../src/db');
5+
6+
const StartDashboardEvaluateParams = new Archetype({
7+
authorization: {
8+
$type: 'string'
9+
},
10+
dashboardId: {
11+
$type: 'string'
12+
},
13+
workspaceId: {
14+
$type: 'string'
15+
},
16+
startedEvaluatingAt: {
17+
$type: Date
18+
}
19+
}).compile('StartDashboardEvaluateParams');
20+
21+
module.exports = async function startDashboardEvaluate(params) {
22+
const { authorization, dashboardId, workspaceId, startedEvaluatingAt } = new StartDashboardEvaluateParams(params);
23+
24+
const db = await connect();
25+
const { AccessToken, DashboardResult, Workspace } = db.models;
26+
27+
let userId = null;
28+
if (authorization) {
29+
const accessToken = await AccessToken.findById(authorization).orFail(new Error('Invalid access token'));
30+
userId = accessToken.userId;
31+
}
32+
33+
// Find the workspace and check user permissions
34+
const workspace = await Workspace.findById(workspaceId).orFail(new Error('Workspace not found'));
35+
36+
const isAuthorized = workspace.members.some(member =>
37+
member.userId.toString() === userId.toString() && member.roles.find(role => ['admin', 'owner', 'member'].includes(role))
38+
);
39+
if (!isAuthorized) {
40+
throw new Error('Unauthorized');
41+
}
42+
43+
const dashboardResult = await DashboardResult.create({
44+
dashboardId,
45+
workspaceId,
46+
userId,
47+
startedEvaluatingAt,
48+
status: 'in_progress'
49+
});
50+
51+
return { dashboardResult };
52+
};

src/db/DashboardResult.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const mongoose = require('mongoose');
4+
5+
const dashboardResultSchema = new mongoose.Schema({
6+
dashboardId: {
7+
type: mongoose.ObjectId,
8+
ref: 'Dashboard'
9+
},
10+
workspaceId: {
11+
type: mongoose.ObjectId,
12+
ref: 'Workspace'
13+
},
14+
userId: {
15+
type: mongoose.ObjectId,
16+
ref: 'User'
17+
},
18+
startedEvaluatingAt: {
19+
type: Date
20+
},
21+
finishedEvaluatingAt: {
22+
type: Date
23+
},
24+
status: {
25+
type: String,
26+
enum: ['in_progress', 'completed', 'failed']
27+
},
28+
error: new mongoose.Schema({
29+
message: String,
30+
extra: String
31+
}),
32+
result: 'Mixed'
33+
}, { timestamps: true });
34+
35+
module.exports = dashboardResultSchema;

src/db/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const mongoose = require('mongoose');
55
let conn = null;
66

77
const accessTokenSchema = require('./AccessToken');
8+
const dashboardResultSchema = require('./DashboardResult');
89
const invitationSchema = require('./invitation');
910
const jobSchema = require('./Job');
1011
const openCollectiveSponsorSchema = require('./OpenCollectiveSponsor');
@@ -22,6 +23,7 @@ module.exports = async function connect() {
2223
await conn.asPromise();
2324
}
2425
conn.model('AccessToken', accessTokenSchema, 'AccessToken');
26+
conn.model('DashboardResult', dashboardResultSchema, 'DashboardResult');
2527
conn.model('Invitation', invitationSchema, 'Invitation');
2628
conn.model('Job', jobSchema, 'Job');
2729
conn.model('OpenCollectiveSponsor', openCollectiveSponsorSchema, 'OpenCollectiveSponsor');

0 commit comments

Comments
 (0)