push to maple-street
This commit is contained in:
@@ -0,0 +1,289 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { auth } from "@/auth/config";
|
||||
import { hasPermission } from "@/auth/permissions";
|
||||
import type { SystemRole } from "@/auth/roles";
|
||||
import { listUsersForAdmin } from "@/db/users";
|
||||
import { listSkillsMatrix } from "@/db/assessments";
|
||||
import { getPositionSkills, listPositions, listSkills } from "@/db/positions";
|
||||
import { listServices } from "@/db/services";
|
||||
import { listNotifications } from "@/db/outbox";
|
||||
import { listRules } from "@/db/reminders";
|
||||
import { getEmailSettings } from "@/db/settings";
|
||||
import { ASSESSMENT_KINDS, USER_STATUSES } from "@/db/schema";
|
||||
import { SYSTEM_ROLES } from "@/auth/roles";
|
||||
import { CHURCH_TIMEZONE, formatInTimezone } from "@/lib/tz";
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from "@/components/ui/card";
|
||||
import {
|
||||
PeoplePanel,
|
||||
type ManagedUser,
|
||||
} from "@/components/app/admin/people-panel";
|
||||
import { ServicesPanel } from "@/components/app/admin/services-panel";
|
||||
import { RemindersPanel } from "@/components/app/admin/reminders-panel";
|
||||
import {
|
||||
NotificationsPanel,
|
||||
type NotificationRow,
|
||||
} from "@/components/app/admin/notifications-panel";
|
||||
import { EmailPanel } from "@/components/app/admin/email-panel";
|
||||
import { BackupPanel } from "@/components/app/admin/backup-panel";
|
||||
import {
|
||||
SkillsMatrixPanel,
|
||||
type MatrixRow,
|
||||
} from "@/components/app/admin/skills-matrix-panel";
|
||||
import {
|
||||
PositionsPanel,
|
||||
type PositionSummary,
|
||||
type RequirementDraft,
|
||||
} from "@/components/app/admin/positions-panel";
|
||||
|
||||
export const metadata = { title: "Admin" };
|
||||
|
||||
function stamp(date: Date): string {
|
||||
return formatInTimezone(date, CHURCH_TIMEZONE, {
|
||||
timeZone: CHURCH_TIMEZONE,
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
hour: "numeric",
|
||||
minute: "2-digit",
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Admin hub. Server-rendered data, client action panels:
|
||||
* - People: invite + approve/deny (B1)
|
||||
* - Skill levels: the competency matrix + verify-level form (C3)
|
||||
* - Positions: seats and their requirements editor (B4)
|
||||
* - Services: recurring templates + the idempotent instance generator (D1/D2)
|
||||
* - Reminder rules: offset sends used at publish time (E7)
|
||||
* - Notification log: outbox state, retry, send-test (E6)
|
||||
*
|
||||
* Guarded by `admin.settings`, the admin-only permission; coordinators and
|
||||
* volunteers are sent back to the schedule.
|
||||
*/
|
||||
export default async function AdminPage() {
|
||||
const session = await auth();
|
||||
const role = (session?.user as { systemRole?: SystemRole } | undefined)?.systemRole;
|
||||
if (!hasPermission(role, "admin.settings")) redirect("/schedule");
|
||||
|
||||
const [users, rules, log, services, matrix, positions, skillRows, emailSettings] =
|
||||
await Promise.all([
|
||||
listUsersForAdmin(),
|
||||
listRules(),
|
||||
listNotifications({ limit: 25 }),
|
||||
listServices({ active: false }),
|
||||
listSkillsMatrix(),
|
||||
listPositions({ active: false }),
|
||||
listSkills({ active: true }),
|
||||
getEmailSettings(),
|
||||
]);
|
||||
|
||||
const requirementSets = await Promise.all(
|
||||
positions.map((position) => getPositionSkills(position.id)),
|
||||
);
|
||||
const requirementsByPosition: Record<string, RequirementDraft[]> = {};
|
||||
positions.forEach((position, index) => {
|
||||
requirementsByPosition[position.id] = requirementSets[index].map((row) => ({
|
||||
skillId: row.skillId,
|
||||
weight: row.weight,
|
||||
requiredLevel: row.requiredLevel,
|
||||
isCore: row.isCore,
|
||||
}));
|
||||
});
|
||||
|
||||
const matrixRows: MatrixRow[] = matrix.map((row) => ({
|
||||
userId: row.userId,
|
||||
userName: row.userName,
|
||||
email: row.email,
|
||||
skillId: row.skillId,
|
||||
skillName: row.skillName,
|
||||
categoryName: row.categoryName ?? "Uncategorized",
|
||||
selfComfort: row.selfComfort,
|
||||
verifiedLevel: row.verifiedLevel,
|
||||
lastServedOn: row.lastServedOn ?? null,
|
||||
}));
|
||||
|
||||
const managedUsers: ManagedUser[] = users.map((user) => ({
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
username: user.username,
|
||||
firstName: user.firstName,
|
||||
lastName: user.lastName,
|
||||
displayName: user.displayName,
|
||||
systemRole: user.systemRole,
|
||||
status: user.status,
|
||||
phoneE164: user.phoneE164,
|
||||
timezone: user.timezone,
|
||||
emergencyContact: user.emergencyContact,
|
||||
notes: user.notes,
|
||||
deactivatedAt: user.deactivatedAt ? user.deactivatedAt.toISOString() : null,
|
||||
passwordSet: user.passwordSet,
|
||||
}));
|
||||
|
||||
const positionRows: PositionSummary[] = positions.map((position) => ({
|
||||
id: position.id,
|
||||
name: position.name,
|
||||
defaultHeadcount: position.defaultHeadcount,
|
||||
callTimeOffsetMinutes: position.callTimeOffsetMinutes,
|
||||
active: position.active,
|
||||
}));
|
||||
|
||||
const rows: NotificationRow[] = log.map((n) => ({
|
||||
id: n.id,
|
||||
templateKey: n.templateKey,
|
||||
subject: typeof n.payload.subject === "string" ? n.payload.subject : null,
|
||||
toAddress: n.toAddress,
|
||||
status: n.status,
|
||||
attempts: n.attempts,
|
||||
scheduledFor: stamp(n.scheduledFor),
|
||||
sentAt: n.sentAt ? stamp(n.sentAt) : null,
|
||||
error: n.error,
|
||||
}));
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<h1 className="text-lg font-semibold tracking-tight">Admin</h1>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
People, competency, positions, and outbound messages for the media
|
||||
team.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">People</CardTitle>
|
||||
<CardDescription>
|
||||
Add people, approve pending sign-ups, and edit every profile and
|
||||
access field. Deactivate to keep a person's history; delete only
|
||||
removes accounts with no scheduling history.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<PeoplePanel
|
||||
users={managedUsers}
|
||||
roles={SYSTEM_ROLES}
|
||||
statuses={USER_STATUSES}
|
||||
skills={skillRows.map((skill) => ({ id: skill.id, name: skill.name }))}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Skill levels</CardTitle>
|
||||
<CardDescription>
|
||||
Who is signed off on what. Verifying records who moved the level and
|
||||
why; the volunteer's own comfort rating is never changed here.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<SkillsMatrixPanel rows={matrixRows} kinds={ASSESSMENT_KINDS} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Positions & requirements</CardTitle>
|
||||
<CardDescription>
|
||||
The seats each service needs and the skills they require. A position
|
||||
saved as publishable needs at least one core skill.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<PositionsPanel
|
||||
positions={positionRows}
|
||||
skills={skillRows.map((skill) => ({ id: skill.id, name: skill.name }))}
|
||||
requirementsByPosition={requirementsByPosition}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Services & schedule</CardTitle>
|
||||
<CardDescription>
|
||||
Recurring service templates, and instance generation for the weeks
|
||||
volunteers will be scheduled into.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<ServicesPanel
|
||||
services={services.map((service) => ({
|
||||
id: service.id,
|
||||
name: service.name,
|
||||
startTimeLocal: service.startTimeLocal,
|
||||
durationMinutes: service.durationMinutes,
|
||||
location: service.location ?? null,
|
||||
active: service.active,
|
||||
}))}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Reminder rules</CardTitle>
|
||||
<CardDescription>
|
||||
Offsets are relative to each service or event start; negative days
|
||||
are before. Service reminders are queued when a week is published,
|
||||
event reminders by the notification cron. Every offset and time is
|
||||
editable.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<RemindersPanel rules={rules} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Email delivery</CardTitle>
|
||||
<CardDescription>
|
||||
Send through the team's Gmail account with an app password.
|
||||
Nothing is stored in plain text, and the password is never shown
|
||||
again after saving.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<EmailPanel
|
||||
emailFrom={emailSettings.emailFrom}
|
||||
notificationEmail={emailSettings.notificationEmail}
|
||||
smtpUser={emailSettings.smtpUser}
|
||||
smtpConfigured={emailSettings.smtpConfigured}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Backup & restore</CardTitle>
|
||||
<CardDescription>
|
||||
Download every database record and uploaded file in one portable
|
||||
ZIP, or restore a backup after a redeploy. Restores are admin-only
|
||||
and require an explicit confirmation.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<BackupPanel />
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card id="notifications">
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Notifications</CardTitle>
|
||||
<CardDescription>
|
||||
The outbox log, newest first, with retry for anything that failed.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<NotificationsPanel rows={rows} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user