diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/pq/service.ts | 14 | ||||
| -rw-r--r-- | lib/tech-vendors/service.ts | 198 | ||||
| -rw-r--r-- | lib/tech-vendors/table/import-button.tsx | 76 | ||||
| -rw-r--r-- | lib/vendor-investigation/service.ts | 66 | ||||
| -rw-r--r-- | lib/vendor-investigation/table/update-investigation-sheet.tsx | 43 |
5 files changed, 326 insertions, 71 deletions
diff --git a/lib/pq/service.ts b/lib/pq/service.ts index 989f8d5c..40d81302 100644 --- a/lib/pq/service.ts +++ b/lib/pq/service.ts @@ -3706,13 +3706,13 @@ export async function updateInvestigationDetailsAction(input: { try {
console.log(`๐ ์ค์ฌ ์ฒจ๋ถํ์ผ ์ฒ๋ฆฌ ์ค: ${file.name} (${file.size} bytes)`);
- // saveFile์ ์ฌ์ฉํ์ฌ ํ์ผ ์ ์ฅ
- const saveResult = await saveFile({
+ // saveDRMFile์ ์ฌ์ฉํ์ฌ ํ์ผ ์ ์ฅ
+ const saveResult = await saveDRMFile(
file,
- directory: `vendor-investigation/${input.investigationId}`,
- originalName: file.name,
- userId: "investigation-update"
- });
+ decryptWithServerAction,
+ `vendor-investigation/${input.investigationId}`,
+ "investigation-update"
+ );
if (!saveResult.success) {
console.error(`โ ํ์ผ ์ ์ฅ ์คํจ: ${file.name}`, saveResult.error);
@@ -3738,7 +3738,7 @@ export async function updateInvestigationDetailsAction(input: { // DB์ ์ฒจ๋ถํ์ผ ๋ ์ฝ๋ ์์ฑ
await tx.insert(vendorInvestigationAttachments).values({
investigationId: input.investigationId,
- fileName: saveResult.originalName!,
+ fileName: saveResult.fileName!,
originalFileName: file.name,
filePath: saveResult.publicPath!,
fileSize: file.size,
diff --git a/lib/tech-vendors/service.ts b/lib/tech-vendors/service.ts index f5380889..e8dcb5dc 100644 --- a/lib/tech-vendors/service.ts +++ b/lib/tech-vendors/service.ts @@ -1423,29 +1423,138 @@ export async function importTechVendorsFromExcel( }); continue; } + // 1. ์ด๋ฉ์ผ๋ก ๊ธฐ์กด ๋ฒค๋ ์ค๋ณต ์ฒดํฌ - const existingVendor = await tx.query.techVendors.findFirst({ + let existingVendor = await tx.query.techVendors.findFirst({ where: eq(techVendors.email, vendor.email), - columns: { id: true, vendorName: true, email: true } + columns: { id: true, vendorName: true, vendorCode: true, email: true } }); + // 2. ์ด๋ฉ์ผ์ด ์ค๋ณต๋์ง ์์ ๊ฒฝ์ฐ ๋ฒค๋ ์ฝ๋๋ ์ด๋ฆ์ผ๋ก ์ถ๊ฐ ํ์ธ + if (!existingVendor && vendor.vendorCode) { + existingVendor = await tx.query.techVendors.findFirst({ + where: eq(techVendors.vendorCode, vendor.vendorCode), + columns: { id: true, vendorName: true, vendorCode: true, email: true } + }); + } + + // 3. ๋ฒค๋ ์ฝ๋๋ ์ผ์นํ์ง ์๋ ๊ฒฝ์ฐ ๋ฒค๋ ์ด๋ฆ์ผ๋ก ํ์ธ + if (!existingVendor) { + existingVendor = await tx.query.techVendors.findFirst({ + where: eq(techVendors.vendorName, vendor.vendorName), + columns: { id: true, vendorName: true, vendorCode: true, email: true } + }); + } + + // 4. ์ผ์นํ๋ ๋ฒค๋๊ฐ ์๋ ๊ฒฝ์ฐ ์ฒ๋ฆฌ if (existingVendor) { - console.log("์ด๋ฏธ ์กด์ฌํ๋ ๋ฒค๋ ์คํต:", vendor.vendorName, vendor.email); + console.log("๊ธฐ์กด ๋ฒค๋์ ๋ด๋น์ ์ถ๊ฐ:", existingVendor.vendorName, vendor.email); + + // ๊ธฐ์กด ๋ฒค๋์ ๋ฒค๋ ํ์
์
๋ฐ์ดํธ (์๋ก์ด ํ์
์ถ๊ฐ) + const existingVendorFull = await tx.query.techVendors.findFirst({ + where: eq(techVendors.id, existingVendor.id), + columns: { id: true, techVendorType: true } + }); + + if (existingVendorFull) { + const existingTypes = existingVendorFull.techVendorType ? existingVendorFull.techVendorType.split(',').map(t => t.trim()) : []; + const newType = vendor.techVendorType.trim(); + + // ์๋ก์ด ํ์
์ด ๊ธฐ์กด์ ์๋ ๊ฒฝ์ฐ์๋ง ์ถ๊ฐ + if (!existingTypes.includes(newType)) { + const updatedTypes = [...existingTypes, newType]; + const updatedTypeString = updatedTypes.join(', '); + + await tx.update(techVendors) + .set({ techVendorType: updatedTypeString }) + .where(eq(techVendors.id, existingVendor.id)); + + console.log(`๋ฒค๋ ํ์
์
๋ฐ์ดํธ: ${existingVendorFull.techVendorType} -> ${updatedTypeString}`); + } + } + + // ๋ด๋น์ ์ ๋ณด๋ฅผ ๊ธฐ์กด ๋ฒค๋์ ์ถ๊ฐ + let contactName = vendor.vendorName; + let contactEmail = vendor.email; + + // vendor.contacts๊ฐ ์๊ณ , contactName์ด ์์ผ๋ฉด contactName ์ฌ์ฉ + if (vendor.contacts && vendor.contacts.length > 0 && vendor.contacts[0].contactName) { + contactName = vendor.contacts[0].contactName; + // ๋ง์ฝ contactEmail์ด ์์ผ๋ฉด ๊ทธ๊ฑธ ์ฌ์ฉ, ์์ผ๋ฉด vendor.email ์ฌ์ฉ + if (vendor.contacts[0].contactEmail) { + contactEmail = vendor.contacts[0].contactEmail; + } + } + + // ๋ด๋น์ ์ด๋ฉ์ผ ์ค๋ณต ์ฒดํฌ + const existingContact = await tx.query.techVendorContacts.findFirst({ + where: and( + eq(techVendorContacts.vendorId, existingVendor.id), + eq(techVendorContacts.contactEmail, contactEmail) + ), + columns: { id: true, contactEmail: true } + }); + + if (existingContact) { + console.log("๋ด๋น์ ์ด๋ฉ์ผ ์ค๋ณต:", contactEmail); + errors.push({ + vendorName: vendor.vendorName, + email: vendor.email, + error: `๋ด๋น์ ์ด๋ฉ์ผ '${contactEmail}'์ด(๊ฐ) ์ด๋ฏธ ๋ฑ๋ก๋์ด ์์ต๋๋ค` + }); + } else { + // ๋ด๋น์ ์์ฑ + await tx.insert(techVendorContacts).values({ + vendorId: existingVendor.id, + contactName: contactName, + contactPosition: null, + contactEmail: contactEmail, + contactPhone: null, + contactCountry: null, + isPrimary: false, + }); + console.log("๋ด๋น์ ์ถ๊ฐ ์ฑ๊ณต:", contactName, contactEmail); + } + + // ๊ธฐ์กด ๋ฒค๋์ ๋ด๋น์ ์ถ๊ฐํ์ผ๋ฏ๋ก ๋ฒค๋ ์์ฑ์ ์คํตํ๊ณ ์ ์ ์์ฑ์ผ๋ก ๋์ด๊ฐ skippedVendors.push({ vendorName: vendor.vendorName, email: vendor.email, - reason: `์ด๋ฏธ ๋ฑ๋ก๋ ์ด๋ฉ์ผ์
๋๋ค (๊ธฐ์กด ์
์ฒด: ${existingVendor.vendorName})` + reason: `๊ธฐ์กด ๋ฒค๋์ ๋ด๋น์ ์ถ๊ฐ๋จ (๊ธฐ์กด ์
์ฒด: ${existingVendor.vendorName})` }); - continue; + + // ์ ์ ์์ฑ (๊ธฐ์กด ๋ฒค๋์ ๋ด๋น์๋ก ์ถ๊ฐ๋ ๊ฒฝ์ฐ) + if (contactEmail) { + console.log("์ ์ ์์ฑ ์๋:", contactEmail); + + // ์ด๋ฏธ ์กด์ฌํ๋ ์ ์ ์ธ์ง ํ์ธ + const existingUser = await tx.query.users.findFirst({ + where: eq(users.email, contactEmail), + columns: { id: true } + }); + + if (!existingUser) { + // ์ ์ ๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ ์์ฑ + await tx.insert(users).values({ + name: contactName, + email: contactEmail, + techCompanyId: existingVendor.id, + domain: "partners", + }); + console.log("์ ์ ์์ฑ ์ฑ๊ณต"); + } else { + // ์ด๋ฏธ ์กด์ฌํ๋ ์ ์ ๋ผ๋ฉด techCompanyId ์
๋ฐ์ดํธ + await tx.update(users) + .set({ techCompanyId: existingVendor.id }) + .where(eq(users.id, existingUser.id)); + console.log("์ด๋ฏธ ์กด์ฌํ๋ ์ ์ , techCompanyId ์
๋ฐ์ดํธ:", existingUser.id); + } + } + + continue; // ๋ฒค๋ ์์ฑ ๋ถ๋ถ์ผ๋ก ๋์ด๊ฐ์ง ์์ } // 2. ๋ฒค๋ ์์ฑ - console.log("๋ฒค๋ ์์ฑ ์๋:", { - vendorName: vendor.vendorName, - email: vendor.email, - techVendorType: vendor.techVendorType - }); - const [newVendor] = await tx.insert(techVendors).values({ vendorName: vendor.vendorName, vendorCode: vendor.vendorCode || null, @@ -1486,30 +1595,21 @@ export async function importTechVendorsFromExcel( }); console.log("๋ด๋น์ ์์ฑ ์ฑ๊ณต:", contact.contactName, contact.contactEmail); } - - // // ๋ฒค๋ ์ด๋ฉ์ผ์ ์ฃผ ๋ด๋น์์ ์ด๋ฉ์ผ๋ก ์
๋ฐ์ดํธ - // const primaryContact = vendor.contacts.find(c => c.isPrimary) || vendor.contacts[0]; - // if (primaryContact && primaryContact.contactEmail !== vendor.email) { - // await tx.update(techVendors) - // .set({ email: primaryContact.contactEmail }) - // .where(eq(techVendors.id, newVendor.id)); - // console.log("๋ฒค๋ ์ด๋ฉ์ผ ์
๋ฐ์ดํธ:", primaryContact.contactEmail); - // } } - // else { - // // ๋ด๋น์ ์ ๋ณด๊ฐ ์๋ ๊ฒฝ์ฐ ๋ฒค๋ ์ ๋ณด๋ก ๊ธฐ๋ณธ ๋ด๋น์ ์์ฑ - // console.log("๊ธฐ๋ณธ ๋ด๋น์ ์์ฑ"); - // await tx.insert(techVendorContacts).values({ - // vendorId: newVendor.id, - // contactName: vendor.representativeName || vendor.vendorName || "๊ธฐ๋ณธ ๋ด๋น์", - // contactPosition: null, - // contactEmail: vendor.email, - // contactPhone: vendor.representativePhone || vendor.phone || null, - // contactCountry: vendor.country || null, - // isPrimary: true, - // }); - // console.log("๊ธฐ๋ณธ ๋ด๋น์ ์์ฑ ์ฑ๊ณต:", vendor.email); - // } + else { + // ๋ด๋น์ ์ ๋ณด๊ฐ ์๋ ๊ฒฝ์ฐ ๋ฒค๋ ์ ๋ณด๋ก ๊ธฐ๋ณธ ๋ด๋น์ ์์ฑ + console.log("๊ธฐ๋ณธ ๋ด๋น์ ์์ฑ"); + await tx.insert(techVendorContacts).values({ + vendorId: newVendor.id, + contactName: vendor.representativeName || vendor.vendorName || "๊ธฐ๋ณธ ๋ด๋น์", + contactPosition: null, + contactEmail: vendor.email, + contactPhone: vendor.representativePhone || vendor.phone || null, + contactCountry: vendor.country || null, + isPrimary: true, + }); + console.log("๊ธฐ๋ณธ ๋ด๋น์ ์์ฑ ์ฑ๊ณต:", vendor.email); + } // 3. ์ ์ ์์ฑ (์ด๋ฉ์ผ์ด ์๋ ๊ฒฝ์ฐ) if (vendor.email) { @@ -3038,11 +3138,37 @@ export async function importPossibleItemsFromExcel( continue } - const vendor = await findVendorByEmail(row.vendorEmail.trim()) + let vendor = await findVendorByEmail(row.vendorEmail.trim()) + + // 2. ๋ฒค๋ ํ
์ด๋ธ์์ ์ฐพ์ ์ ์๋ ๊ฒฝ์ฐ, ๋ด๋น์ ํ
์ด๋ธ์์ ์ฐพ๊ธฐ + if (!vendor) { + console.log(`๋ฒค๋ ํ
์ด๋ธ์์ ์ฐพ์ ์ ์์, ๋ด๋น์ ํ
์ด๋ธ์์ ๊ฒ์: ${row.vendorEmail}`) + + // ๋ด๋น์ ํ
์ด๋ธ์์ ํด๋น ์ด๋ฉ์ผ๋ก ๊ฒ์ + const contact = await db.query.techVendorContacts.findFirst({ + where: eq(techVendorContacts.contactEmail, row.vendorEmail.trim()), + columns: { vendorId: true, contactEmail: true } + }) + + if (contact) { + console.log(`๋ด๋น์ ํ
์ด๋ธ์์ ์ฐพ์, ๋ฒค๋ ID: ${contact.vendorId}`) + + // ํด๋น ๋ฒค๋ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ + vendor = await db.query.techVendors.findFirst({ + where: eq(techVendors.id, contact.vendorId), + columns: { id: true, vendorName: true, email: true } + }) + + if (vendor) { + console.log(`๋ด๋น์๋ฅผ ํตํด ๋ฒค๋ ์ฐพ์: ${vendor.vendorName}`) + } + } + } + if (!vendor) { result.failedRows.push({ row: rowNumber, - error: `๋ฒค๋ ์ด๋ฉ์ผ '${row.vendorEmail}'์(๋ฅผ) ์ฐพ์ ์ ์์ต๋๋ค.`, + error: `๋ฒค๋ ์ด๋ฉ์ผ '${row.vendorEmail}'์(๋ฅผ) ์ฐพ์ ์ ์์ต๋๋ค. (๋ฒค๋ ํ
์ด๋ธ๊ณผ ๋ด๋น์ ํ
์ด๋ธ ๋ชจ๋์์ ๊ฒ์ ์คํจ)`, vendorEmail: row.vendorEmail, itemCode: row.itemCode, itemType: row.itemType as "์กฐ์ " | "ํด์TOP" | "ํด์HULL", diff --git a/lib/tech-vendors/table/import-button.tsx b/lib/tech-vendors/table/import-button.tsx index 1d3bf242..b268d29d 100644 --- a/lib/tech-vendors/table/import-button.tsx +++ b/lib/tech-vendors/table/import-button.tsx @@ -101,8 +101,14 @@ export function ImportTechVendorButton({ onSuccess }: ImportTechVendorButtonProp // ํค๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ธ๋ฑ์ค ๋งคํ ์์ฑ
const headerMapping: Record<string, number> = {};
headerValues.forEach((value, index) => {
- if (typeof value === 'string') {
- headerMapping[value] = index;
+ // ๋ฆฌ์นํ
์คํธ๋ฅผ ์ผ๋ฐ ํ
์คํธ๋ก ๋ณํ
+ const cleanValue = typeof value === 'string' ? value :
+ value && typeof value === 'object' && 'richText' in value ?
+ value.richText?.map((rt: any) => rt.text || '').join('') || '' :
+ String(value || '');
+
+ if (cleanValue && cleanValue.trim()) {
+ headerMapping[cleanValue] = index;
}
});
@@ -156,7 +162,19 @@ export function ImportTechVendorButton({ onSuccess }: ImportTechVendorButtonProp // ํค๋ ๋งคํ์ ๋ฐ๋ผ ๋ฐ์ดํฐ ์ถ์ถ
Object.entries(headerMapping).forEach(([header, index]) => {
- rowData[header] = values[index] || "";
+ const rawValue = values[index];
+
+ // ๋ฆฌ์นํ
์คํธ๋ฅผ ์ผ๋ฐ ํ
์คํธ๋ก ๋ณํ
+ let cleanValue = "";
+ if (typeof rawValue === 'string') {
+ cleanValue = rawValue;
+ } else if (rawValue && typeof rawValue === 'object' && 'richText' in rawValue) {
+ cleanValue = rawValue.richText?.map((rt: any) => rt.text || '').join('') || '';
+ } else if (rawValue !== null && rawValue !== undefined) {
+ cleanValue = String(rawValue);
+ }
+
+ rowData[header] = cleanValue;
});
// ๋น ํ์ด ์๋ ๊ฒฝ์ฐ๋ง ์ถ๊ฐ
@@ -271,6 +289,58 @@ export function ImportTechVendorButton({ onSuccess }: ImportTechVendorButtonProp toast.error(result.error || "๋ฒค๋ ๊ฐ์ ธ์ค๊ธฐ์ ์คํจํ์ต๋๋ค.");
}
+ // ์๋ฌ๊ฐ ์์ผ๋ฉด ์๋ฌ ์์
๋ค์ด๋ก๋ (์ฑ๊ณต/์คํจ ์๊ด์์ด)
+ if (result.details?.errors && result.details.errors.length > 0) {
+ try {
+ // ์๋ฌ ๋ฐ์ดํฐ๋ฅผ Excel๋ก ๋ณํ
+ const errorWorkbook = new ExcelJS.Workbook();
+ const errorWorksheet = errorWorkbook.addWorksheet("์ค๋ฅ๋ด์ญ");
+
+ // ํค๋ ์ถ๊ฐ
+ errorWorksheet.columns = [
+ { header: "์
์ฒด๋ช
", key: "vendorName", width: 25 },
+ { header: "์ด๋ฉ์ผ", key: "email", width: 30 },
+ { header: "์ค๋ฅ๋ด์ฉ", key: "error", width: 80, style: { alignment: { wrapText: true }, font: { color: { argb: "FFFF0000" } } } },
+ ];
+
+ // ํค๋ ์คํ์ผ ์ค์
+ const headerRow = errorWorksheet.getRow(1);
+ headerRow.font = { bold: true };
+ headerRow.fill = {
+ type: "pattern",
+ pattern: "solid",
+ fgColor: { argb: "FFFFCCCC" },
+ };
+
+ // ์ค๋ฅ ๋ฐ์ดํฐ ์ถ๊ฐ
+ result.details.errors.forEach(errorItem => {
+ errorWorksheet.addRow({
+ vendorName: errorItem.vendorName || "N/A",
+ email: errorItem.email || "N/A",
+ error: errorItem.error || "์ ์ ์๋ ์ค๋ฅ",
+ });
+ });
+
+ const buffer = await errorWorkbook.xlsx.writeBuffer();
+ const blob = new Blob([buffer], {
+ type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
+ });
+ const url = window.URL.createObjectURL(blob);
+ const link = document.createElement("a");
+ link.href = url;
+ link.download = "๋ฒค๋_import_์๋ฌ.xlsx";
+ document.body.appendChild(link);
+ link.click();
+ document.body.removeChild(link);
+ window.URL.revokeObjectURL(url);
+
+ toast.info("์๋ฌ ๋ด์ญ ํ์ผ์ด ๋ค์ด๋ก๋๋์์ต๋๋ค.");
+ } catch (excelError) {
+ console.error("์๋ฌ ์์
์์ฑ ์คํจ:", excelError);
+ toast.error("์๋ฌ ์์
์์ฑ์ ์คํจํ์ต๋๋ค.");
+ }
+ }
+
// ์ํ ์ด๊ธฐํ ๋ฐ ๋ค์ด์ผ๋ก๊ทธ ๋ซ๊ธฐ
setFile(null);
setOpen(false);
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 |
