Archived
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>
137 lines
4.2 KiB
TypeScript
137 lines
4.2 KiB
TypeScript
import { z } from 'zod';
|
|
import { TRPCError } from '@trpc/server';
|
|
import { desc, eq, sql, sum } from 'drizzle-orm';
|
|
import { deal, gtmStep, area } from '@cockpit/db';
|
|
import { DealSchemas } from '@cockpit/shared';
|
|
import { router, protectedProcedure } from '../trpc.js';
|
|
|
|
const SALES_AREA_SLUG = 'vertrieb';
|
|
|
|
async function getSalesAreaId(db: import('@cockpit/db').Db): Promise<string> {
|
|
const a = (await db.select().from(area).where(eq(area.slug, SALES_AREA_SLUG)).limit(1))[0];
|
|
if (!a) throw new Error('Sales-Area fehlt — bitte seed laufen lassen.');
|
|
return a.id;
|
|
}
|
|
|
|
export const dealRouter = router({
|
|
list: protectedProcedure.query(async ({ ctx }) => {
|
|
return ctx.db.select().from(deal).orderBy(desc(deal.enteredStageAt));
|
|
}),
|
|
|
|
kpis: protectedProcedure.query(async ({ ctx }) => {
|
|
const allDeals = await ctx.db.select().from(deal);
|
|
const open = allDeals.filter((d) => !d.wonAt);
|
|
const won = allDeals.filter((d) => d.wonAt);
|
|
|
|
const weightedPipeline = open.reduce((acc, d) => acc + (d.valueEur * d.probability) / 100, 0);
|
|
|
|
// „Laufendes Quartal" = aktuelles Kalenderquartal (Server-Zeit).
|
|
const now = new Date();
|
|
const qStart = new Date(now.getFullYear(), Math.floor(now.getMonth() / 3) * 3, 1);
|
|
const wonThisQuarter = won.filter((d) => d.wonAt && d.wonAt >= qStart);
|
|
const wonValueQuarter = wonThisQuarter.reduce((acc, d) => acc + (d.wonValueEur ?? 0), 0);
|
|
|
|
return {
|
|
weightedPipelineEur: Math.round(weightedPipeline),
|
|
openDealCount: open.length,
|
|
wonCountQuarter: wonThisQuarter.length,
|
|
wonValueQuarterEur: wonValueQuarter,
|
|
};
|
|
}),
|
|
|
|
create: protectedProcedure.input(DealSchemas.createDealInput).mutation(async ({ ctx, input }) => {
|
|
const areaId = await getSalesAreaId(ctx.db);
|
|
const [created] = await ctx.db
|
|
.insert(deal)
|
|
.values({
|
|
areaId,
|
|
name: input.name,
|
|
ownerUserId: input.ownerUserId ?? null,
|
|
valueEur: input.valueEur,
|
|
probability: input.probability,
|
|
stage: input.stage,
|
|
})
|
|
.returning();
|
|
return created;
|
|
}),
|
|
|
|
updateStage: protectedProcedure
|
|
.input(DealSchemas.updateDealStageInput)
|
|
.mutation(async ({ ctx, input }) => {
|
|
const [updated] = await ctx.db
|
|
.update(deal)
|
|
.set({
|
|
stage: input.stage,
|
|
enteredStageAt: new Date(),
|
|
wonAt: input.stage === 'abschluss' ? new Date() : null,
|
|
wonValueEur:
|
|
input.stage === 'abschluss'
|
|
? sql`COALESCE(${deal.valueEur} * ${deal.probability} / 100, 0)`
|
|
: null,
|
|
})
|
|
.where(eq(deal.id, input.dealId))
|
|
.returning();
|
|
if (!updated) throw new TRPCError({ code: 'NOT_FOUND', message: 'Deal nicht gefunden.' });
|
|
return updated;
|
|
}),
|
|
});
|
|
|
|
export const gtmRouter = router({
|
|
list: protectedProcedure
|
|
.input(z.object({ product: z.string().optional() }))
|
|
.query(async ({ ctx, input }) => {
|
|
if (input.product) {
|
|
return ctx.db
|
|
.select()
|
|
.from(gtmStep)
|
|
.where(eq(gtmStep.product, input.product))
|
|
.orderBy(gtmStep.order);
|
|
}
|
|
return ctx.db.select().from(gtmStep).orderBy(gtmStep.product, gtmStep.order);
|
|
}),
|
|
|
|
create: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
product: z.string().min(1).max(100),
|
|
title: z.string().min(1).max(200),
|
|
ownerUserId: z.string().uuid().optional(),
|
|
}),
|
|
)
|
|
.mutation(async ({ ctx, input }) => {
|
|
const existing = await ctx.db
|
|
.select()
|
|
.from(gtmStep)
|
|
.where(eq(gtmStep.product, input.product));
|
|
const [created] = await ctx.db
|
|
.insert(gtmStep)
|
|
.values({
|
|
product: input.product,
|
|
title: input.title,
|
|
ownerUserId: input.ownerUserId ?? null,
|
|
order: existing.length,
|
|
})
|
|
.returning();
|
|
return created;
|
|
}),
|
|
|
|
setStatus: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
id: z.string().uuid(),
|
|
status: z.enum(['open', 'in_progress', 'done']),
|
|
}),
|
|
)
|
|
.mutation(async ({ ctx, input }) => {
|
|
const [updated] = await ctx.db
|
|
.update(gtmStep)
|
|
.set({ status: input.status })
|
|
.where(eq(gtmStep.id, input.id))
|
|
.returning();
|
|
return updated;
|
|
}),
|
|
});
|
|
|
|
// linter
|
|
void sum;
|