diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-09-02 10:31:53 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-09-02 10:31:53 +0000 |
| commit | d84602bdf369636e29d298a96218bd672de4afd7 (patch) | |
| tree | 4b03de7ab7807d728e9d34945ca333cf3c080ab6 /lib/vendor-investigation/service.ts | |
| parent | 581b415e6707d9f1d0d0b667b84c4314461bfe37 (diff) | |
(최겸) 기술영업 수정사항 반영 및 구매 PQ/실사 첨부오류 변경
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 |
