feat: comment to test run's case (#390)
This commit is contained in:
@@ -5,6 +5,7 @@ import defineProject from '../models/projects.js';
|
||||
import defineFolder from '../models/folders.js';
|
||||
import defineCase from '../models/cases.js';
|
||||
import defineRun from '../models/runs.js';
|
||||
import defineRunCase from '../models/runCases.js';
|
||||
|
||||
export default function verifyEditableMiddleware(sequelize) {
|
||||
/**
|
||||
@@ -243,6 +244,55 @@ export default function verifyEditableMiddleware(sequelize) {
|
||||
return res.status(403).json({ error: 'Forbidden' });
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify user is reporter of the project by CommentableId
|
||||
* (have to be called after verifySignedIn() middleware)
|
||||
*/
|
||||
async function verifyProjectReporterFromCommentableId(req, res, next) {
|
||||
const commentableType = req.params.commentableType || req.query.commentableType;
|
||||
const commentableId = req.params.commentableId || req.query.commentableId;
|
||||
if (!commentableType || !commentableId) {
|
||||
return res.status(400).json({ error: 'commentableType and commentableId are required' });
|
||||
}
|
||||
|
||||
if (commentableType === 'Run') {
|
||||
// not implemented yet
|
||||
next();
|
||||
return;
|
||||
} else if (commentableType === 'Case') {
|
||||
// not implemented yet
|
||||
next();
|
||||
return;
|
||||
} else if (commentableType === 'RunCase') {
|
||||
const RunCase = defineRunCase(sequelize, DataTypes);
|
||||
const runCaseId = req.params.commentableId || req.query.commentableId;
|
||||
if (!runCaseId) {
|
||||
return res.status(400).json({ error: 'runCaseId is required' });
|
||||
}
|
||||
|
||||
const runCase = await RunCase.findByPk(runCaseId);
|
||||
const runId = runCase && runCase.runId;
|
||||
if (!runId) {
|
||||
return res.status(404).send('failed to find runId');
|
||||
}
|
||||
|
||||
const Run = defineRun(sequelize, DataTypes);
|
||||
const run = await Run.findByPk(runId);
|
||||
const projectId = run && run.projectId;
|
||||
if (!projectId) {
|
||||
return res.status(404).send('failed to find projectId');
|
||||
}
|
||||
|
||||
const isReporterRet = await isReporter(projectId, req.userId);
|
||||
if (isReporterRet) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
return res.status(400).json({ error: 'unsupported commentableType' });
|
||||
}
|
||||
}
|
||||
|
||||
async function isReporter(projectId, userId) {
|
||||
const Project = defineProject(sequelize, DataTypes);
|
||||
const Member = defineMember(sequelize, DataTypes);
|
||||
@@ -289,5 +339,6 @@ export default function verifyEditableMiddleware(sequelize) {
|
||||
verifyProjectDeveloperFromCaseId,
|
||||
verifyProjectReporterFromProjectId,
|
||||
verifyProjectReporterFromRunId,
|
||||
verifyProjectReporterFromCommentableId,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user