feat: Sprint 1 — staffing assign, dashboard cache, bulk ops, notifications

Staffing "Assign" Button:
- Inline assignment form on each suggestion card in StaffingPanel
- Pre-fills project, dates, hours from search criteria
- 1-click confirm creates allocation with PROPOSED status
- Success/error toasts, removes assigned suggestions from list

Dashboard Redis Caching:
- New cache utility (packages/api/src/lib/cache.ts) with get/set/invalidate
- All 5 dashboard queries wrapped with 60s TTL cache-aside pattern
- Auto-invalidation on allocation + project mutations (fire-and-forget)
- Graceful fallthrough to DB if Redis unavailable

Bulk Operations:
- CSV export for selected resources and projects (apps/web/src/lib/csv-export.ts)
- Project batch delete mutation with cascade (assignments, demands, rules)
- Export/Delete buttons added to BatchActionBar on both list pages

Budget Overrun Notifications:
- checkBudgetThresholds() alerts at 80% (HIGH) and 100% (URGENT)
- Called after every allocation mutation, duplicate-safe
- Targets ADMIN + MANAGER users with SSE delivery

Estimate Approval Reminders:
- checkPendingEstimateReminders() finds SUBMITTED versions > 3 days old
- Cron endpoint: GET /api/cron/estimate-reminders (optional CRON_SECRET auth)
- Creates in-app REMINDER notifications, duplicate-safe

Co-Authored-By: claude-flow <ruv@ruv.net>
This commit is contained in:
2026-03-19 20:43:36 +01:00
parent 0d78fe1770
commit 4118995319
14 changed files with 1042 additions and 71 deletions
+49
View File
@@ -29,7 +29,9 @@ import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { findUniqueOrThrow } from "../db/helpers.js";
import { anonymizeResource, getAnonymizationDirectory } from "../lib/anonymization.js";
import { checkBudgetThresholds } from "../lib/budget-alerts.js";
import { emitAllocationCreated, emitAllocationDeleted, emitAllocationUpdated, emitNotificationCreated } from "../sse/event-bus.js";
import { invalidateDashboardCache } from "../lib/cache.js";
import { createTRPCRouter, managerProcedure, protectedProcedure, requirePermission } from "../trpc.js";
import { PROJECT_BRIEF_SELECT, RESOURCE_BRIEF_SELECT, ROLE_BRIEF_SELECT } from "../db/selects.js";
@@ -242,6 +244,9 @@ export const allocationRouter = createTRPCRouter({
projectId: allocation.projectId,
resourceId: allocation.resourceId,
});
void invalidateDashboardCache();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
void checkBudgetThresholds(ctx.db as any, allocation.projectId);
return allocation;
}),
@@ -445,6 +450,7 @@ export const allocationRouter = createTRPCRouter({
projectId: demandRequirement.projectId,
resourceId: null,
});
void invalidateDashboardCache();
// Create staffing tasks for managers
const [project, roleEntity, managers] = await Promise.all([
@@ -487,6 +493,8 @@ export const allocationRouter = createTRPCRouter({
emitNotificationCreated(manager.id, task.id);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
void checkBudgetThresholds(ctx.db as any, demandRequirement.projectId);
return demandRequirement;
}),
@@ -508,6 +516,9 @@ export const allocationRouter = createTRPCRouter({
projectId: updated.projectId,
resourceId: null,
});
void invalidateDashboardCache();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
void checkBudgetThresholds(ctx.db as any, updated.projectId);
return updated;
}),
@@ -529,6 +540,9 @@ export const allocationRouter = createTRPCRouter({
projectId: assignment.projectId,
resourceId: assignment.resourceId,
});
void invalidateDashboardCache();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
void checkBudgetThresholds(ctx.db as any, assignment.projectId);
return assignment;
}),
@@ -551,6 +565,9 @@ export const allocationRouter = createTRPCRouter({
projectId: updated.projectId,
resourceId: updated.resourceId,
});
void invalidateDashboardCache();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
void checkBudgetThresholds(ctx.db as any, updated.projectId);
return updated;
}),
@@ -585,6 +602,9 @@ export const allocationRouter = createTRPCRouter({
});
emitAllocationDeleted(existing.id, existing.projectId);
void invalidateDashboardCache();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
void checkBudgetThresholds(ctx.db as any, existing.projectId);
return { success: true };
}),
@@ -607,6 +627,9 @@ export const allocationRouter = createTRPCRouter({
projectId: result.updatedDemandRequirement.projectId,
resourceId: null,
});
void invalidateDashboardCache();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
void checkBudgetThresholds(ctx.db as any, result.assignment.projectId);
return result;
}),
@@ -623,6 +646,9 @@ export const allocationRouter = createTRPCRouter({
if (result.updatedAllocation) {
emitAllocationUpdated(result.updatedAllocation);
}
void invalidateDashboardCache();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
void checkBudgetThresholds(ctx.db as any, result.createdAllocation.projectId as string);
return result;
}),
@@ -665,6 +691,9 @@ export const allocationRouter = createTRPCRouter({
projectId: updated.projectId,
resourceId: updated.resourceId,
});
void invalidateDashboardCache();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
void checkBudgetThresholds(ctx.db as any, updated.projectId);
return updated;
}),
@@ -699,6 +728,9 @@ export const allocationRouter = createTRPCRouter({
});
emitAllocationDeleted(existing.id, existing.projectId);
void invalidateDashboardCache();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
void checkBudgetThresholds(ctx.db as any, existing.projectId);
return { success: true };
}),
@@ -726,6 +758,9 @@ export const allocationRouter = createTRPCRouter({
});
emitAllocationDeleted(existing.entry.id, existing.projectId);
void invalidateDashboardCache();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
void checkBudgetThresholds(ctx.db as any, existing.projectId);
return { success: true };
}),
@@ -760,6 +795,13 @@ export const allocationRouter = createTRPCRouter({
for (const a of existing) {
emitAllocationDeleted(a.entry.id, a.projectId);
}
void invalidateDashboardCache();
// Check budget thresholds for each affected project
const affectedProjectIds = [...new Set(existing.map((a) => a.projectId))];
for (const pid of affectedProjectIds) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
void checkBudgetThresholds(ctx.db as any, pid);
}
return { count: existing.length };
}),
@@ -804,6 +846,13 @@ export const allocationRouter = createTRPCRouter({
for (const a of updated) {
emitAllocationUpdated({ id: a.id, projectId: a.projectId, resourceId: a.resourceId });
}
void invalidateDashboardCache();
// Check budget thresholds for each affected project
const affectedProjectIds = [...new Set(updated.map((a) => a.projectId))];
for (const pid of affectedProjectIds) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
void checkBudgetThresholds(ctx.db as any, pid);
}
return { count: updated.length };
}),
+54 -13
View File
@@ -8,9 +8,20 @@ import {
getDashboardTopValueResources,
} from "@planarchy/application";
import { anonymizeResources, getAnonymizationDirectory } from "../lib/anonymization.js";
import { cacheGet, cacheSet } from "../lib/cache.js";
const DEFAULT_TTL = 60; // seconds
export const dashboardRouter = createTRPCRouter({
getOverview: protectedProcedure.query(({ ctx }) => getDashboardOverview(ctx.db)),
getOverview: protectedProcedure.query(async ({ ctx }) => {
const cacheKey = "overview";
const cached = await cacheGet<Awaited<ReturnType<typeof getDashboardOverview>>>(cacheKey);
if (cached) return cached;
const result = await getDashboardOverview(ctx.db);
await cacheSet(cacheKey, result, DEFAULT_TTL);
return result;
}),
getPeakTimes: protectedProcedure
.input(
@@ -21,27 +32,40 @@ export const dashboardRouter = createTRPCRouter({
groupBy: z.enum(["project", "chapter", "resource"]).default("project"),
}),
)
.query(({ ctx, input }) =>
getDashboardPeakTimes(ctx.db, {
.query(async ({ ctx, input }) => {
const cacheKey = `peakTimes:${input.startDate}:${input.endDate}:${input.granularity}:${input.groupBy}`;
const cached = await cacheGet<Awaited<ReturnType<typeof getDashboardPeakTimes>>>(cacheKey);
if (cached) return cached;
const result = await getDashboardPeakTimes(ctx.db, {
startDate: new Date(input.startDate),
endDate: new Date(input.endDate),
granularity: input.granularity,
groupBy: input.groupBy,
}),
),
});
await cacheSet(cacheKey, result, DEFAULT_TTL);
return result;
}),
getTopValueResources: protectedProcedure
.input(z.object({ limit: z.number().int().min(1).max(50).default(10) }))
.query(async ({ ctx, input }) => {
const userRole =
(ctx.session.user as { role?: string } | undefined)?.role ?? "USER";
const cacheKey = `topValue:${input.limit}:${userRole}`;
const cached = await cacheGet<Awaited<ReturnType<typeof anonymizeResources>>>(cacheKey);
if (cached) return cached;
const [resources, directory] = await Promise.all([
getDashboardTopValueResources(ctx.db, {
limit: input.limit,
userRole:
(ctx.session.user as { role?: string } | undefined)?.role ?? "USER",
userRole,
}),
getAnonymizationDirectory(ctx.db),
]);
return anonymizeResources(resources, directory);
const result = anonymizeResources(resources, directory);
await cacheSet(cacheKey, result, DEFAULT_TTL);
return result;
}),
getDemand: protectedProcedure
@@ -52,13 +76,19 @@ export const dashboardRouter = createTRPCRouter({
groupBy: z.enum(["project", "person", "chapter"]).default("project"),
}),
)
.query(({ ctx, input }) =>
getDashboardDemand(ctx.db, {
.query(async ({ ctx, input }) => {
const cacheKey = `demand:${input.startDate}:${input.endDate}:${input.groupBy}`;
const cached = await cacheGet<Awaited<ReturnType<typeof getDashboardDemand>>>(cacheKey);
if (cached) return cached;
const result = await getDashboardDemand(ctx.db, {
startDate: new Date(input.startDate),
endDate: new Date(input.endDate),
groupBy: input.groupBy,
}),
),
});
await cacheSet(cacheKey, result, DEFAULT_TTL);
return result;
}),
getChargeabilityOverview: controllerProcedure
.input(
@@ -71,6 +101,15 @@ export const dashboardRouter = createTRPCRouter({
}),
)
.query(async ({ ctx, input }) => {
const cacheKey = `chargeability:${input.includeProposed}:${input.topN}:${input.watchlistThreshold}:${(input.countryIds ?? []).join(",")}:${input.departed ?? ""}`;
type ChargeResult = Awaited<ReturnType<typeof getDashboardChargeabilityOverview>>;
const cached = await cacheGet<{
top: unknown[];
watchlist: unknown[];
[key: string]: unknown;
}>(cacheKey);
if (cached) return cached;
const [overview, directory] = await Promise.all([
getDashboardChargeabilityOverview(ctx.db, {
includeProposed: input.includeProposed,
@@ -82,10 +121,12 @@ export const dashboardRouter = createTRPCRouter({
getAnonymizationDirectory(ctx.db),
]);
return {
const result = {
...overview,
top: anonymizeResources(overview.top, directory),
watchlist: anonymizeResources(overview.watchlist, directory),
};
await cacheSet(cacheKey, result, DEFAULT_TTL);
return result;
}),
});
+48 -1
View File
@@ -12,6 +12,7 @@ import { buildDynamicFieldWhereClauses } from "./custom-field-filters.js";
import { loadProjectPlanningReadModel } from "./project-planning-read-model.js";
import { adminProcedure, controllerProcedure, createTRPCRouter, managerProcedure, protectedProcedure, requirePermission } from "../trpc.js";
import { createDalleClient, isDalleConfigured, parseAiError } from "../ai-client.js";
import { invalidateDashboardCache } from "../lib/cache.js";
const MAX_COVER_SIZE = 4 * 1024 * 1024; // 4 MB base64 string length limit (client compresses before upload)
@@ -155,6 +156,7 @@ export const projectRouter = createTRPCRouter({
},
});
void invalidateDashboardCache();
return project;
}),
@@ -207,6 +209,7 @@ export const projectRouter = createTRPCRouter({
},
});
void invalidateDashboardCache();
return updated;
}),
@@ -214,10 +217,12 @@ export const projectRouter = createTRPCRouter({
.input(z.object({ id: z.string(), status: z.nativeEnum(ProjectStatus) }))
.mutation(async ({ ctx, input }) => {
requirePermission(ctx, PermissionKey.MANAGE_PROJECTS);
return ctx.db.project.update({
const result = await ctx.db.project.update({
where: { id: input.id },
data: { status: input.status },
});
void invalidateDashboardCache();
return result;
}),
batchUpdateStatus: managerProcedure
@@ -244,6 +249,7 @@ export const projectRouter = createTRPCRouter({
},
});
void invalidateDashboardCache();
return { count: updated.length };
}),
@@ -349,9 +355,50 @@ export const projectRouter = createTRPCRouter({
});
});
void invalidateDashboardCache();
return { id: input.id, name: project.name };
}),
batchDelete: adminProcedure
.input(
z.object({
ids: z.array(z.string()).min(1).max(50),
}),
)
.mutation(async ({ ctx, input }) => {
const projects = await ctx.db.project.findMany({
where: { id: { in: input.ids } },
select: { id: true, name: true, shortCode: true },
});
if (projects.length === 0) {
throw new TRPCError({ code: "NOT_FOUND", message: "No projects found" });
}
await ctx.db.$transaction(async (tx) => {
const ids = projects.map((p) => p.id);
await tx.assignment.deleteMany({ where: { projectId: { in: ids } } });
await tx.demandRequirement.deleteMany({ where: { projectId: { in: ids } } });
await tx.calculationRule.updateMany({
where: { projectId: { in: ids } },
data: { projectId: null },
});
await tx.project.deleteMany({ where: { id: { in: ids } } });
await tx.auditLog.create({
data: {
entityType: "Project",
entityId: ids.join(","),
action: "DELETE",
...(ctx.dbUser?.id ? { userId: ctx.dbUser.id } : {}),
changes: { before: projects } as never,
},
});
});
void invalidateDashboardCache();
return { count: projects.length };
}),
// ─── Cover Art ──────────────────────────────────────────────────────────────
generateCover: managerProcedure