Merge dev into production release without education
This commit is contained in:
commit
fc541f024d
50 files changed
+2986
-458
No files matched your search
@@ -0,0 +1,47 @@
|
||||
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];
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { db } from '../db/client.js';
|
||||
|
||||
// Only reveal whether an exact normalized band match is available, never its profile or former flock.
|
||||
export async function hasUnassignedBird(tagId: string): Promise<boolean> {
|
||||
const result = await db.query<{ id: string }>(
|
||||
`SELECT id FROM birds WHERE workspace_id IS NULL AND memorialized_at IS NULL
|
||||
AND LOWER(BTRIM(tag_id)) = LOWER(BTRIM($1)) LIMIT 1`, [tagId],
|
||||
);
|
||||
return result.rows.length > 0;
|
||||
}
|
||||
|
||||
export async function removeBirdFromFlock(birdId: string, workspaceId: number, userId: string): Promise<boolean> {
|
||||
return db.transaction(async (client) => {
|
||||
const result = await client.query<{ id: string }>(
|
||||
`UPDATE birds SET workspace_id = NULL, public_profile_enabled = FALSE
|
||||
WHERE id = $1 AND workspace_id = $2 AND memorialized_at IS NULL RETURNING id`, [birdId, workspaceId],
|
||||
);
|
||||
if (!result.rows.length) return false;
|
||||
await client.query(`UPDATE flock_notes SET workspace_id = NULL WHERE bird_id = $1`, [birdId]);
|
||||
await client.query(`UPDATE bird_transfer_codes SET revoked_at = CURRENT_TIMESTAMP
|
||||
WHERE bird_id = $1 AND completed_at IS NULL AND revoked_at IS NULL`, [birdId]);
|
||||
await client.query(`DELETE FROM pending_bird_transfers WHERE bird_id = $1 AND completed_at IS NULL`, [birdId]);
|
||||
await client.query(`INSERT INTO bird_timeline_events
|
||||
(bird_id, event_type, from_workspace_id, from_workspace_name, note, created_by_user_id)
|
||||
SELECT $1, 'removed_from_flock', id, name, 'Removed from flock; profile and history retained.', $3
|
||||
FROM workspaces WHERE id = $2`, [birdId, workspaceId, userId]);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
export async function claimUnassignedBird(tagId: string, workspaceId: number, userId: string): Promise<string | null> {
|
||||
return db.transaction(async (client) => {
|
||||
// The row lock and repeated unassigned predicate prevent competing flocks from claiming the same bird.
|
||||
const match = await client.query<{ id: string }>(
|
||||
`SELECT id FROM birds WHERE workspace_id IS NULL AND memorialized_at IS NULL
|
||||
AND LOWER(BTRIM(tag_id)) = LOWER(BTRIM($1)) FOR UPDATE`, [tagId],
|
||||
);
|
||||
if (!match.rows.length) return null;
|
||||
const birdId = match.rows[0].id;
|
||||
const claimed = await client.query(`UPDATE birds SET workspace_id = $2, public_profile_enabled = FALSE
|
||||
WHERE id = $1 AND workspace_id IS NULL RETURNING id`, [birdId, workspaceId]);
|
||||
if (!claimed.rows.length) return null;
|
||||
await client.query(`UPDATE flock_notes SET workspace_id = $2 WHERE bird_id = $1 AND workspace_id IS NULL`, [birdId, workspaceId]);
|
||||
await client.query(`INSERT INTO bird_timeline_events
|
||||
(bird_id, event_type, to_workspace_id, to_workspace_name, note, created_by_user_id)
|
||||
SELECT $1, 'joined_flock', id, name, 'Added by matching band ID; existing profile and history retained.', $3
|
||||
FROM workspaces WHERE id = $2`, [birdId, workspaceId, userId]);
|
||||
return birdId;
|
||||
});
|
||||
}
|
||||
@@ -789,18 +789,6 @@ export const updateMemorialReminderPreference = async ({
|
||||
return result.rows[0] ?? null;
|
||||
};
|
||||
|
||||
export const deleteBird = async (birdId: string, workspaceId: number) => {
|
||||
const result = await db.query<{ id: string }>(
|
||||
`DELETE FROM birds
|
||||
WHERE id = $1
|
||||
AND workspace_id = $2
|
||||
RETURNING id`,
|
||||
[birdId, workspaceId],
|
||||
);
|
||||
|
||||
return Boolean(result.rowCount);
|
||||
};
|
||||
|
||||
export const transferBirdToWorkspace = async (birdId: string, sourceWorkspaceId: number, targetWorkspaceId: number) => {
|
||||
const result = await db.query<BirdRow>(
|
||||
`UPDATE birds
|
||||
|
||||
Reference in new issue
Block a user