mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-22 11:54:35 +01:00
## Thinking Path > - Paperclip helps people manage AI agents and their work. > - The inbox collects requests that need a human decision. > - Teams need to know who owns each request and who may act on it. > - A shared close operation must differ from hiding a card for one person. > - This pull request adds personal routing and durable outcomes to the existing attention service. > - People can claim or transfer work and inspect what another person did for them. > - Domain permissions still decide who may resolve each request. ## Linked Issues or Issue Description **Subsystem affected** Server, shared contracts, and database. This is the routing foundation for a stacked inbox UI change. **Problem or motivation** A company-wide attention feed does not give teams a clear personal queue. Archive, withdrawal, transfer, and decisions by another person need distinct scopes and records. **Proposed solution** Resolve a request audience from its addressee, task participants, and configured owners. Provide personal, unassigned, company, all-assignee, and archived views. Add claim, transfer, personal archive and snooze, shared withdrawal, receipts, and personal history. **Alternatives considered** A single shared inbox leaves ownership and personal hiding ambiguous. The service keeps one canonical request and projects its audience instead of copying requests for each person. **Roadmap alignment** This extends the existing Multiple Human Users and Agent Reviews and Approvals capabilities in ROADMAP.md. **Additional context** Refs #19363 and #19520 for the current inbox navigation and confirmation replacement rules. Refs #20268 for related actionable-request work. That PR filters actionability; this stack adds audience routing and personal outcomes. This is part 1 of a two-PR stack. The inbox UI PR #20961 targets this branch. ## What Changed - Add nullable company and project decision owners and a company budget owner. - Add audience views and personal badge counts to the canonical attention projections. - Add claim, transfer, withdrawal, personal archive, snooze, and restore operations with permission checks and activity records. Lock addressed transfers to the current addressee or routing administrators, and validate the recipient against canonical resolution policies. - Add receipts and scoped history for work handled by another person. - Keep closed-task requests and replaced confirmations out of current cards. - Add an idempotent migration and a concurrent receipt-history index. - Update typed fixtures so the existing UI remains compatible with the new contracts. ## Verification - Server layered typechecking passes, including all three test-tree partitions. - Seven routing and interaction-resolution suites pass all 249 tests against a dedicated external PostgreSQL test database on `4caecc21d8`. Six new regressions failed before the repair; they cover takeover followed by approval, legitimate delegation, excluded creators/review requesters, and unauthorized claims. - Prior focused verification covers private approval history and receipts, transfer visibility, audience escalation, withdrawal governance, hidden-card counts, per-recipient confirmation replacement, and SDK source contracts. - A synthetic three-person company passed 37 routing and lifecycle acceptance checks. The UI stack adds browser acceptance for the shared controls. - The complete [GitHub Actions CI matrix](https://github.com/paperclipai/paperclip/actions/runs/43134997331) passes on `4caecc21d891677323a7335f23b1a015cce24c13` (Linux/Node 24: typecheck, build, general and serialized tests, Runner support, browser shards, and aggregate gates). Greptile is 5/5 on that exact head with no open findings. The fresh security scan passes after explicitly rechecking the addressee-transfer repair. - `pnpm verify` reports exit 2 because a CI-global script changed; full CI supplies that broader coverage. Embedded PostgreSQL could not start on this Mac because shared-memory slots were exhausted, so local database checks use a separate external test database. Linux CI covers the embedded setup. ## Risks - Personal badge counts change to the viewer's assigned requests. Accessible feed totals and task membership keep their existing scope. - The migration adds nullable owner fields and a dismissal fingerprint. Its index runs outside a transaction. The migration is classified and rerunnable. - Routing changes the addressee used by resolution authorization. The current addressee or a routing administrator must authorize an addressed transfer; the recipient must satisfy writer access, canonical resolver exclusions, and task review policy under row locks. - The shared validator export barrel crosses 1,000 lines because it exports the new attention schemas. Schema implementation stays in the dedicated attention validator module. ## Model Used OpenAI GPT-6 through Codex. Used repository tools, code execution, and browser checks. This session does not expose a more specific runtime model ID or its context-window size. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links - [x] My branch name describes the change and contains no internal Paperclip ticket id - [x] I have run focused tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes (OpenAPI contracts and docs/api/attention.md) - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Scott Tong <scott@scottsmbpm5max.lan> Co-authored-by: Paperclip <noreply@paperclip.ing>
485 lines
22 KiB
TypeScript
485 lines
22 KiB
TypeScript
import { importProviderDailyCosts } from "../services/provider-billing-import.js";
|
|
import { subscriptionPriceSchema, mergeSubscriptionsSchema } from "@paperclipai/shared";
|
|
import { subscriptionService } from "../services/subscriptions.js";
|
|
import { subscriptionCostReport } from "../services/subscription-report.js";
|
|
import { refreshCompanySubscriptions } from "../services/subscription-refresh.js";
|
|
import { canManageAiConnections } from "./ai-connections.js";
|
|
import { importProviderCostsSchema } from "@paperclipai/shared";
|
|
import { accountingIntegrityService } from "../services/accounting-integrity.js";
|
|
import { costDataQualityReport } from "../services/cost-data-quality.js";
|
|
import { billingReconciliationService } from "../services/billing-reconciliation.js";
|
|
import { adjustCostSchema, importBillingInvoiceSchema, repairAccountingSchema, retryAccountingSchema } from "@paperclipai/shared";
|
|
import { Router } from "express";
|
|
import { z } from "zod";
|
|
import type { Db } from "@paperclipai/db";
|
|
import {
|
|
createCostEventSchema,
|
|
createFinanceEventSchema,
|
|
normalizeIssueIdentifier,
|
|
resolveBudgetIncidentSchema,
|
|
updateBudgetSchema,
|
|
upsertBudgetPolicySchema,
|
|
} from "@paperclipai/shared";
|
|
import { validate } from "../middleware/validate.js";
|
|
import {
|
|
budgetService,
|
|
costService,
|
|
financeService,
|
|
companyService,
|
|
agentService,
|
|
issueService,
|
|
heartbeatService,
|
|
accessService,
|
|
} from "../services/index.js";
|
|
import { assertBoard, assertCompanyAccess, getAccessibleResource, getActorInfo } from "./authz.js";
|
|
import { fetchCompanyQuotaWindows } from "../services/quota-windows.js";
|
|
import { attentionHandledDetails, attentionHandledFor } from "../services/attention-receipts.js";
|
|
import { badRequest, forbidden } from "../errors.js";
|
|
import type { PluginWorkerManager } from "../services/plugin-worker-manager.js";
|
|
|
|
const reportDateSchema = z.union([z.iso.datetime({ offset: true }), z.iso.date()]);
|
|
|
|
export function parseCostDateRange(query: Record<string, unknown>) {
|
|
if (query.period !== undefined && query.period !== "all" && query.period !== "month") throw badRequest("invalid 'period'");
|
|
if (query.period === "all") {
|
|
if (query.from !== undefined || query.to !== undefined) throw badRequest("all-time period cannot have date bounds");
|
|
return { allTime: true };
|
|
}
|
|
if (query.from !== undefined && !reportDateSchema.safeParse(query.from).success) throw badRequest("invalid 'from' date");
|
|
if (query.to !== undefined && !reportDateSchema.safeParse(query.to).success) throw badRequest("invalid 'to' date");
|
|
const fromRaw = query.from as string | undefined;
|
|
const toRaw = query.to as string | undefined;
|
|
const from = fromRaw ? new Date(fromRaw) : undefined;
|
|
const to = toRaw ? new Date(toRaw) : undefined;
|
|
if (from && isNaN(from.getTime())) throw badRequest("invalid 'from' date");
|
|
if (to && isNaN(to.getTime())) throw badRequest("invalid 'to' date");
|
|
if (from && to && from > to) throw badRequest("from must not be after to");
|
|
return (from || to) ? { from, to } : undefined;
|
|
}
|
|
|
|
export function parseCostLimit(query: Record<string, unknown>) {
|
|
const raw = query.limit;
|
|
if (raw == null || raw === "") return 100;
|
|
const limit = typeof raw === "number" || typeof raw === "string" ? Number(raw) : Number.NaN;
|
|
if (!Number.isInteger(limit) || limit <= 0 || limit > 500) {
|
|
throw badRequest("invalid 'limit' value");
|
|
}
|
|
return limit;
|
|
}
|
|
|
|
export function costRoutes(
|
|
db: Db,
|
|
options: { pluginWorkerManager?: PluginWorkerManager } = {},
|
|
) {
|
|
const router = Router();
|
|
const heartbeat = heartbeatService(db, {
|
|
pluginWorkerManager: options.pluginWorkerManager,
|
|
});
|
|
const budgetHooks = {
|
|
cancelWorkForScope: heartbeat.cancelBudgetScopeWork,
|
|
};
|
|
const costs = costService(db, budgetHooks);
|
|
const finance = financeService(db);
|
|
const budgets = budgetService(db, budgetHooks);
|
|
const companies = companyService(db);
|
|
const agents = agentService(db);
|
|
const issues = issueService(db);
|
|
const access = accessService(db);
|
|
|
|
function costReaderActor(req: Parameters<typeof assertCompanyAccess>[0], companyId: string) {
|
|
assertBoard(req);
|
|
assertCompanyAccess(req, companyId);
|
|
return { userId: getActorInfo(req).actorId,
|
|
readOnly: req.actor.memberships?.some(member => member.companyId === companyId && member.membershipRole === "viewer") ?? false };
|
|
}
|
|
|
|
async function subscriptionActor(req: Parameters<typeof assertCompanyAccess>[0], companyId: string) {
|
|
return { ...costReaderActor(req, companyId), canManage: await canManageAiConnections(db, req, companyId) };
|
|
}
|
|
|
|
router.get("/companies/:companyId/costs/data-quality", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
const actor = costReaderActor(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
res.json(await costDataQualityReport(db, companyId, !actor.readOnly, parseCostDateRange(req.query)));
|
|
});
|
|
router.post("/companies/:companyId/costs/retry-accounting", validate(retryAccountingSchema), async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
const actor = costReaderActor(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
if (actor.readOnly) throw forbidden("Viewers cannot retry cost recording");
|
|
res.json(await accountingIntegrityService(db, budgetHooks).retry(companyId, req.body.runId, actor.userId));
|
|
});
|
|
|
|
router.get("/companies/:companyId/costs/subscriptions", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
const actor = await subscriptionActor(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
res.json(await subscriptionCostReport(db, companyId, actor, parseCostDateRange(req.query)));
|
|
});
|
|
router.post("/companies/:companyId/costs/subscriptions/refresh", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
const actor = await subscriptionActor(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
if (actor.readOnly) throw forbidden("Viewers cannot refresh subscription accounts");
|
|
void refreshCompanySubscriptions(db, companyId, actor.userId);
|
|
res.status(202).json({ status: "accepted" });
|
|
});
|
|
router.patch("/companies/:companyId/costs/subscriptions/:subscriptionId", validate(subscriptionPriceSchema), async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
const actor = await subscriptionActor(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const id = z.string().uuid().safeParse(req.params.subscriptionId);
|
|
if (!id.success) throw badRequest("Invalid subscription ID");
|
|
res.json(await subscriptionService(db).updatePrice(companyId, id.data, req.body, actor));
|
|
});
|
|
router.post("/companies/:companyId/costs/subscriptions/:subscriptionId/link", validate(mergeSubscriptionsSchema), async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
const actor = await subscriptionActor(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const id = z.string().uuid().safeParse(req.params.subscriptionId);
|
|
if (!id.success) throw badRequest("Invalid subscription ID");
|
|
res.json(await subscriptionService(db).merge(companyId, id.data, req.body.targetId, req.body.expectedRevision, req.body.targetRevision, actor));
|
|
});
|
|
|
|
async function resolveIssueByRef(rawId: string) {
|
|
const identifier = normalizeIssueIdentifier(rawId);
|
|
if (identifier) {
|
|
return issues.getByIdentifier(identifier);
|
|
}
|
|
return issues.getById(rawId);
|
|
}
|
|
|
|
async function assertCompanyCostReadAllowed(req: Parameters<typeof assertCompanyAccess>[0], res: any, companyId: string) {
|
|
const decision = await access.decide({
|
|
actor: req.actor,
|
|
action: "company_scope:read",
|
|
resource: { type: "company", companyId },
|
|
});
|
|
if (decision.allowed) return true;
|
|
res.status(403).json({ error: "Costs are outside this actor's authorization boundary" });
|
|
return false;
|
|
}
|
|
|
|
async function assertIssueCostReadAllowed(req: Parameters<typeof assertCompanyAccess>[0], res: any, issue: {
|
|
id: string;
|
|
companyId: string;
|
|
projectId: string | null;
|
|
parentId: string | null;
|
|
assigneeAgentId: string | null;
|
|
assigneeUserId: string | null;
|
|
status: string;
|
|
}) {
|
|
const decision = await access.decide({
|
|
actor: req.actor,
|
|
action: "issue:read",
|
|
resource: {
|
|
type: "issue",
|
|
companyId: issue.companyId,
|
|
issueId: issue.id,
|
|
projectId: issue.projectId,
|
|
parentIssueId: issue.parentId,
|
|
assigneeAgentId: issue.assigneeAgentId,
|
|
assigneeUserId: issue.assigneeUserId,
|
|
status: issue.status,
|
|
},
|
|
});
|
|
if (decision.allowed) return true;
|
|
res.status(403).json({ error: "Issue costs are outside this actor's authorization boundary" });
|
|
return false;
|
|
}
|
|
|
|
router.post("/companies/:companyId/cost-events", validate(createCostEventSchema), async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
|
|
if (req.actor.type === "agent" && req.actor.agentId !== req.body.agentId) {
|
|
res.status(403).json({ error: "Agent can only report its own costs" });
|
|
return;
|
|
}
|
|
|
|
const actor = getActorInfo(req);
|
|
const event = await costs.createEvent(companyId, {
|
|
...req.body,
|
|
occurredAt: new Date(req.body.occurredAt),
|
|
}, { actorType: actor.actorType, actorId: actor.actorId, agentId: actor.agentId });
|
|
|
|
res.status(201).json(event);
|
|
});
|
|
|
|
router.post("/companies/:companyId/finance-events", validate(createFinanceEventSchema), async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
assertBoard(req);
|
|
|
|
const actor = getActorInfo(req);
|
|
const event = await finance.createEvent(companyId, {
|
|
...req.body,
|
|
occurredAt: new Date(req.body.occurredAt),
|
|
}, { actorType: actor.actorType, actorId: actor.actorId, agentId: actor.agentId });
|
|
|
|
res.status(201).json(event);
|
|
});
|
|
|
|
router.get("/companies/:companyId/costs/summary", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const range = parseCostDateRange(req.query);
|
|
const summary = await costs.summary(companyId, range);
|
|
res.json(summary);
|
|
});
|
|
|
|
router.get("/issues/:id/cost-summary", async (req, res) => {
|
|
const rawId = req.params.id as string;
|
|
const issue = await getAccessibleResource(req, res, resolveIssueByRef(rawId), "Issue not found");
|
|
if (!issue) return;
|
|
if (!(await assertIssueCostReadAllowed(req, res, issue))) return;
|
|
const excludeRoot = req.query.excludeRoot === "true" || req.query.excludeRoot === "1";
|
|
const summary = await costs.issueTreeSummary(issue.companyId, issue.id, { excludeRoot });
|
|
res.json(summary);
|
|
});
|
|
|
|
router.get("/companies/:companyId/costs/by-user", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const range = parseCostDateRange(req.query);
|
|
res.json(await costs.byUser(companyId, range));
|
|
});
|
|
|
|
router.get("/companies/:companyId/costs/by-agent", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const range = parseCostDateRange(req.query);
|
|
const rows = await costs.byAgent(companyId, range);
|
|
res.json(rows);
|
|
});
|
|
|
|
router.get("/companies/:companyId/costs/by-agent-model", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const range = parseCostDateRange(req.query);
|
|
const rows = await costs.byAgentModel(companyId, range);
|
|
res.json(rows);
|
|
});
|
|
|
|
router.get("/companies/:companyId/costs/by-provider", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const range = parseCostDateRange(req.query);
|
|
const rows = await costs.byProvider(companyId, range);
|
|
res.json(rows);
|
|
});
|
|
|
|
router.get("/companies/:companyId/costs/by-biller", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const range = parseCostDateRange(req.query);
|
|
const rows = await costs.byBiller(companyId, range);
|
|
res.json(rows);
|
|
});
|
|
|
|
router.get("/companies/:companyId/costs/finance-summary", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const range = parseCostDateRange(req.query);
|
|
const summary = await finance.summary(companyId, range);
|
|
res.json(summary);
|
|
});
|
|
|
|
router.get("/companies/:companyId/costs/finance-by-biller", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const range = parseCostDateRange(req.query);
|
|
const rows = await finance.byBiller(companyId, range);
|
|
res.json(rows);
|
|
});
|
|
|
|
router.get("/companies/:companyId/costs/finance-by-kind", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const range = parseCostDateRange(req.query);
|
|
const rows = await finance.byKind(companyId, range);
|
|
res.json(rows);
|
|
});
|
|
|
|
router.get("/companies/:companyId/costs/finance-events", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const range = parseCostDateRange(req.query);
|
|
const limit = parseCostLimit(req.query);
|
|
const rows = await finance.list(companyId, range, limit);
|
|
res.json(rows);
|
|
});
|
|
|
|
router.get("/companies/:companyId/costs/window-spend", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const rows = await costs.windowSpend(companyId);
|
|
res.json(rows);
|
|
});
|
|
|
|
router.get("/companies/:companyId/costs/quota-windows", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
assertBoard(req);
|
|
// validate companyId resolves to a real company so the "__none__" sentinel
|
|
// and any forged ids are rejected before we touch provider credentials
|
|
const company = await companies.getById(companyId);
|
|
if (!company) {
|
|
res.status(404).json({ error: "Company not found" });
|
|
return;
|
|
}
|
|
const results = await fetchCompanyQuotaWindows(db, companyId, getActorInfo(req).actorId);
|
|
res.json(results);
|
|
});
|
|
|
|
router.get("/companies/:companyId/budgets/overview", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const overview = await budgets.overview(companyId);
|
|
res.json(overview);
|
|
});
|
|
|
|
router.post(
|
|
"/companies/:companyId/budgets/policies",
|
|
validate(upsertBudgetPolicySchema),
|
|
async (req, res) => {
|
|
assertBoard(req);
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
const summary = await budgets.upsertPolicy(companyId, req.body, req.actor.userId ?? "board");
|
|
res.json(summary);
|
|
},
|
|
);
|
|
|
|
router.post(
|
|
"/companies/:companyId/budget-incidents/:incidentId/resolve",
|
|
validate(resolveBudgetIncidentSchema),
|
|
async (req, res) => {
|
|
assertBoard(req);
|
|
const companyId = req.params.companyId as string;
|
|
const incidentId = req.params.incidentId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
const actorUserId = req.actor.userId ?? "board";
|
|
// A hard stop is the budget owner's card. When someone else raises or
|
|
// keeps the limit, read that before the incident closes, so the owner
|
|
// gets "Kept paused by @x" or "Limit raised by @x", the verbs of the
|
|
// card's buttons and of Decision history (PAP-749 plan §6; PAP-767).
|
|
const handledFor = await attentionHandledFor(
|
|
db, companyId, { sourceKind: "budget_alert", sourceId: incidentId }, req.actor.userId,
|
|
);
|
|
const incident = await budgets.resolveIncident(companyId, incidentId, req.body, actorUserId, req.actor.source ?? null, {
|
|
activityDetails: attentionHandledDetails(handledFor, req.body.action === "keep_paused" ? "kept_paused" : "limit_raised"),
|
|
});
|
|
res.json(incident);
|
|
},
|
|
);
|
|
|
|
router.get("/companies/:companyId/costs/by-project", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (!(await assertCompanyCostReadAllowed(req, res, companyId))) return;
|
|
const range = parseCostDateRange(req.query);
|
|
const rows = await costs.byProject(companyId, range);
|
|
res.json(rows);
|
|
});
|
|
|
|
router.patch("/companies/:companyId/budgets", validate(updateBudgetSchema), async (req, res) => {
|
|
assertBoard(req);
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
await budgets.upsertPolicy(
|
|
companyId,
|
|
{
|
|
scopeType: "company",
|
|
scopeId: companyId,
|
|
amount: req.body.budgetMonthlyCents,
|
|
isActive: req.body.budgetMonthlyCents > 0,
|
|
windowKind: "calendar_month_utc",
|
|
},
|
|
req.actor.userId ?? "board",
|
|
);
|
|
|
|
res.json(await companies.getById(companyId));
|
|
});
|
|
|
|
router.patch("/agents/:agentId/budgets", validate(updateBudgetSchema), async (req, res) => {
|
|
const agentId = req.params.agentId as string;
|
|
const agent = await getAccessibleResource(req, res, agents.getById(agentId), "Agent not found");
|
|
if (!agent) return;
|
|
|
|
assertBoard(req);
|
|
|
|
await budgets.upsertPolicy(
|
|
agent.companyId,
|
|
{
|
|
scopeType: "agent",
|
|
scopeId: agent.id,
|
|
amount: req.body.budgetMonthlyCents,
|
|
isActive: req.body.budgetMonthlyCents > 0,
|
|
windowKind: "calendar_month_utc",
|
|
},
|
|
req.actor.type === "board" ? req.actor.userId ?? "board" : null,
|
|
);
|
|
|
|
res.json(await agents.getById(agentId));
|
|
});
|
|
|
|
const integrity = accountingIntegrityService(db, budgetHooks);
|
|
const billing = billingReconciliationService(db, budgetHooks);
|
|
// Operational accounting evidence and repairs are board-only. These payloads
|
|
// can contain company-wide billing identifiers even for an agent's own run.
|
|
router.use("/companies/:companyId/accounting", (req, _res, next) => {
|
|
assertCompanyAccess(req, req.params.companyId as string); assertBoard(req); next();
|
|
});
|
|
router.post("/companies/:companyId/accounting/provider-costs/import", (req, _res, next) => {
|
|
// Unlike ledger inspection, this operation discloses a stored credential
|
|
// to a provider. Ordinary company membership does not authorize that use.
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
assertBoard(req);
|
|
const membership = req.actor.memberships?.find((item) => item.companyId === companyId);
|
|
if (req.actor.source !== "local_implicit" && !req.actor.isInstanceAdmin
|
|
&& !(membership?.status === "active" && ["owner", "admin"].includes(String(membership.membershipRole)))) {
|
|
throw forbidden("Company admin access required to import provider billing.");
|
|
}
|
|
next();
|
|
}, validate(importProviderCostsSchema), async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
assertBoard(req);
|
|
const result = await importProviderDailyCosts(db, companyId, req.body, getActorInfo(req).actorId);
|
|
res.json(result);
|
|
});
|
|
|
|
router.get("/companies/:companyId/accounting/health", async (req, res) => res.json(await integrity.health(req.params.companyId as string)));
|
|
router.get("/companies/:companyId/accounting/inspect", async (req, res) => res.json(await integrity.inspect(req.params.companyId as string)));
|
|
router.post("/companies/:companyId/accounting/repair", validate(repairAccountingSchema), async (req, res) =>
|
|
res.json(await integrity.repair(req.params.companyId as string, req.body.fingerprint, req.body.reason, getActorInfo(req).actorId)));
|
|
router.post("/companies/:companyId/accounting/retry", validate(retryAccountingSchema), async (req, res) =>
|
|
res.json(await integrity.retry(req.params.companyId as string, req.body.runId, getActorInfo(req).actorId)));
|
|
router.get("/companies/:companyId/accounting/invoices", async (req, res) => res.json(await billing.list(req.params.companyId as string)));
|
|
router.post("/companies/:companyId/accounting/invoices", validate(importBillingInvoiceSchema), async (req, res) =>
|
|
res.status(201).json(await billing.importInvoice(req.params.companyId as string, req.body, getActorInfo(req).actorId)));
|
|
router.get("/companies/:companyId/accounting/invoices/:invoiceId", async (req, res) =>
|
|
res.json(await billing.reconcile(req.params.companyId as string, req.params.invoiceId as string)));
|
|
router.get("/companies/:companyId/accounting/events/:eventId/adjustments", async (req, res) =>
|
|
res.json(await billing.adjustments(req.params.companyId as string, req.params.eventId as string)));
|
|
router.post("/companies/:companyId/accounting/events/:eventId/adjustments", validate(adjustCostSchema), async (req, res) =>
|
|
res.status(201).json(await billing.adjust(req.params.companyId as string, req.params.eventId as string, req.body, getActorInfo(req).actorId)));
|
|
|
|
return router;
|
|
}
|