summaryrefslogtreecommitdiff
path: root/lib/pq/service.ts
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-10-01 09:48:03 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-10-01 09:48:03 +0000
commit33e8452331c301430191b3506825ebaf3edac93a (patch)
tree6d92d754dbd30cafe0f3f920a14d6d6031c624b8 /lib/pq/service.ts
parent8ac4e8d9faa6e86ca6c7ab475efd7462d76fc9b6 (diff)
(최겸) 구매 PQ 리스트 기능 수정, 견적 첨부파일 리비전 액션 추가, 기타 등
Diffstat (limited to 'lib/pq/service.ts')
-rw-r--r--lib/pq/service.ts133
1 files changed, 111 insertions, 22 deletions
diff --git a/lib/pq/service.ts b/lib/pq/service.ts
index 7aa80dfa..67be5398 100644
--- a/lib/pq/service.ts
+++ b/lib/pq/service.ts
@@ -1,7 +1,7 @@
"use server"
import db from "@/db/db"
-import { CopyPqListInput, CreatePqListInput, copyPqListSchema, createPqListSchema, GetPqListsSchema, GetPQSchema, GetPQSubmissionsSchema } from "./validations"
+import { CopyPqListInput, CreatePqListInput, UpdatePqValidToInput, copyPqListSchema, createPqListSchema, updatePqValidToSchema, GetPqListsSchema, GetPQSchema, GetPQSubmissionsSchema } from "./validations"
import { unstable_cache } from "@/lib/unstable-cache";
import { filterColumns } from "@/lib/filter-columns";
import { getErrorMessage } from "@/lib/handle-error";
@@ -2977,7 +2977,9 @@ export async function getPQLists(input: GetPqListsSchema) {
const s = `%${input.search}%`;
globalWhere = or(
ilike(pqLists.name, s),
- ilike(pqLists.type, s)
+ ilike(pqLists.type, s),
+ ilike(projects.code, s),
+ ilike(projects.name, s)
);
}
@@ -3125,6 +3127,17 @@ export async function createPQListAction(input: CreatePqListInput) {
// 프로젝트 PQ인 경우 중복 체크
if (validated.type === "PROJECT" && validated.projectId) {
+ // 프로젝트 정보 조회 (이름과 코드 포함)
+ const projectInfo = await db
+ .select({
+ code: projects.code,
+ name: projects.name
+ })
+ .from(projects)
+ .where(eq(projects.id, validated.projectId))
+ .limit(1)
+ .then(rows => rows[0]);
+
const existingPQ = await db
.select()
.from(pqLists)
@@ -3136,11 +3149,12 @@ export async function createPQListAction(input: CreatePqListInput) {
)
)
.limit(1);
-
+
if (existingPQ.length > 0) {
+ const projectDisplayName = projectInfo ? `${projectInfo.code} - ${projectInfo.name}` : "알 수 없는 프로젝트";
return {
success: false,
- error: "해당 프로젝트에 대한 PQ가 이미 존재합니다"
+ error: `${projectDisplayName} 프로젝트에 대한 PQ가 이미 존재합니다`
};
}
}
@@ -3315,24 +3329,38 @@ export async function copyPQListAction(input: CopyPqListInput) {
};
}
- // 2. 대상 프로젝트에 이미 PQ가 존재하는지 확인
- const existingProjectPQ = await tx
- .select()
- .from(pqLists)
- .where(
- and(
- eq(pqLists.projectId, validated.targetProjectId),
- eq(pqLists.type, "PROJECT"),
- eq(pqLists.isDeleted, false)
- )
- )
- .limit(1);
+ // 2. 프로젝트 PQ인 경우에만 대상 프로젝트에 이미 PQ가 존재하는지 확인
+ if (sourcePqList.type === "PROJECT" && validated.targetProjectId) {
+ // 프로젝트 정보 조회 (이름과 코드 포함)
+ const projectInfo = await tx
+ .select({
+ code: projects.code,
+ name: projects.name
+ })
+ .from(projects)
+ .where(eq(projects.id, validated.targetProjectId))
+ .limit(1)
+ .then(rows => rows[0]);
- if (existingProjectPQ.length > 0) {
- return {
- success: false,
- error: "해당 프로젝트에 대한 PQ가 이미 존재합니다"
- };
+ const existingProjectPQ = await tx
+ .select()
+ .from(pqLists)
+ .where(
+ and(
+ eq(pqLists.projectId, validated.targetProjectId),
+ eq(pqLists.type, "PROJECT"),
+ eq(pqLists.isDeleted, false)
+ )
+ )
+ .limit(1);
+
+ if (existingProjectPQ.length > 0) {
+ const projectDisplayName = projectInfo ? `${projectInfo.code} - ${projectInfo.name}` : "알 수 없는 프로젝트";
+ return {
+ success: false,
+ error: `${projectDisplayName} 프로젝트에 대한 PQ가 이미 존재합니다`
+ };
+ }
}
// 3. 새 PQ 목록 생성
@@ -3344,7 +3372,7 @@ export async function copyPQListAction(input: CopyPqListInput) {
.values({
name: newName || sourcePqList.name,
type: sourcePqList.type,
- projectId: validated.targetProjectId,
+ projectId: sourcePqList.type === "PROJECT" ? validated.targetProjectId : null,
isDeleted: false,
createdAt: now,
updatedAt: now,
@@ -3957,6 +3985,67 @@ export async function autoDeactivateExpiredPQLists() {
}
}
+// PQ 유효일 수정 서버액션
+export async function updatePqValidToAction(input: UpdatePqValidToInput) {
+ try {
+ const validated = updatePqValidToSchema.parse(input);
+ const session = await getServerSession(authOptions);
+ const userId = session?.user?.id;
+
+ if (!userId) {
+ return {
+ success: false,
+ error: "인증이 필요합니다"
+ };
+ }
+
+ // PQ 목록 존재 확인
+ const existingPqList = await db
+ .select()
+ .from(pqLists)
+ .where(eq(pqLists.id, validated.pqListId))
+ .limit(1)
+ .then(rows => rows[0]);
+
+ if (!existingPqList) {
+ return {
+ success: false,
+ error: "PQ 목록을 찾을 수 없습니다"
+ };
+ }
+
+ // 유효일 업데이트
+ await db
+ .update(pqLists)
+ .set({
+ validTo: validated.validTo,
+ updatedAt: new Date(),
+ updatedBy: userId,
+ })
+ .where(eq(pqLists.id, validated.pqListId));
+
+ // 캐시 재검증
+ revalidateTag("pq-lists");
+
+ return {
+ success: true,
+ message: "유효일이 성공적으로 수정되었습니다"
+ };
+ } catch (error) {
+ console.error("Error updating PQ valid to:", error);
+ if (error instanceof z.ZodError) {
+ return {
+ success: false,
+ error: "입력 데이터가 올바르지 않습니다"
+ };
+ }
+ return {
+ success: false,
+ error: "유효일 수정에 실패했습니다"
+ };
+ }
+}
+
// SHI 참석자 총 인원수 계산 함수
export async function getTotalShiAttendees(shiAttendees: Record<string, unknown> | null): Promise<number> {