Initial commit: everythingbutwork MVP
Internes Project-Management-Tool für ToolCTRL — ergänzend zu Plane (Tickets) und Outline (Doku). Umfasst Puls-Übersicht, Tagesrhythmus (Lock-in + Reflexion), Bereichs-Detail-Views (Entwicklung/Vertrieb/Marketing), Pflicht-Minimum-Check und Blocker-Lifecycle gemäß Projektmanagement-Leitfaden v3.0. Stack: pnpm-Monorepo · Vite + React 18 + Tailwind · Fastify + tRPC v11 + Drizzle ORM · Postgres 16 · Authentik OIDC (Phase 2, Dev-Auto-Login aktiv). Planungs-Artefakte unter _bmad-output/planning-artifacts/. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
commit
4f33509a0f
131 files changed
+34339
No files matched your search
@@ -0,0 +1,335 @@
|
||||
import { TRPCError } from '@trpc/server';
|
||||
import { and, eq, isNull, isNotNull, lt, asc, inArray } from 'drizzle-orm';
|
||||
import { dayEntry, todo, blocker, type Todo } from '@cockpit/db';
|
||||
import { DayEntrySchemas } from '@cockpit/shared';
|
||||
import { router, protectedProcedure } from '../trpc.js';
|
||||
import { todayIso } from '../lib/werktag.js';
|
||||
|
||||
export const dayEntryRouter = router({
|
||||
/**
|
||||
* Heutige ToDos aller aktiven Mitarbeiter, gruppiert pro User.
|
||||
* Pro PRD-Änderung 2026-05-25: Heutige ToDos sind teamweit sichtbar
|
||||
* (ersetzt die ältere private-vor-Publikation-Regel).
|
||||
*/
|
||||
todayTeam: protectedProcedure.query(async ({ ctx }) => {
|
||||
const date = todayIso();
|
||||
const entries = await ctx.db
|
||||
.select()
|
||||
.from(dayEntry)
|
||||
.where(eq(dayEntry.date, date));
|
||||
if (entries.length === 0) return [] as Array<{ userId: string; entryStatus: string; todos: Todo[] }>;
|
||||
const ids = entries.map((e) => e.id);
|
||||
const todos = await ctx.db
|
||||
.select()
|
||||
.from(todo)
|
||||
.where(inArray(todo.dayEntryId, ids))
|
||||
.orderBy(asc(todo.order));
|
||||
const todosByEntry = new Map<string, Todo[]>();
|
||||
for (const t of todos) {
|
||||
const arr = todosByEntry.get(t.dayEntryId) ?? [];
|
||||
arr.push(t);
|
||||
todosByEntry.set(t.dayEntryId, arr);
|
||||
}
|
||||
return entries.map((e) => ({
|
||||
userId: e.userId,
|
||||
entryStatus: e.status,
|
||||
todos: todosByEntry.get(e.id) ?? [],
|
||||
}));
|
||||
}),
|
||||
|
||||
/**
|
||||
* Offene ToDos der vergangenen Tage, die noch nicht übertragen wurden.
|
||||
* Werden auf der Heute-Seite vor dem Einstempeln eingeblendet (siehe FR-22 Erweiterung).
|
||||
*/
|
||||
pendingCarryOver: protectedProcedure.query(async ({ ctx }) => {
|
||||
return ctx.db
|
||||
.select({
|
||||
id: todo.id,
|
||||
title: todo.title,
|
||||
bezugKind: todo.bezugKind,
|
||||
bezugId: todo.bezugId,
|
||||
fromDate: todo.date,
|
||||
})
|
||||
.from(todo)
|
||||
.where(
|
||||
and(
|
||||
eq(todo.userId, ctx.user.id),
|
||||
eq(todo.status, 'open'),
|
||||
eq(todo.carriedOver, false),
|
||||
lt(todo.date, todayIso()),
|
||||
),
|
||||
)
|
||||
.orderBy(asc(todo.date), asc(todo.order))
|
||||
.limit(20);
|
||||
}),
|
||||
|
||||
/**
|
||||
* Heute-Status der eigenen Person.
|
||||
*/
|
||||
today: protectedProcedure.query(async ({ ctx }) => {
|
||||
const date = todayIso();
|
||||
const rows = await ctx.db
|
||||
.select()
|
||||
.from(dayEntry)
|
||||
.where(and(eq(dayEntry.userId, ctx.user.id), eq(dayEntry.date, date)))
|
||||
.limit(1);
|
||||
const entry = rows[0] ?? null;
|
||||
if (!entry) {
|
||||
return { date, entry: null, todos: [] as Todo[] };
|
||||
}
|
||||
const todos = await ctx.db
|
||||
.select()
|
||||
.from(todo)
|
||||
.where(eq(todo.dayEntryId, entry.id))
|
||||
.orderBy(todo.order);
|
||||
return { date, entry, todos };
|
||||
}),
|
||||
|
||||
/**
|
||||
* Morgen-Einstempeln + ToDo-Liste setzen.
|
||||
*/
|
||||
checkIn: protectedProcedure
|
||||
.input(DayEntrySchemas.checkInInput)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const date = input.date ?? todayIso();
|
||||
|
||||
// Verhindert Doppel-Einstempeln. Wenn bereits Einträge existieren, Fehler.
|
||||
const existing = await ctx.db
|
||||
.select()
|
||||
.from(dayEntry)
|
||||
.where(and(eq(dayEntry.userId, ctx.user.id), eq(dayEntry.date, date)))
|
||||
.limit(1);
|
||||
if (existing[0]) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Du bist heute schon locked in.',
|
||||
});
|
||||
}
|
||||
|
||||
const result = await ctx.db.transaction(async (tx) => {
|
||||
const [created] = await tx
|
||||
.insert(dayEntry)
|
||||
.values({
|
||||
userId: ctx.user.id,
|
||||
date,
|
||||
status: 'begun',
|
||||
checkedInAt: new Date(),
|
||||
})
|
||||
.returning();
|
||||
if (!created) throw new Error('DayEntry konnte nicht angelegt werden.');
|
||||
|
||||
const todoRows = await tx
|
||||
.insert(todo)
|
||||
.values(
|
||||
input.todos.map((t, idx) => ({
|
||||
dayEntryId: created.id,
|
||||
userId: ctx.user.id,
|
||||
date,
|
||||
title: t.title,
|
||||
order: idx,
|
||||
bezugKind: t.bezugKind ?? null,
|
||||
bezugId: t.bezugId ?? null,
|
||||
})),
|
||||
)
|
||||
.returning();
|
||||
|
||||
// Alle alten offenen ToDos der Person als "übertragen" markieren.
|
||||
// Egal, ob sie in der heutigen Liste landeten oder nicht — der User hat
|
||||
// beim Einstempeln über sie entschieden.
|
||||
const stale = await tx
|
||||
.select({ id: todo.id })
|
||||
.from(todo)
|
||||
.where(
|
||||
and(
|
||||
eq(todo.userId, ctx.user.id),
|
||||
eq(todo.status, 'open'),
|
||||
eq(todo.carriedOver, false),
|
||||
lt(todo.date, date),
|
||||
),
|
||||
);
|
||||
if (stale.length > 0) {
|
||||
await tx
|
||||
.update(todo)
|
||||
.set({ carriedOver: true })
|
||||
.where(
|
||||
inArray(
|
||||
todo.id,
|
||||
stale.map((s) => s.id),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return { entry: created, todos: todoRows, carriedOver: stale.length };
|
||||
});
|
||||
|
||||
return result;
|
||||
}),
|
||||
|
||||
/**
|
||||
* ToDos im Verlauf des Tages ändern (vor Reflexion).
|
||||
*/
|
||||
updateTodos: protectedProcedure
|
||||
.input(DayEntrySchemas.todoMutationInput)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const entries = await ctx.db
|
||||
.select()
|
||||
.from(dayEntry)
|
||||
.where(and(eq(dayEntry.id, input.dayEntryId), eq(dayEntry.userId, ctx.user.id)))
|
||||
.limit(1);
|
||||
const entry = entries[0];
|
||||
if (!entry) {
|
||||
throw new TRPCError({ code: 'NOT_FOUND', message: 'Tageseintrag nicht gefunden.' });
|
||||
}
|
||||
if (entry.status === 'full') {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Du hast schon reflektiert. ToDos sind eingefroren.',
|
||||
});
|
||||
}
|
||||
|
||||
const result = await ctx.db.transaction(async (tx) => {
|
||||
await tx.delete(todo).where(eq(todo.dayEntryId, entry.id));
|
||||
if (input.todos.length === 0) return { todos: [] as Todo[] };
|
||||
const newTodos = await tx
|
||||
.insert(todo)
|
||||
.values(
|
||||
input.todos.map((t, idx) => ({
|
||||
dayEntryId: entry.id,
|
||||
userId: ctx.user.id,
|
||||
date: entry.date,
|
||||
title: t.title,
|
||||
order: idx,
|
||||
bezugKind: t.bezugKind ?? null,
|
||||
bezugId: t.bezugId ?? null,
|
||||
})),
|
||||
)
|
||||
.returning();
|
||||
return { todos: newTodos };
|
||||
});
|
||||
return result;
|
||||
}),
|
||||
|
||||
/**
|
||||
* Abend-Reflexion. Setzt Ampel + Trend, optional Blocker.
|
||||
*/
|
||||
submitReflection: protectedProcedure
|
||||
.input(DayEntrySchemas.submitReflectionInput)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const entries = await ctx.db
|
||||
.select()
|
||||
.from(dayEntry)
|
||||
.where(and(eq(dayEntry.id, input.dayEntryId), eq(dayEntry.userId, ctx.user.id)))
|
||||
.limit(1);
|
||||
const entry = entries[0];
|
||||
if (!entry) {
|
||||
throw new TRPCError({ code: 'NOT_FOUND', message: 'Tageseintrag nicht gefunden.' });
|
||||
}
|
||||
if (entry.status === 'full' && entry.editLockedAt && entry.editLockedAt < new Date()) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Die Reflexion ist nicht mehr editierbar.',
|
||||
});
|
||||
}
|
||||
|
||||
// Pflicht-Trend bei Bezug=Projekt (Story 2.4)
|
||||
const todoRows = await ctx.db.select().from(todo).where(eq(todo.dayEntryId, entry.id));
|
||||
const hasProjectBezug = todoRows.some((t) => t.bezugKind === 'project');
|
||||
if (hasProjectBezug && !input.trend) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Bei Projektbezug ist der Trend Pflicht.',
|
||||
});
|
||||
}
|
||||
|
||||
const result = await ctx.db.transaction(async (tx) => {
|
||||
// ToDo-Statuswechsel.
|
||||
for (const t of input.todos) {
|
||||
await tx
|
||||
.update(todo)
|
||||
.set({
|
||||
status: t.status,
|
||||
dropReason: t.dropReason ?? null,
|
||||
completedAt: t.status === 'done' ? new Date() : null,
|
||||
droppedAt: t.status === 'dropped' ? new Date() : null,
|
||||
})
|
||||
.where(and(eq(todo.id, t.id), eq(todo.dayEntryId, entry.id)));
|
||||
}
|
||||
|
||||
let blockerRecord: { id: string } | null = null;
|
||||
if (input.blocker) {
|
||||
const [b] = await tx
|
||||
.insert(blocker)
|
||||
.values({
|
||||
title: input.blocker.title,
|
||||
description: input.blocker.description ?? null,
|
||||
openedBy: ctx.user.id,
|
||||
})
|
||||
.returning({ id: blocker.id });
|
||||
blockerRecord = b ?? null;
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const [updated] = await tx
|
||||
.update(dayEntry)
|
||||
.set({
|
||||
status: 'full',
|
||||
reflectedAt: now,
|
||||
reflectionText: input.reflectionText,
|
||||
ampel: input.ampel,
|
||||
trend: input.trend ?? null,
|
||||
url: input.url ?? null,
|
||||
blockerId: blockerRecord?.id ?? entry.blockerId,
|
||||
editLockedAt: new Date(now.getTime() + 30 * 60 * 1000),
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(eq(dayEntry.id, entry.id))
|
||||
.returning();
|
||||
if (!updated) throw new Error('Update fehlgeschlagen.');
|
||||
return { entry: updated, blocker: blockerRecord };
|
||||
});
|
||||
|
||||
return result;
|
||||
}),
|
||||
|
||||
/**
|
||||
* Konsolidierter Vortrags-Feed: alle bereits publizierten Einträge.
|
||||
* Nutzbar in Story 3.4 (Puls-Feed).
|
||||
*/
|
||||
feed: protectedProcedure.query(async ({ ctx }) => {
|
||||
const entries = await ctx.db
|
||||
.select()
|
||||
.from(dayEntry)
|
||||
.where(isNotNull(dayEntry.publishedAt))
|
||||
.orderBy(dayEntry.publishedAt)
|
||||
.limit(50);
|
||||
return entries;
|
||||
}),
|
||||
|
||||
/**
|
||||
* „Heute fehlt"-Liste (Anwesenheits-Übersicht).
|
||||
*/
|
||||
todayPresence: protectedProcedure.query(async ({ ctx }) => {
|
||||
const date = todayIso();
|
||||
const rows = await ctx.db
|
||||
.select({
|
||||
userId: dayEntry.userId,
|
||||
status: dayEntry.status,
|
||||
})
|
||||
.from(dayEntry)
|
||||
.where(eq(dayEntry.date, date));
|
||||
return rows;
|
||||
}),
|
||||
});
|
||||
|
||||
// Helper für Cron-Endpoint
|
||||
export async function publishYesterdaysEntries(
|
||||
ctx: { db: import('@cockpit/db').Db },
|
||||
yesterdayIso: string,
|
||||
): Promise<number> {
|
||||
const now = new Date();
|
||||
const result = await ctx.db
|
||||
.update(dayEntry)
|
||||
.set({ publishedAt: now })
|
||||
.where(and(eq(dayEntry.date, yesterdayIso), isNull(dayEntry.publishedAt)));
|
||||
return result.rowCount ?? 0;
|
||||
}
|
||||
Reference in new issue
Block a user