Add flock exports, sharing invitations, and improved flock switching
This commit is contained in:
1 parent
ff177dbaed
commit
55353e4d46
12 files changed
+557
-100
No files matched your search
@@ -0,0 +1,31 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { exportFlock } from './flockExportRepository.js';
|
||||
import { mockDb } from '../test/mockDb.js';
|
||||
|
||||
test('export scopes every history to the selected flock without date or memorial filters', async () => {
|
||||
const data = { formatVersion: 1, birds: [], workspace: { id: 42, name: 'Flock' } };
|
||||
const { calls } = mockDb({ rows: [{ data }] });
|
||||
assert.deepEqual(await exportFlock(42), data);
|
||||
assert.equal(calls.length, 1);
|
||||
const { text, params } = calls[0];
|
||||
assert.deepEqual(params, [42, null]);
|
||||
assert.match(text, /SELECT \* FROM birds WHERE workspace_id = \$1/);
|
||||
for (const table of ['weight_records', 'vet_visits', 'medications', 'medication_administrations', 'behavior_occurrences', 'bird_timeline_events']) {
|
||||
assert.ok(text.includes(`FROM ${table} r JOIN flock_birds b ON b.id = r.bird_id`));
|
||||
}
|
||||
assert.match(text, /FROM flock_notes n WHERE n.workspace_id = \$1/);
|
||||
assert.doesNotMatch(text, /LIMIT|memorialized_at IS NULL|INTERVAL/);
|
||||
assert.doesNotMatch(text, /auth_sessions|integration_tokens|stripe|magic_link/);
|
||||
});
|
||||
|
||||
test('single member export restricts profiles, notes, and behavior types to the selected bird', async () => {
|
||||
const birdId = 'd0ab1d12-7859-4a64-a354-dd58bf738b94';
|
||||
const data = { birds: [{ id: birdId }] };
|
||||
const { calls } = mockDb({ rows: [{ data }] });
|
||||
assert.deepEqual(await exportFlock(42, birdId), data);
|
||||
assert.deepEqual(calls[0].params, [42, birdId]);
|
||||
assert.ok(calls[0].text.includes('workspace_id = $1 AND ($2::uuid IS NULL OR id = $2)'));
|
||||
assert.ok(calls[0].text.includes('AND ($2::uuid IS NULL OR n.bird_id IN (SELECT id FROM flock_birds))'));
|
||||
assert.ok(calls[0].text.includes('WHERE ($2::uuid IS NULL AND (t.is_default OR t.workspace_id = $1)) OR EXISTS'));
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
import { db } from '../db/client.js';
|
||||
|
||||
type ExportBird = Record<string, unknown> & {
|
||||
photo_object_key?: string | null;
|
||||
photo_content_type?: string | null;
|
||||
photo_data_url?: string | null;
|
||||
};
|
||||
|
||||
export type FlockExport = {
|
||||
formatVersion: number;
|
||||
exportedAt: string;
|
||||
workspace: { id: number; name: string };
|
||||
birds: ExportBird[];
|
||||
[key: string]: unknown;
|
||||
};
|
||||
|
||||
// A single statement provides a consistent snapshot, including all dates and memorialized birds.
|
||||
export const exportFlock = async (workspaceId: number, birdId?: string): Promise<FlockExport> => {
|
||||
const birdTables = {
|
||||
weights: 'weight_records',
|
||||
vetVisits: 'vet_visits',
|
||||
medications: 'medications',
|
||||
medicationAdministrations: 'medication_administrations',
|
||||
behaviors: 'behavior_occurrences',
|
||||
timeline: 'bird_timeline_events',
|
||||
};
|
||||
const histories = Object.entries(birdTables).map(([key, table]) =>
|
||||
`'${key}', (SELECT COALESCE(jsonb_agg(to_jsonb(r) ORDER BY r.id), '[]'::jsonb)
|
||||
FROM ${table} r JOIN flock_birds b ON b.id = r.bird_id)`
|
||||
);
|
||||
const result = await db.query<{ data: FlockExport }>(`
|
||||
WITH flock_birds AS (SELECT * FROM birds WHERE workspace_id = $1 AND ($2::uuid IS NULL OR id = $2))
|
||||
SELECT jsonb_build_object(
|
||||
'formatVersion', 1,
|
||||
'exportedAt', CURRENT_TIMESTAMP,
|
||||
'workspace', (SELECT jsonb_build_object('id', id, 'name', name) FROM workspaces WHERE id = $1),
|
||||
'birds', (SELECT COALESCE(jsonb_agg(to_jsonb(b) ORDER BY b.id), '[]'::jsonb) FROM flock_birds b),
|
||||
'notes', (SELECT COALESCE(jsonb_agg(to_jsonb(n) ORDER BY n.id), '[]'::jsonb)
|
||||
FROM flock_notes n WHERE n.workspace_id = $1
|
||||
AND ($2::uuid IS NULL OR n.bird_id IN (SELECT id FROM flock_birds))),
|
||||
'behaviorTypes', (SELECT COALESCE(jsonb_agg(to_jsonb(t) - 'workspace_id' ORDER BY t.id), '[]'::jsonb)
|
||||
FROM behavior_types t WHERE ($2::uuid IS NULL AND (t.is_default OR t.workspace_id = $1)) OR EXISTS (
|
||||
SELECT 1 FROM behavior_occurrences o JOIN flock_birds b ON b.id = o.bird_id
|
||||
WHERE o.behavior_type_id = t.id)),
|
||||
${histories.join(',\n')}
|
||||
) AS data`, [workspaceId, birdId ?? null]);
|
||||
return result.rows[0].data;
|
||||
};
|
||||
Reference in new issue
Block a user