Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

Delete job title and location

Khuram Niazeef3d72ed31b
diff --git a/src/actions/jobLocation.actions.ts b/src/actions/jobLocation.actions.ts
index 7e96cb8..ee0c556 100644
--- a/src/actions/jobLocation.actions.ts
+++ b/src/actions/jobLocation.actions.ts
@@ -1,5 +1,6 @@
"use server";
import prisma from "@/lib/db";
+import { handleError } from "@/lib/utils";
import { getCurrentUser } from "@/utils/user.utils";
export const getAllJobLocations = async (): Promise<any | undefined> => {
@@ -8,8 +9,7 @@ export const getAllJobLocations = async (): Promise<any | undefined> => {
return list;
} catch (error) {
const msg = "Failed to fetch job location list. ";
- console.error(msg, error);
- throw new Error(msg);
+ return handleError(error, msg);
}
};
@@ -66,7 +66,63 @@ export const getJobLocationsList = async (
return { data, total };
} catch (error) {
const msg = "Failed to fetch job location list. ";
- console.error(msg, error);
- throw new Error(msg);
+ return handleError(error, msg);
+ }
+};
+
+export const deleteJobLocationById = async (
+ locationId: string
+): Promise<any | undefined> => {
+ try {
+ const user = await getCurrentUser();
+
+ if (!user) {
+ throw new Error("Not authenticated");
+ }
+
+ const experiences = await prisma.workExperience.count({
+ where: {
+ locationId,
+ },
+ });
+ if (experiences > 0) {
+ throw new Error(
+ `Job location cannot be deleted due to its use in experience section of one of the resume! `
+ );
+ }
+
+ const educations = await prisma.education.count({
+ where: {
+ locationId,
+ },
+ });
+ if (educations > 0) {
+ throw new Error(
+ `Job location cannot be deleted due to its use in education section of one of the resume! `
+ );
+ }
+
+ const jobs = await prisma.job.count({
+ where: {
+ locationId,
+ },
+ });
+
+ if (jobs > 0) {
+ throw new Error(
+ `Location cannot be deleted due to ${jobs} number of associated jobs! `
+ );
+ }
+
+ const res = await prisma.location.delete({
+ where: {
+ id: locationId,
+ createdBy: user.id,
+ },
+ });
+ return { res, success: true };
+ } catch (error) {
+ const msg = "Failed to delete job location.";
+ return handleError(error, msg);
}
};
diff --git a/src/actions/jobtitle.actions.ts b/src/actions/jobtitle.actions.ts
index 8fd2794..e01011e 100644
--- a/src/actions/jobtitle.actions.ts
+++ b/src/actions/jobtitle.actions.ts
@@ -1,5 +1,6 @@
"use server";
import prisma from "@/lib/db";
+import { handleError } from "@/lib/utils";
import { getCurrentUser } from "@/utils/user.utils";
export const getAllJobTitles = async (): Promise<any | undefined> => {
@@ -16,8 +17,7 @@ export const getAllJobTitles = async (): Promise<any | undefined> => {
return list;
} catch (error) {
const msg = "Failed to fetch job title list. ";
- console.error(msg, error);
- throw new Error(msg);
+ return handleError(error, msg);
}
};
@@ -74,8 +74,7 @@ export const getJobTitleList = async (
return { data, total };
} catch (error) {
const msg = "Failed to fetch job title list. ";
- console.error(msg, error);
- throw new Error(msg);
+ return handleError(error, msg);
}
};
@@ -100,7 +99,51 @@ export const createJobTitle = async (
return upsertedTitle;
} catch (error) {
const msg = "Failed to create job title. ";
- console.error(msg, error);
- throw new Error(msg);
+ return handleError(error, msg);
+ }
+};
+
+export const deleteJobTitleById = async (
+ jobTitleId: string
+): Promise<any | undefined> => {
+ try {
+ const user = await getCurrentUser();
+
+ if (!user) {
+ throw new Error("Not authenticated");
+ }
+
+ const experiences = await prisma.workExperience.count({
+ where: {
+ jobTitleId,
+ },
+ });
+ if (experiences > 0) {
+ throw new Error(
+ `Job title cannot be deleted due to its use in experience section of one of the resume! `
+ );
+ }
+ const jobs = await prisma.job.count({
+ where: {
+ jobTitleId,
+ },
+ });
+
+ if (jobs > 0) {
+ throw new Error(
+ `Job title cannot be deleted due to ${jobs} number of associated jobs! `
+ );
+ }
+
+ const res = await prisma.jobTitle.delete({
+ where: {
+ id: jobTitleId,
+ createdBy: user.id,
+ },
+ });
+ return { res, success: true };
+ } catch (error) {
+ const msg = "Failed to delete job title.";
+ return handleError(error, msg);
}
};
diff --git a/src/components/DeleteAlertDialog.tsx b/src/components/DeleteAlertDialog.tsx
index d2b6c18..b5adfcf 100644
--- a/src/components/DeleteAlertDialog.tsx
+++ b/src/components/DeleteAlertDialog.tsx
@@ -15,6 +15,9 @@ interface DeleteAlertDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
onDelete: () => void;
+ alertTitle?: string;
+ alertDescription?: string;
+ deleteAction?: boolean;
}
export function DeleteAlertDialog({
@@ -22,27 +25,29 @@ export function DeleteAlertDialog({
open,
onOpenChange,
onDelete,
+ alertTitle,
+ alertDescription = "This action cannot be undone. This will permanently delete and remove data from server.",
+ deleteAction = true,
}: DeleteAlertDialogProps) {
return (
<AlertDialog open={open} onOpenChange={onOpenChange}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
- Are you sure you want to delete this {pageTitle}?
+ {alertTitle ?? `Are you sure you want to delete this ${pageTitle}?`}
</AlertDialogTitle>
- <AlertDialogDescription>
- This action cannot be undone. This will permanently delete and
- remove data from server.
- </AlertDialogDescription>
+ <AlertDialogDescription>{alertDescription}</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
- <AlertDialogAction
- className={buttonVariants({ variant: "destructive" })}
- onClick={onDelete}
- >
- Delete
- </AlertDialogAction>
+ {deleteAction && (
+ <AlertDialogAction
+ className={buttonVariants({ variant: "destructive" })}
+ onClick={onDelete}
+ >
+ Delete
+ </AlertDialogAction>
+ )}
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
diff --git a/src/components/admin/CompaniesTable.tsx b/src/components/admin/CompaniesTable.tsx
index adf915d..b671466 100644
--- a/src/components/admin/CompaniesTable.tsx
+++ b/src/components/admin/CompaniesTable.tsx
@@ -19,10 +19,10 @@ import {
import { Company } from "@/models/job.model";
import { MoreHorizontal, Pencil, Trash } from "lucide-react";
import { useState } from "react";
-import { AlertDialog } from "../ui/alert-dialog";
import { deleteCompanyById } from "@/actions/company.actions";
import { toast } from "../ui/use-toast";
import { DeleteAlertDialog } from "../DeleteAlertDialog";
+import { AlertDialog } from "@/models/alertDialog.model";
type CompaniesTableProps = {
companies: Company[];
@@ -30,14 +30,6 @@ type CompaniesTableProps = {
editCompany: (id: string) => void;
};
-type AlertDialog = {
- openState: boolean;
- title?: string;
- description?: string;
- deleteAction: boolean;
- itemId?: string;
-};
-
function CompaniesTable({
companies,
reloadCompanies,
@@ -60,9 +52,6 @@ function CompaniesTable({
} else {
setAlert({
openState: true,
- title: "Are you sure you want to delete this job?",
- description:
- "This action cannot be undone. This will permanently delete and remove data from server.",
deleteAction: true,
itemId: company.id,
});
@@ -162,6 +151,9 @@ function CompaniesTable({
open={alert.openState}
onOpenChange={() => setAlert({ openState: false, deleteAction: false })}
onDelete={() => deleteCompany(alert.itemId)}
+ alertTitle={alert.title}
+ alertDescription={alert.description}
+ deleteAction={alert.deleteAction}
/>
</>
);
diff --git a/src/components/admin/JobLocationsTable.tsx b/src/components/admin/JobLocationsTable.tsx
index b73ce44..8b877b3 100644
--- a/src/components/admin/JobLocationsTable.tsx
+++ b/src/components/admin/JobLocationsTable.tsx
@@ -1,4 +1,6 @@
"use client";
+import { useState } from "react";
+import { DeleteAlertDialog } from "../DeleteAlertDialog";
import { Button } from "../ui/button";
import {
@@ -17,7 +19,10 @@ import {
TableRow,
} from "../ui/table";
import { JobLocation } from "@/models/job.model";
-import { MoreHorizontal } from "lucide-react";
+import { MoreHorizontal, Trash } from "lucide-react";
+import { AlertDialog } from "@/models/alertDialog.model";
+import { toast } from "../ui/use-toast";
+import { deleteJobLocationById } from "@/actions/jobLocation.actions";
type JobLocationsTableProps = {
jobLocations: JobLocation[];
@@ -28,6 +33,45 @@ function JobLocationsTable({
jobLocations,
reloadJobLocations,
}: JobLocationsTableProps) {
+ const [alert, setAlert] = useState<AlertDialog>({
+ openState: false,
+ deleteAction: false,
+ });
+ const onDeleteJobLocation = (location: JobLocation) => {
+ if (location._count?.jobsApplied! > 0) {
+ setAlert({
+ openState: true,
+ title: "Applied jobs exist!",
+ description:
+ "Associated jobs applied must be 0 to be able to delete this job location",
+ deleteAction: false,
+ });
+ } else {
+ setAlert({
+ openState: true,
+ deleteAction: true,
+ itemId: location.id,
+ });
+ }
+ };
+ const deleteJobLocation = async (locationId: string) => {
+ if (locationId) {
+ const { success, message } = await deleteJobLocationById(locationId);
+ if (success) {
+ toast({
+ variant: "success",
+ description: `Job location has been deleted successfully`,
+ });
+ reloadJobLocations();
+ } else {
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description: message,
+ });
+ }
+ }
+ };
return (
<>
<Table>
@@ -63,7 +107,13 @@ function JobLocationsTable({
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuLabel>Actions</DropdownMenuLabel>
- <DropdownMenuItem>Delete</DropdownMenuItem>
+ <DropdownMenuItem
+ className="text-red-600 cursor-pointer"
+ onClick={() => onDeleteJobLocation(location)}
+ >
+ <Trash className="mr-2 h-4 w-4" />
+ Delete
+ </DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</TableCell>
@@ -72,6 +122,15 @@ function JobLocationsTable({
})}
</TableBody>
</Table>
+ <DeleteAlertDialog
+ pageTitle="location"
+ open={alert.openState}
+ onOpenChange={() => setAlert({ openState: false, deleteAction: false })}
+ onDelete={() => deleteJobLocation(alert.itemId!)}
+ alertTitle={alert.title}
+ alertDescription={alert.description}
+ deleteAction={alert.deleteAction}
+ />
</>
);
}
diff --git a/src/components/admin/JobTitlesTable.tsx b/src/components/admin/JobTitlesTable.tsx
index 1cf1cf2..ae92444 100644
--- a/src/components/admin/JobTitlesTable.tsx
+++ b/src/components/admin/JobTitlesTable.tsx
@@ -1,4 +1,5 @@
"use client";
+import { useState } from "react";
import { Button } from "../ui/button";
import {
@@ -17,7 +18,11 @@ import {
TableRow,
} from "../ui/table";
import { JobTitle } from "@/models/job.model";
-import { MoreHorizontal } from "lucide-react";
+import { MoreHorizontal, Trash } from "lucide-react";
+import { AlertDialog } from "@/models/alertDialog.model";
+import { DeleteAlertDialog } from "../DeleteAlertDialog";
+import { deleteJobTitleById } from "@/actions/jobtitle.actions";
+import { toast } from "../ui/use-toast";
type JobTitlesTableProps = {
jobTitles: JobTitle[];
@@ -25,6 +30,47 @@ type JobTitlesTableProps = {
};
function JobTitlesTable({ jobTitles, reloadJobTitles }: JobTitlesTableProps) {
+ const [alert, setAlert] = useState<AlertDialog>({
+ openState: false,
+ deleteAction: false,
+ });
+
+ const onDeleteJobTitle = (title: JobTitle) => {
+ if (title._count?.jobs! > 0) {
+ setAlert({
+ openState: true,
+ title: "Applied jobs exist!",
+ description:
+ "Associated jobs applied must be 0 to be able to delete this job title",
+ deleteAction: false,
+ });
+ } else {
+ setAlert({
+ openState: true,
+ deleteAction: true,
+ itemId: title.id,
+ });
+ }
+ };
+ const deleteJobTitle = async (titleId: string) => {
+ if (titleId) {
+ const { success, message } = await deleteJobTitleById(titleId);
+ if (success) {
+ toast({
+ variant: "success",
+ description: `Job title has been deleted successfully`,
+ });
+ reloadJobTitles();
+ } else {
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description: message,
+ });
+ }
+ }
+ };
+
return (
<>
<Table>
@@ -60,7 +106,13 @@ function JobTitlesTable({ jobTitles, reloadJobTitles }: JobTitlesTableProps) {
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuLabel>Actions</DropdownMenuLabel>
- <DropdownMenuItem>Delete</DropdownMenuItem>
+ <DropdownMenuItem
+ className="text-red-600 cursor-pointer"
+ onClick={() => onDeleteJobTitle(title)}
+ >
+ <Trash className="mr-2 h-4 w-4" />
+ Delete
+ </DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</TableCell>
@@ -69,6 +121,15 @@ function JobTitlesTable({ jobTitles, reloadJobTitles }: JobTitlesTableProps) {
})}
</TableBody>
</Table>
+ <DeleteAlertDialog
+ pageTitle="title"
+ open={alert.openState}
+ onOpenChange={() => setAlert({ openState: false, deleteAction: false })}
+ onDelete={() => deleteJobTitle(alert.itemId!)}
+ alertTitle={alert.title}
+ alertDescription={alert.description}
+ deleteAction={alert.deleteAction}
+ />
</>
);
}
diff --git a/src/models/alertDialog.model.ts b/src/models/alertDialog.model.ts
new file mode 100644
index 0000000..a77bbd9
--- /dev/null
+++ b/src/models/alertDialog.model.ts
@@ -0,0 +1,7 @@
+export type AlertDialog = {
+ openState: boolean;
+ title?: string;
+ description?: string;
+ deleteAction: boolean;
+ itemId?: string;
+};