diff options
Diffstat (limited to 'lib/vendor-investigation/service.ts')
| -rw-r--r-- | lib/vendor-investigation/service.ts | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/lib/vendor-investigation/service.ts b/lib/vendor-investigation/service.ts index f0eb411e..c5097e75 100644 --- a/lib/vendor-investigation/service.ts +++ b/lib/vendor-investigation/service.ts @@ -15,6 +15,8 @@ import { v4 as uuid } from "uuid" import { vendorsLogs } from "@/db/schema"; import { cache } from "react" import { deleteFile } from "../file-stroage"; +import { saveDRMFile } from "../file-stroage"; +import { decryptWithServerAction } from "@/components/drm/drmUtils"; export async function getVendorsInvestigation(input: GetVendorsInvestigationSchema) { return unstable_cache( @@ -629,4 +631,66 @@ export const getAllItems = cache(async () => { console.error("Error fetching all items:", error) throw new Error("Failed to fetch items") } -})
\ No newline at end of file +}) + +/** + * Create vendor investigation attachment + */ +export async function createVendorInvestigationAttachmentAction(input: { + investigationId: number; + file: File; + userId?: string; +}) { + unstable_noStore(); + + try { + console.log(`π μ€μ¬ 첨λΆνμΌ μμ± μμ: ${input.file.name}`); + + // 1. saveDRMFileμ μ¬μ©νμ¬ νμΌ μ μ₯ + const saveResult = await saveDRMFile( + input.file, + decryptWithServerAction, + `vendor-investigation/${input.investigationId}`, + input.userId + ); + + if (!saveResult.success) { + throw new Error(`νμΌ μ μ₯ μ€ν¨: ${input.file.name} - ${saveResult.error}`); + } + + console.log(`β
νμΌ μ μ₯ μλ£: ${input.file.name} -> ${saveResult.fileName}`); + + // 2. DBμ 첨λΆνμΌ λ μ½λ μμ± + const [insertedAttachment] = await db + .insert(vendorInvestigationAttachments) + .values({ + investigationId: input.investigationId, + fileName: saveResult.fileName!, + originalFileName: input.file.name, + filePath: saveResult.publicPath!, + fileSize: input.file.size, + mimeType: input.file.type || 'application/octet-stream', + attachmentType: 'DOCUMENT', // λλ νμΌ νμ
μ λ°λΌ κ²°μ + createdAt: new Date(), + updatedAt: new Date(), + }) + .returning(); + + console.log(`β
첨λΆνμΌ DB λ μ½λ μμ± μλ£: ID ${insertedAttachment.id}`); + + // 3. μΊμ 무ν¨ν + revalidateTag(`vendor-investigation-${input.investigationId}`); + revalidateTag("vendor-investigations"); + + return { + success: true, + attachment: insertedAttachment, + }; + } catch (error) { + console.error(`β μ€μ¬ 첨λΆνμΌ μμ± μ€ν¨: ${input.file.name}`, error); + return { + success: false, + error: error instanceof Error ? error.message : "μ μ μλ μ€λ₯", + }; + } +}
\ No newline at end of file |
