e1f2be7ecf
Wire up Better Auth for session-based authentication. Add actor middleware that resolves local_trusted mode to an implicit board actor and authenticated mode to Better Auth sessions. Add access service with membership, permission, invite, and join-request management. Register access routes for member/invite/ join-request CRUD. Update health endpoint to report deployment mode and bootstrap status. Enforce tasks:assign and agents:create permissions in issue and agent routes. Add deployment mode validation at startup with guardrails (loopback-only for local_trusted, auth config required for authenticated). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { and, desc, eq, inArray, not, sql } from "drizzle-orm";
|
|
import type { Db } from "@paperclip/db";
|
|
import { agents, approvals, heartbeatRuns } from "@paperclip/db";
|
|
import type { SidebarBadges } from "@paperclip/shared";
|
|
|
|
const ACTIONABLE_APPROVAL_STATUSES = ["pending", "revision_requested"];
|
|
const FAILED_HEARTBEAT_STATUSES = ["failed", "timed_out"];
|
|
|
|
export function sidebarBadgeService(db: Db) {
|
|
return {
|
|
get: async (companyId: string, extra?: { joinRequests?: number }): Promise<SidebarBadges> => {
|
|
const actionableApprovals = await db
|
|
.select({ count: sql<number>`count(*)` })
|
|
.from(approvals)
|
|
.where(
|
|
and(
|
|
eq(approvals.companyId, companyId),
|
|
inArray(approvals.status, ACTIONABLE_APPROVAL_STATUSES),
|
|
),
|
|
)
|
|
.then((rows) => Number(rows[0]?.count ?? 0));
|
|
|
|
const latestRunByAgent = await db
|
|
.selectDistinctOn([heartbeatRuns.agentId], {
|
|
runStatus: heartbeatRuns.status,
|
|
})
|
|
.from(heartbeatRuns)
|
|
.innerJoin(agents, eq(heartbeatRuns.agentId, agents.id))
|
|
.where(
|
|
and(
|
|
eq(heartbeatRuns.companyId, companyId),
|
|
eq(agents.companyId, companyId),
|
|
not(eq(agents.status, "terminated")),
|
|
),
|
|
)
|
|
.orderBy(heartbeatRuns.agentId, desc(heartbeatRuns.createdAt));
|
|
|
|
const failedRuns = latestRunByAgent.filter((row) =>
|
|
FAILED_HEARTBEAT_STATUSES.includes(row.runStatus),
|
|
).length;
|
|
|
|
const joinRequests = extra?.joinRequests ?? 0;
|
|
return {
|
|
inbox: actionableApprovals + failedRuns + joinRequests,
|
|
approvals: actionableApprovals,
|
|
failedRuns,
|
|
joinRequests,
|
|
};
|
|
},
|
|
};
|
|
}
|