Skip to content

Commit da0893c

Browse files
committed
feat: add import rollback
1 parent f9e73ab commit da0893c

4 files changed

Lines changed: 68 additions & 19 deletions

File tree

collection/app/(app)/ImportsList.tsx

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,62 @@
11
"use client";
22

3+
import { ConfirmModal } from "@/components/ConfirmModal";
34
import { ImportTable } from "@/components/tables/ImportTable";
4-
import { ImportList } from "@/lib/crud/importCsv";
5+
import { ImportList, rollbackImport } from "@/lib/crud/importCsv";
56
import { Accordion, Badge, Button, Group, Paper, Stack, Text, Title } from "@mantine/core";
7+
import { useDisclosure } from "@mantine/hooks";
68
import React from "react";
79
import { FaUndo } from "react-icons/fa";
810

911
interface ImportsListProps {
1012
imports: ImportList;
1113
}
1214

15+
const ImportItem: React.FC<{ importItem: ImportList[0] }> = ({ importItem }) => {
16+
const [opened, { open, close }] = useDisclosure(false);
17+
18+
return (
19+
<Stack gap="xl">
20+
<Paper p="md" withBorder>
21+
<ConfirmModal
22+
onConfirm={async () => {
23+
await rollbackImport(importItem.id);
24+
}}
25+
confirmButtonText={`Rollback Import`}
26+
opened={opened}
27+
close={close}
28+
>
29+
<Text>
30+
This will delete all <strong>{importItem._count.OrderItem}</strong> items
31+
imported in the import <strong>{importItem.name}</strong>!
32+
</Text>
33+
<Text>This action cannot be undone. Are you sure you want to proceed?</Text>
34+
</ConfirmModal>
35+
<Group justify="space-between">
36+
<Title order={5}>Actions</Title>
37+
<Group>
38+
<Button
39+
color="red"
40+
variant="outline"
41+
leftSection={<FaUndo />}
42+
onClick={open}
43+
>
44+
Rollback Import
45+
</Button>
46+
</Group>
47+
</Group>
48+
</Paper>
49+
<ImportTable importId={importItem.id} />
50+
</Stack>
51+
);
52+
};
53+
1354
export const ImportsList: React.FC<ImportsListProps> = ({ imports }) => {
1455
return (
1556
<Paper p="lg" withBorder mt="xl">
1657
<Stack>
1758
<Title order={3}>Previous Imports</Title>
59+
1860
<Accordion>
1961
{imports.map((importItem, index) => (
2062
<Accordion.Item key={index} value={importItem.id}>
@@ -25,23 +67,7 @@ export const ImportsList: React.FC<ImportsListProps> = ({ imports }) => {
2567
</Group>
2668
</Accordion.Control>
2769
<Accordion.Panel pl="md" pr="md">
28-
<Stack gap="xl">
29-
<Paper p="md" withBorder>
30-
<Group justify="space-between">
31-
<Title order={5}>Actions</Title>
32-
<Group>
33-
<Button
34-
color="red"
35-
variant="outline"
36-
leftSection={<FaUndo />}
37-
>
38-
Rollback Import
39-
</Button>
40-
</Group>
41-
</Group>
42-
</Paper>
43-
<ImportTable importId={importItem.id} />
44-
</Stack>
70+
<ImportItem importItem={importItem} />
4571
</Accordion.Panel>
4672
</Accordion.Item>
4773
))}

collection/lib/crud/importCsv.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,21 @@ export interface ImportItemList extends OrderItemImport {
253253
};
254254
}[];
255255
}
256+
257+
export async function rollbackImport(importId: string) {
258+
if (!importId || typeof importId !== "string") {
259+
return {
260+
status: "error",
261+
error: "No import ID provided",
262+
};
263+
}
264+
265+
await prisma.orderItemImport.delete({
266+
where: {
267+
id: importId,
268+
},
269+
});
270+
271+
revalidatePath("/");
272+
revalidateTag("purchases:*");
273+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- DropForeignKey
2+
ALTER TABLE "OrderItem" DROP CONSTRAINT "OrderItem_importId_fkey";
3+
4+
-- AddForeignKey
5+
ALTER TABLE "OrderItem" ADD CONSTRAINT "OrderItem_importId_fkey" FOREIGN KEY ("importId") REFERENCES "OrderItemImport"("id") ON DELETE CASCADE ON UPDATE CASCADE;

collection/prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ model OrderItem {
106106
// tag the import this came from
107107
importId String @db.Uuid
108108
109-
Import OrderItemImport @relation(fields: [importId], references: [id])
109+
Import OrderItemImport @relation(fields: [importId], references: [id], onDelete: Cascade, onUpdate: Cascade)
110110
Order Order @relation(fields: [orderId], references: [id])
111111
Variant Variant @relation(fields: [variantId], references: [id])
112112
}

0 commit comments

Comments
 (0)