diff options
Diffstat (limited to 'lib/compliance/services.ts')
| -rw-r--r-- | lib/compliance/services.ts | 94 |
1 files changed, 93 insertions, 1 deletions
diff --git a/lib/compliance/services.ts b/lib/compliance/services.ts index 2d3ec092..a603a091 100644 --- a/lib/compliance/services.ts +++ b/lib/compliance/services.ts @@ -10,6 +10,7 @@ import { complianceResponses, complianceResponseAnswers, complianceResponseFiles, + redFlagManagers, } from "@/db/schema/compliance"; import { users } from "@/db/schema"; import { basicContract, basicContractTemplates } from "@/db/schema/basicContractDocumnet"; @@ -861,7 +862,6 @@ export async function deleteComplianceSurveyTemplate(templateId: number) { .where(eq(complianceSurveyTemplates.id, templateId)) .returning(); - console.log(`✅ 템플릿 ${templateId} 삭제 완료:`, template); // 캐시 무효화 revalidatePath("/evcp/compliance"); @@ -961,3 +961,95 @@ export async function getComplianceResponseByBasicContractId(basicContractId: nu return null; } } + +// ==================== 레드플래그 담당자 관리 ==================== + +// 레드플래그 담당자 조회 +export async function getRedFlagManagers() { + try { + const managers = await db.query.redFlagManagers.findFirst({ + with: { + purchasingManager: true, + complianceManager: true, + }, + orderBy: [desc(redFlagManagers.createdAt)], + }); + + return managers || null; + } catch (error) { + console.error("Error fetching red flag managers:", error); + return null; + } +} + +// 레드플래그 담당자 생성 +export async function createRedFlagManagers(data: { + purchasingManagerId: number | null; + complianceManagerId: number | null; +}) { + try { + const [newManager] = await db + .insert(redFlagManagers) + .values({ + purchasingManagerId: data.purchasingManagerId, + complianceManagerId: data.complianceManagerId, + }) + .returning(); + + revalidatePath("/[lng]/evcp/compliance"); + return newManager; + } catch (error) { + + throw error; + } +} + +// 레드플래그 담당자 수정 +export async function updateRedFlagManagers( + id: number, + data: { + purchasingManagerId?: number | null; + complianceManagerId?: number | null; + } +) { + try { + const [updated] = await db + .update(redFlagManagers) + .set({ + ...data, + updatedAt: new Date(), + }) + .where(eq(redFlagManagers.id, id)) + .returning(); + + revalidatePath("/[lng]/evcp/compliance"); + return updated; + } catch (error) { + + throw error; + } +} + +// 레드플래그 담당자 조회 또는 생성 +export async function getOrCreateRedFlagManagers() { + try { + const existing = await getRedFlagManagers(); + + if (existing) { + return existing; + } + + // 존재하지 않으면 빈 레코드 생성 + await createRedFlagManagers({ + purchasingManagerId: null, + complianceManagerId: null, + }); + + // 다시 relations를 포함해서 조회 + const newManager = await getRedFlagManagers(); + return newManager; + } catch (error) { + + throw error; + } +} |
