clearing db actions in settings
This commit is contained in:
parent
5e623e8504
commit
d6336d1bec
@ -151,23 +151,11 @@ export const App: React.FC = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const handleClearDatabase = async () => {
|
||||
try {
|
||||
const result = await api.clearDatabase();
|
||||
toast.success("Database cleared", { description: `Deleted ${result.jobsDeleted} jobs.` });
|
||||
await loadJobs();
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to clear database";
|
||||
toast.error(message);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Header
|
||||
onRunPipeline={handleRunPipeline}
|
||||
onRefresh={loadJobs}
|
||||
onClearDatabase={handleClearDatabase}
|
||||
isPipelineRunning={isPipelineRunning}
|
||||
isLoading={isLoading}
|
||||
pipelineSources={pipelineSources}
|
||||
|
||||
@ -134,4 +134,16 @@ export async function clearDatabase(): Promise<{
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteJobsByStatus(status: string): Promise<{
|
||||
message: string;
|
||||
count: number;
|
||||
}> {
|
||||
return fetchApi<{
|
||||
message: string;
|
||||
count: number;
|
||||
}>(`/jobs/status/${status}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
}
|
||||
|
||||
// Bulk operations (intentionally none - processing is manual)
|
||||
|
||||
@ -40,7 +40,6 @@ import type { JobSource } from "../../shared/types";
|
||||
interface HeaderProps {
|
||||
onRunPipeline: () => void;
|
||||
onRefresh: () => void;
|
||||
onClearDatabase: () => void;
|
||||
isPipelineRunning: boolean;
|
||||
isLoading: boolean;
|
||||
pipelineSources: JobSource[];
|
||||
@ -50,7 +49,6 @@ interface HeaderProps {
|
||||
export const Header: React.FC<HeaderProps> = ({
|
||||
onRunPipeline,
|
||||
onRefresh,
|
||||
onClearDatabase,
|
||||
isPipelineRunning,
|
||||
isLoading,
|
||||
pipelineSources,
|
||||
@ -91,35 +89,6 @@ export const Header: React.FC<HeaderProps> = ({
|
||||
</Link>
|
||||
|
||||
<div className='flex flex-wrap items-center gap-1.5'>
|
||||
<AlertDialog>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Button
|
||||
variant='outline'
|
||||
size='sm'
|
||||
disabled={isLoading}
|
||||
title='Clear all jobs from database'
|
||||
>
|
||||
<Trash2 className='h-4 w-4' />
|
||||
<span className='hidden sm:inline'>Clear DB</span>
|
||||
</Button>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Clear all jobs?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
This deletes all jobs from the database. This action cannot be
|
||||
undone.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction onClick={onClearDatabase}>
|
||||
Clear database
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
|
||||
<Button
|
||||
variant='outline'
|
||||
size='sm'
|
||||
|
||||
@ -3,10 +3,22 @@
|
||||
*/
|
||||
|
||||
import React, { useEffect, useMemo, useState } from "react"
|
||||
import { Trash2 } from "lucide-react"
|
||||
import { toast } from "sonner"
|
||||
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
AlertDialogTrigger,
|
||||
} from "@/components/ui/alert-dialog"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { Checkbox } from "@/components/ui/checkbox"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { Separator } from "@/components/ui/separator"
|
||||
@ -218,7 +230,31 @@ export const SettingsPage: React.FC = () => {
|
||||
setIsSaving(false)
|
||||
}
|
||||
}
|
||||
const handleClearDatabase = async () => {
|
||||
try {
|
||||
setIsSaving(true)
|
||||
const result = await api.clearDatabase()
|
||||
toast.success("Database cleared", { description: `Deleted ${result.jobsDeleted} jobs.` })
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to clear database"
|
||||
toast.error(message)
|
||||
} finally {
|
||||
setIsSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleClearDiscovered = async () => {
|
||||
try {
|
||||
setIsSaving(true)
|
||||
const result = await api.deleteJobsByStatus("discovered")
|
||||
toast.success("Discovered jobs cleared", { description: `Deleted ${result.count} jobs.` })
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to clear discovered jobs"
|
||||
toast.error(message)
|
||||
} finally {
|
||||
setIsSaving(false)
|
||||
}
|
||||
}
|
||||
const handleReset = async () => {
|
||||
try {
|
||||
setIsSaving(true)
|
||||
@ -703,6 +739,80 @@ export const SettingsPage: React.FC = () => {
|
||||
Reset to default
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<Card className="border-destructive/20 bg-destructive/5">
|
||||
<CardHeader>
|
||||
<CardTitle className="text-destructive">Danger Zone</CardTitle>
|
||||
<CardDescription>
|
||||
Irreversible actions that modify your database.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="space-y-0.5">
|
||||
<div className="text-sm font-medium">Clear Discovered Jobs</div>
|
||||
<div className="text-xs text-muted-foreground">
|
||||
Delete all jobs with the status "discovered". Ready, applied, and rejected jobs are kept.
|
||||
</div>
|
||||
</div>
|
||||
<AlertDialog>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Button variant="destructive" size="sm" disabled={isLoading || isSaving}>
|
||||
<Trash2 className="mr-2 h-4 w-4" />
|
||||
Clear Discovered
|
||||
</Button>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Clear discovered jobs?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
This deletes all jobs with the status "discovered". This action cannot be undone.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction onClick={handleClearDiscovered} className="bg-destructive text-destructive-foreground hover:bg-destructive/90">
|
||||
Clear discovered
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</div>
|
||||
|
||||
<Separator className="bg-destructive/10" />
|
||||
|
||||
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="space-y-0.5">
|
||||
<div className="text-sm font-medium">Clear Database</div>
|
||||
<div className="text-xs text-muted-foreground">
|
||||
Delete all jobs and pipeline runs from the database.
|
||||
</div>
|
||||
</div>
|
||||
<AlertDialog>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Button variant="destructive" size="sm" disabled={isLoading || isSaving}>
|
||||
<Trash2 className="mr-2 h-4 w-4" />
|
||||
Clear Database
|
||||
</Button>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Clear all jobs?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
This deletes all jobs and pipeline runs from the database. This action cannot be undone.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction onClick={handleClearDatabase} className="bg-destructive text-destructive-foreground hover:bg-destructive/90">
|
||||
Clear database
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</main>
|
||||
)
|
||||
}
|
||||
|
||||
@ -632,6 +632,27 @@ apiRouter.post('/webhook/trigger', async (req: Request, res: Response) => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* DELETE /api/jobs/status/:status - Clear jobs with a specific status
|
||||
*/
|
||||
apiRouter.delete('/jobs/status/:status', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const status = req.params.status as JobStatus;
|
||||
const count = await jobsRepo.deleteJobsByStatus(status);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
message: `Cleared ${count} ${status} jobs`,
|
||||
count,
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Unknown error';
|
||||
res.status(500).json({ success: false, error: message });
|
||||
}
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// Database Management
|
||||
// ============================================================================
|
||||
|
||||
@ -209,6 +209,14 @@ export async function getUnscoredDiscoveredJobs(limit?: number): Promise<Job[]>
|
||||
return rows.map(mapRowToJob);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete jobs by status.
|
||||
*/
|
||||
export async function deleteJobsByStatus(status: JobStatus): Promise<number> {
|
||||
const result = await db.delete(jobs).where(eq(jobs.status, status)).run();
|
||||
return result.changes;
|
||||
}
|
||||
|
||||
// Helper to map database row to Job type
|
||||
function mapRowToJob(row: typeof jobs.$inferSelect): Job {
|
||||
return {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user