summaryrefslogtreecommitdiff
path: root/lib/vendor-regular-registrations/service.ts
blob: b587ec237a0c74314151541b92df650bcfa0a2cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
"use server"
import { revalidateTag, unstable_cache } from "next/cache";
import {
  getVendorRegularRegistrations,
  createVendorRegularRegistration,
  updateVendorRegularRegistration,
  getVendorRegularRegistrationById,
} from "./repository";

import { getServerSession } from "next-auth";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import { headers } from "next/headers";
import { sendEmail } from "@/lib/mail/sendEmail";
import { 
  vendors, 
  vendorRegularRegistrations,
  vendorAttachments, 
  vendorInvestigations, 
  vendorInvestigationAttachments,
  basicContract,
  vendorPQSubmissions,
  vendorBusinessContacts,
  vendorAdditionalInfo
} from "@/db/schema";
import db from "@/db/db";
import { inArray, eq, desc } from "drizzle-orm";

// 캐싱과 에러 핸들링이 포함된 조회 함수
export async function fetchVendorRegularRegistrations(input?: {
  search?: string;
  status?: string[];
  page?: number;
  perPage?: number;
}) {
  return unstable_cache(
    async () => {
      try {
        const registrations = await getVendorRegularRegistrations();
        
        let filteredData = registrations;

        // 검색 필터링
        if (input?.search) {
          const searchLower = input.search.toLowerCase();
          filteredData = filteredData.filter(
            (reg) =>
              reg.companyName.toLowerCase().includes(searchLower) ||
              reg.businessNumber.toLowerCase().includes(searchLower) ||
              reg.potentialCode?.toLowerCase().includes(searchLower) ||
              reg.representative?.toLowerCase().includes(searchLower)
          );
        }

        // 상태 필터링
        if (input?.status && input.status.length > 0) {
          filteredData = filteredData.filter((reg) =>
            input.status!.includes(reg.status)
          );
        }

        // 페이지네이션
        const page = input?.page || 1;
        const perPage = input?.perPage || 50;
        const offset = (page - 1) * perPage;
        const paginatedData = filteredData.slice(offset, offset + perPage);
        const pageCount = Math.ceil(filteredData.length / perPage);

        return {
          success: true,
          data: paginatedData,
          pageCount,
          total: filteredData.length,
        };
      } catch (error) {
        console.error("Error in fetchVendorRegularRegistrations:", error);
        return {
          success: false,
          error: error instanceof Error ? error.message : "정규업체 등록 목록을 가져오는 중 오류가 발생했습니다.",
        };
      }
    },
    [JSON.stringify(input || {})],
    {
      revalidate: 300, // 5분 캐시
      tags: ["vendor-regular-registrations"],
    }
  )();
}

export async function getCurrentUserInfo() {
  const session = await getServerSession(authOptions);
  return {
    userId: session?.user?.id ? String(session.user.id) : null,
    userName: session?.user?.name || null,
  };
}

export async function createVendorRegistration(data: {
  vendorId: number;
  status?: string;
  potentialCode?: string;
  majorItems?: Record<string, unknown>[];
  assignedDepartment?: string;
  assignedDepartmentCode?: string;
  assignedUser?: string;
  assignedUserCode?: string;
  remarks?: string;
}) {
  try {
    const majorItemsJson = data.majorItems ? JSON.stringify(data.majorItems) : undefined;
    
    const registration = await createVendorRegularRegistration({
      ...data,
      majorItems: majorItemsJson,
    });

    // 캐시 무효화
    revalidateTag("vendor-regular-registrations");
    
    return { success: true, data: registration };
  } catch (error) {
    console.error("Error in createVendorRegistration:", error);
    return {
      success: false,
      error: error instanceof Error ? error.message : "정규업체 등록을 생성하는 중 오류가 발생했습니다.",
    };
  }
}

export async function updateVendorRegistration(
  id: number,
  data: Partial<{
    status: string;
    potentialCode: string;
    majorItems: Record<string, unknown>[];
    registrationRequestDate: string;
    assignedDepartment: string;
    assignedDepartmentCode: string;
    assignedUser: string;
    assignedUserCode: string;
    remarks: string;
  }>
) {
  try {
    const updateData: Partial<{
      status: string;
      potentialCode: string;
      majorItems: string;
      registrationRequestDate: string;
      assignedDepartment: string;
      assignedDepartmentCode: string;
      assignedUser: string;
      assignedUserCode: string;
      remarks: string;
    }> = {};
    
    // majorItems를 제외한 다른 필드들을 복사
    Object.keys(data).forEach(key => {
      if (key !== 'majorItems') {
        updateData[key as keyof typeof updateData] = data[key as keyof typeof data] as never;
      }
    });
    
    if (data.majorItems) {
      updateData.majorItems = JSON.stringify(data.majorItems);
    }
    
    const registration = await updateVendorRegularRegistration(id, updateData);

    // 캐시 무효화
    revalidateTag("vendor-regular-registrations");
    
    return { success: true, data: registration };
  } catch (error) {
    console.error("Error in updateVendorRegistration:", error);
    return {
      success: false,
      error: error instanceof Error ? error.message : "정규업체 등록을 수정하는 중 오류가 발생했습니다.",
    };
  }
}

export async function fetchVendorRegistrationById(id: number) {
  try {
    const registration = await getVendorRegularRegistrationById(id);
    return { success: true, data: registration };
  } catch (error) {
    console.error("Error in fetchVendorRegistrationById:", error);
    return {
      success: false,
      error: error instanceof Error ? error.message : "정규업체 등록 정보를 가져오는 중 오류가 발생했습니다.",
    };
  }
}



export async function requestRegularRegistration(registrationId: number) {
  try {
    // 정규업체 등록 요청 처리
    const now = new Date().toISOString().split('T')[0];
    
    const registration = await updateVendorRegularRegistration(registrationId, {
      status: "in_review",
      registrationRequestDate: now,
    });

    // 캐시 무효화
    revalidateTag("vendor-regular-registrations");
    
    return { success: true, message: "정규업체 등록 요청이 완료되었습니다.", data: registration };
  } catch (error) {
    console.error("Error in requestRegularRegistration:", error);
    return {
      success: false,
      error: error instanceof Error ? error.message : "정규업체 등록 요청 중 오류가 발생했습니다.",
    };
  }
}

export async function approveRegularRegistration(registrationId: number) {
  try {
    // 정규업체 등록 승인 처리
    const registration = await updateVendorRegularRegistration(registrationId, {
      status: "approval_ready",
    });

    // 캐시 무효화
    revalidateTag("vendor-regular-registrations");
    
    return { success: true, message: "정규업체 등록이 승인되었습니다.", data: registration };
  } catch (error) {
    console.error("Error in approveRegularRegistration:", error);
    return {
      success: false,
      error: error instanceof Error ? error.message : "정규업체 등록 승인 중 오류가 발생했습니다.",
    };
  }
}



// 누락계약요청 이메일 발송
export async function sendMissingContractRequestEmails(vendorIds: number[]) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user) {
      return { success: false, error: "로그인이 필요합니다." };
    }

    // 벤더 정보 조회
    const vendorList = await db
      .select({
        id: vendors.id,
        vendorName: vendors.vendorName,
        email: vendors.email,
      })
      .from(vendors)
      .where(inArray(vendors.id, vendorIds));

    if (vendorList.length === 0) {
      return { success: false, error: "선택된 업체를 찾을 수 없습니다." };
    }

    const headersList = await headers();
    const host = headersList.get('host') || 'localhost:3000';
    const protocol = process.env.NODE_ENV === 'production' ? 'https' : 'http';
    const baseUrl = `${protocol}://${host}`;
    const contractManagementUrl = `${baseUrl}/ko/login`; // 실제 기본계약 관리 페이지 URL로 수정 필요

    let successCount = 0;
    let errorCount = 0;

    // 각 벤더에게 이메일 발송
    await Promise.all(
      vendorList.map(async (vendor) => {
        if (!vendor.email) {
          errorCount++;
          return;
        }

        try {

          await sendEmail({
            to: vendor.email,
            subject: "[SHI] 정규업체 등록을 위한 기본계약/서약 진행 요청",
            template: "vendor-missing-contract-request",
            context: {
              vendorName: vendor.vendorName,
              contractManagementUrl,
              senderName: session.user.name || "구매담당자",
              senderEmail: session.user.email || "",
              currentYear: new Date().getFullYear(),
            },
          });
          successCount++;
        } catch (error) {
          console.error(`Failed to send email to ${vendor.vendorName}:`, error);
          errorCount++;
        }
      })
    );

    if (errorCount > 0) {
      return {
        success: false,
        error: `${successCount}개 업체에 발송 성공, ${errorCount}개 업체 발송 실패`,
      };
    }

    return {
      success: true,
      message: `${successCount}개 업체에 누락계약요청 이메일을 발송했습니다.`,
    };
  } catch (error) {
    console.error("Error sending missing contract request emails:", error);
    return {
      success: false,
      error: error instanceof Error ? error.message : "이메일 발송 중 오류가 발생했습니다.",
    };
  }
}

// 추가정보요청 이메일 발송
export async function sendAdditionalInfoRequestEmails(vendorIds: number[]) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user) {
      return { success: false, error: "로그인이 필요합니다." };
    }

    // 벤더 정보 조회
    const vendorList = await db
      .select({
        id: vendors.id,
        vendorName: vendors.vendorName,
        email: vendors.email,
      })
      .from(vendors)
      .where(inArray(vendors.id, vendorIds));

    if (vendorList.length === 0) {
      return { success: false, error: "선택된 업체를 찾을 수 없습니다." };
    }

    const headersList = await headers();
    const host = headersList.get('host') || 'localhost:3000';
    const protocol = process.env.NODE_ENV === 'production' ? 'https' : 'http';
    const baseUrl = `${protocol}://${host}`;
    const vendorInfoUrl = `${baseUrl}/ko/login`; // 실제 업체정보 관리 페이지 URL로 수정 필요

    let successCount = 0;
    let errorCount = 0;

    // 각 벤더에게 이메일 발송
    await Promise.all(
      vendorList.map(async (vendor) => {
        if (!vendor.email) {
          errorCount++;
          return;
        }

        try {
          await sendEmail({
            to: vendor.email,
            subject: "[SHI] 정규업체 등록을 위한 추가정보 입력 요청",
            template: "vendor-regular-registration-request",
            context: {
              vendorName: vendor.vendorName,
              vendorInfoUrl,
              senderName: session.user.name || "구매담당자",
              senderEmail: session.user.email || "",
              currentYear: new Date().getFullYear(),
            },
          });
          successCount++;
        } catch (error) {
          console.error(`Failed to send email to ${vendor.vendorName}:`, error);
          errorCount++;
        }
      })
    );

    if (errorCount > 0) {
      return {
        success: false,
        error: `${successCount}개 업체에 발송 성공, ${errorCount}개 업체 발송 실패`,
      };
    }

    return {
      success: true,
      message: `${successCount}개 업체에 추가정보요청 이메일을 발송했습니다.`,
    };
  } catch (error) {
    console.error("Error sending additional info request emails:", error);
    return {
      success: false,
      error: error instanceof Error ? error.message : "이메일 발송 중 오류가 발생했습니다.",
    };
  }
}

// 법무검토 Skip 기능
export async function skipLegalReview(vendorIds: number[], skipReason: string) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user) {
      return { success: false, error: "로그인이 필요합니다." };
    }

    let successCount = 0;
    let errorCount = 0;

    for (const vendorId of vendorIds) {
      try {
        // 해당 벤더의 registration 찾기 또는 생성
        const vendorList = await db
          .select({ id: vendors.id })
          .from(vendors)
          .where(eq(vendors.id, vendorId));

        if (vendorList.length === 0) {
          errorCount++;
          continue;
        }

        // registration 조회
        const existingRegistrations = await db
          .select()
          .from(vendorRegularRegistrations)
          .where(eq(vendorRegularRegistrations.vendorId, vendorId));

        let registrationId;
        if (existingRegistrations.length === 0) {
          // 새로 생성
          const newRegistration = await createVendorRegularRegistration({
            vendorId: vendorId,
            status: "cp_finished", // CP완료로 변경
            remarks: `법무검토 Skip: ${skipReason}`,
          });
          registrationId = newRegistration.id;
        } else {
          // 기존 registration 업데이트
          registrationId = existingRegistrations[0].id;
          const currentRemarks = existingRegistrations[0].remarks || "";
          const newRemarks = currentRemarks 
            ? `${currentRemarks}\n법무검토 Skip: ${skipReason}`
            : `법무검토 Skip: ${skipReason}`;

          await updateVendorRegularRegistration(registrationId, {
            status: "cp_finished", // CP완료로 변경
            remarks: newRemarks,
          });
        }

        successCount++;
      } catch (error) {
        console.error(`Failed to skip legal review for vendor ${vendorId}:`, error);
        errorCount++;
      }
    }

    if (errorCount > 0) {
      return {
        success: false,
        error: `${successCount}개 업체 처리 성공, ${errorCount}개 업체 처리 실패`,
      };
    }

    return {
      success: true,
      message: `${successCount}개 업체의 법무검토를 Skip 처리했습니다.`,
    };
  } catch (error) {
    console.error("Error skipping legal review:", error);
    return {
      success: false,
      error: error instanceof Error ? error.message : "법무검토 Skip 처리 중 오류가 발생했습니다.",
    };
  }
}

// 안전적격성평가 Skip 기능
export async function skipSafetyQualification(vendorIds: number[], skipReason: string) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user) {
      return { success: false, error: "로그인이 필요합니다." };
    }

    let successCount = 0;
    let errorCount = 0;

    for (const vendorId of vendorIds) {
      try {
        // 해당 벤더의 registration 찾기 또는 생성
        const vendorList = await db
          .select({ id: vendors.id })
          .from(vendors)
          .where(eq(vendors.id, vendorId));

        if (vendorList.length === 0) {
          errorCount++;
          continue;
        }

        // registration 조회
        const existingRegistrations = await db
          .select()
          .from(vendorRegularRegistrations)
          .where(eq(vendorRegularRegistrations.vendorId, vendorId));

        let registrationId;
        if (existingRegistrations.length === 0) {
          // 새로 생성
          const newRegistration = await createVendorRegularRegistration({
            vendorId: vendorId,
            status: "audit_pass",
            remarks: `안전적격성평가 Skip: ${skipReason}`,
          });
          registrationId = newRegistration.id;
        } else {
          // 기존 registration 업데이트
          registrationId = existingRegistrations[0].id;
          const currentRemarks = existingRegistrations[0].remarks || "";
          const newRemarks = currentRemarks 
            ? `${currentRemarks}\n안전적격성평가 Skip: ${skipReason}`
            : `안전적격성평가 Skip: ${skipReason}`;

          await updateVendorRegularRegistration(registrationId, {
            remarks: newRemarks,
          });
        }

        // 안전적격성평가 상태를 완료로 처리 (계약 동의 현황은 이제 실시간으로 조회하므로 별도 처리 불필요)
        // updateContractAgreement 함수는 제거되었으므로 계약 동의 현황은 basic_contract와 vendor_pq_submissions에서 실시간으로 조회됩니다.

        successCount++;
      } catch (error) {
        console.error(`Failed to skip safety qualification for vendor ${vendorId}:`, error);
        errorCount++;
      }
    }

    if (errorCount > 0) {
      return {
        success: false,
        error: `${successCount}개 업체 처리 성공, ${errorCount}개 업체 처리 실패`,
      };
    }

    return {
      success: true,
      message: `${successCount}개 업체의 안전적격성평가를 Skip 처리했습니다.`,
    };
  } catch (error) {
    console.error("Error skipping safety qualification:", error);
    return {
      success: false,
      error: error instanceof Error ? error.message : "안전적격성평가 Skip 처리 중 오류가 발생했습니다.",
    };
  }
}

// 벤더용 현황 조회 함수들
export async function fetchVendorRegistrationStatus(vendorId: number) {
  return unstable_cache(
    async () => {
      try {
        // 벤더 기본 정보
        const vendor = await db
          .select()
          .from(vendors)
          .where(eq(vendors.id, vendorId))
          .limit(1)

        if (!vendor[0]) {
          return {
            success: false,
            error: "벤더 정보를 찾을 수 없습니다.",
          }
        }

        // 정규업체 등록 정보
        const registration = await db
          .select()
          .from(vendorRegularRegistrations)
          .where(eq(vendorRegularRegistrations.vendorId, vendorId))
          .limit(1)

        // 벤더 첨부파일 조회
        const vendorFiles = await db
          .select()
          .from(vendorAttachments)
          .where(eq(vendorAttachments.vendorId, vendorId))

        // 실사 결과 조회 (vendor_investigation_attachments)
        const investigationFiles = await db
          .select({
            attachmentId: vendorInvestigationAttachments.id,
            createdAt: vendorInvestigationAttachments.createdAt,
          })
          .from(vendorInvestigationAttachments)
          .innerJoin(vendorInvestigations, eq(vendorInvestigationAttachments.investigationId, vendorInvestigations.id))
          .where(eq(vendorInvestigations.vendorId, vendorId))

        // PQ 제출 정보
        const pqSubmission = await db
          .select()
          .from(vendorPQSubmissions)
          .where(eq(vendorPQSubmissions.vendorId, vendorId))
          .orderBy(desc(vendorPQSubmissions.createdAt))
          .limit(1)

        // 기본계약 정보 
        const contractInfo = await db
          .select()
          .from(basicContract)
          .where(eq(basicContract.vendorId, vendorId))
          .limit(1)

        // 업무담당자 정보
        const businessContacts = await db
          .select()
          .from(vendorBusinessContacts)
          .where(eq(vendorBusinessContacts.vendorId, vendorId))

        // 추가정보
        const additionalInfo = await db
          .select()
          .from(vendorAdditionalInfo)
          .where(eq(vendorAdditionalInfo.vendorId, vendorId))
          .limit(1)

        // 문서 제출 현황 계산
        const documentStatus = {
          businessRegistration: vendorFiles.some(f => f.attachmentType === "BUSINESS_REGISTRATION"),
          creditEvaluation: vendorFiles.some(f => f.attachmentType === "CREDIT_EVALUATION"),
          bankCopy: vendorFiles.some(f => f.attachmentType === "BANK_COPY"),
          auditResult: investigationFiles.length > 0, // DocumentStatusDialog에서 사용하는 키
          cpDocument: !!contractInfo[0]?.status && contractInfo[0].status === "completed",
          gtc: !!contractInfo[0]?.status && contractInfo[0].status === "completed",
          standardSubcontract: !!contractInfo[0]?.status && contractInfo[0].status === "completed",
          safetyHealth: !!contractInfo[0]?.status && contractInfo[0].status === "completed",
          ethics: !!contractInfo[0]?.status && contractInfo[0].status === "completed",
          domesticCredit: !!contractInfo[0]?.status && contractInfo[0].status === "completed",
          safetyQualification: investigationFiles.length > 0,
        }

        // 미완성 항목 계산
        const missingDocuments = Object.entries(documentStatus)
          .filter(([, value]) => !value)
          .map(([key]) => key)

        const requiredContactTypes = ["sales", "design", "delivery", "quality", "tax_invoice"]
        const existingContactTypes = businessContacts.map(contact => contact.contactType)
        const missingContactTypes = requiredContactTypes.filter(type => !existingContactTypes.includes(type))

        return {
          success: true,
          data: {
            vendor: vendor[0],
            registration: registration[0] || null,
            documentStatus,
            missingDocuments,
            businessContacts,
            missingContactTypes,
            additionalInfo: additionalInfo[0] || null,
            pqSubmission: pqSubmission[0] || null,
            auditPassed: investigationFiles.length > 0,
            contractInfo: contractInfo[0] || null,
            incompleteItemsCount: {
              documents: missingDocuments.length,
              contacts: missingContactTypes.length,
              additionalInfo: !additionalInfo[0] ? 1 : 0,
            }
          }
        }
      } catch (error) {
        console.error("Error in fetchVendorRegistrationStatus:", error)
        return {
          success: false,
          error: error instanceof Error ? error.message : "현황 조회 중 오류가 발생했습니다.",
        }
      }
    },
    [`vendor-registration-status-${vendorId}`],
    {
      revalidate: 300, // 5분 캐시
      tags: ["vendor-registration-status", `vendor-${vendorId}`],
    }
  )()
}

// 서명/직인 업로드 (임시 - 실제로는 파일 업로드 로직 필요)
export async function uploadVendorSignature(vendorId: number, signatureData: {
  type: "signature" | "seal"
  signerName?: string
  imageFile: string // base64 or file path
}) {
  try {
    // TODO: 실제 파일 업로드 및 저장 로직 구현
    console.log("Signature upload for vendor:", vendorId, signatureData)
    
    // 캐시 무효화
    revalidateTag(`vendor-registration-status`)
    revalidateTag(`vendor-${vendorId}`)
    
    return {
      success: true,
      message: "서명/직인이 등록되었습니다.",
    }
  } catch (error) {
    console.error("Error uploading signature:", error)
    return {
      success: false,
      error: error instanceof Error ? error.message : "서명/직인 등록 중 오류가 발생했습니다.",
    }
  }
}

// 업무담당자 정보 저장
export async function saveVendorBusinessContacts(
  vendorId: number,
  contacts: Array<{
    contactType: "sales" | "design" | "delivery" | "quality" | "tax_invoice"
    contactName: string
    position: string
    department: string
    responsibility: string
    email: string
  }>
) {
  try {
    // 기존 데이터 삭제
    await db
      .delete(vendorBusinessContacts)
      .where(eq(vendorBusinessContacts.vendorId, vendorId))

    // 새 데이터 삽입
    if (contacts.length > 0) {
      await db
        .insert(vendorBusinessContacts)
        .values(contacts.map(contact => ({
          ...contact,
          vendorId,
        })))
    }
    
    // 캐시 무효화
    revalidateTag("vendor-registration-status")
    revalidateTag(`vendor-${vendorId}`)
    
    return {
      success: true,
      message: "업무담당자 정보가 저장되었습니다.",
    }
  } catch (error) {
    console.error("Error saving business contacts:", error)
    return {
      success: false,
      error: error instanceof Error ? error.message : "업무담당자 정보 저장 중 오류가 발생했습니다.",
    }
  }
}

// 추가정보 저장
export async function saveVendorAdditionalInfo(
  vendorId: number,
  info: {
    businessType?: string
    industryType?: string
    companySize?: string
    revenue?: string
    factoryEstablishedDate?: string
    preferredContractTerms?: string
  }
) {
  try {
    const existing = await db
      .select()
      .from(vendorAdditionalInfo)
      .where(eq(vendorAdditionalInfo.vendorId, vendorId))
      .limit(1)
    
    if (existing[0]) {
      // 업데이트
      await db
        .update(vendorAdditionalInfo)
        .set({
          ...info,
          factoryEstablishedDate: info.factoryEstablishedDate || null,
          revenue: info.revenue || null,
          updatedAt: new Date(),
        })
        .where(eq(vendorAdditionalInfo.vendorId, vendorId))
    } else {
      // 신규 삽입
      await db
        .insert(vendorAdditionalInfo)
        .values({
          ...info,
          vendorId,
          factoryEstablishedDate: info.factoryEstablishedDate || null,
          revenue: info.revenue || null,
        })
    }
    
    // 캐시 무효화
    revalidateTag("vendor-registration-status")
    revalidateTag(`vendor-${vendorId}`)
    
    return {
      success: true,
      message: "추가정보가 저장되었습니다.",
    }
  } catch (error) {
    console.error("Error saving additional info:", error)
    return {
      success: false,
      error: error instanceof Error ? error.message : "추가정보 저장 중 오류가 발생했습니다.",
    }
  }
}