fixed transfer process
This commit is contained in:
1 parent
765d6c61db
commit
3a0e30085c
9 files changed
+480
-231
No files matched your search
@@ -1,7 +1,14 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { createBird, getBirdById, listWeightsForBird, transferBirdToWorkspace } from './birdRepository.js';
|
||||
import {
|
||||
completePendingBirdTransfersForOwner,
|
||||
createBird,
|
||||
createPendingBirdTransfer,
|
||||
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 () => {
|
||||
@@ -101,3 +108,86 @@ test('transferBirdToWorkspace moves the bird to the target workspace', async ()
|
||||
assert.deepEqual(calls[0].params, ['bird-1', 10, 22]);
|
||||
assert.match(calls[0].text, /UPDATE birds/);
|
||||
});
|
||||
|
||||
test('createPendingBirdTransfer stores an open transfer for auto-completion', async () => {
|
||||
const { calls } = mockDb({
|
||||
rowCount: 1,
|
||||
rows: [
|
||||
{
|
||||
id: 'transfer-1',
|
||||
bird_id: 'bird-1',
|
||||
source_workspace_id: 10,
|
||||
destination_owner_email: 'receiver@example.com',
|
||||
requested_by_user_id: 'user-1',
|
||||
completed_at: null,
|
||||
completed_workspace_id: null,
|
||||
last_error: null,
|
||||
created_at: '2026-04-15T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const transfer = await createPendingBirdTransfer({
|
||||
birdId: 'bird-1',
|
||||
sourceWorkspaceId: 10,
|
||||
destinationOwnerEmail: 'receiver@example.com',
|
||||
requestedByUserId: 'user-1',
|
||||
});
|
||||
|
||||
assert.equal(transfer?.id, 'transfer-1');
|
||||
assert.deepEqual(calls[0].params, ['bird-1', 10, 'receiver@example.com', 'user-1']);
|
||||
assert.match(calls[0].text, /INSERT INTO pending_bird_transfers/);
|
||||
assert.match(calls[0].text, /ON CONFLICT/);
|
||||
});
|
||||
|
||||
test('completePendingBirdTransfersForOwner moves pending birds and marks completion', async () => {
|
||||
const { calls } = mockDb(
|
||||
{
|
||||
rowCount: 1,
|
||||
rows: [
|
||||
{
|
||||
id: 'transfer-1',
|
||||
bird_id: 'bird-1',
|
||||
source_workspace_id: 10,
|
||||
destination_owner_email: 'receiver@example.com',
|
||||
requested_by_user_id: 'user-1',
|
||||
completed_at: null,
|
||||
completed_workspace_id: null,
|
||||
last_error: null,
|
||||
created_at: '2026-04-15T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
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',
|
||||
},
|
||||
],
|
||||
},
|
||||
{ rowCount: 1, rows: [] },
|
||||
);
|
||||
|
||||
const result = await completePendingBirdTransfersForOwner('receiver@example.com', 22);
|
||||
|
||||
assert.deepEqual(result, { completed: 1, failed: 0 });
|
||||
assert.deepEqual(calls[0].params, ['receiver@example.com']);
|
||||
assert.deepEqual(calls[1].params, ['bird-1', 10, 22]);
|
||||
assert.deepEqual(calls[2].params, ['transfer-1', 22]);
|
||||
assert.match(calls[2].text, /completed_at = CURRENT_TIMESTAMP/);
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { db } from '../db/client.js';
|
||||
import type { BirdGender, BirdRow, VetVisitRow, WeightRow } from '../types.js';
|
||||
import type { BirdGender, BirdRow, PendingBirdTransferRow, VetVisitRow, WeightRow } from '../types.js';
|
||||
|
||||
const birdSelectFields = `
|
||||
birds.id,
|
||||
@@ -195,6 +195,97 @@ export const transferBirdToWorkspace = async (birdId: string, sourceWorkspaceId:
|
||||
return result.rows[0] ?? null;
|
||||
};
|
||||
|
||||
export const createPendingBirdTransfer = async ({
|
||||
birdId,
|
||||
sourceWorkspaceId,
|
||||
destinationOwnerEmail,
|
||||
requestedByUserId,
|
||||
}: {
|
||||
birdId: string;
|
||||
sourceWorkspaceId: number;
|
||||
destinationOwnerEmail: string;
|
||||
requestedByUserId: string;
|
||||
}) => {
|
||||
const result = await db.query<PendingBirdTransferRow>(
|
||||
`INSERT INTO pending_bird_transfers (bird_id, source_workspace_id, destination_owner_email, requested_by_user_id)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (bird_id) WHERE completed_at IS NULL DO UPDATE
|
||||
SET destination_owner_email = EXCLUDED.destination_owner_email,
|
||||
requested_by_user_id = EXCLUDED.requested_by_user_id,
|
||||
last_error = NULL,
|
||||
created_at = CURRENT_TIMESTAMP
|
||||
RETURNING id, bird_id, source_workspace_id, destination_owner_email, requested_by_user_id, completed_at::text, completed_workspace_id, last_error, created_at`,
|
||||
[birdId, sourceWorkspaceId, destinationOwnerEmail, requestedByUserId],
|
||||
);
|
||||
|
||||
return result.rows[0] ?? null;
|
||||
};
|
||||
|
||||
export const listPendingBirdTransfersForOwnerEmail = async (ownerEmail: string) => {
|
||||
const result = await db.query<PendingBirdTransferRow>(
|
||||
`SELECT id, bird_id, source_workspace_id, destination_owner_email, requested_by_user_id, completed_at::text, completed_workspace_id, last_error, created_at
|
||||
FROM pending_bird_transfers
|
||||
WHERE LOWER(destination_owner_email) = LOWER($1)
|
||||
AND completed_at IS NULL
|
||||
ORDER BY created_at ASC`,
|
||||
[ownerEmail],
|
||||
);
|
||||
|
||||
return result.rows;
|
||||
};
|
||||
|
||||
export const markPendingBirdTransferCompleted = async (transferId: string, completedWorkspaceId: number) => {
|
||||
await db.query(
|
||||
`UPDATE pending_bird_transfers
|
||||
SET completed_at = CURRENT_TIMESTAMP,
|
||||
completed_workspace_id = $2,
|
||||
last_error = NULL
|
||||
WHERE id = $1`,
|
||||
[transferId, completedWorkspaceId],
|
||||
);
|
||||
};
|
||||
|
||||
export const markPendingBirdTransferFailed = async (transferId: string, lastError: string) => {
|
||||
await db.query(
|
||||
`UPDATE pending_bird_transfers
|
||||
SET last_error = $2
|
||||
WHERE id = $1`,
|
||||
[transferId, lastError],
|
||||
);
|
||||
};
|
||||
|
||||
export const completePendingBirdTransfersForOwner = async (ownerEmail: string, targetWorkspaceId: number) => {
|
||||
const transfers = await listPendingBirdTransfersForOwnerEmail(ownerEmail);
|
||||
let completed = 0;
|
||||
let failed = 0;
|
||||
|
||||
for (const transfer of transfers) {
|
||||
try {
|
||||
const bird = await transferBirdToWorkspace(transfer.bird_id, transfer.source_workspace_id, targetWorkspaceId);
|
||||
|
||||
if (!bird) {
|
||||
failed += 1;
|
||||
await markPendingBirdTransferFailed(transfer.id, 'Bird is no longer available in the source flock.');
|
||||
continue;
|
||||
}
|
||||
|
||||
await markPendingBirdTransferCompleted(transfer.id, targetWorkspaceId);
|
||||
completed += 1;
|
||||
} catch (error) {
|
||||
failed += 1;
|
||||
const message =
|
||||
typeof error === 'object' && error && 'code' in error && error.code === '23505'
|
||||
? 'The receiving flock already has a bird using the same band/tag ID.'
|
||||
: error instanceof Error
|
||||
? error.message
|
||||
: 'Unable to complete pending bird transfer.';
|
||||
await markPendingBirdTransferFailed(transfer.id, message);
|
||||
}
|
||||
}
|
||||
|
||||
return { completed, failed };
|
||||
};
|
||||
|
||||
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,14 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { createWorkspace, deleteWorkspaceIfEmpty, ensurePersonalWorkspaceForUser, findAlternateWorkspaceForUser, updateWorkspace } from './workspaceRepository.js';
|
||||
import {
|
||||
createWorkspace,
|
||||
deleteWorkspaceIfEmpty,
|
||||
ensurePersonalWorkspaceForUser,
|
||||
findAlternateWorkspaceForUser,
|
||||
listOwnedWorkspacesByOwnerEmail,
|
||||
updateWorkspace,
|
||||
} from './workspaceRepository.js';
|
||||
import { mockDb } from '../test/mockDb.js';
|
||||
import type { UserRow } from '../types.js';
|
||||
|
||||
@@ -141,3 +148,30 @@ test('findAlternateWorkspaceForUser returns another workspace when available', a
|
||||
assert.equal(workspaceId, 84);
|
||||
assert.deepEqual(calls[0].params, ['user-1', 42]);
|
||||
});
|
||||
|
||||
test('listOwnedWorkspacesByOwnerEmail resolves accepted owner flocks by email', async () => {
|
||||
const { calls } = mockDb({
|
||||
rowCount: 1,
|
||||
rows: [
|
||||
{
|
||||
id: 84,
|
||||
name: 'Receiving Flock',
|
||||
workspace_type: 'standard',
|
||||
billing_email: 'receiver@example.com',
|
||||
billing_plan: 'household_basic',
|
||||
subscription_status: 'active',
|
||||
rescue_verification_status: 'not_required',
|
||||
created_at: '2026-04-14T00:00:00.000Z',
|
||||
updated_at: '2026-04-14T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const workspaces = await listOwnedWorkspacesByOwnerEmail('Receiver@Example.com', 42);
|
||||
|
||||
assert.equal(workspaces[0]?.id, 84);
|
||||
assert.deepEqual(calls[0].params, ['Receiver@Example.com', 42]);
|
||||
assert.match(calls[0].text, /workspace_members\.role = 'owner'/);
|
||||
assert.match(calls[0].text, /accepted_at IS NOT NULL/);
|
||||
assert.match(calls[0].text, /workspaces\.id <> \$2/);
|
||||
});
|
||||
@@ -250,6 +250,22 @@ export const findAlternateWorkspaceForUser = async (userId: string, excludeWorks
|
||||
return result.rows[0] ? Number(result.rows[0].workspace_id) : null;
|
||||
};
|
||||
|
||||
export const listOwnedWorkspacesByOwnerEmail = async (ownerEmail: string, excludeWorkspaceId: number) => {
|
||||
const result = await db.query<WorkspaceRow>(
|
||||
`SELECT workspaces.id, workspaces.name, workspaces.workspace_type, workspaces.billing_email, workspaces.billing_plan, workspaces.subscription_status, workspaces.rescue_verification_status, workspaces.created_at, workspaces.updated_at
|
||||
FROM workspace_members
|
||||
INNER JOIN workspaces ON workspaces.id = workspace_members.workspace_id
|
||||
WHERE LOWER(COALESCE(workspace_members.invite_email, workspace_members.email)) = LOWER($1)
|
||||
AND workspace_members.role = 'owner'
|
||||
AND workspace_members.accepted_at IS NOT NULL
|
||||
AND workspaces.id <> $2
|
||||
ORDER BY workspaces.created_at ASC`,
|
||||
[ownerEmail, excludeWorkspaceId],
|
||||
);
|
||||
|
||||
return result.rows;
|
||||
};
|
||||
|
||||
export const getWorkspaceBirdCount = async (workspaceId: number) => {
|
||||
const birdCount = await db.query<{ count: string }>(
|
||||
`SELECT COUNT(*)::text AS count
|
||||
|
||||
Reference in new issue
Block a user