Roadmap 2,3,4
This commit is contained in:
@@ -10,6 +10,7 @@ const GRCA_CORE = {
|
||||
heart: ['heart_ofa', 'heart_echo'],
|
||||
eye: ['eye_caer'],
|
||||
};
|
||||
const VALID_TEST_TYPES = ['hip_ofa', 'hip_pennhip', 'elbow_ofa', 'heart_ofa', 'heart_echo', 'eye_caer', 'thyroid_ofa', 'dna_panel'];
|
||||
|
||||
// Helper: compute clearance summary for a dog
|
||||
function getClearanceSummary(db, dogId) {
|
||||
@@ -103,17 +104,6 @@ router.get('/dog/:dogId/chic-eligible', (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// GET single health record
|
||||
router.get('/:id', (req, res) => {
|
||||
try {
|
||||
const db = getDatabase();
|
||||
const record = db.prepare('SELECT * FROM health_records WHERE id = ?').get(req.params.id);
|
||||
if (!record) return res.status(404).json({ error: 'Health record not found' });
|
||||
res.json(record);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// POST create health record
|
||||
router.post('/', (req, res) => {
|
||||
@@ -128,6 +118,10 @@ router.post('/', (req, res) => {
|
||||
return res.status(400).json({ error: 'dog_id, record_type, and test_date are required' });
|
||||
}
|
||||
|
||||
if (test_type && !VALID_TEST_TYPES.includes(test_type)) {
|
||||
return res.status(400).json({ error: 'Invalid test_type' });
|
||||
}
|
||||
|
||||
const db = getDatabase();
|
||||
const dbResult = db.prepare(`
|
||||
INSERT INTO health_records
|
||||
@@ -157,6 +151,10 @@ router.put('/:id', (req, res) => {
|
||||
document_url, result, vet_name, next_due, notes
|
||||
} = req.body;
|
||||
|
||||
if (test_type && !VALID_TEST_TYPES.includes(test_type)) {
|
||||
return res.status(400).json({ error: 'Invalid test_type' });
|
||||
}
|
||||
|
||||
const db = getDatabase();
|
||||
db.prepare(`
|
||||
UPDATE health_records
|
||||
@@ -190,4 +188,70 @@ router.delete('/:id', (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// GET cancer history for a dog
|
||||
router.get('/dog/:dogId/cancer-history', (req, res) => {
|
||||
try {
|
||||
const db = getDatabase();
|
||||
const records = db.prepare(`
|
||||
SELECT * FROM cancer_history
|
||||
WHERE dog_id = ?
|
||||
ORDER BY age_at_diagnosis ASC, created_at DESC
|
||||
`).all(req.params.dogId);
|
||||
res.json(records);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// POST create cancer history record
|
||||
router.post('/cancer-history', (req, res) => {
|
||||
try {
|
||||
const {
|
||||
dog_id, cancer_type, age_at_diagnosis, age_at_death, cause_of_death, notes
|
||||
} = req.body;
|
||||
|
||||
if (!dog_id || !cancer_type) {
|
||||
return res.status(400).json({ error: 'dog_id and cancer_type are required' });
|
||||
}
|
||||
|
||||
const db = getDatabase();
|
||||
|
||||
// Update dog's age_at_death and cause_of_death if provided
|
||||
if (age_at_death || cause_of_death) {
|
||||
db.prepare(`
|
||||
UPDATE dogs SET
|
||||
age_at_death = COALESCE(?, age_at_death),
|
||||
cause_of_death = COALESCE(?, cause_of_death)
|
||||
WHERE id = ?
|
||||
`).run(age_at_death || null, cause_of_death || null, dog_id);
|
||||
}
|
||||
|
||||
const dbResult = db.prepare(`
|
||||
INSERT INTO cancer_history
|
||||
(dog_id, cancer_type, age_at_diagnosis, age_at_death, cause_of_death, notes)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
`).run(
|
||||
dog_id, cancer_type, age_at_diagnosis || null,
|
||||
age_at_death || null, cause_of_death || null, notes || null
|
||||
);
|
||||
|
||||
const record = db.prepare('SELECT * FROM cancer_history WHERE id = ?').get(dbResult.lastInsertRowid);
|
||||
res.status(201).json(record);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET single health record (wildcard should go last to prevent overlap)
|
||||
router.get('/:id', (req, res) => {
|
||||
try {
|
||||
const db = getDatabase();
|
||||
const record = db.prepare('SELECT * FROM health_records WHERE id = ?').get(req.params.id);
|
||||
if (!record) return res.status(404).json({ error: 'Health record not found' });
|
||||
res.json(record);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user