fixing rescue settings
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user