From 399aec0848e1813180ae059ae7b2f8f53a468ee2 Mon Sep 17 00:00:00 2001 From: Kishan Sambhi Date: Wed, 21 Aug 2024 19:29:12 +0100 Subject: [PATCH] fix: allow products to be deleted when variants 0 --- collection/app/(app)/CSVImport.tsx | 6 ++- collection/app/(app)/ImportsList.tsx | 2 +- collection/app/(app)/UserSearch.tsx | 9 ++++ .../app/(app)/products/VariantsTable.tsx | 42 ++++++++++++------- collection/components/tables/ImportTable.tsx | 6 +++ collection/lib/crud/importCsv.ts | 2 + collection/lib/crud/products.ts | 14 ------- .../migration.sql | 5 +++ collection/prisma/schema.prisma | 2 +- 9 files changed, 54 insertions(+), 34 deletions(-) create mode 100644 collection/prisma/migrations/20240821182841_cascade_delete_variants/migration.sql diff --git a/collection/app/(app)/CSVImport.tsx b/collection/app/(app)/CSVImport.tsx index bd2a746..41470d7 100644 --- a/collection/app/(app)/CSVImport.tsx +++ b/collection/app/(app)/CSVImport.tsx @@ -57,7 +57,7 @@ const CSVImportForm: React.FC = ({ mode: "controlled", initialValues: { productId: - productsByAcademicYear[Object.keys(productsByAcademicYear)[0]][0].id.toString(10), + productsByAcademicYear[academicYear][0].id.toString(10), csv: [], academicYear: academicYear, }, @@ -106,6 +106,8 @@ const CSVImportForm: React.FC = ({ return; } + console.log(academicYear); + startTransition(async () => { const csvString = await values.csv[0].text(); const filename = values.csv[0].name; @@ -130,7 +132,7 @@ const CSVImportForm: React.FC = ({ label="Academic year" name="academicYear" key={form.key("academicYear")} - description="Import into this academic year" + description="Import into this academic year (note: applies to new order IDs only)" data={validAcademicYears} required {...form.getInputProps("academicYear")} diff --git a/collection/app/(app)/ImportsList.tsx b/collection/app/(app)/ImportsList.tsx index 5e39d6b..1823f4f 100644 --- a/collection/app/(app)/ImportsList.tsx +++ b/collection/app/(app)/ImportsList.tsx @@ -17,7 +17,7 @@ const ImportItem: React.FC<{ importItem: ImportList[0] }> = ({ importItem }) => return ( - + { await rollbackImport(importItem.id); diff --git a/collection/app/(app)/UserSearch.tsx b/collection/app/(app)/UserSearch.tsx index 432a8ed..759463b 100644 --- a/collection/app/(app)/UserSearch.tsx +++ b/collection/app/(app)/UserSearch.tsx @@ -105,6 +105,15 @@ export const UserSearch: React.FC = ({ [router, fetchPurchases], ); + // Run fetchPurchases on initial render + useEffect(() => { + if (shortcodeURLParam) { + startTransition(async () => { + await fetchPurchases(shortcodeURLParam); + }); + } + }, []); + return ( diff --git a/collection/app/(app)/products/VariantsTable.tsx b/collection/app/(app)/products/VariantsTable.tsx index 181ce82..06764b5 100644 --- a/collection/app/(app)/products/VariantsTable.tsx +++ b/collection/app/(app)/products/VariantsTable.tsx @@ -1,7 +1,7 @@ "use client"; import TanstackTable from "@/components/tables/TanStackTable"; -import { Group, Text } from "@mantine/core"; +import { Group, Text, Stack } from "@mantine/core"; import { createColumnHelper } from "@tanstack/react-table"; import React from "react"; @@ -45,21 +45,31 @@ export const VariantsTable: React.FC = ({ variants, productI ); } + const allVariantsHaveCountZero = variants.every((variant) => variant.count === 0); + return ( - + + + {(!variants || variants.length === 0) && No variants added yet} + {(!variants || variants.length === 0 || allVariantsHaveCountZero) && ( + + )} + + + ); }; diff --git a/collection/components/tables/ImportTable.tsx b/collection/components/tables/ImportTable.tsx index 9c7d6aa..81d78b4 100644 --- a/collection/components/tables/ImportTable.tsx +++ b/collection/components/tables/ImportTable.tsx @@ -16,6 +16,12 @@ const columns = [ id: "orderNo", sortingFn: "alphanumeric", }), + // columnHelper.accessor("Order.academicYear", { + // cell: (info) => info.getValue(), + // header: "Academic Year", + // id: "academicYear", + // sortingFn: "alphanumeric", + // }), // Shortcode columnHelper.accessor("Order.ImperialStudent.shortcode", { cell: (info) => info.getValue(), diff --git a/collection/lib/crud/importCsv.ts b/collection/lib/crud/importCsv.ts index 8bc9589..4905025 100644 --- a/collection/lib/crud/importCsv.ts +++ b/collection/lib/crud/importCsv.ts @@ -227,6 +227,7 @@ export async function getItemsInImport(importId: string) { shortcode: true, }, }, + academicYear: true, }, }, }, @@ -250,6 +251,7 @@ export interface ImportItemList extends OrderItemImport { ImperialStudent: { shortcode: string; }; + academicYear: string; }; }[]; } diff --git a/collection/lib/crud/products.ts b/collection/lib/crud/products.ts index 96d1e6b..82ac1f8 100644 --- a/collection/lib/crud/products.ts +++ b/collection/lib/crud/products.ts @@ -122,20 +122,6 @@ export async function addProducts(academicYear: string, products: string[]): Pro export async function deleteProduct(productId: number): Promise { try { - // Validate it has no variants - const variants = await prisma.variant.findMany({ - where: { - rootItemId: productId, - }, - }); - - if (variants.length > 0) { - return { - status: "error", - error: "Product has variants", - }; - } - await prisma.rootItem.delete({ where: { id: productId, diff --git a/collection/prisma/migrations/20240821182841_cascade_delete_variants/migration.sql b/collection/prisma/migrations/20240821182841_cascade_delete_variants/migration.sql new file mode 100644 index 0000000..4f73b7d --- /dev/null +++ b/collection/prisma/migrations/20240821182841_cascade_delete_variants/migration.sql @@ -0,0 +1,5 @@ +-- DropForeignKey +ALTER TABLE "Variant" DROP CONSTRAINT "Variant_rootItemId_fkey"; + +-- AddForeignKey +ALTER TABLE "Variant" ADD CONSTRAINT "Variant_rootItemId_fkey" FOREIGN KEY ("rootItemId") REFERENCES "RootItem"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/collection/prisma/schema.prisma b/collection/prisma/schema.prisma index 17623d0..bf61cd8 100644 --- a/collection/prisma/schema.prisma +++ b/collection/prisma/schema.prisma @@ -127,7 +127,7 @@ model Variant { variantName String rootItemId Int OrderItem OrderItem[] - RootItem RootItem @relation(fields: [rootItemId], references: [id]) + RootItem RootItem @relation(fields: [rootItemId], references: [id], onDelete: Cascade, onUpdate: Cascade) @@unique([variantName, rootItemId]) }