Refactor scheduler module and enhance scheduled message handling
- Updated `SchedulerValidationError` to include error codes for better error management. - Renamed functions for clarity: `listSchedules` to `listScheduledMessagesDashboard` and `createSchedule` to `createScheduledMessageDashboard`. - Improved validation logic for cron expressions and scheduling times. - Enhanced the `deleteSchedule` function to ensure proper handling of existing scheduled messages. - Adjusted database query order and limits for better performance in fetching scheduled messages.
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { type NextRequest, NextResponse } from 'next/server';
|
||||
import { requireGuildAccess, toApiErrorResponse } from '@/lib/auth';
|
||||
import { writeDashboardAudit } from '@/lib/audit';
|
||||
import { deleteScheduledMessageDashboard } from '@/lib/module-configs/scheduler';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
interface RouteParams {
|
||||
params: Promise<{ guildId: string; scheduleId: string }>;
|
||||
}
|
||||
|
||||
export async function DELETE(_request: NextRequest, { params }: RouteParams) {
|
||||
const { guildId, scheduleId } = await params;
|
||||
try {
|
||||
const session = await requireGuildAccess(guildId);
|
||||
const deleted = await deleteScheduledMessageDashboard(guildId, scheduleId);
|
||||
if (!deleted) {
|
||||
return NextResponse.json({ error: 'Schedule not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
await writeDashboardAudit(prisma, {
|
||||
guildId,
|
||||
actorUserId: session.user.id,
|
||||
action: 'scheduler.delete',
|
||||
path: `/dashboard/${guildId}/scheduler`,
|
||||
before: { id: scheduleId }
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (error) {
|
||||
return toApiErrorResponse(error);
|
||||
}
|
||||
}
|
||||
51
apps/webui/src/app/api/guilds/[guildId]/scheduler/route.ts
Normal file
51
apps/webui/src/app/api/guilds/[guildId]/scheduler/route.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { ScheduledMessageDashboardCreateSchema } from '@nexumi/shared';
|
||||
import { type NextRequest, NextResponse } from 'next/server';
|
||||
import { requireGuildAccess, toApiErrorResponse } from '@/lib/auth';
|
||||
import { writeDashboardAudit } from '@/lib/audit';
|
||||
import {
|
||||
createScheduledMessageDashboard,
|
||||
listScheduledMessagesDashboard,
|
||||
SchedulerValidationError
|
||||
} from '@/lib/module-configs/scheduler';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
interface RouteParams {
|
||||
params: Promise<{ guildId: string }>;
|
||||
}
|
||||
|
||||
export async function GET(_request: NextRequest, { params }: RouteParams) {
|
||||
const { guildId } = await params;
|
||||
try {
|
||||
await requireGuildAccess(guildId);
|
||||
const schedules = await listScheduledMessagesDashboard(guildId);
|
||||
return NextResponse.json({ schedules });
|
||||
} catch (error) {
|
||||
return toApiErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
const { guildId } = await params;
|
||||
try {
|
||||
const session = await requireGuildAccess(guildId);
|
||||
const body = await request.json();
|
||||
const input = ScheduledMessageDashboardCreateSchema.parse(body);
|
||||
|
||||
const schedule = await createScheduledMessageDashboard(guildId, session.user.id, input);
|
||||
|
||||
await writeDashboardAudit(prisma, {
|
||||
guildId,
|
||||
actorUserId: session.user.id,
|
||||
action: 'scheduler.create',
|
||||
path: `/dashboard/${guildId}/scheduler`,
|
||||
after: schedule
|
||||
});
|
||||
|
||||
return NextResponse.json(schedule, { status: 201 });
|
||||
} catch (error) {
|
||||
if (error instanceof SchedulerValidationError) {
|
||||
return NextResponse.json({ error: error.code }, { status: 400 });
|
||||
}
|
||||
return toApiErrorResponse(error);
|
||||
}
|
||||
}
|
||||
13
apps/webui/src/app/dashboard/[guildId]/feeds/loading.tsx
Normal file
13
apps/webui/src/app/dashboard/[guildId]/feeds/loading.tsx
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
|
||||
export default function FeedsLoading() {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="space-y-2">
|
||||
<Skeleton className="h-8 w-48" />
|
||||
<Skeleton className="h-4 w-80" />
|
||||
</div>
|
||||
<Skeleton className="h-72 rounded-lg" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
14
apps/webui/src/app/dashboard/[guildId]/scheduler/loading.tsx
Normal file
14
apps/webui/src/app/dashboard/[guildId]/scheduler/loading.tsx
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
|
||||
export default function SchedulerLoading() {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="space-y-2">
|
||||
<Skeleton className="h-8 w-48" />
|
||||
<Skeleton className="h-4 w-80" />
|
||||
</div>
|
||||
<Skeleton className="h-64 rounded-lg" />
|
||||
<Skeleton className="h-48 rounded-lg" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
22
apps/webui/src/app/dashboard/[guildId]/scheduler/page.tsx
Normal file
22
apps/webui/src/app/dashboard/[guildId]/scheduler/page.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
import { SchedulerManager } from '@/components/modules/scheduler-manager';
|
||||
import { getLocale, t } from '@/lib/i18n';
|
||||
import { listScheduledMessagesDashboard } from '@/lib/module-configs/scheduler';
|
||||
|
||||
interface SchedulerPageProps {
|
||||
params: Promise<{ guildId: string }>;
|
||||
}
|
||||
|
||||
export default async function SchedulerPage({ params }: SchedulerPageProps) {
|
||||
const { guildId } = await params;
|
||||
const [locale, schedules] = await Promise.all([getLocale(), listScheduledMessagesDashboard(guildId)]);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold">{t(locale, 'modules.scheduler.label')}</h1>
|
||||
<p className="text-sm text-muted-foreground">{t(locale, 'modules.scheduler.description')}</p>
|
||||
</div>
|
||||
<SchedulerManager guildId={guildId} initialSchedules={schedules} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user