fixing rescue settings

This commit is contained in:
blaisadmin committed 2026-04-15 22:57:34 -04:00
1 parent ce2b7a15bf
commit 765d6c61db
8 files changed
+476 -11

No files matched your search

@@ -1,7 +1,7 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createBird, getBirdById, listWeightsForBird } from './birdRepository.js';
import { createBird, getBirdById, listWeightsForBird, transferBirdToWorkspace } from './birdRepository.js';
import { mockDb } from '../test/mockDb.js';
test('getBirdById returns null when the bird does not exist in the workspace', async () => {
@@ -70,3 +70,34 @@ test('listWeightsForBird scopes by bird, workspace, and day window', async () =>
assert.deepEqual(calls[0].params, ['bird-1', 30, 10]);
assert.match(calls[0].text, /FROM weight_records/);
});
test('transferBirdToWorkspace moves the bird to the target workspace', async () => {
const { calls } = mockDb({
rowCount: 1,
rows: [
{
id: 'bird-1',
workspace_id: 22,
name: 'Kiwi',
tag_id: 'A-1',
species: 'Cockatiel',
gender: 'female',
date_of_birth: null,
gotcha_day: null,
chart_color: '#cb3a35',
photo_data_url: null,
notify_on_dob: false,
notify_on_gotcha_day: false,
created_at: '2026-04-14T00:00:00.000Z',
latest_weight_grams: '92',
latest_recorded_on: '2026-04-14',
},
],
});
const bird = await transferBirdToWorkspace('bird-1', 10, 22);
assert.equal(bird?.workspace_id, 22);
assert.deepEqual(calls[0].params, ['bird-1', 10, 22]);
assert.match(calls[0].text, /UPDATE birds/);
});
@@ -168,6 +168,33 @@ export const deleteBird = async (birdId: string, workspaceId: number) => {
return Boolean(result.rowCount);
};
export const transferBirdToWorkspace = async (birdId: string, sourceWorkspaceId: number, targetWorkspaceId: number) => {
const result = await db.query<BirdRow>(
`UPDATE birds
SET workspace_id = $3
WHERE id = $1
AND workspace_id = $2
RETURNING id, workspace_id, name, tag_id, species, gender, date_of_birth::text, gotcha_day::text, chart_color, photo_data_url, notify_on_dob, notify_on_gotcha_day, created_at,
(
SELECT weight_grams::text
FROM weight_records
WHERE bird_id = birds.id
ORDER BY recorded_on DESC
LIMIT 1
) AS latest_weight_grams,
(
SELECT recorded_on::text
FROM weight_records
WHERE bird_id = birds.id
ORDER BY recorded_on DESC
LIMIT 1
) AS latest_recorded_on`,
[birdId, sourceWorkspaceId, targetWorkspaceId],
);
return result.rows[0] ?? null;
};
export const listWeightsForBird = async (birdId: string, workspaceId: number, days: number) => {
const result = await db.query<WeightRow>(
`SELECT id, bird_id, weight_grams, recorded_on::text, notes
@@ -1,7 +1,7 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createWorkspace, ensurePersonalWorkspaceForUser, updateWorkspace } from './workspaceRepository.js';
import { createWorkspace, deleteWorkspaceIfEmpty, ensurePersonalWorkspaceForUser, findAlternateWorkspaceForUser, updateWorkspace } from './workspaceRepository.js';
import { mockDb } from '../test/mockDb.js';
import type { UserRow } from '../types.js';
@@ -95,3 +95,49 @@ test('updateWorkspace converts an existing household flock to rescue without ins
assert.doesNotMatch(calls[0].text, /INSERT INTO workspaces/);
assert.deepEqual(calls[0].params, [42, 'Converted Rescue', 'rescue', 'billing@example.com', 'rescue_free']);
});
test('deleteWorkspaceIfEmpty blocks deletion when birds are still assigned', async () => {
const { calls } = mockDb(
{
rowCount: 1,
rows: [{ count: '2' }],
},
);
const result = await deleteWorkspaceIfEmpty(42);
assert.deepEqual(result, { deleted: false, reason: 'birds_present' });
assert.equal(calls.length, 1);
assert.match(calls[0].text, /FROM birds/);
});
test('deleteWorkspaceIfEmpty deletes an empty workspace', async () => {
const { calls } = mockDb(
{
rowCount: 1,
rows: [{ count: '0' }],
},
{
rowCount: 1,
rows: [{ id: 42 }],
},
);
const result = await deleteWorkspaceIfEmpty(42);
assert.deepEqual(result, { deleted: true });
assert.equal(calls.length, 2);
assert.match(calls[1].text, /DELETE FROM workspaces/);
});
test('findAlternateWorkspaceForUser returns another workspace when available', async () => {
const { calls } = mockDb({
rowCount: 1,
rows: [{ workspace_id: 84 }],
});
const workspaceId = await findAlternateWorkspaceForUser('user-1', 42);
assert.equal(workspaceId, 84);
assert.deepEqual(calls[0].params, ['user-1', 42]);
});
@@ -236,6 +236,50 @@ export const listWorkspaceMembers = async (workspaceId: number) => {
return result.rows;
};
export const findAlternateWorkspaceForUser = async (userId: string, excludeWorkspaceId: number) => {
const result = await db.query<{ workspace_id: number }>(
`SELECT workspace_id
FROM workspace_members
WHERE user_id = $1
AND workspace_id <> $2
ORDER BY created_at ASC
LIMIT 1`,
[userId, excludeWorkspaceId],
);
return result.rows[0] ? Number(result.rows[0].workspace_id) : null;
};
export const getWorkspaceBirdCount = async (workspaceId: number) => {
const birdCount = await db.query<{ count: string }>(
`SELECT COUNT(*)::text AS count
FROM birds
WHERE workspace_id = $1`,
[workspaceId],
);
return Number(birdCount.rows[0]?.count ?? 0);
};
export const deleteWorkspaceIfEmpty = async (workspaceId: number) => {
if ((await getWorkspaceBirdCount(workspaceId)) > 0) {
return { deleted: false as const, reason: 'birds_present' as const };
}
const deleted = await db.query<{ id: number }>(
`DELETE FROM workspaces
WHERE id = $1
RETURNING id`,
[workspaceId],
);
if (!deleted.rowCount) {
return { deleted: false as const, reason: 'not_found' as const };
}
return { deleted: true as const };
};
export const upsertWorkspaceMember = async ({
workspaceId,
inviteEmail,