Ghosted memorial birds
This commit is contained in:
1 parent
270652f1a8
commit
ff177dbaed
4 files changed
+94
-42
No files matched your search
@@ -0,0 +1,20 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { createFlockNote, deleteFlockNote } from './auditRepository.js';
|
||||
import { mockDb } from '../test/mockDb.js';
|
||||
|
||||
test('bird notes require a current, non-memorialized bird in the workspace', async () => {
|
||||
const { calls } = mockDb({ rows: [] });
|
||||
const result = await createFlockNote({ workspaceId: 1, birdId: 'bird-1', body: 'Note', createdByUserId: 'user-1' });
|
||||
assert.equal(result, null);
|
||||
assert.match(calls[0].text, /birds.workspace_id = \$1\s+AND birds.memorialized_at IS NULL/);
|
||||
assert.deepEqual(calls[0].params, [1, 'bird-1', 'Note', 'Note', 'user-1']);
|
||||
});
|
||||
|
||||
test('deleting bird notes preserves memorialized and transferred bird history', async () => {
|
||||
const { calls } = mockDb({ rows: [] });
|
||||
assert.equal(await deleteFlockNote('note-1', 1), null);
|
||||
assert.match(calls[0].text, /bird_id IS NULL OR EXISTS/);
|
||||
assert.match(calls[0].text, /birds.id = flock_notes.bird_id/);
|
||||
assert.match(calls[0].text, /birds.workspace_id = \$2\s+AND birds.memorialized_at IS NULL/);
|
||||
});
|
||||
@@ -98,6 +98,7 @@ export const createFlockNote = async ({
|
||||
FROM birds
|
||||
WHERE birds.id = $2
|
||||
AND birds.workspace_id = $1
|
||||
AND birds.memorialized_at IS NULL
|
||||
)
|
||||
RETURNING id, workspace_id, bird_id, title, body, created_by_user_id, created_at, updated_at
|
||||
)
|
||||
@@ -125,6 +126,12 @@ export const deleteFlockNote = async (noteId: string, workspaceId: number) => {
|
||||
`DELETE FROM flock_notes
|
||||
WHERE id = $1
|
||||
AND workspace_id = $2
|
||||
AND (bird_id IS NULL OR EXISTS (
|
||||
SELECT 1 FROM birds
|
||||
WHERE birds.id = flock_notes.bird_id
|
||||
AND birds.workspace_id = $2
|
||||
AND birds.memorialized_at IS NULL
|
||||
))
|
||||
RETURNING id, title`,
|
||||
[noteId, workspaceId],
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user