diff options
Diffstat (limited to 'lib/vendor-document-list/enhanced-document-service.ts')
| -rw-r--r-- | lib/vendor-document-list/enhanced-document-service.ts | 163 |
1 files changed, 162 insertions, 1 deletions
diff --git a/lib/vendor-document-list/enhanced-document-service.ts b/lib/vendor-document-list/enhanced-document-service.ts index 05ace8d5..f2d9c26f 100644 --- a/lib/vendor-document-list/enhanced-document-service.ts +++ b/lib/vendor-document-list/enhanced-document-service.ts @@ -2,7 +2,7 @@ "use server" import { revalidatePath, unstable_cache } from "next/cache" -import { and, asc, desc, eq, ilike, or, count, avg, inArray, sql } from "drizzle-orm" +import { and, asc, desc, eq, ilike, or, count, avg, inArray, sql, ne } from "drizzle-orm" import db from "@/db/db" import { StageDocumentsView, documentAttachments, documentStagesOnlyView, documents, enhancedDocumentsView, issueStages, revisions, simplifiedDocumentsView, type EnhancedDocumentsView } from "@/db/schema/vendorDocu" import { filterColumns } from "@/lib/filter-columns" @@ -1175,3 +1175,164 @@ export async function getDocumentDetails(documentId: number) { + export interface UpdateRevisionInput { + revisionId: number + revision: string // ✅ revision 필드 추가 + comment?: string | null + usage: string + usageType?: string | null + } + + export interface UpdateRevisionResult { + success: boolean + message?: string + error?: string + updatedRevision?: any + } + + export async function updateRevisionAction( + input: UpdateRevisionInput + ): Promise<UpdateRevisionResult> { + try { + const { revisionId, revision, comment, usage, usageType } = input + + // 1. 리비전 존재 여부 확인 + const existingRevision = await db + .select() + .from(revisions) + .where(eq(revisions.id, revisionId)) + .limit(1) + + if (!existingRevision || existingRevision.length === 0) { + return { + success: false, + error: "Revision not found" + } + } + + // 2. 동일한 revision 번호가 같은 문서에 이미 존재하는지 확인 (자기 자신 제외) + const duplicateRevision = await db + .select() + .from(revisions) + .innerJoin(issueStages, eq(revisions.issueStageId, issueStages.id)) + .where( + and( + eq(revisions.revision, revision.trim()), + eq(issueStages.documentId, existingRevision[0].issueStageId), // 같은 문서 내에서 + ne(revisions.id, revisionId) // 자기 자신 제외 + ) + ) + .limit(1) + + if (duplicateRevision && duplicateRevision.length > 0) { + return { + success: false, + error: `Revision "${revision.trim()}" already exists in this document` + } + } + + // 3. 첨부파일이 처리된 상태인지 확인 (수정 가능 여부 체크) + const attachments = await db + .select() + .from(documentAttachments) + .where(eq(documentAttachments.revisionId, revisionId)) + + const hasProcessedFiles = attachments.some(att => + att.dolceFilePath && att.dolceFilePath.trim() !== '' + ) + + if (hasProcessedFiles) { + return { + success: false, + error: "Cannot edit revision with processed files" + } + } + + // 4. 리비전 업데이트 + const [updatedRevision] = await db + .update(revisions) + .set({ + revision: revision.trim(), // ✅ revision 필드 업데이트 추가 + comment: comment?.trim() || null, + usage: usage.trim(), + usageType: usageType?.trim() || null, + updatedAt: new Date(), + }) + .where(eq(revisions.id, revisionId)) + .returning() + + revalidatePath("/partners/document-list-ship") // ✅ 경로 오타 수정 + + return { + success: true, + message: `Revision ${revision.trim()} updated successfully`, // ✅ 새 revision 값 사용 + updatedRevision + } + + } catch (error) { + console.error("❌ Revision update server action error:", error) + + return { + success: false, + error: error instanceof Error ? error.message : "Failed to update revision" + } + } + } + // 삭제 서버 액션도 함께 만들어드릴게요 + export interface DeleteRevisionInput { + revisionId: number + } + + export interface DeleteRevisionResult { + success: boolean + message?: string + error?: string + deletedRevisionId?: number + deletedAttachmentsCount?: number + } + + export async function deleteRevisionAction( + input: DeleteRevisionInput + ): Promise<DeleteRevisionResult> { + try { + const { revisionId } = input + + // 1. 리비전과 첨부파일 정보 조회 + const revision = await db + .select() + .from(revisions) + .where(eq(revisions.id, revisionId)) + .limit(1) + + if (!revision || revision.length === 0) { + return { + success: false, + error: "Revision not found" + } + } + + + // 5. 리비전 삭제 + await db + .delete(revisions) + .where(eq(revisions.id, revisionId)) + + // 6. 캐시 재검증 + revalidatePath("/parnters/document-list-ship") + + return { + success: true, + message: `Revision ${revision[0].revision} deleted successfully`, + deletedRevisionId: revisionId, + deletedAttachmentsCount: 0 // revisionAttachments.length + } + + } catch (error) { + console.error("❌ Revision delete server action error:", error) + + return { + success: false, + error: error instanceof Error ? error.message : "Failed to delete revision" + } + } + }
\ No newline at end of file |
