mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 21:35:19 +00:00
feat: filters, settings, backups fix: ts compile errors feat: new dashboard, webp previews and settings feat: use webp for pdfs feat: use webp fix: analyze resets old data fix: switch to corsproxy fix: switch to free cors fix: max upload limit fix: currency conversion feat: transaction export fix: currency conversion feat: refactor settings actions feat: new loader feat: README + LICENSE doc: update readme doc: update readme doc: update readme doc: update screenshots ci: bump prisma
64 lines
2.1 KiB
TypeScript
64 lines
2.1 KiB
TypeScript
"use server"
|
|
|
|
import { deleteFile, getFileById, updateFile } from "@/data/files"
|
|
import { createTransaction, updateTransactionFiles } from "@/data/transactions"
|
|
import { transactionFormSchema } from "@/forms/transactions"
|
|
import { getTransactionFileUploadPath } from "@/lib/files"
|
|
import { mkdir, rename } from "fs/promises"
|
|
import { revalidatePath } from "next/cache"
|
|
import path from "path"
|
|
|
|
export async function saveFileAsTransactionAction(prevState: any, formData: FormData) {
|
|
try {
|
|
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)
|
|
if (!file) throw new Error("File not found")
|
|
|
|
// Create transaction
|
|
const transaction = await createTransaction(validatedForm.data)
|
|
|
|
// Move file to processed location
|
|
const originalFileName = path.basename(file.path)
|
|
const { fileUuid, filePath: newFilePath } = await getTransactionFileUploadPath(originalFileName, transaction)
|
|
|
|
// Move file to new location and name
|
|
await mkdir(path.dirname(newFilePath), { recursive: true })
|
|
await rename(path.resolve(file.path), path.resolve(newFilePath))
|
|
|
|
// Update file record
|
|
await updateFile(file.id, {
|
|
id: fileUuid,
|
|
path: newFilePath,
|
|
isReviewed: true,
|
|
})
|
|
|
|
await updateTransactionFiles(transaction.id, [fileUuid])
|
|
|
|
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 {
|
|
await deleteFile(fileId)
|
|
revalidatePath("/unsorted")
|
|
return { success: true }
|
|
} catch (error) {
|
|
console.error("Failed to delete file:", error)
|
|
return { success: false, error: "Failed to delete file" }
|
|
}
|
|
}
|