summaryrefslogtreecommitdiff
path: root/lib/gtc-contract/service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gtc-contract/service.ts')
-rw-r--r--lib/gtc-contract/service.ts65
1 files changed, 36 insertions, 29 deletions
diff --git a/lib/gtc-contract/service.ts b/lib/gtc-contract/service.ts
index 23cdd422..308c52bf 100644
--- a/lib/gtc-contract/service.ts
+++ b/lib/gtc-contract/service.ts
@@ -1,7 +1,7 @@
'use server'
-import { unstable_cache } from "next/cache"
-import { and, desc, asc, eq, or, ilike, count, max } from "drizzle-orm"
+import { revalidateTag, unstable_cache } from "next/cache"
+import { and, desc, asc, eq, or, ilike, count, max , inArray} from "drizzle-orm"
import db from "@/db/db"
import { gtcDocuments, gtcDocumentsView, type GtcDocument, type GtcDocumentWithRelations } from "@/db/schema/gtc"
import { projects } from "@/db/schema/projects"
@@ -164,6 +164,8 @@ export async function createGtcDocument(
})
.returning()
+ revalidateTag("gtc-documents")
+
return newDocument
}
@@ -174,6 +176,9 @@ export async function updateGtcDocument(
id: number,
data: UpdateGtcDocumentSchema & { updatedById: number }
): Promise<GtcDocument | null> {
+
+ console.log(data, "data")
+
const [updatedDocument] = await db
.update(gtcDocuments)
.set({
@@ -183,6 +188,8 @@ export async function updateGtcDocument(
.where(eq(gtcDocuments.id, id))
.returning()
+ revalidateTag("gtc-documents")
+
return updatedDocument || null
}
@@ -241,33 +248,6 @@ export async function deleteGtcDocument(
return !!updated
}
-/**
- * 프로젝트별 GTC 문서 목록 조회
- */
-export async function getGtcDocumentsByProject(projectId: number): Promise<GtcDocumentWithRelations[]> {
- return await selectGtcDocumentsWithRelations()
- .where(
- and(
- eq(gtcDocuments.projectId, projectId),
- eq(gtcDocuments.isActive, true)
- )
- )
- .orderBy(desc(gtcDocuments.revision))
-}
-
-/**
- * 표준 GTC 문서 목록 조회
- */
-export async function getStandardGtcDocuments(): Promise<GtcDocumentWithRelations[]> {
- return await selectGtcDocumentsWithRelations()
- .where(
- and(
- eq(gtcDocuments.type, "standard"),
- eq(gtcDocuments.isActive, true)
- )
- )
- .orderBy(desc(gtcDocuments.revision))
-}
// 타입 정의
export type ProjectForFilter = {
@@ -323,4 +303,31 @@ export async function getUsersForFilter(): Promise<UserForFilter[]> {
.from(users)
.where(eq(users.isActive, true)) // 활성 사용자만
.orderBy(users.name)
+}
+
+/**
+ * GTC 문서 단일 조회
+ */
+export async function getGtcDocumentById(id: number) {
+ return unstable_cache(
+ async () => {
+ try {
+ const [document] = await db
+ .select()
+ .from(gtcDocumentsView)
+ .where(eq(gtcDocumentsView.id, id))
+ .limit(1)
+
+ return document || null
+ } catch (err) {
+ console.error("Error fetching GTC document by ID:", err)
+ return null
+ }
+ },
+ [`gtc-document-${id}`],
+ {
+ revalidate: 3600,
+ tags: [`gtc-document-${id}`, "gtc-documents"],
+ }
+ )()
} \ No newline at end of file