@@ -67,7 +91,10 @@ const CustomerTable = ({ lists: customers }: { lists: CustomerInfo[] }) => {
|
-
+
{customers?.length === 0 && (
@@ -87,13 +114,27 @@ const CustomerTable = ({ lists: customers }: { lists: CustomerInfo[] }) => {
onHandlePreviousButton={onHandlePreviousButton}
/>
)}
+
>
)
}
export default CustomerTable
-const CustomerBody = ({ customers }: { customers: CustomerInfo[] }) => {
+const CustomerBody = ({
+ customers,
+ onSelectCustomerToDelete
+}: {
+ customers: CustomerInfo[]
+ onSelectCustomerToDelete: (customerId: string) => void
+}) => {
return (
{customers?.map((customer, ind) => {
@@ -140,7 +181,14 @@ const CustomerBody = ({ customers }: { customers: CustomerInfo[] }) => {
Customer Action
Update
- Delete
+ {
+ onSelectCustomerToDelete(customer.id)
+ }}
+ >
+ Delete
+
@@ -190,3 +238,73 @@ const PaginationUI = ({
)
}
+
+const DeleteAlert = ({
+ customerInfo,
+ onCloseAlertDialog
+}: {
+ customerInfo: CustomerInfo | undefined
+ onCloseAlertDialog: () => void
+}) => {
+ const [isDeleting, setDeleting] = useState(false)
+ const router = useRouter()
+
+ console.log(customerInfo)
+ async function onDeleteInvoice() {
+ setDeleting(true)
+ try {
+ const response = await fetch(`/api/customer/${customerInfo?.id}`, {
+ method: 'DELETE'
+ })
+ const delRes = await response.json()
+ if (delRes.ok) {
+ toast.success(
+ 'You successfully deleted customer ' + customerInfo?.legalName,
+ {
+ position: 'top-right'
+ }
+ )
+ setDeleting(false)
+ router.refresh()
+ onCloseAlertDialog()
+ return
+ } else {
+ throw new Error(`Error Deleting: ${delRes.data}`)
+ }
+ } catch (error) {
+ console.error('Error deleting customer:', error)
+ toast.error(`Error deleting customer: ${error}`, {
+ position: 'top-right'
+ })
+ setDeleting(false)
+ }
+ }
+
+ return (
+
+
+ Delete customer {customerInfo?.legalName}
+
+ Are you sure you want to delete? This action cannot be undone.
+
+
+
+
+
+
+
+ )
+}
diff --git a/packages/db/prisma/migrations/20240429175343_added_delete_cascade/migration.sql b/packages/db/prisma/migrations/20240429175343_added_delete_cascade/migration.sql
new file mode 100644
index 0000000..71220de
--- /dev/null
+++ b/packages/db/prisma/migrations/20240429175343_added_delete_cascade/migration.sql
@@ -0,0 +1,5 @@
+-- DropForeignKey
+ALTER TABLE "Invoice" DROP CONSTRAINT "Invoice_customerId_fkey";
+
+-- AddForeignKey
+ALTER TABLE "Invoice" ADD CONSTRAINT "Invoice_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "CustomerInfo"("id") ON DELETE CASCADE ON UPDATE CASCADE;
diff --git a/packages/db/prisma/schema.prisma b/packages/db/prisma/schema.prisma
index bb11fd1..a84bca2 100644
--- a/packages/db/prisma/schema.prisma
+++ b/packages/db/prisma/schema.prisma
@@ -151,7 +151,7 @@ model Invoice {
instantInvoiceLink String?
sendingMethod SendMethods @default(MAIL)
customerId String
- customerInfo CustomerInfo @relation(fields: [customerId], references: [id])
+ customerInfo CustomerInfo @relation(fields: [customerId], references: [id], onDelete: Cascade)
dateIssue DateTime
dueDate DateTime
shippingCharge Float?