medication tracking started

This commit is contained in:
blaisadmin
2026-04-19 02:30:22 -04:00
parent 1ff8100b2c
commit 263b98d3d8
6 changed files with 624 additions and 3 deletions
+80 -1
View File
@@ -1,5 +1,5 @@
import { db } from '../db/client.js';
import type { BirdGender, BirdRow, LostBirdMatchRow, PendingBirdTransferRow, VetVisitRow, WeightRow } from '../types.js';
import type { BirdGender, BirdRow, LostBirdMatchRow, MedicationRow, PendingBirdTransferRow, VetVisitRow, WeightRow } from '../types.js';
const birdSelectFields = `
birds.id,
@@ -403,3 +403,82 @@ export const deleteVetVisitForBird = async (visitId: string, birdId: string) =>
return (result.rowCount ?? 0) > 0;
};
export const listMedicationsForBird = async (birdId: string, workspaceId: number) => {
const result = await db.query<MedicationRow>(
`SELECT id, bird_id, name, dosage, frequency, route, start_date::text, end_date::text, notes
FROM medications
WHERE bird_id = $1
AND EXISTS (
SELECT 1
FROM birds
WHERE birds.id = medications.bird_id
AND birds.workspace_id = $2
)
ORDER BY COALESCE(end_date, '9999-12-31'::date) DESC, start_date DESC, created_at DESC`,
[birdId, workspaceId],
);
return result.rows;
};
export const createMedicationForBird = async (
birdId: string,
name: string,
dosage: string,
frequency: string,
route: string | null,
startDate: string,
endDate: string | null,
notes: string | null,
) => {
const result = await db.query<MedicationRow>(
`INSERT INTO medications (bird_id, name, dosage, frequency, route, start_date, end_date, notes)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id, bird_id, name, dosage, frequency, route, start_date::text, end_date::text, notes`,
[birdId, name, dosage, frequency, route, startDate, endDate, notes],
);
return result.rows[0] ?? null;
};
export const updateMedicationForBird = async (
medicationId: string,
birdId: string,
name: string,
dosage: string,
frequency: string,
route: string | null,
startDate: string,
endDate: string | null,
notes: string | null,
) => {
const result = await db.query<MedicationRow>(
`UPDATE medications
SET name = $3,
dosage = $4,
frequency = $5,
route = $6,
start_date = $7,
end_date = $8,
notes = $9
WHERE id = $1
AND bird_id = $2
RETURNING id, bird_id, name, dosage, frequency, route, start_date::text, end_date::text, notes`,
[medicationId, birdId, name, dosage, frequency, route, startDate, endDate, notes],
);
return result.rows[0] ?? null;
};
export const deleteMedicationForBird = async (medicationId: string, birdId: string) => {
const result = await db.query<{ id: string }>(
`DELETE FROM medications
WHERE id = $1
AND bird_id = $2
RETURNING id`,
[medicationId, birdId],
);
return (result.rowCount ?? 0) > 0;
};