Skip to content

Commit

Permalink
fix: allow products to be deleted when variants 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Gum-Joe committed Aug 21, 2024
1 parent 0132bd2 commit 399aec0
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 34 deletions.
6 changes: 4 additions & 2 deletions collection/app/(app)/CSVImport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const CSVImportForm: React.FC<CSVImportFormProp> = ({
mode: "controlled",
initialValues: {
productId:
productsByAcademicYear[Object.keys(productsByAcademicYear)[0]][0].id.toString(10),
productsByAcademicYear[academicYear][0].id.toString(10),
csv: [],
academicYear: academicYear,
},
Expand Down Expand Up @@ -106,6 +106,8 @@ const CSVImportForm: React.FC<CSVImportFormProp> = ({
return;
}

console.log(academicYear);

startTransition(async () => {
const csvString = await values.csv[0].text();
const filename = values.csv[0].name;
Expand All @@ -130,7 +132,7 @@ const CSVImportForm: React.FC<CSVImportFormProp> = ({
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")}
Expand Down
2 changes: 1 addition & 1 deletion collection/app/(app)/ImportsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const ImportItem: React.FC<{ importItem: ImportList[0] }> = ({ importItem }) =>

return (
<Stack gap="xl">
<Paper p="md" withBorder>
<Paper p="md" withBorder mt="md">
<ConfirmModal
onConfirm={async () => {
await rollbackImport(importItem.id);
Expand Down
9 changes: 9 additions & 0 deletions collection/app/(app)/UserSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ export const UserSearch: React.FC<UserSearchProps> = ({
[router, fetchPurchases],
);

// Run fetchPurchases on initial render
useEffect(() => {
if (shortcodeURLParam) {
startTransition(async () => {
await fetchPurchases(shortcodeURLParam);
});
}
}, []);

return (
<Container w="70%">
<Stack gap="lg">
Expand Down
42 changes: 26 additions & 16 deletions collection/app/(app)/products/VariantsTable.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -45,21 +45,31 @@ export const VariantsTable: React.FC<VariantsTableProps> = ({ variants, productI
);
}

const allVariantsHaveCountZero = variants.every((variant) => variant.count === 0);

return (
<TanstackTable
columns={columns}
data={variants}
enablePagination={false}
enableSearch={false}
tableProps={{
striped: true,
withTableBorder: true,
withColumnBorders: true,
verticalSpacing: "sm",
highlightOnHover: true,
}}
differentHeaderColour
initialSort={[{ id: "name", desc: false }]}
/>
<Stack gap="sm">
<Group>
{(!variants || variants.length === 0) && <Text>No variants added yet</Text>}
{(!variants || variants.length === 0 || allVariantsHaveCountZero) && (
<DeleteProduct productId={productId} />
)}
</Group>
<TanstackTable
columns={columns}
data={variants}
enablePagination={false}
enableSearch={false}
tableProps={{
striped: true,
withTableBorder: true,
withColumnBorders: true,
verticalSpacing: "sm",
highlightOnHover: true,
}}
differentHeaderColour
initialSort={[{ id: "name", desc: false }]}
/>
</Stack>
);
};
6 changes: 6 additions & 0 deletions collection/components/tables/ImportTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions collection/lib/crud/importCsv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export async function getItemsInImport(importId: string) {
shortcode: true,
},
},
academicYear: true,
},
},
},
Expand All @@ -250,6 +251,7 @@ export interface ImportItemList extends OrderItemImport {
ImperialStudent: {
shortcode: string;
};
academicYear: string;
};
}[];
}
Expand Down
14 changes: 0 additions & 14 deletions collection/lib/crud/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,6 @@ export async function addProducts(academicYear: string, products: string[]): Pro

export async function deleteProduct(productId: number): Promise<StatusReturn> {
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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion collection/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

0 comments on commit 399aec0

Please sign in to comment.