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.ts105
1 files changed, 34 insertions, 71 deletions
diff --git a/lib/gtc-contract/service.ts b/lib/gtc-contract/service.ts
index 61e69995..23cdd422 100644
--- a/lib/gtc-contract/service.ts
+++ b/lib/gtc-contract/service.ts
@@ -1,7 +1,9 @@
+'use server'
+
import { unstable_cache } from "next/cache"
import { and, desc, asc, eq, or, ilike, count, max } from "drizzle-orm"
import db from "@/db/db"
-import { gtcDocuments, type GtcDocument, type GtcDocumentWithRelations } from "@/db/schema/gtc"
+import { gtcDocuments, gtcDocumentsView, type GtcDocument, type GtcDocumentWithRelations } from "@/db/schema/gtc"
import { projects } from "@/db/schema/projects"
import { users } from "@/db/schema/users"
import { filterColumns } from "@/lib/filter-columns"
@@ -20,44 +22,7 @@ export async function checkProjectExists(projectId: number): Promise<boolean> {
return result.length > 0
}
-/**
- * GTC 문서 관련 뷰/조인 쿼리를 위한 기본 select
- */
-function selectGtcDocumentsWithRelations() {
- return db
- .select({
- id: gtcDocuments.id,
- type: gtcDocuments.type,
- projectId: gtcDocuments.projectId,
- revision: gtcDocuments.revision,
- createdAt: gtcDocuments.createdAt,
- createdById: gtcDocuments.createdById,
- updatedAt: gtcDocuments.updatedAt,
- updatedById: gtcDocuments.updatedById,
- editReason: gtcDocuments.editReason,
- isActive: gtcDocuments.isActive,
- // 관계 데이터
- project: {
- id: projects.id,
- code: projects.code,
- name: projects.name,
- },
- createdBy: {
- id: users.id,
- name: users.name,
- email: users.email,
- },
- updatedBy: {
- id: users.id,
- name: users.name,
- email: users.email,
- },
- })
- .from(gtcDocuments)
- .leftJoin(projects, eq(gtcDocuments.projectId, projects.id))
- .leftJoin(users, eq(gtcDocuments.createdById, users.id))
- .leftJoin(users, eq(gtcDocuments.updatedById, users.id))
-}
+
/**
* GTC 문서 개수 조회
@@ -82,7 +47,7 @@ export async function getGtcDocuments(input: GetGtcDocumentsSchema) {
// (1) advancedWhere - 고급 필터
const advancedWhere = filterColumns({
- table: gtcDocuments,
+ table: gtcDocumentsView,
filters: input.filters,
joinOperator: input.joinOperator,
})
@@ -92,49 +57,37 @@ export async function getGtcDocuments(input: GetGtcDocumentsSchema) {
if (input.search) {
const s = `%${input.search}%`
globalWhere = or(
- ilike(gtcDocuments.editReason, s),
+ ilike(gtcDocumentsView.editReason, s),
ilike(projects.name, s),
ilike(projects.code, s)
)
}
- // (3) 기본 필터들
- const basicFilters = []
-
- if (input.type && input.type !== "") {
- basicFilters.push(eq(gtcDocuments.type, input.type))
- }
-
- if (input.projectId && input.projectId > 0) {
- basicFilters.push(eq(gtcDocuments.projectId, input.projectId))
- }
-
- // 활성 문서만 조회 (기본값)
- basicFilters.push(eq(gtcDocuments.isActive, true))
// (4) 최종 where 조건
const finalWhere = and(
advancedWhere,
globalWhere,
- ...basicFilters
)
// (5) 정렬
const orderBy =
input.sort.length > 0
? input.sort.map((item) => {
- const column = gtcDocuments[item.id as keyof typeof gtcDocuments]
+ const column = gtcDocumentsView[item.id as keyof typeof gtcDocumentsView]
return item.desc ? desc(column) : asc(column)
})
- : [desc(gtcDocuments.updatedAt)]
+ : [desc(gtcDocumentsView.updatedAt)]
// (6) 데이터 조회
const { data, total } = await db.transaction(async (tx) => {
- const data = await selectGtcDocumentsWithRelations()
- .where(finalWhere)
- .orderBy(...orderBy)
- .offset(offset)
- .limit(input.perPage)
+ const data =await db
+ .select()
+ .from(gtcDocumentsView)
+ .where(finalWhere)
+ .orderBy(...orderBy)
+ .limit(input.perPage)
+ .offset(offset);
const total = await countGtcDocuments(tx, finalWhere)
return { data, total }
@@ -155,17 +108,27 @@ export async function getGtcDocuments(input: GetGtcDocumentsSchema) {
)()
}
-/**
- * 특정 GTC 문서 조회
- */
-export async function getGtcDocumentById(id: number): Promise<GtcDocumentWithRelations | null> {
- const result = await selectGtcDocumentsWithRelations()
- .where(eq(gtcDocuments.id, id))
- .limit(1)
+// 성공한 ID들을 반환하는 버전
+export async function deleteGtcDocuments(
+ ids: number[],
+ updatedById: number
+): Promise<number[]> {
+ if (ids.length === 0) {
+ return [];
+ }
- return result[0] || null
-}
+ const updated = await db
+ .update(gtcDocuments)
+ .set({
+ isActive: false,
+ updatedById,
+ updatedAt: new Date(),
+ })
+ .where(inArray(gtcDocuments.id, ids))
+ .returning({ id: gtcDocuments.id });
+ return updated.map(doc => doc.id);
+}
/**
* 다음 리비전 번호 조회
*/