chore: test code for cases index (#279)
This commit is contained in:
68
backend/routes/cases/index.mjs
Normal file
68
backend/routes/cases/index.mjs
Normal file
@@ -0,0 +1,68 @@
|
||||
import express from 'express';
|
||||
const router = express.Router();
|
||||
import { DataTypes, Op } from 'sequelize';
|
||||
import defineCase from '../../models/cases';
|
||||
import authMiddleware from '../../middleware/auth.js';
|
||||
import visibilityMiddleware from '../../middleware/verifyVisible';
|
||||
|
||||
export default function (sequelize) {
|
||||
const { verifySignedIn } = authMiddleware(sequelize);
|
||||
const Case = defineCase(sequelize, DataTypes);
|
||||
const { verifyProjectVisibleFromFolderId } = visibilityMiddleware(sequelize);
|
||||
|
||||
router.get('/', verifySignedIn, verifyProjectVisibleFromFolderId, async (req, res) => {
|
||||
const { folderId, priority, type, q } = req.query;
|
||||
|
||||
if (!folderId) {
|
||||
return res.status(400).json({ error: 'folderId is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const whereClause = {
|
||||
folderId: folderId,
|
||||
};
|
||||
|
||||
if (q) {
|
||||
const searchTerm = q.trim();
|
||||
|
||||
if (searchTerm.length > 100) {
|
||||
return res.status(400).json({ error: 'Search term too long' });
|
||||
}
|
||||
|
||||
if (searchTerm.length >= 2) {
|
||||
whereClause[Op.or] = [{ title: { [Op.like]: `%${q}%` } }];
|
||||
}
|
||||
}
|
||||
|
||||
if (priority) {
|
||||
const priorityValues = priority
|
||||
.split(',')
|
||||
.map((p) => parseInt(p.trim(), 10))
|
||||
.filter((p) => !isNaN(p));
|
||||
if (priorityValues.length > 0) {
|
||||
whereClause.priority = { [Op.in]: priorityValues };
|
||||
}
|
||||
}
|
||||
|
||||
if (type) {
|
||||
const typeValues = type
|
||||
.split(',')
|
||||
.map((t) => parseInt(t.trim(), 10))
|
||||
.filter((t) => !isNaN(t));
|
||||
if (typeValues.length > 0) {
|
||||
whereClause.type = { [Op.in]: typeValues };
|
||||
}
|
||||
}
|
||||
|
||||
const cases = await Case.findAll({
|
||||
where: whereClause,
|
||||
});
|
||||
res.json(cases);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send('Internal Server Error');
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
Reference in New Issue
Block a user