|
| 1 | +const prGradingConfigController = function (PRGradingConfig) { |
| 2 | + const getAllConfigs = async (req, res) => { |
| 3 | + try { |
| 4 | + const configs = await PRGradingConfig.find().sort({ createdAt: -1 }); |
| 5 | + res.status(200).json(configs); |
| 6 | + } catch (err) { |
| 7 | + res.status(500).json({ error: 'Failed to fetch configurations', details: err.message }); |
| 8 | + } |
| 9 | + }; |
| 10 | + |
| 11 | + const createConfig = async (req, res) => { |
| 12 | + try { |
| 13 | + const { teamName, reviewerCount, testDataType, reviewerNames, notes } = req.body; |
| 14 | + |
| 15 | + if (!teamName || !reviewerCount || !testDataType) { |
| 16 | + return res |
| 17 | + .status(400) |
| 18 | + .json({ error: 'teamName, reviewerCount, and testDataType are required.' }); |
| 19 | + } |
| 20 | + |
| 21 | + if (typeof reviewerCount !== 'number' || reviewerCount < 1) { |
| 22 | + return res.status(400).json({ error: 'reviewerCount must be a positive number.' }); |
| 23 | + } |
| 24 | + |
| 25 | + const existing = await PRGradingConfig.findOne({ teamName: teamName.trim() }); |
| 26 | + if (existing) { |
| 27 | + return res |
| 28 | + .status(409) |
| 29 | + .json({ error: `A configuration with team name "${teamName}" already exists.` }); |
| 30 | + } |
| 31 | + |
| 32 | + const newConfig = new PRGradingConfig({ |
| 33 | + teamName: teamName.trim(), |
| 34 | + reviewerCount, |
| 35 | + testDataType, |
| 36 | + reviewerNames: reviewerNames || [], |
| 37 | + notes: notes || '', |
| 38 | + }); |
| 39 | + |
| 40 | + const saved = await newConfig.save(); |
| 41 | + res.status(201).json(saved); |
| 42 | + } catch (err) { |
| 43 | + res.status(500).json({ error: 'Failed to create configuration', details: err.message }); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + const deleteConfig = async (req, res) => { |
| 48 | + try { |
| 49 | + const { id } = req.params; |
| 50 | + const deleted = await PRGradingConfig.findByIdAndDelete(id); |
| 51 | + if (!deleted) { |
| 52 | + return res.status(404).json({ error: 'Configuration not found.' }); |
| 53 | + } |
| 54 | + res.status(200).json({ message: 'Configuration deleted successfully.' }); |
| 55 | + } catch (err) { |
| 56 | + res.status(500).json({ error: 'Failed to delete configuration', details: err.message }); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + return { getAllConfigs, createConfig, deleteConfig }; |
| 61 | +}; |
| 62 | + |
| 63 | +module.exports = prGradingConfigController; |
0 commit comments