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 company with no job applied

Khuram Niazae8da45525e7
diff --git a/src/actions/company.actions.ts b/src/actions/company.actions.ts
index a786370..5a51cf1 100644
--- a/src/actions/company.actions.ts
+++ b/src/actions/company.actions.ts
@@ -197,3 +197,48 @@ export const getCompanyById = async (
}
}
};
+
+export const deleteCompanyById = async (
+ companyId: string
+): Promise<any | undefined> => {
+ try {
+ const user = await getCurrentUser();
+
+ if (!user) {
+ throw new Error("Not authenticated");
+ }
+
+ const experiences = await prisma.workExperience.count({
+ where: {
+ companyId,
+ },
+ });
+ if (experiences > 0) {
+ throw new Error(
+ `Company cannot be deleted due to its use in experience section of one of the resume! `
+ );
+ }
+ const jobs = await prisma.job.count({
+ where: {
+ companyId,
+ },
+ });
+
+ if (jobs > 0) {
+ throw new Error(
+ `Company cannot be deleted due to ${jobs} number of associated jobs! `
+ );
+ }
+
+ const res = await prisma.company.delete({
+ where: {
+ id: companyId,
+ createdBy: user.id,
+ },
+ });
+ return { res, success: true };
+ } catch (error) {
+ const msg = "Failed to delete company.";
+ return handleError(error, msg);
+ }
+};
diff --git a/src/components/admin/CompaniesContainer.tsx b/src/components/admin/CompaniesContainer.tsx
index 344cc0e..f62a1fb 100644
--- a/src/components/admin/CompaniesContainer.tsx
+++ b/src/components/admin/CompaniesContainer.tsx
@@ -47,8 +47,8 @@ function CompaniesContainer({
[recordsPerPage]
);
- const reloadCompanies = () => {
- loadCompanies(1);
+ const reloadCompanies = (page = 1) => {
+ loadCompanies(page);
};
const resetEditCompany = () => {
@@ -92,6 +92,7 @@ function CompaniesContainer({
{!loading ? (
<CompaniesTable
companies={companies}
+ reloadCompanies={reloadCompanies}
currentPage={currentPage}
totalPages={totalPages}
recordsPerPage={recordsPerPage}
diff --git a/src/components/admin/CompaniesTable.tsx b/src/components/admin/CompaniesTable.tsx
index ff388c0..6ebcd1a 100644
--- a/src/components/admin/CompaniesTable.tsx
+++ b/src/components/admin/CompaniesTable.tsx
@@ -1,5 +1,5 @@
"use client";
-import { Button } from "../ui/button";
+import { Button, buttonVariants } from "../ui/button";
import Image from "next/image";
import {
@@ -18,11 +18,26 @@ import {
TableRow,
} from "../ui/table";
import { Company } from "@/models/job.model";
-import { MoreHorizontal, Pencil } from "lucide-react";
+import { MoreHorizontal, Pencil, Trash } from "lucide-react";
import { TablePagination } from "../TablePagination";
+import { useState } from "react";
+import {
+ AlertDialog,
+ AlertDialogAction,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogDescription,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
+} from "../ui/alert-dialog";
+import { deleteCompanyById } from "@/actions/company.actions";
+import { toast } from "../ui/use-toast";
+import { redirect } from "next/navigation";
type CompaniesTableProps = {
companies: Company[];
+ reloadCompanies: (p: number) => void;
currentPage: number;
totalPages: number;
totalCompanies: number;
@@ -31,8 +46,17 @@ type CompaniesTableProps = {
editCompany: (id: string) => void;
};
+type AlertDialog = {
+ openState: boolean;
+ title?: string;
+ description?: string;
+ deleteAction: boolean;
+ itemId?: string;
+};
+
function CompaniesTable({
companies,
+ reloadCompanies,
currentPage,
totalPages,
totalCompanies,
@@ -40,9 +64,53 @@ function CompaniesTable({
onPageChange,
editCompany,
}: CompaniesTableProps) {
+ const [alert, setAlert] = useState<AlertDialog>({
+ openState: false,
+ deleteAction: false,
+ });
const startPostIndex = (currentPage - 1) * recordsPerPage + 1;
const endPostIndex = Math.min(currentPage * recordsPerPage, totalCompanies);
+ const onDeleteCompany = (company: Company) => {
+ if (company._count?.jobsApplied! > 0) {
+ setAlert({
+ openState: true,
+ title: "Applied jobs exist!",
+ description:
+ "Associated jobs applied must be 0 to be able to delete this company",
+ deleteAction: false,
+ });
+ } 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,
+ });
+ }
+ };
+
+ const deleteCompany = async (companyId: string | undefined) => {
+ if (companyId) {
+ const { res, success, message } = await deleteCompanyById(companyId);
+ if (success) {
+ toast({
+ variant: "success",
+ description: `Company has been deleted successfully`,
+ });
+ reloadCompanies(currentPage);
+ } else {
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description: message,
+ });
+ }
+ }
+ };
+
return (
<>
<Table>
@@ -97,7 +165,13 @@ function CompaniesTable({
<Pencil className="mr-2 h-4 w-4" />
Edit Company
</DropdownMenuItem>
- <DropdownMenuItem>Delete</DropdownMenuItem>
+ <DropdownMenuItem
+ className="text-red-600 cursor-pointer"
+ onClick={() => onDeleteCompany(company)}
+ >
+ <Trash className="mr-2 h-4 w-4" />
+ Delete
+ </DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</TableCell>
@@ -121,6 +195,28 @@ function CompaniesTable({
onPageChange={onPageChange}
/>
)}
+ <AlertDialog
+ open={alert.openState}
+ onOpenChange={() => setAlert({ openState: false, deleteAction: false })}
+ >
+ <AlertDialogContent>
+ <AlertDialogHeader>
+ <AlertDialogTitle>{alert.title}</AlertDialogTitle>
+ <AlertDialogDescription>{alert.description}</AlertDialogDescription>
+ </AlertDialogHeader>
+ <AlertDialogFooter>
+ <AlertDialogCancel>Cancel</AlertDialogCancel>
+ {alert.deleteAction && (
+ <AlertDialogAction
+ className={buttonVariants({ variant: "destructive" })}
+ onClick={() => deleteCompany(alert.itemId)}
+ >
+ Delete
+ </AlertDialogAction>
+ )}
+ </AlertDialogFooter>
+ </AlertDialogContent>
+ </AlertDialog>
</>
);
}