48 lines
3.6 KiB
TypeScript
48 lines
3.6 KiB
TypeScript
import { db } from '../db/client.js';
|
|
import type { BehaviorInput, BehaviorType, BehaviorOccurrence } from '../behaviorSchemas.js';
|
|
|
|
const typeFields = `id, name, description, created_at AS "createdAt"`;
|
|
const occurrenceFields = `o.id, o.bird_id AS "birdId", o.behavior_type_id AS "behaviorTypeId",
|
|
t.name AS "behaviorName", o.occurred_at AS "occurredAt", o.duration_seconds AS "durationSeconds",
|
|
o.severity, o.notes, o.created_at AS "createdAt", o.updated_at AS "updatedAt"`;
|
|
|
|
export async function listBehaviorTypes(workspaceId: number, birdId: string) {
|
|
// Include types used by this bird before a flock transfer, without exposing other flock types.
|
|
return (await db.query<BehaviorType>(`SELECT ${typeFields} FROM behavior_types t
|
|
WHERE is_default OR workspace_id = $1 OR EXISTS (
|
|
SELECT 1 FROM behavior_occurrences o JOIN birds b ON b.id = o.bird_id
|
|
WHERE o.behavior_type_id = t.id AND b.id = $2 AND b.workspace_id = $1)
|
|
ORDER BY name`, [workspaceId, birdId])).rows;
|
|
}
|
|
export async function createBehaviorType(workspaceId: number, name: string, description?: string | null) {
|
|
return (await db.query<BehaviorType>(`INSERT INTO behavior_types (workspace_id, name, description) VALUES ($1, $2, $3)
|
|
ON CONFLICT (workspace_id, lower(name)) WHERE workspace_id IS NOT NULL
|
|
DO UPDATE SET name = behavior_types.name RETURNING ${typeFields}`, [workspaceId, name, description || null])).rows[0];
|
|
}
|
|
export async function listBehaviorOccurrences(birdId: string, workspaceId: number, from: string, to: string, typeId?: string) {
|
|
return (await db.query<BehaviorOccurrence>(`SELECT ${occurrenceFields} FROM behavior_occurrences o
|
|
JOIN birds b ON b.id = o.bird_id JOIN behavior_types t ON t.id = o.behavior_type_id
|
|
WHERE b.id = $1 AND b.workspace_id = $2 AND o.occurred_at >= $3 AND o.occurred_at < $4
|
|
AND ($5::uuid IS NULL OR o.behavior_type_id = $5) ORDER BY o.occurred_at DESC, o.id`,
|
|
[birdId, workspaceId, from, to, typeId || null])).rows;
|
|
}
|
|
export async function saveBehaviorOccurrence(birdId: string, workspaceId: number, input: BehaviorInput, id?: string) {
|
|
const params = [birdId, workspaceId, input.behaviorTypeId, input.occurredAt, input.durationSeconds ?? null, input.severity ?? null, input.notes || null];
|
|
// Keep authorization in the mutation as well as the route to protect against concurrent transfers.
|
|
const allowed = `EXISTS (SELECT 1 FROM birds b WHERE b.id = $1 AND b.workspace_id = $2 AND b.memorialized_at IS NULL)
|
|
AND EXISTS (SELECT 1 FROM behavior_types t WHERE t.id = $3 AND
|
|
(t.is_default OR t.workspace_id = $2 OR EXISTS
|
|
(SELECT 1 FROM behavior_occurrences previous WHERE previous.bird_id = $1 AND previous.behavior_type_id = t.id)))`;
|
|
const mutation = id
|
|
? `UPDATE behavior_occurrences SET behavior_type_id = $3, occurred_at = $4, duration_seconds = $5,
|
|
severity = $6, notes = $7, updated_at = CURRENT_TIMESTAMP WHERE bird_id = $1 AND id = $8 AND ${allowed} RETURNING *`
|
|
: `INSERT INTO behavior_occurrences (bird_id, behavior_type_id, occurred_at, duration_seconds, severity, notes)
|
|
SELECT $1, $3, $4, $5, $6, $7 WHERE ${allowed} RETURNING *`;
|
|
return (await db.query<BehaviorOccurrence>(`WITH saved AS (${mutation}) SELECT ${occurrenceFields}
|
|
FROM saved o JOIN behavior_types t ON t.id = o.behavior_type_id`, id ? [...params, id] : params)).rows[0];
|
|
}
|
|
export async function deleteBehaviorOccurrence(birdId: string, workspaceId: number, id: string) {
|
|
return (await db.query<{ id: string }>(`DELETE FROM behavior_occurrences o USING birds b WHERE o.bird_id = b.id
|
|
AND b.id = $1 AND b.workspace_id = $2 AND b.memorialized_at IS NULL AND o.id = $3 RETURNING o.id`, [birdId, workspaceId, id])).rows[0];
|
|
}
|