From ffd838fd491d8e2b271bb3c01a1563c209d3316a Mon Sep 17 00:00:00 2001 From: "Dennis (via Claude+Gemma)" Date: Sat, 23 May 2026 05:55:08 +0200 Subject: [PATCH] =?UTF-8?q?feat(customer-csv-import):=20CSV-Import=20f?= =?UTF-8?q?=C3=BCr=20Customers=20(admin)=20+=20Upload-UI=20[tsc:fail]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .phase8-state.json | 5 +-- GENERATION_LOG.md | 19 +++++++++++ apps/api/src/routes/customers.ts | 40 +++++++++++++++++++++++ apps/web/src/pages/Customers.tsx | 54 +++++++++++++++++++++++++++----- 4 files changed, 109 insertions(+), 9 deletions(-) diff --git a/.phase8-state.json b/.phase8-state.json index ee3b3a5..7ec3b67 100644 --- a/.phase8-state.json +++ b/.phase8-state.json @@ -1,8 +1,9 @@ { "completed_features": [], - "current_feature": "time-entry-bulk-actions", + "current_feature": "customer-csv-import", "started_at": "2026-05-23T05:49:48.673340", "attempted_features": [ - "recent-activity-widget" + "recent-activity-widget", + "time-entry-bulk-actions" ] } \ No newline at end of file diff --git a/GENERATION_LOG.md b/GENERATION_LOG.md index 66834da..cf36807 100644 --- a/GENERATION_LOG.md +++ b/GENERATION_LOG.md @@ -928,3 +928,22 @@ src/index.ts(27,25): error TS2769: No overload matches this call. Overload 2 of 3, '(plugin: FastifyPluginAsync<{ limits: { fileSize: number; }; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error. Argument of type 'Promise' is not assignable to parameter of type 'FastifyPluginAsync<{ limits: { fileSize: number; }; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>'. Type 'Promise' provides no match for the signature '(instance: FastifyInstance, FastifyBaseLogger, FastifyTy +- `05:53:42` **INFO** Committed feature time-entry-bulk-actions +- `05:53:42` **INFO** Pushed: rc=0 + +## Phase-3 Feature: customer-csv-import (2026-05-23 05:53:42) + +- `05:53:42` **INFO** Description: CSV-Import für Customers (admin) + Upload-UI +- `05:53:42` **INFO** Generating apps/api/src/routes/customers.ts (ERWEITERT — behalte alles. Füge POST /import (multipart-Upload CSV mit…) +- `05:54:21` **INFO** wrote 4351 chars in 39.2s (attempt 1) +- `05:54:21` **INFO** Generating apps/web/src/pages/Customers.tsx (ERWEITERT — behalte bestehendes UI. Füge 'CSV importieren' Button rech…) +- `05:55:07` **INFO** wrote 5630 chars in 45.3s (attempt 1) +- `05:55:07` **INFO** Running tsc --noEmit on api… +- `05:55:08` **WARN** tsc errors: +src/index.ts(27,25): error TS2769: No overload matches this call. + Overload 1 of 3, '(plugin: FastifyPluginCallback<{ limits: { fileSize: number; }; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error. + Argument of type 'Promise' is not assignable to parameter of type 'FastifyPluginCallback<{ limits: { fileSize: number; }; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>'. + Type 'Promise' provides no match for the signature '(instance: FastifyInstance, FastifyBaseLogger, FastifyTypeProvider>, opts: { ...; }, done: (err?: Error | undefined) => void): void'. + Overload 2 of 3, '(plugin: FastifyPluginAsync<{ limits: { fileSize: number; }; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error. + Argument of type 'Promise' is not assignable to parameter of type 'FastifyPluginAsync<{ limits: { fileSize: number; }; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>'. + Type 'Promise' provides no match for the signature '(instance: FastifyInstance, FastifyBaseLogger, FastifyTy diff --git a/apps/api/src/routes/customers.ts b/apps/api/src/routes/customers.ts index 2c79e73..6b26660 100644 --- a/apps/api/src/routes/customers.ts +++ b/apps/api/src/routes/customers.ts @@ -129,4 +129,44 @@ export default async function customerRoutes(fastify: FastifyInstance) { return reply.code(204).send() }) + + fastify.post("/import", async (request, reply) => { + const user = request.user as any + if (user?.role !== "admin") { + return reply.code(403).send({ message: "Admin privileges required" }) + } + + const data = await request.file() + if (!data) { + return reply.code(400).send({ message: "No file uploaded" }) + } + + const content = await data.toBuffer() + const csvText = content.toString("utf-8") + const lines = csvText.split(/\r?\n/).filter(line => line.trim()) + + // Remove header + const rows = lines.slice(1) + let imported = 0 + const errors: string[] = [] + + for (let i = 0; i < rows.length; i++) { + try { + const [name, activeStr] = rows[i].split(",").map(s => s.trim()) + if (!name) throw new Error("Name is missing") + + const active = activeStr?.toLowerCase() === "true" + + await db.insert(customers).values({ + name, + active + }) + imported++ + } catch (err: any) { + errors.push(`Line ${i + 2}: ${err.message}`) + } + } + + return { imported, errors } + }) } \ No newline at end of file diff --git a/apps/web/src/pages/Customers.tsx b/apps/web/src/pages/Customers.tsx index 4df7c41..1ba4157 100644 --- a/apps/web/src/pages/Customers.tsx +++ b/apps/web/src/pages/Customers.tsx @@ -1,10 +1,11 @@ -import { useState } from "react" +import { useState, useRef } from "react" import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query" import { api } from "../lib/api" export default function Customers() { const queryClient = useQueryClient() const [name, setName] = useState("") + const fileInputRef = useRef(null) const { data: customers, isLoading, isError } = useQuery({ queryKey: ["customers"], @@ -26,20 +27,59 @@ export default function Customers() { } }) + const importMutation = useMutation({ + mutationFn: (file: File) => api.importCustomersCsv(file), + onSuccess: (data) => { + alert(`Import successful: ${data.count} customers imported.`) + queryClient.invalidateQueries({ queryKey: ["customers"] }) + }, + onError: (error: any) => { + alert(`Import failed: ${error.message || "Unknown error"}`) + } + }) + const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if (!name.trim()) return createMutation.mutate(name) } + const handleFileChange = (e: React.ChangeEvent) => { + const file = e.target.files?.[0] + if (file) { + importMutation.mutate(file) + } + if (fileInputRef.current) { + fileInputRef.current.value = "" + } + } + if (isLoading) return
Loading customers...
if (isError) return
Error loading customers.
return (
-
-

Customers

-

Manage your client database

+
+
+

Customers

+

Manage your client database

+
+
+ + +
@@ -70,8 +110,8 @@ export default function Customers() {
- - + + @@ -99,7 +139,7 @@ export default function Customers() { ) : ( )}
Name Actions
- No customers found. Add your first customer above. + No customers found.