From ca7d206b07ed760b85859ab05323a652954d2e0f Mon Sep 17 00:00:00 2001 From: vas3k Date: Fri, 23 May 2025 14:59:12 +0200 Subject: [PATCH] fix: errors on saving transactions --- ai/schema.ts | 2 +- components/files/screen-drop-area.tsx | 30 +++++++++++++-------------- models/transactions.ts | 12 +++++------ 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/ai/schema.ts b/ai/schema.ts index e6d70b2..fa5706f 100644 --- a/ai/schema.ts +++ b/ai/schema.ts @@ -16,7 +16,7 @@ export const fieldsToJsonSchema = (fields: Field[]) => { ...schemaProperties, items: { type: "array", - description: "Included items or products in the transaction which have own name and price", + description: "Separate items, products or transactions in the file which have own name and price or sum. Find all items!", items: { type: "object", properties: schemaProperties, diff --git a/components/files/screen-drop-area.tsx b/components/files/screen-drop-area.tsx index 972f2e7..54acf6e 100644 --- a/components/files/screen-drop-area.tsx +++ b/components/files/screen-drop-area.tsx @@ -5,7 +5,7 @@ import { uploadFilesAction } from "@/app/(app)/files/actions" import { uploadTransactionFilesAction } from "@/app/(app)/transactions/actions" import { AlertCircle, CloudUpload, Loader2 } from "lucide-react" import { useParams, useRouter } from "next/navigation" -import { startTransition, useCallback, useEffect, useRef, useState } from "react" +import { useCallback, useEffect, useRef, useState } from "react" export default function ScreenDropArea({ children }: { children: React.ReactNode }) { const router = useRouter() @@ -77,26 +77,24 @@ export default function ScreenDropArea({ children }: { children: React.ReactNode formData.append("files", files[i]) } - startTransition(async () => { - const result = transactionId - ? await uploadTransactionFilesAction(formData) - : await uploadFilesAction(formData) + const result = transactionId + ? await uploadTransactionFilesAction(formData) + : await uploadFilesAction(formData) - if (result.success) { - showNotification({ code: "sidebar.unsorted", message: "new" }) - setTimeout(() => showNotification({ code: "sidebar.unsorted", message: "" }), 3000) - if (!transactionId) { - router.push("/unsorted") - } - } else { - setUploadError(result.error ? result.error : "Something went wrong...") + if (result.success) { + showNotification({ code: "sidebar.unsorted", message: "new" }) + setTimeout(() => showNotification({ code: "sidebar.unsorted", message: "" }), 3000) + if (!transactionId) { + router.push("/unsorted") } - setIsUploading(false) - }) + } else { + setUploadError(result.error ? result.error : "Something went wrong...") + } } catch (error) { console.error("Upload error:", error) - setIsUploading(false) setUploadError(error instanceof Error ? error.message : "Something went wrong...") + } finally { + setIsUploading(false) } } }, diff --git a/models/transactions.ts b/models/transactions.ts index ed87453..a66f5d4 100644 --- a/models/transactions.ts +++ b/models/transactions.ts @@ -13,9 +13,9 @@ export type TransactionData = { convertedTotal?: number | null convertedCurrencyCode?: string | null type?: string | null - items?: TransactionData[] | null + items?: TransactionData[] | undefined note?: string | null - files?: string[] | null + files?: string[] | undefined extra?: Record categoryCode?: string | null projectCode?: string | null @@ -135,10 +135,8 @@ export const createTransaction = async (userId: string, data: TransactionData): ...standard, extra: extra, items: data.items as Prisma.InputJsonValue, - user: { - connect: { id: userId } - } - } as Prisma.TransactionCreateInput, + userId, + }, }) } @@ -151,7 +149,7 @@ export const updateTransaction = async (id: string, userId: string, data: Transa ...standard, extra: extra, items: data.items ? data.items as Prisma.InputJsonValue : [], - } as Prisma.TransactionUpdateInput, + }, }) }