feat: comment to test run's case (#390)

This commit is contained in:
kimatata
2026-02-23 12:26:07 +09:00
committed by GitHub
parent 1f4ac0ae7b
commit a9674c81ab
27 changed files with 1045 additions and 40 deletions

View File

@@ -109,7 +109,19 @@ export default function (sequelize) {
},
{
model: RunCase,
attributes: ['id', 'runId', 'status'],
attributes: [
'id',
'runId',
'status',
[
sequelize.literal(
'(SELECT COUNT(*) FROM `comments` WHERE `comments`.`commentableType` = ' +
sequelize.escape('RunCase') +
' AND `comments`.`commentableId` = `RunCases`.`id`)'
),
'commentCount',
],
],
// Must be 'true' when filtering by status, otherwise all cases are returned.
required: runCaseRequired,
where: {

View File

@@ -7,6 +7,7 @@ import defineTag from '../../models/tags.js';
import defineAttachment from '../../models/attachments.js';
import authMiddleware from '../../middleware/auth.js';
import visibilityMiddleware from '../../middleware/verifyVisible.js';
import defineRunCase from '../../models/runCases.js';
export default function (sequelize) {
const Case = defineCase(sequelize, DataTypes);
@@ -19,6 +20,10 @@ export default function (sequelize) {
Attachment.belongsToMany(Case, { through: 'caseAttachments' });
Case.belongsToMany(Tags, { through: 'caseTags', foreignKey: 'caseId', otherKey: 'tagId' });
Tags.belongsToMany(Case, { through: 'caseTags', foreignKey: 'tagId', otherKey: 'caseId' });
const RunCase = defineRunCase(sequelize, DataTypes);
RunCase.belongsTo(Case, { foreignKey: 'caseId' });
Case.hasMany(RunCase, { foreignKey: 'caseId' });
const { verifySignedIn } = authMiddleware(sequelize);
const { verifyProjectVisibleFromCaseId } = visibilityMiddleware(sequelize);
@@ -44,6 +49,9 @@ export default function (sequelize) {
attributes: ['id', 'name'],
through: { attributes: [] },
},
{
model: RunCase,
},
],
});
return res.json(testcase);

View File

@@ -0,0 +1,38 @@
import express from 'express';
const router = express.Router();
import { DataTypes } from 'sequelize';
import defineComment from '../../models/comments.js';
import authMiddleware from '../../middleware/auth.js';
export default function (sequelize) {
const { verifySignedIn } = authMiddleware(sequelize);
const Comment = defineComment(sequelize, DataTypes);
router.delete('/:commentId', verifySignedIn, async (req, res) => {
const commentId = req.params.commentId;
if (!commentId) {
return res.status(400).json({ error: 'commentId is required' });
}
try {
const comment = await Comment.findByPk(commentId);
if (!comment) {
return res.status(404).json({ error: 'Comment not found' });
}
// Verify the user owns the comment
if (comment.userId !== req.userId) {
return res.status(403).json({ error: 'Unauthorized' });
}
await comment.destroy();
res.json({ success: true });
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
return router;
}

View File

@@ -0,0 +1,53 @@
import express from 'express';
const router = express.Router();
import { DataTypes } from 'sequelize';
import defineComment from '../../models/comments.js';
import defineUser from '../../models/users.js';
import authMiddleware from '../../middleware/auth.js';
export default function (sequelize) {
const { verifySignedIn } = authMiddleware(sequelize);
const Comment = defineComment(sequelize, DataTypes);
const User = defineUser(sequelize, DataTypes);
Comment.belongsTo(User, { foreignKey: 'userId' });
router.put('/:commentId', verifySignedIn, async (req, res) => {
const commentId = req.params.commentId;
const { content } = req.body;
if (!commentId || !content) {
return res.status(400).json({ error: 'id and content are required' });
}
try {
const comment = await Comment.findByPk(commentId);
if (!comment) {
return res.status(404).json({ error: 'Comment not found' });
}
// Verify the user owns the comment
if (comment.userId !== req.userId) {
return res.status(403).json({ error: 'Unauthorized' });
}
await comment.update({ content });
// Fetch the comment with user data
const commentWithUser = await Comment.findByPk(commentId, {
include: [
{
model: sequelize.models.User,
attributes: ['id', 'username', 'email'],
},
],
});
res.json(commentWithUser);
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
return router;
}

View File

@@ -0,0 +1,46 @@
import express from 'express';
const router = express.Router();
import { DataTypes } from 'sequelize';
import defineComment from '../../models/comments.js';
import defineUser from '../../models/users.js';
import authMiddleware from '../../middleware/auth.js';
import visibilityMiddleware from '../../middleware/verifyVisible.js';
export default function (sequelize) {
const { verifySignedIn } = authMiddleware(sequelize);
const { verifyProjectVisibleFromCommentableId } = visibilityMiddleware(sequelize);
const Comment = defineComment(sequelize, DataTypes);
const User = defineUser(sequelize, DataTypes);
Comment.belongsTo(User, { foreignKey: 'userId', onDelete: 'CASCADE' });
User.hasMany(Comment, { foreignKey: 'userId', onDelete: 'CASCADE' });
router.get('/', verifySignedIn, verifyProjectVisibleFromCommentableId, async (req, res) => {
const { commentableType, commentableId } = req.query;
if (!commentableType || !commentableId) {
return res.status(400).json({ error: 'commentableType and commentableId are required' });
}
try {
const comments = await Comment.findAll({
where: {
commentableType: commentableType,
commentableId: commentableId,
},
include: [
{
model: User,
attributes: ['id', 'username', 'email'],
},
],
order: [['createdAt', 'ASC']],
});
res.json(comments);
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
return router;
}

View File

@@ -0,0 +1,50 @@
import express from 'express';
const router = express.Router();
import { DataTypes } from 'sequelize';
import defineComment from '../../models/comments.js';
import defineUser from '../../models/users.js';
import authMiddleware from '../../middleware/auth.js';
import editableMiddleware from '../../middleware/verifyEditable.js';
export default function (sequelize) {
const { verifySignedIn } = authMiddleware(sequelize);
const { verifyProjectReporterFromCommentableId } = editableMiddleware(sequelize);
const Comment = defineComment(sequelize, DataTypes);
const User = defineUser(sequelize, DataTypes);
Comment.belongsTo(User, { foreignKey: 'userId' });
router.post('/', verifySignedIn, verifyProjectReporterFromCommentableId, async (req, res) => {
const { commentableType, commentableId } = req.query;
const { content } = req.body;
if (!commentableType || !commentableId || !content) {
return res.status(400).json({ error: 'commentableType, commentableId, and content are required' });
}
try {
const newComment = await Comment.create({
commentableType: commentableType,
commentableId: commentableId,
userId: req.userId,
content: content,
});
// Fetch the comment with user data
const commentWithUser = await Comment.findByPk(newComment.id, {
include: [
{
model: sequelize.models.User,
attributes: ['id', 'username', 'email'],
},
],
});
res.json(commentWithUser);
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
return router;
}