updated medicine schedules
This commit is contained in:
@@ -4,6 +4,7 @@ import type {
|
||||
BirdRow,
|
||||
LostBirdMatchRow,
|
||||
MedicationAdministrationRow,
|
||||
MedicationDoseScheduleItem,
|
||||
MedicationRow,
|
||||
PendingBirdTransferRow,
|
||||
VetVisitRow,
|
||||
@@ -415,7 +416,7 @@ export const deleteVetVisitForBird = async (visitId: string, birdId: string) =>
|
||||
|
||||
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
|
||||
`SELECT id, bird_id, name, dosage, frequency, dose_schedule, route, start_date::text, end_date::text, notes
|
||||
FROM medications
|
||||
WHERE bird_id = $1
|
||||
AND EXISTS (
|
||||
@@ -436,16 +437,17 @@ export const createMedicationForBird = async (
|
||||
name: string,
|
||||
dosage: string,
|
||||
frequency: string,
|
||||
doseSchedule: MedicationDoseScheduleItem[],
|
||||
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],
|
||||
`INSERT INTO medications (bird_id, name, dosage, frequency, dose_schedule, route, start_date, end_date, notes)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
RETURNING id, bird_id, name, dosage, frequency, dose_schedule, route, start_date::text, end_date::text, notes`,
|
||||
[birdId, name, dosage, frequency, JSON.stringify(doseSchedule), route, startDate, endDate, notes],
|
||||
);
|
||||
|
||||
return result.rows[0] ?? null;
|
||||
@@ -457,6 +459,7 @@ export const updateMedicationForBird = async (
|
||||
name: string,
|
||||
dosage: string,
|
||||
frequency: string,
|
||||
doseSchedule: MedicationDoseScheduleItem[],
|
||||
route: string | null,
|
||||
startDate: string,
|
||||
endDate: string | null,
|
||||
@@ -467,14 +470,15 @@ export const updateMedicationForBird = async (
|
||||
SET name = $3,
|
||||
dosage = $4,
|
||||
frequency = $5,
|
||||
route = $6,
|
||||
start_date = $7,
|
||||
end_date = $8,
|
||||
notes = $9
|
||||
dose_schedule = $6,
|
||||
route = $7,
|
||||
start_date = $8,
|
||||
end_date = $9,
|
||||
notes = $10
|
||||
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],
|
||||
RETURNING id, bird_id, name, dosage, frequency, dose_schedule, route, start_date::text, end_date::text, notes`,
|
||||
[medicationId, birdId, name, dosage, frequency, JSON.stringify(doseSchedule), route, startDate, endDate, notes],
|
||||
);
|
||||
|
||||
return result.rows[0] ?? null;
|
||||
@@ -494,7 +498,7 @@ export const deleteMedicationForBird = async (medicationId: string, birdId: stri
|
||||
|
||||
export const listMedicationAdministrationsForBird = async (birdId: string, workspaceId: number) => {
|
||||
const result = await db.query<MedicationAdministrationRow>(
|
||||
`SELECT id, medication_id, bird_id, administered_on::text, status, notes, created_by_user_id, created_at
|
||||
`SELECT id, medication_id, bird_id, administered_on::text, administration_slot, status, notes, created_by_user_id, created_at
|
||||
FROM medication_administrations
|
||||
WHERE bird_id = $1
|
||||
AND EXISTS (
|
||||
@@ -515,13 +519,14 @@ export const upsertMedicationAdministrationForBird = async (
|
||||
birdId: string,
|
||||
workspaceId: number,
|
||||
administeredOn: string,
|
||||
administrationSlot: string,
|
||||
status: 'administered' | 'missed',
|
||||
notes: string | null,
|
||||
createdByUserId: string | null,
|
||||
) => {
|
||||
const result = await db.query<MedicationAdministrationRow>(
|
||||
`INSERT INTO medication_administrations (medication_id, bird_id, administered_on, status, notes, created_by_user_id)
|
||||
SELECT $1, $2, $4, $5, $6, $7
|
||||
`INSERT INTO medication_administrations (medication_id, bird_id, administered_on, administration_slot, status, notes, created_by_user_id)
|
||||
SELECT $1, $2, $4, $5, $6, $7, $8
|
||||
WHERE EXISTS (
|
||||
SELECT 1
|
||||
FROM medications
|
||||
@@ -530,13 +535,13 @@ export const upsertMedicationAdministrationForBird = async (
|
||||
AND medications.bird_id = $2
|
||||
AND birds.workspace_id = $3
|
||||
)
|
||||
ON CONFLICT (medication_id, administered_on)
|
||||
ON CONFLICT (medication_id, administered_on, administration_slot)
|
||||
DO UPDATE SET status = EXCLUDED.status,
|
||||
notes = EXCLUDED.notes,
|
||||
created_by_user_id = EXCLUDED.created_by_user_id,
|
||||
created_at = CURRENT_TIMESTAMP
|
||||
RETURNING id, medication_id, bird_id, administered_on::text, status, notes, created_by_user_id, created_at`,
|
||||
[medicationId, birdId, workspaceId, administeredOn, status, notes, createdByUserId],
|
||||
RETURNING id, medication_id, bird_id, administered_on::text, administration_slot, status, notes, created_by_user_id, created_at`,
|
||||
[medicationId, birdId, workspaceId, administeredOn, administrationSlot, status, notes, createdByUserId],
|
||||
);
|
||||
|
||||
return result.rows[0] ?? null;
|
||||
|
||||
Reference in New Issue
Block a user