2026-03-12 08:50:31 -05:00
|
|
|
import { and, asc, eq, isNull } from "drizzle-orm";
|
2026-03-03 08:45:26 -06:00
|
|
|
import type { Db } from "@paperclipai/db";
|
|
|
|
|
import { goals } from "@paperclipai/db";
|
2026-02-16 13:31:58 -06:00
|
|
|
|
2026-03-12 08:50:31 -05:00
|
|
|
type GoalReader = Pick<Db, "select">;
|
|
|
|
|
|
|
|
|
|
export async function getDefaultCompanyGoal(db: GoalReader, companyId: string) {
|
|
|
|
|
const activeRootGoal = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(goals)
|
|
|
|
|
.where(
|
|
|
|
|
and(
|
|
|
|
|
eq(goals.companyId, companyId),
|
|
|
|
|
eq(goals.level, "company"),
|
|
|
|
|
eq(goals.status, "active"),
|
|
|
|
|
isNull(goals.parentId),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.orderBy(asc(goals.createdAt))
|
|
|
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
|
if (activeRootGoal) return activeRootGoal;
|
|
|
|
|
|
|
|
|
|
const anyRootGoal = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(goals)
|
|
|
|
|
.where(
|
|
|
|
|
and(
|
|
|
|
|
eq(goals.companyId, companyId),
|
|
|
|
|
eq(goals.level, "company"),
|
|
|
|
|
isNull(goals.parentId),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.orderBy(asc(goals.createdAt))
|
|
|
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
|
if (anyRootGoal) return anyRootGoal;
|
|
|
|
|
|
|
|
|
|
return db
|
|
|
|
|
.select()
|
|
|
|
|
.from(goals)
|
|
|
|
|
.where(and(eq(goals.companyId, companyId), eq(goals.level, "company")))
|
|
|
|
|
.orderBy(asc(goals.createdAt))
|
|
|
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 13:31:58 -06:00
|
|
|
export function goalService(db: Db) {
|
|
|
|
|
return {
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
list: (companyId: string) => db.select().from(goals).where(eq(goals.companyId, companyId)),
|
2026-02-16 13:31:58 -06:00
|
|
|
|
|
|
|
|
getById: (id: string) =>
|
|
|
|
|
db
|
|
|
|
|
.select()
|
|
|
|
|
.from(goals)
|
|
|
|
|
.where(eq(goals.id, id))
|
|
|
|
|
.then((rows) => rows[0] ?? null),
|
|
|
|
|
|
2026-03-12 08:50:31 -05:00
|
|
|
getDefaultCompanyGoal: (companyId: string) => getDefaultCompanyGoal(db, companyId),
|
|
|
|
|
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
create: (companyId: string, data: Omit<typeof goals.$inferInsert, "companyId">) =>
|
2026-02-16 13:31:58 -06:00
|
|
|
db
|
|
|
|
|
.insert(goals)
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
.values({ ...data, companyId })
|
2026-02-16 13:31:58 -06:00
|
|
|
.returning()
|
|
|
|
|
.then((rows) => rows[0]),
|
|
|
|
|
|
|
|
|
|
update: (id: string, data: Partial<typeof goals.$inferInsert>) =>
|
|
|
|
|
db
|
|
|
|
|
.update(goals)
|
|
|
|
|
.set({ ...data, updatedAt: new Date() })
|
|
|
|
|
.where(eq(goals.id, id))
|
|
|
|
|
.returning()
|
|
|
|
|
.then((rows) => rows[0] ?? null),
|
|
|
|
|
|
|
|
|
|
remove: (id: string) =>
|
|
|
|
|
db
|
|
|
|
|
.delete(goals)
|
|
|
|
|
.where(eq(goals.id, id))
|
|
|
|
|
.returning()
|
|
|
|
|
.then((rows) => rows[0] ?? null),
|
|
|
|
|
};
|
|
|
|
|
}
|