fixed adding new workspace at login

This commit is contained in:
blaisadmin committed 2026-05-21 13:10:34 -04:00
1 parent e6211d7f5e
commit 62afc94f2f
3 files changed
+69 -2

No files matched your search

@@ -4,6 +4,7 @@ import test from 'node:test';
import {
createWorkspace,
deleteWorkspaceIfEmpty,
ensureDefaultWorkspaceForUser,
ensurePersonalWorkspaceForUser,
findAlternateWorkspaceForUser,
getPlatformAdminSummary,
@@ -64,6 +65,53 @@ test('ensurePersonalWorkspaceForUser creates a fresh workspace instead of claimi
assert.deepEqual(calls[2].params, [43, "Owner's Flock", 'owner@example.com']);
});
test('ensureDefaultWorkspaceForUser reuses an existing rescue workspace without creating a household flock', async () => {
const { calls } = mockDb({
rowCount: 1,
rows: [{ workspace_id: 84 }],
});
const workspaceId = await ensureDefaultWorkspaceForUser(user);
assert.equal(workspaceId, 84);
assert.equal(calls.length, 1);
assert.match(calls[0].text, /FROM workspace_members/);
assert.doesNotMatch(calls[0].text, /workspaces\.workspace_type = 'standard'/);
});
test('ensureDefaultWorkspaceForUser creates a household flock when the user has no workspace', async () => {
const { calls } = mockDb(
{
rowCount: 0,
rows: [],
},
{
rowCount: 0,
rows: [],
},
{
rowCount: 1,
rows: [{ next_id: 43 }],
},
{
rowCount: 1,
rows: [],
},
{
rowCount: 1,
rows: [],
},
);
const workspaceId = await ensureDefaultWorkspaceForUser(user);
assert.equal(workspaceId, 43);
assert.equal(calls.length, 5);
assert.match(calls[0].text, /FROM workspace_members/);
assert.match(calls[1].text, /workspaces\.workspace_type = 'standard'/);
assert.match(calls[3].text, /INSERT INTO workspaces/);
});
test('createWorkspace inserts owner membership and returns the created workspace', async () => {
const { calls } = mockDb(
{ rowCount: 1, rows: [] },
@@ -114,6 +114,24 @@ export const ensurePersonalWorkspaceForUser = async (user: UserRow) => {
return workspaceId;
};
export const ensureDefaultWorkspaceForUser = async (user: UserRow) => {
const existing = await db.query<{ workspace_id: number }>(
`SELECT workspace_id
FROM workspace_members
INNER JOIN workspaces ON workspaces.id = workspace_members.workspace_id
WHERE workspace_members.user_id = $1
ORDER BY workspaces.created_at ASC
LIMIT 1`,
[user.id],
);
if (existing.rowCount) {
return Number(existing.rows[0].workspace_id);
}
return ensurePersonalWorkspaceForUser(user);
};
export const claimWorkspaceInvites = async (user: UserRow) => {
await db.query(
`UPDATE workspace_members