mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 13:25:18 +00:00
fix: rename data -> models
This commit is contained in:
79
models/files.ts
Normal file
79
models/files.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
"use server"
|
||||
|
||||
import { prisma } from "@/lib/db"
|
||||
import { unlink } from "fs/promises"
|
||||
import path from "path"
|
||||
import { cache } from "react"
|
||||
import { getTransactionById } from "./transactions"
|
||||
|
||||
export const getUnsortedFiles = cache(async () => {
|
||||
return await prisma.file.findMany({
|
||||
where: {
|
||||
isReviewed: false,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
export const getUnsortedFilesCount = cache(async () => {
|
||||
return await prisma.file.count({
|
||||
where: {
|
||||
isReviewed: false,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
export const getFileById = cache(async (id: string) => {
|
||||
return await prisma.file.findFirst({
|
||||
where: { id },
|
||||
})
|
||||
})
|
||||
|
||||
export const getFilesByTransactionId = cache(async (id: string) => {
|
||||
const transaction = await getTransactionById(id)
|
||||
if (transaction && transaction.files) {
|
||||
return await prisma.file.findMany({
|
||||
where: {
|
||||
id: {
|
||||
in: transaction.files as string[],
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "asc",
|
||||
},
|
||||
})
|
||||
}
|
||||
return []
|
||||
})
|
||||
|
||||
export const createFile = async (data: any) => {
|
||||
return await prisma.file.create({
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
export const updateFile = async (id: string, data: any) => {
|
||||
return await prisma.file.update({
|
||||
where: { id },
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteFile = async (id: string) => {
|
||||
const file = await getFileById(id)
|
||||
if (!file) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await unlink(path.resolve(file.path))
|
||||
} catch (error) {
|
||||
console.error("Error deleting file:", error)
|
||||
}
|
||||
|
||||
return await prisma.file.delete({
|
||||
where: { id },
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user