diff options
| author | 0-Zz-ang <s1998319@gmail.com> | 2025-10-28 14:57:14 +0900 |
|---|---|---|
| committer | 0-Zz-ang <s1998319@gmail.com> | 2025-10-28 14:57:14 +0900 |
| commit | 5b0994f2af11c77b61ac59df6211ccb20fae4d44 (patch) | |
| tree | 3cdd7d9039b058d9cde776536e4c38275a720178 /lib/basic-contract | |
| parent | 06cf51e5dd14e118fa8dbb8c666d78ace61cbf9b (diff) | |
(박서영)준법설문 관련요구사항 반영
Diffstat (limited to 'lib/basic-contract')
| -rw-r--r-- | lib/basic-contract/service.ts | 89 | ||||
| -rw-r--r-- | lib/basic-contract/status-detail/basic-contracts-detail-columns.tsx | 22 | ||||
| -rw-r--r-- | lib/basic-contract/viewer/SurveyComponent.tsx | 20 | ||||
| -rw-r--r-- | lib/basic-contract/viewer/basic-contract-sign-viewer.tsx | 44 |
4 files changed, 150 insertions, 25 deletions
diff --git a/lib/basic-contract/service.ts b/lib/basic-contract/service.ts index 8999a109..123d2367 100644 --- a/lib/basic-contract/service.ts +++ b/lib/basic-contract/service.ts @@ -543,16 +543,62 @@ export async function requestBasicContractInfo({ if (!vendor.email) return; // 이메일이 없으면 스킵
try {
+ const isComplianceTemplate = template.templateName?.includes('준법');
+ let selectedTemplateId = template.id;
+ let selectedTemplate = template;
+
+ if (isComplianceTemplate) {
+ const vendorUser = await db.query.users.findFirst({
+ where: and(
+ eq(users.email, vendor.email),
+ eq(users.domain, 'partners')
+ )
+ });
+
+ const userLanguage = vendorUser?.language || 'en'; // 기본값은 영어
+
+ if (userLanguage === 'ko') {
+ // 한글 준법서약 템플릿 찾기
+ const koreanTemplate = await db.query.basicContractTemplates.findFirst({
+ where: and(
+ sql`${basicContractTemplates.templateName} LIKE '%준법%'`,
+ sql`${basicContractTemplates.templateName} NOT LIKE '%영문%'`,
+ eq(basicContractTemplates.status, 'ACTIVE')
+ )
+ });
+
+ if (koreanTemplate) {
+ selectedTemplateId = koreanTemplate.id;
+ selectedTemplate = koreanTemplate;
+ }
+ } else {
+ // 영문 준법서약 템플릿 찾기
+ const englishTemplate = await db.query.basicContractTemplates.findFirst({
+ where: and(
+ sql`${basicContractTemplates.templateName} LIKE '%준법%'`,
+ sql`${basicContractTemplates.templateName} LIKE '%영문%'`,
+ eq(basicContractTemplates.status, 'ACTIVE')
+ )
+ });
+
+ if (englishTemplate) {
+ selectedTemplateId = englishTemplate.id;
+ selectedTemplate = englishTemplate;
+ console.log(`✅ 영어 사용자 ${vendor.vendorName}에게 영문 준법서약 템플릿 전송`);
+ }
+ }
+ }
+
// 3-1. basic_contract 테이블에 레코드 추가
const [newContract] = await db
.insert(basicContract)
.values({
- templateId: template.id,
+ templateId: selectedTemplateId, // 언어별로 선택된 템플릿 ID 사용
vendorId: vendor.id,
requestedBy: requestedBy,
status: "PENDING",
- fileName: template.fileName, // 템플릿 파일 이름 사용
- filePath: template.filePath, // 템플릿 파일 경로 사용
+ fileName: selectedTemplate.fileName, // 선택된 템플릿 파일 이름 사용
+ filePath: selectedTemplate.filePath, // 선택된 템플릿 파일 경로 사용
})
.returning();
@@ -1322,22 +1368,45 @@ export interface SurveyQuestionOption { /**
* 활성화된 첫 번째 설문조사 템플릿과 관련 데이터를 모두 가져오기
*/
-export async function getActiveSurveyTemplate(): Promise<SurveyTemplateWithQuestions | null> {
+export async function getActiveSurveyTemplate(language: string = 'ko'): Promise<SurveyTemplateWithQuestions | null> {
try {
- // 1. 활성화된 첫 번째 템플릿 가져오기
- const template = await db
+ // 1. 활성화된 템플릿 가져오기 (언어에 따라 필터링)
+ const templates = await db
.select()
.from(complianceSurveyTemplates)
.where(eq(complianceSurveyTemplates.isActive, true))
- .orderBy(complianceSurveyTemplates.id)
- .limit(1);
+ .orderBy(complianceSurveyTemplates.id);
- if (!template || template.length === 0) {
+ if (!templates || templates.length === 0) {
console.log('활성화된 설문조사 템플릿이 없습니다.');
return null;
}
- const templateData = template[0];
+ // 언어에 따라 적절한 템플릿 선택
+ let templateData;
+ if (language === 'en') {
+ // 영문 템플릿 찾기 (이름에 '영문' 또는 'English' 포함)
+ templateData = templates.find(t =>
+ t.name.includes('영문') ||
+ t.name.toLowerCase().includes('english') ||
+ t.name.toLowerCase().includes('en')
+ );
+ } else {
+ // 한글 템플릿 찾기 (영문이 아닌 것)
+ templateData = templates.find(t =>
+ !t.name.includes('영문') &&
+ !t.name.toLowerCase().includes('english') &&
+ !t.name.toLowerCase().includes('en')
+ );
+ }
+
+ // 적절한 템플릿을 찾지 못하면 첫 번째 템플릿 사용
+ if (!templateData) {
+ console.log(`언어 '${language}'에 맞는 템플릿을 찾지 못해 기본 템플릿을 사용합니다.`);
+ templateData = templates[0];
+ }
+
+ console.log(`✅ 선택된 설문조사 템플릿: ${templateData.name} (언어: ${language})`);
// 2. 해당 템플릿의 모든 질문 가져오기 (displayOrder 순)
const questions = await db
diff --git a/lib/basic-contract/status-detail/basic-contracts-detail-columns.tsx b/lib/basic-contract/status-detail/basic-contracts-detail-columns.tsx index 9a140b27..c6f82fc8 100644 --- a/lib/basic-contract/status-detail/basic-contracts-detail-columns.tsx +++ b/lib/basic-contract/status-detail/basic-contracts-detail-columns.tsx @@ -30,6 +30,7 @@ import { BasicContractView } from "@/db/schema" import { downloadFile, quickPreview } from "@/lib/file-download" import { toast } from "sonner" import { useRouter } from "next/navigation" +import { getComplianceResponseByBasicContractId } from "@/lib/compliance/services" interface GetColumnsProps { setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<BasicContractView> | null>> @@ -151,7 +152,26 @@ export function getDetailColumns({ <Mail className="mr-2 h-4 w-4" /> 재발송 </DropdownMenuItem> - <DropdownMenuItem onClick={() => setRowAction({ type: "view", row })}> + <DropdownMenuItem onClick={async () => { + // 준법서약 템플릿인 경우 compliance 응답 페이지로 이동 + if (contract.templateName?.includes('준법')) { + try { + const response = await getComplianceResponseByBasicContractId(contract.id); + + if (response) { + router.push(`/evcp/compliance/${response.templateId}/responses/${response.id}`); + } else { + toast.error("준법서약 응답을 찾을 수 없습니다."); + setRowAction({ type: "view", row }); + } + } catch (error) { + console.error("Error fetching compliance response:", error); + toast.error("응답 정보를 가져오는데 실패했습니다."); + } + } else { + setRowAction({ type: "view", row }); + } + }}> <FileText className="mr-2 h-4 w-4" /> 상세 정보 </DropdownMenuItem> diff --git a/lib/basic-contract/viewer/SurveyComponent.tsx b/lib/basic-contract/viewer/SurveyComponent.tsx index 299fe6fa..8662155e 100644 --- a/lib/basic-contract/viewer/SurveyComponent.tsx +++ b/lib/basic-contract/viewer/SurveyComponent.tsx @@ -34,6 +34,7 @@ interface SurveyComponentProps { contractId?: number; surveyTemplate: SurveyTemplateWithQuestions | null; surveyLoading: boolean; + surveyLoadAttempted?: boolean; // 로드 시도 여부 추가 conditionalHandler: ConditionalSurveyHandler | null; onSurveyComplete?: () => void; onSurveyDataUpdate: (data: any) => void; @@ -45,6 +46,7 @@ export const SurveyComponent: React.FC<SurveyComponentProps> = ({ contractId, surveyTemplate, surveyLoading, + surveyLoadAttempted = false, conditionalHandler, onSurveyComplete, onSurveyDataUpdate, @@ -487,7 +489,21 @@ export const SurveyComponent: React.FC<SurveyComponentProps> = ({ ); } + if (!surveyTemplate) { + if (!surveyLoadAttempted) { + return ( + <div className="h-full w-full"> + <Card className="h-full"> + <CardContent className="flex flex-col items-center justify-center h-full py-12"> + <Loader2 className="h-8 w-8 text-blue-500 animate-spin mb-4" /> + <p className="text-sm text-muted-foreground">설문조사를 준비하는 중...</p> + </CardContent> + </Card> + </div> + ); + } + return ( <div className="h-full w-full"> <Card className="h-full"> @@ -836,8 +852,8 @@ export const SurveyComponent: React.FC<SurveyComponentProps> = ({ {uploadedFiles[question.id].map((file, index) => ( <div key={index} className="flex items-center space-x-2 text-sm"> <FileText className="h-4 w-4 text-blue-500" /> - <span>{file.fileName}</span> - <span className="text-gray-500">({(file.fileSize / 1024).toFixed(1)} KB)</span> + <span>{file.name || file.fileName}</span> + <span className="text-gray-500">({((file.size || file.fileSize || 0) / 1024).toFixed(1)} KB)</span> </div> ))} </div> diff --git a/lib/basic-contract/viewer/basic-contract-sign-viewer.tsx b/lib/basic-contract/viewer/basic-contract-sign-viewer.tsx index 77bfaf41..b6024b29 100644 --- a/lib/basic-contract/viewer/basic-contract-sign-viewer.tsx +++ b/lib/basic-contract/viewer/basic-contract-sign-viewer.tsx @@ -641,6 +641,7 @@ export function BasicContractSignViewer({ const [isInitialLoaded, setIsInitialLoaded] = useState<boolean>(false); const [surveyTemplate, setSurveyTemplate] = useState<SurveyTemplateWithQuestions | null>(null); const [surveyLoading, setSurveyLoading] = useState<boolean>(false); + const [surveyLoadAttempted, setSurveyLoadAttempted] = useState<boolean>(false); const [gtcCommentStatus, setGtcCommentStatus] = useState<{ hasComments: boolean; commentCount: number; @@ -756,16 +757,16 @@ export function BasicContractSignViewer({ useEffect(() => { setShowDialog(isOpen); - // 구매자 모드가 아닐 때만 설문조사 템플릿 로드 - if (isOpen && isComplianceTemplate && !surveyTemplate && mode !== 'buyer') { - loadSurveyTemplate(); - } - if (isOpen) { setIsInitialLoaded(false); currentDocumentPath.current = ""; } - }, [isOpen, isComplianceTemplate, mode]); + + // 구매자 모드가 아닐 때만 설문조사 템플릿 로드 + if (isOpen && isComplianceTemplate && !surveyTemplate && !surveyLoading && mode !== 'buyer') { + loadSurveyTemplate(); + } + }, [isOpen, isComplianceTemplate, surveyTemplate, surveyLoading, mode]); useEffect(() => { if (!filePath) return; @@ -790,13 +791,28 @@ export function BasicContractSignViewer({ }, [filePath, instance]); const loadSurveyTemplate = async () => { + // 이미 로딩 중이거나 템플릿이 있으면 중복 호출 방지 + if (surveyLoading || surveyTemplate) { + return; + } + setSurveyLoading(true); + setSurveyLoadAttempted(true); // 로드 시도 표시 try { - const template = await getActiveSurveyTemplate(); + + // 계약서 템플릿 이름에서 언어 판단 + let language = 'ko'; // 기본값 한글 + if (templateName && (templateName.includes('영문') || templateName.toLowerCase().includes('english'))) { + language = 'en'; + } + + + const template = await getActiveSurveyTemplate(language); + setSurveyTemplate(template); } catch (error) { - console.error('📛 설문조사 템플릿 로드 실패:', error); + setSurveyTemplate(null); } finally { setSurveyLoading(false); @@ -1046,15 +1062,17 @@ export function BasicContractSignViewer({ const handleTabChange = async (newTab: string) => { setActiveTab(newTab); - if (newTab === "survey" || newTab === "clauses") return; - - const currentInstance = webViewerInstance.current || instance; - if (!currentInstance || fileLoading) return; + // survey 탭으로 변경 시 템플릿 로드 확인 if (newTab === 'survey' && !surveyTemplate && !surveyLoading && mode !== 'buyer') { loadSurveyTemplate(); } + if (newTab === "survey" || newTab === "clauses") return; + + const currentInstance = webViewerInstance.current || instance; + if (!currentInstance || fileLoading) return; + let targetFile: FileInfo | undefined; if (newTab === "main") { targetFile = allFiles.find(f => f.type === "main"); @@ -1272,6 +1290,7 @@ export function BasicContractSignViewer({ contractId={contractId} surveyTemplate={surveyTemplate} surveyLoading={surveyLoading} + surveyLoadAttempted={surveyLoadAttempted} conditionalHandler={conditionalHandler} onSurveyComplete={onSurveyComplete} onSurveyDataUpdate={setSurveyData} @@ -1456,6 +1475,7 @@ export function BasicContractSignViewer({ contractId={contractId} surveyTemplate={surveyTemplate} surveyLoading={surveyLoading} + surveyLoadAttempted={surveyLoadAttempted} conditionalHandler={conditionalHandler} onSurveyComplete={onSurveyComplete} onSurveyDataUpdate={setSurveyData} |
