|
| 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 | +}; |
0 commit comments