summaryrefslogtreecommitdiff
path: root/lib/vendor-investigation
diff options
context:
space:
mode:
Diffstat (limited to 'lib/vendor-investigation')
-rw-r--r--lib/vendor-investigation/service.ts66
-rw-r--r--lib/vendor-investigation/table/update-investigation-sheet.tsx43
2 files changed, 84 insertions, 25 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
diff --git a/lib/vendor-investigation/table/update-investigation-sheet.tsx b/lib/vendor-investigation/table/update-investigation-sheet.tsx
index 14350815..37d1b2cd 100644
--- a/lib/vendor-investigation/table/update-investigation-sheet.tsx
+++ b/lib/vendor-investigation/table/update-investigation-sheet.tsx
@@ -64,7 +64,7 @@ import {
updateVendorInvestigationSchema,
type UpdateVendorInvestigationSchema,
} from "../validations"
-import { updateVendorInvestigationAction, getInvestigationAttachments, deleteInvestigationAttachment } from "../service"
+import { updateVendorInvestigationAction, getInvestigationAttachments, deleteInvestigationAttachment, createVendorInvestigationAttachmentAction } from "../service"
import { VendorInvestigationsViewWithContacts } from "@/config/vendorInvestigationsColumnsConfig"
import prettyBytes from "pretty-bytes"
import { downloadFile } from "@/lib/file-download"
@@ -183,15 +183,7 @@ export function UpdateVendorInvestigationSheet({
if (!investigation) return
try {
- const response = await fetch(`/api/vendor-investigations/${investigation.investigationId}/attachments?attachmentId=${attachmentId}`, {
- method: "DELETE",
- })
-
- if (!response.ok) {
- const errorData = await response.json()
- throw new Error(errorData.error || "μ²¨λΆ€νŒŒμΌ μ‚­μ œ μ‹€νŒ¨")
- }
-
+ await deleteInvestigationAttachment(attachmentId)
toast.success("μ²¨λΆ€νŒŒμΌμ΄ μ‚­μ œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
// λͺ©λ‘ μƒˆλ‘œκ³ μΉ¨
loadExistingAttachments(investigation.investigationId)
@@ -409,23 +401,26 @@ export function UpdateVendorInvestigationSheet({
// 파일 μ—…λ‘œλ“œ ν•¨μˆ˜
const uploadFiles = async (files: File[], investigationId: number) => {
const uploadPromises = files.map(async (file) => {
- const formData = new FormData()
- formData.append("file", file)
-
- const response = await fetch(`/api/vendor-investigations/${investigationId}/attachments`, {
- method: "POST",
- body: formData,
- })
+ try {
+ // μ„œλ²„ μ•‘μ…˜μ„ ν˜ΈμΆœν•˜μ—¬ 파일 μ €μž₯ 및 DB λ ˆμ½”λ“œ 생성
+ const result = await createVendorInvestigationAttachmentAction({
+ investigationId,
+ file,
+ userId: undefined // ν•„μš”μ‹œ μ‚¬μš©μž ID μΆ”κ°€
+ });
+
+ if (!result.success) {
+ throw new Error(result.error || "파일 μ—…λ‘œλ“œ μ‹€νŒ¨");
+ }
- if (!response.ok) {
- const errorData = await response.json()
- throw new Error(errorData.error || "파일 μ—…λ‘œλ“œ μ‹€νŒ¨")
+ return result.attachment;
+ } catch (error) {
+ console.error(`파일 μ—…λ‘œλ“œ μ‹€νŒ¨: ${file.name}`, error);
+ throw error;
}
+ });
- return await response.json()
- })
-
- return await Promise.all(uploadPromises)
+ return await Promise.all(uploadPromises);
}
// Submit handler