summaryrefslogtreecommitdiff
path: root/lib/vendor-document
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-04-28 02:13:30 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-04-28 02:13:30 +0000
commitef4c533ebacc2cdc97e518f30e9a9350004fcdfb (patch)
tree345251a3ed0f4429716fa5edaa31024d8f4cb560 /lib/vendor-document
parent9ceed79cf32c896f8a998399bf1b296506b2cd4a (diff)
~20250428 작업사항
Diffstat (limited to 'lib/vendor-document')
-rw-r--r--lib/vendor-document/service.ts106
1 files changed, 106 insertions, 0 deletions
diff --git a/lib/vendor-document/service.ts b/lib/vendor-document/service.ts
index c0a30808..d81e703c 100644
--- a/lib/vendor-document/service.ts
+++ b/lib/vendor-document/service.ts
@@ -12,6 +12,7 @@ import { countVendorDocuments, selectVendorDocuments } from "./repository"
import path from "path";
import fs from "fs/promises";
import { v4 as uuidv4 } from "uuid"
+import { contractItems } from "@/db/schema"
/**
* 특정 vendorId에 속한 문서 목록 조회
@@ -342,4 +343,109 @@ export async function getStageNamesByDocumentId(documentId: number) {
console.error("Error fetching stage names:", error);
return []; // 오류 발생시 빈 배열 반환
}
+}
+
+
+// Define the return types
+export interface Document {
+ id: number;
+ docNumber: string;
+ title: string;
+}
+
+export interface IssueStage {
+ id: number;
+ stageName: string;
+}
+
+export interface Revision {
+ revision: string;
+}
+
+// Server Action: Fetch documents by packageId (contractItems.id)
+export async function fetchDocumentsByPackageId(packageId: number): Promise<Document[]> {
+ try {
+ // First, find the contractId from contractItems where id = packageId
+ const contractItemResult = await db.select({ contractId: contractItems.contractId })
+ .from(contractItems)
+ .where(eq(contractItems.id, packageId))
+ .limit(1);
+
+ if (!contractItemResult.length) {
+ return [];
+ }
+
+ const contractId = contractItemResult[0].contractId;
+
+ // Then, get documents associated with this contractId
+ const docsResult = await db.select({
+ id: documents.id,
+ docNumber: documents.docNumber,
+ title: documents.title,
+ })
+ .from(documents)
+ .where(eq(documents.contractId, contractId))
+ .orderBy(documents.docNumber);
+
+ return docsResult;
+ } catch (error) {
+ console.error("Error fetching documents:", error);
+ return [];
+ }
+}
+
+// Server Action: Fetch stages by documentId
+export async function fetchStagesByDocumentId(documentId: number): Promise<IssueStage[]> {
+ try {
+ const stagesResult = await db.select({
+ id: issueStages.id,
+ stageName: issueStages.stageName,
+ })
+ .from(issueStages)
+ .where(eq(issueStages.documentId, documentId))
+ .orderBy(issueStages.stageName);
+
+ return stagesResult;
+ } catch (error) {
+ console.error("Error fetching stages:", error);
+ return [];
+ }
+}
+
+// Server Action: Fetch revisions by documentId and stageName
+export async function fetchRevisionsByStageParams(
+ documentId: number,
+ stageName: string
+): Promise<Revision[]> {
+ try {
+ // First, find the issueStageId
+ const stageResult = await db.select({ id: issueStages.id })
+ .from(issueStages)
+ .where(
+ and(
+ eq(issueStages.documentId, documentId),
+ eq(issueStages.stageName, stageName)
+ )
+ )
+ .limit(1);
+
+ if (!stageResult.length) {
+ return [];
+ }
+
+ const issueStageId = stageResult[0].id;
+
+ // Then, get revisions for this stage
+ const revisionsResult = await db.select({
+ revision: revisions.revision,
+ })
+ .from(revisions)
+ .where(eq(revisions.issueStageId, issueStageId))
+ .orderBy(revisions.revision);
+
+ return revisionsResult;
+ } catch (error) {
+ console.error("Error fetching revisions:", error);
+ return [];
+ }
} \ No newline at end of file