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
+141
View File
@@ -0,0 +1,141 @@
import { listAssignmentBookings } from "@planarchy/application";
import { emitNotificationCreated } from "../sse/event-bus.js";
type DbClient = Parameters<typeof listAssignmentBookings>[0] & {
project: {
findUnique: (args: {
where: { id: string };
select: { id: true; name: true; shortCode: true; budgetCents: true };
}) => Promise<{
id: string;
name: string;
shortCode: string;
budgetCents: number;
} | null>;
};
notification: {
findFirst: (args: {
where: {
entityId: string;
entityType: string;
type: string;
};
select: { id: true };
}) => Promise<{ id: string } | null>;
create: (args: {
data: {
userId: string;
type: string;
category: string;
priority: string;
title: string;
body: string;
entityId: string;
entityType: string;
link: string;
channel: string;
};
}) => Promise<{ id: string; userId: string }>;
};
user: {
findMany: (args: {
where: { systemRole: { in: string[] } };
select: { id: true };
}) => Promise<Array<{ id: string }>>;
};
};
const THRESHOLDS = [
{ percent: 100, type: "BUDGET_OVERRUN_100", label: "100%", priority: "URGENT" as const },
{ percent: 80, type: "BUDGET_OVERRUN_80", label: "80%", priority: "HIGH" as const },
] as const;
/**
* Check whether a project's current spend has crossed 80% or 100% of its budget.
* Creates in-app notifications for all managers/admins when a threshold is
* crossed for the first time.
*
* Safe to call repeatedly -- duplicate notifications are prevented by checking
* whether a notification with the same entityId + type already exists.
*/
export async function checkBudgetThresholds(
db: DbClient,
projectId: string,
): Promise<void> {
const project = await db.project.findUnique({
where: { id: projectId },
select: { id: true, name: true, shortCode: true, budgetCents: true },
});
if (!project || project.budgetCents <= 0) return;
// Compute total spend from assignment bookings (same logic as listWithCosts)
const bookings = await listAssignmentBookings(db, {
startDate: new Date("1900-01-01T00:00:00.000Z"),
endDate: new Date("2100-12-31T23:59:59.999Z"),
projectIds: [projectId],
});
let totalCostCents = 0;
for (const booking of bookings) {
const days =
(new Date(booking.endDate).getTime() -
new Date(booking.startDate).getTime()) /
(1000 * 60 * 60 * 24) +
1;
totalCostCents += booking.dailyCostCents * days;
}
totalCostCents = Math.round(totalCostCents);
const spendPercent = (totalCostCents / project.budgetCents) * 100;
for (const threshold of THRESHOLDS) {
if (spendPercent < threshold.percent) continue;
// Check if we already sent this alert
const existing = await db.notification.findFirst({
where: {
entityId: projectId,
entityType: "project_budget",
type: threshold.type,
},
select: { id: true },
});
if (existing) continue;
// Get all managers and admins
const managers = await db.user.findMany({
where: { systemRole: { in: ["ADMIN", "MANAGER"] } },
select: { id: true },
});
const formattedSpend = (totalCostCents / 100).toLocaleString("de-DE", {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
const formattedBudget = (project.budgetCents / 100).toLocaleString(
"de-DE",
{ minimumFractionDigits: 2, maximumFractionDigits: 2 },
);
for (const manager of managers) {
const notification = await db.notification.create({
data: {
userId: manager.id,
type: threshold.type,
category: "NOTIFICATION",
priority: threshold.priority,
title: `Budget alert: ${project.name} has reached ${threshold.label} of budget`,
body: `Project ${project.shortCode} "${project.name}" has spent ${formattedSpend} EUR of ${formattedBudget} EUR budget (${Math.round(spendPercent)}%).`,
entityId: projectId,
entityType: "project_budget",
link: `/projects/${projectId}`,
channel: "in_app",
},
});
emitNotificationCreated(manager.id, notification.id);
}
}
}
+95
View File
@@ -0,0 +1,95 @@
import { Redis } from "ioredis";
const REDIS_URL = process.env["REDIS_URL"] ?? "redis://localhost:6380";
const KEY_PREFIX = "dashboard:";
const DEFAULT_TTL_SECONDS = 60;
let redis: Redis | null = null;
function getRedis(): Redis {
if (!redis) {
redis = new Redis(REDIS_URL, {
lazyConnect: false,
enableReadyCheck: false,
// Don't let cache operations block the app if Redis is slow
commandTimeout: 2000,
});
redis.on("error", (e: unknown) => {
console.error("[Redis cache]", e);
});
}
return redis;
}
/**
* Retrieve a cached value by key.
* Returns null on cache miss or if Redis is unavailable.
*/
export async function cacheGet<T>(key: string): Promise<T | null> {
try {
const raw = await getRedis().get(`${KEY_PREFIX}${key}`);
if (raw === null) return null;
return JSON.parse(raw) as T;
} catch {
// Redis down or parse error — fall through to DB
return null;
}
}
/**
* Store a value in the cache with a TTL.
* Silently ignores errors when Redis is unavailable.
*/
export async function cacheSet(
key: string,
value: unknown,
ttlSeconds: number = DEFAULT_TTL_SECONDS,
): Promise<void> {
try {
await getRedis().set(
`${KEY_PREFIX}${key}`,
JSON.stringify(value),
"EX",
ttlSeconds,
);
} catch {
// Redis down — silently ignore, data will be served from DB next time
}
}
/**
* Delete all keys matching a glob pattern (e.g. "dashboard:*").
* The pattern is automatically prefixed with the KEY_PREFIX unless it already starts with it.
*/
export async function cacheInvalidate(pattern: string): Promise<void> {
try {
const fullPattern = pattern.startsWith(KEY_PREFIX)
? pattern
: `${KEY_PREFIX}${pattern}`;
const r = getRedis();
let cursor = "0";
do {
const [nextCursor, keys] = await r.scan(
cursor,
"MATCH",
fullPattern,
"COUNT",
100,
);
cursor = nextCursor;
if (keys.length > 0) {
await r.del(...keys);
}
} while (cursor !== "0");
} catch {
// Redis down — nothing to invalidate
}
}
/**
* Invalidate all dashboard cache entries.
* Convenience wrapper used from mutation hooks.
*/
export async function invalidateDashboardCache(): Promise<void> {
await cacheInvalidate("*");
}
+164
View File
@@ -0,0 +1,164 @@
import { emitNotificationCreated } from "../sse/event-bus.js";
type DbClient = {
estimate: {
findMany: (args: {
where: {
versions: {
some: {
status: string;
submittedAt: { lte: Date };
};
};
};
select: {
id: true;
name: true;
projectId: true;
versions: {
where: { status: string };
select: { id: true; versionNumber: true; submittedAt: true };
orderBy: { versionNumber: "desc" };
take: 1;
};
};
}) => Promise<
Array<{
id: string;
name: string;
projectId: string | null;
versions: Array<{
id: string;
versionNumber: number;
submittedAt: Date | null;
}>;
}>
>;
};
notification: {
findFirst: (args: {
where: {
entityId: string;
entityType: string;
type: string;
};
select: { id: true };
}) => Promise<{ id: string } | null>;
create: (args: {
data: {
userId: string;
type: string;
category: string;
priority: string;
title: string;
body: string;
entityId: string;
entityType: string;
link: string;
channel: string;
};
}) => Promise<{ id: string; userId: string }>;
};
user: {
findMany: (args: {
where: { systemRole: { in: string[] } };
select: { id: true };
}) => Promise<Array<{ id: string }>>;
};
};
const REMINDER_DAYS = 3;
/**
* Find all estimates that have a version in SUBMITTED status for longer than
* REMINDER_DAYS days and create a single reminder notification per estimate
* for all managers/admins.
*
* Returns the number of new reminders created.
*/
export async function checkPendingEstimateReminders(
db: DbClient,
): Promise<number> {
const cutoff = new Date();
cutoff.setDate(cutoff.getDate() - REMINDER_DAYS);
const pendingEstimates = await db.estimate.findMany({
where: {
versions: {
some: {
status: "SUBMITTED",
submittedAt: { lte: cutoff },
},
},
},
select: {
id: true,
name: true,
projectId: true,
versions: {
where: { status: "SUBMITTED" },
select: { id: true, versionNumber: true, submittedAt: true },
orderBy: { versionNumber: "desc" },
take: 1,
},
},
});
if (pendingEstimates.length === 0) return 0;
const managers = await db.user.findMany({
where: { systemRole: { in: ["ADMIN", "MANAGER"] } },
select: { id: true },
});
if (managers.length === 0) return 0;
let reminderCount = 0;
for (const estimate of pendingEstimates) {
const version = estimate.versions[0];
if (!version) continue;
// Check if we already sent a reminder for this version
const existing = await db.notification.findFirst({
where: {
entityId: version.id,
entityType: "estimate_approval_reminder",
type: "ESTIMATE_APPROVAL_REMINDER",
},
select: { id: true },
});
if (existing) continue;
const daysPending = version.submittedAt
? Math.floor(
(Date.now() - new Date(version.submittedAt).getTime()) /
(1000 * 60 * 60 * 24),
)
: REMINDER_DAYS;
for (const manager of managers) {
const notification = await db.notification.create({
data: {
userId: manager.id,
type: "ESTIMATE_APPROVAL_REMINDER",
category: "REMINDER",
priority: "HIGH",
title: `Estimate awaiting approval: ${estimate.name} (v${version.versionNumber})`,
body: `Estimate "${estimate.name}" version ${version.versionNumber} has been pending approval for ${daysPending} days.`,
entityId: version.id,
entityType: "estimate_approval_reminder",
link: `/estimates/${estimate.id}`,
channel: "in_app",
},
});
emitNotificationCreated(manager.id, notification.id);
}
reminderCount++;
}
return reminderCount;
}