Skip to content

Commit 457e305

Browse files
committed
feat: add previous import listing
1 parent 951b952 commit 457e305

5 files changed

Lines changed: 64 additions & 5 deletions

File tree

collection/app/(app)/CSVImport.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ const CSVImportForm: React.FC<CSVImportFormProp> = ({
106106

107107
startTransition(async () => {
108108
const csvString = await values.csv[0].text();
109-
const res = await importCsv(csvString, productId, academicYear);
109+
const filename = values.csv[0].name;
110+
const res = await importCsv(csvString, filename, productId, academicYear);
110111
setFormState(res);
111112
});
112113
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use client";
2+
3+
import { Accordion, Paper, Stack, Title } from "@mantine/core";
4+
import { OrderItemImport } from "@prisma/client";
5+
import React from "react";
6+
7+
interface ImportsListProps {
8+
imports: OrderItemImport[];
9+
}
10+
11+
export const ImportsList: React.FC<ImportsListProps> = ({ imports }) => {
12+
return (
13+
<Paper p="lg" withBorder mt="xl">
14+
<Stack>
15+
<Title order={3}>Previous Imports</Title>
16+
<Accordion>
17+
{imports.map((importItem, index) => (
18+
<Accordion.Item key={index} value={importItem.id}>
19+
<Accordion.Control>{importItem.name}</Accordion.Control>
20+
<Accordion.Panel>
21+
<div>{importItem.name}</div>
22+
</Accordion.Panel>
23+
</Accordion.Item>
24+
))}
25+
</Accordion>
26+
</Stack>
27+
</Paper>
28+
);
29+
};

collection/app/(app)/page.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { auth } from "@/auth";
22
import { getAcademicYear } from "@/lib/config";
3+
import { getAcademicYearsInDB } from "@/lib/crud/academic-year";
4+
import { getImportList } from "@/lib/crud/importCsv";
35
import { Stack, Title } from "@mantine/core";
46
import { redirect } from "next/navigation";
57
import React from "react";
68

9+
import { ImportsList } from "./ImportsList";
710
import { UserSearch } from "./UserSearch";
8-
import { getAcademicYearsInDB } from "@/lib/crud/academic-year";
911

1012
export default async function Index() {
1113
const session = await auth();
@@ -16,13 +18,16 @@ export default async function Index() {
1618
const currentAcademicYear = await getAcademicYear();
1719
const academicYears = await getAcademicYearsInDB();
1820

21+
const imports = await getImportList();
22+
1923
return (
2024
<Stack gap="lg">
2125
<Title order={1}>DoCSoc Collection System</Title>
2226
<UserSearch
2327
currentAcademicYear={currentAcademicYear}
2428
validAcaemicYears={academicYears}
2529
/>
30+
<ImportsList imports={imports} />
2631
</Stack>
2732
);
2833
}

collection/lib/crud/importCsv.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import { StatusReturn } from "../types";
1010

1111
const logger = createLogger("collection.importCsv");
1212

13-
async function importFile(fileContents: string, itemId: number, academicYear: AcademicYear) {
13+
async function importFile(
14+
fileContents: string,
15+
fileName: string,
16+
itemId: number,
17+
academicYear: AcademicYear,
18+
) {
1419
logger.info("Importing file for: ", itemId);
1520

1621
const iter = parse(fileContents, {
@@ -30,6 +35,15 @@ async function importFile(fileContents: string, itemId: number, academicYear: Ac
3035

3136
const academicYearReference = academicYearDB.year;
3237

38+
// 0.1: Create import
39+
// Name: <csv file name> DD/MM/YYYY HH:MM
40+
const importName = `${fileName} @ ${new Date().toLocaleString("en-GB")}`;
41+
const importItem = await prisma.orderItemImport.create({
42+
data: {
43+
name: importName,
44+
},
45+
});
46+
3347
for await (const record of iter) {
3448
logger.debug(`Record: ${JSON.stringify(record)}`);
3549

@@ -123,6 +137,7 @@ async function importFile(fileContents: string, itemId: number, academicYear: Ac
123137
variantId: varientDB.id,
124138
quantity: quantity,
125139
collected: false,
140+
importId: importItem.id,
126141
},
127142
});
128143
}
@@ -135,6 +150,7 @@ export interface CSVFormValues {
135150

136151
export async function importCsv(
137152
fileContents: string,
153+
fileName: string,
138154
productId: number,
139155
academicYear: AcademicYear,
140156
): Promise<StatusReturn> {
@@ -145,7 +161,7 @@ export async function importCsv(
145161
});
146162

147163
try {
148-
await importFile(fileContents, productId, academicYear);
164+
await importFile(fileContents, fileName, productId, academicYear);
149165
} catch (e: any) {
150166
return {
151167
status: "error",
@@ -161,3 +177,11 @@ export async function importCsv(
161177
message: `Data imported for ${product?.name} in ${academicYear}`,
162178
};
163179
}
180+
181+
export async function getImportList() {
182+
return prisma.orderItemImport.findMany({
183+
orderBy: {
184+
date: "asc",
185+
},
186+
});
187+
}

collection/prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ model Order {
8989

9090
// Imports of data, to allow rollback
9191
model OrderItemImport {
92-
id String @id @default(cuid()) @db.Uuid
92+
id String @id @default(uuid()) @db.Uuid
9393
date DateTime @default(now())
9494
name String
9595

0 commit comments

Comments
 (0)