mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 21:35:19 +00:00
BREAKING: postgres + saas
This commit is contained in:
117
app/(app)/unsorted/actions.ts
Normal file
117
app/(app)/unsorted/actions.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
"use server"
|
||||
|
||||
import { analyzeTransaction } from "@/ai/analyze"
|
||||
import { AnalyzeAttachment, loadAttachmentsForAI } from "@/ai/attachments"
|
||||
import { buildLLMPrompt } from "@/ai/prompt"
|
||||
import { fieldsToJsonSchema } from "@/ai/schema"
|
||||
import { transactionFormSchema } from "@/forms/transactions"
|
||||
import { getCurrentUser } from "@/lib/auth"
|
||||
import { IS_SELF_HOSTED_MODE } from "@/lib/constants"
|
||||
import { getTransactionFileUploadPath, getUserUploadsDirectory } from "@/lib/files"
|
||||
import { DEFAULT_PROMPT_ANALYSE_NEW_FILE } from "@/models/defaults"
|
||||
import { deleteFile, getFileById, updateFile } from "@/models/files"
|
||||
import { createTransaction, updateTransactionFiles } from "@/models/transactions"
|
||||
import { Category, Field, File, Project } from "@prisma/client"
|
||||
import { mkdir, rename } from "fs/promises"
|
||||
import { revalidatePath } from "next/cache"
|
||||
import path from "path"
|
||||
|
||||
export async function analyzeFileAction(
|
||||
file: File,
|
||||
settings: Record<string, string>,
|
||||
fields: Field[],
|
||||
categories: Category[],
|
||||
projects: Project[]
|
||||
): Promise<{ success: boolean; data?: Record<string, any>; error?: string }> {
|
||||
const user = await getCurrentUser()
|
||||
|
||||
if (!file || file.userId !== user.id) {
|
||||
return { success: false, error: "File not found or does not belong to the user" }
|
||||
}
|
||||
|
||||
let attachments: AnalyzeAttachment[] = []
|
||||
try {
|
||||
attachments = await loadAttachmentsForAI(user, file)
|
||||
} catch (error) {
|
||||
console.error("Failed to retrieve files:", error)
|
||||
return { success: false, error: "Failed to retrieve files: " + error }
|
||||
}
|
||||
|
||||
const prompt = buildLLMPrompt(
|
||||
settings.prompt_analyse_new_file || DEFAULT_PROMPT_ANALYSE_NEW_FILE,
|
||||
fields,
|
||||
categories,
|
||||
projects
|
||||
)
|
||||
|
||||
const schema = fieldsToJsonSchema(fields)
|
||||
|
||||
const results = await analyzeTransaction(
|
||||
prompt,
|
||||
schema,
|
||||
attachments,
|
||||
IS_SELF_HOSTED_MODE ? settings.openai_api_key : process.env.OPENAI_API_KEY || ""
|
||||
)
|
||||
|
||||
console.log("Analysis results:", results)
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
export async function saveFileAsTransactionAction(prevState: any, formData: FormData) {
|
||||
try {
|
||||
const user = await getCurrentUser()
|
||||
const validatedForm = transactionFormSchema.safeParse(Object.fromEntries(formData.entries()))
|
||||
|
||||
if (!validatedForm.success) {
|
||||
return { success: false, error: validatedForm.error.message }
|
||||
}
|
||||
|
||||
// Get the file record
|
||||
const fileId = formData.get("fileId") as string
|
||||
const file = await getFileById(fileId, user.id)
|
||||
if (!file) throw new Error("File not found")
|
||||
|
||||
// Create transaction
|
||||
const transaction = await createTransaction(user.id, validatedForm.data)
|
||||
|
||||
// Move file to processed location
|
||||
const userUploadsDirectory = await getUserUploadsDirectory(user)
|
||||
const originalFileName = path.basename(file.path)
|
||||
const newRelativeFilePath = await getTransactionFileUploadPath(file.id, originalFileName, transaction)
|
||||
|
||||
// Move file to new location and name
|
||||
const oldFullFilePath = path.join(userUploadsDirectory, file.path)
|
||||
const newFullFilePath = path.join(userUploadsDirectory, newRelativeFilePath)
|
||||
await mkdir(path.dirname(newFullFilePath), { recursive: true })
|
||||
await rename(path.resolve(oldFullFilePath), path.resolve(newFullFilePath))
|
||||
|
||||
// Update file record
|
||||
await updateFile(file.id, user.id, {
|
||||
path: newRelativeFilePath,
|
||||
isReviewed: true,
|
||||
})
|
||||
|
||||
await updateTransactionFiles(transaction.id, user.id, [file.id])
|
||||
|
||||
revalidatePath("/unsorted")
|
||||
revalidatePath("/transactions")
|
||||
|
||||
return { success: true, transactionId: transaction.id }
|
||||
} catch (error) {
|
||||
console.error("Failed to save transaction:", error)
|
||||
return { success: false, error: `Failed to save transaction: ${error}` }
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteUnsortedFileAction(prevState: any, fileId: string) {
|
||||
try {
|
||||
const user = await getCurrentUser()
|
||||
await deleteFile(fileId, user.id)
|
||||
revalidatePath("/unsorted")
|
||||
return { success: true }
|
||||
} catch (error) {
|
||||
console.error("Failed to delete file:", error)
|
||||
return { success: false, error: "Failed to delete file" }
|
||||
}
|
||||
}
|
||||
3
app/(app)/unsorted/layout.tsx
Normal file
3
app/(app)/unsorted/layout.tsx
Normal file
@@ -0,0 +1,3 @@
|
||||
export default function UnsortedLayout({ children }: { children: React.ReactNode }) {
|
||||
return <div className="flex flex-col gap-4 p-4 max-w-6xl">{children}</div>
|
||||
}
|
||||
31
app/(app)/unsorted/loading.tsx
Normal file
31
app/(app)/unsorted/loading.tsx
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Skeleton } from "@/components/ui/skeleton"
|
||||
import { Loader2 } from "lucide-react"
|
||||
|
||||
export default function Loading() {
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
<header className="flex items-center justify-between">
|
||||
<h2 className="text-3xl font-bold tracking-tight flex flex-row gap-2">
|
||||
<Loader2 className="h-10 w-10 animate-spin" /> <span>Loading unsorted files...</span>
|
||||
</h2>
|
||||
</header>
|
||||
|
||||
<Skeleton className="w-full h-[800px] flex flex-row flex-wrap md:flex-nowrap justify-center items-start gap-5 p-6">
|
||||
<Skeleton className="w-full h-full" />
|
||||
<div className="w-full flex flex-col gap-5">
|
||||
<Skeleton className="w-full h-12 mb-7" />
|
||||
{[...Array(4)].map((_, i) => (
|
||||
<div key={i} className="flex flex-col gap-2">
|
||||
<Skeleton className="w-[120px] h-4" />
|
||||
<Skeleton className="w-full h-9" />
|
||||
</div>
|
||||
))}
|
||||
<div className="flex flex-row justify-end gap-2 mt-2">
|
||||
<Skeleton className="w-[80px] h-9" />
|
||||
<Skeleton className="w-[130px] h-9" />
|
||||
</div>
|
||||
</div>
|
||||
</Skeleton>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
106
app/(app)/unsorted/page.tsx
Normal file
106
app/(app)/unsorted/page.tsx
Normal file
@@ -0,0 +1,106 @@
|
||||
import { FilePreview } from "@/components/files/preview"
|
||||
import { UploadButton } from "@/components/files/upload-button"
|
||||
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Card } from "@/components/ui/card"
|
||||
import AnalyzeForm from "@/components/unsorted/analyze-form"
|
||||
import { getCurrentUser } from "@/lib/auth"
|
||||
import { IS_SELF_HOSTED_MODE } from "@/lib/constants"
|
||||
import { getCategories } from "@/models/categories"
|
||||
import { getCurrencies } from "@/models/currencies"
|
||||
import { getFields } from "@/models/fields"
|
||||
import { getUnsortedFiles } from "@/models/files"
|
||||
import { getProjects } from "@/models/projects"
|
||||
import { getSettings } from "@/models/settings"
|
||||
import { FileText, PartyPopper, Settings, Upload } from "lucide-react"
|
||||
import { Metadata } from "next"
|
||||
import Link from "next/link"
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "Unsorted",
|
||||
description: "Analyze unsorted files",
|
||||
}
|
||||
|
||||
export default async function UnsortedPage() {
|
||||
const user = await getCurrentUser()
|
||||
const files = await getUnsortedFiles(user.id)
|
||||
const categories = await getCategories(user.id)
|
||||
const projects = await getProjects(user.id)
|
||||
const currencies = await getCurrencies(user.id)
|
||||
const fields = await getFields(user.id)
|
||||
const settings = await getSettings(user.id)
|
||||
|
||||
return (
|
||||
<>
|
||||
<header className="flex items-center justify-between">
|
||||
<h2 className="text-3xl font-bold tracking-tight">You have {files.length} unsorted files</h2>
|
||||
</header>
|
||||
|
||||
{IS_SELF_HOSTED_MODE && !settings.openai_api_key && (
|
||||
<Alert>
|
||||
<Settings className="h-4 w-4 mt-2" />
|
||||
<div className="flex flex-row justify-between pt-2">
|
||||
<div className="flex flex-col">
|
||||
<AlertTitle>ChatGPT API Key is required for analyzing files</AlertTitle>
|
||||
<AlertDescription>
|
||||
Please set your OpenAI API key in the settings to use the analyze form.
|
||||
</AlertDescription>
|
||||
</div>
|
||||
<Link href="/settings/llm">
|
||||
<Button>Go to Settings</Button>
|
||||
</Link>
|
||||
</div>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<main className="flex flex-col gap-5">
|
||||
{files.map((file) => (
|
||||
<Card
|
||||
key={file.id}
|
||||
id={file.id}
|
||||
className="flex flex-row flex-wrap md:flex-nowrap justify-center items-start gap-5 p-5 bg-accent"
|
||||
>
|
||||
<div className="w-full max-w-[500px]">
|
||||
<Card>
|
||||
<FilePreview file={file} />
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<div className="w-full">
|
||||
<AnalyzeForm
|
||||
file={file}
|
||||
categories={categories}
|
||||
projects={projects}
|
||||
currencies={currencies}
|
||||
fields={fields}
|
||||
settings={settings}
|
||||
/>
|
||||
</div>
|
||||
</Card>
|
||||
))}
|
||||
{files.length == 0 && (
|
||||
<div className="flex flex-col items-center justify-center gap-2 h-full min-h-[600px]">
|
||||
<PartyPopper className="w-12 h-12 text-muted-foreground" />
|
||||
<p className="pt-4 text-muted-foreground">Everything is clear! Congrats!</p>
|
||||
<p className="flex flex-row gap-2 text-muted-foreground">
|
||||
<span>Drag and drop new files here to analyze</span>
|
||||
<Upload />
|
||||
</p>
|
||||
|
||||
<div className="flex flex-row gap-5 mt-8">
|
||||
<UploadButton>
|
||||
<Upload /> Upload New File
|
||||
</UploadButton>
|
||||
<Button variant="outline" asChild>
|
||||
<Link href="/transactions">
|
||||
<FileText />
|
||||
Go to Transactions
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</main>
|
||||
</>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user