summaryrefslogtreecommitdiff
path: root/lib/swp/vendor-actions.ts
blob: 7d6dfa85487f21152aed91b03047e6e170f6ac72 (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
"use server";

import { getServerSession } from "next-auth";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import db from "@/db/db";
import { vendors } from "@/db/schema/vendors";
import { contracts } from "@/db/schema/contract";
import { projects } from "@/db/schema/projects";
import { swpDocumentFiles, swpDocumentRevisions } from "@/db/schema/SWP/swp-documents";
import { eq, and, sql } from "drizzle-orm";
import { fetchSwpDocuments, type SwpTableParams } from "./actions";

// ============================================================================
// 벤더 세션 정보 조회
// ============================================================================

interface VendorSessionInfo {
  vendorId: number;
  vendorCode: string;
  vendorName: string;
  companyId: number;
}

export async function getVendorSessionInfo(): Promise<VendorSessionInfo | null> {
  const session = await getServerSession(authOptions);
  
  if (!session?.user?.companyId) {
    return null;
  }

  const companyId = typeof session.user.companyId === 'string' 
    ? parseInt(session.user.companyId, 10)
    : session.user.companyId as number;

  // vendors 테이블에서 companyId로 벤더 정보 조회
  const vendor = await db
    .select({
      id: vendors.id,
      vendorCode: vendors.vendorCode,
      vendorName: vendors.vendorName,
    })
    .from(vendors)
    .where(eq(vendors.id, companyId))
    .limit(1);

  if (!vendor[0] || !vendor[0].vendorCode) {
    return null;
  }

  return {
    vendorId: vendor[0].id,
    vendorCode: vendor[0].vendorCode,
    vendorName: vendor[0].vendorName,
    companyId,
  };
}

// ============================================================================
// 벤더의 프로젝트 목록 조회
// ============================================================================

export async function fetchVendorProjects() {
  try {
    const vendorInfo = await getVendorSessionInfo();
    
    if (!vendorInfo) {
      throw new Error("벤더 정보를 찾을 수 없습니다.");
    }

    // contracts 테이블에서 해당 벤더의 계약들의 프로젝트 조회
    const vendorProjects = await db
      .selectDistinct({
        PROJ_NO: projects.code,
        PROJ_NM: projects.name,
        contract_count: sql<number>`COUNT(DISTINCT ${contracts.id})::int`,
      })
      .from(contracts)
      .innerJoin(projects, eq(contracts.projectId, projects.id))
      .where(eq(contracts.vendorId, vendorInfo.vendorId))
      .groupBy(projects.id, projects.code, projects.name)
      .orderBy(sql`COUNT(DISTINCT ${contracts.id}) DESC`);

    return vendorProjects;
  } catch (error) {
    console.error("[fetchVendorProjects] 오류:", error);
    return [];
  }
}

// ============================================================================
// 벤더 필터링된 문서 목록 조회
// ============================================================================

export async function fetchVendorDocuments(params: SwpTableParams) {
  try {
    const vendorInfo = await getVendorSessionInfo();
    
    if (!vendorInfo) {
      throw new Error("벤더 정보를 찾을 수 없습니다.");
    }

    // 벤더 코드를 필터에 자동 추가
    const vendorParams: SwpTableParams = {
      ...params,
      filters: {
        ...params.filters,
        vndrCd: vendorInfo.vendorCode, // 벤더 코드 필터 강제 적용
      },
    };

    // 기존 fetchSwpDocuments 재사용
    return await fetchSwpDocuments(vendorParams);
  } catch (error) {
    console.error("[fetchVendorDocuments] 오류:", error);
    throw new Error("문서 목록 조회 실패");
  }
}

// ============================================================================
// 파일 업로드
// ============================================================================

export interface FileUploadParams {
  revisionId: number;
  file: {
    FILE_NM: string;
    FILE_SEQ: string;
    FILE_SZ: string;
    FLD_PATH: string;
    STAT?: string;
    STAT_NM?: string;
  };
}

export async function uploadFileToRevision(params: FileUploadParams) {
  try {
    const vendorInfo = await getVendorSessionInfo();
    
    if (!vendorInfo) {
      throw new Error("벤더 정보를 찾을 수 없습니다.");
    }

    const { revisionId, file } = params;

    // 1. 해당 리비전이 벤더에게 제공된 문서인지 확인
    const revisionCheck = await db
      .select({
        DOC_NO: swpDocumentRevisions.DOC_NO,
        VNDR_CD: sql<string>`(
          SELECT d."VNDR_CD" 
          FROM swp.swp_documents d 
          WHERE d."DOC_NO" = ${swpDocumentRevisions.DOC_NO}
        )`,
      })
      .from(swpDocumentRevisions)
      .where(eq(swpDocumentRevisions.id, revisionId))
      .limit(1);

    if (!revisionCheck[0]) {
      throw new Error("리비전을 찾을 수 없습니다.");
    }

    // 벤더 코드가 일치하는지 확인
    if (revisionCheck[0].VNDR_CD !== vendorInfo.vendorCode) {
      throw new Error("이 문서에 대한 권한이 없습니다.");
    }

    // 2. 파일 정보 저장 (upsert)
    const existingFile = await db
      .select({ id: swpDocumentFiles.id })
      .from(swpDocumentFiles)
      .where(
        and(
          eq(swpDocumentFiles.revision_id, revisionId),
          eq(swpDocumentFiles.FILE_SEQ, file.FILE_SEQ)
        )
      )
      .limit(1);

    if (existingFile[0]) {
      // 업데이트
      await db.execute(sql`
        UPDATE swp.swp_document_files
        SET 
          "FILE_NM" = ${file.FILE_NM},
          "FILE_SZ" = ${file.FILE_SZ},
          "FLD_PATH" = ${file.FLD_PATH},
          "STAT" = ${file.STAT || null},
          "STAT_NM" = ${file.STAT_NM || null},
          sync_status = 'synced',
          updated_at = NOW()
        WHERE id = ${existingFile[0].id}
      `);

      return { success: true, fileId: existingFile[0].id, action: "updated" };
    } else {
      // 삽입
      const result = await db.execute<{ id: number }>(sql`
        INSERT INTO swp.swp_document_files 
          (revision_id, "FILE_NM", "FILE_SEQ", "FILE_SZ", "FLD_PATH", "STAT", "STAT_NM", sync_status)
        VALUES 
          (${revisionId}, ${file.FILE_NM}, ${file.FILE_SEQ}, ${file.FILE_SZ}, ${file.FLD_PATH}, ${file.STAT || null}, ${file.STAT_NM || null}, 'synced')
        RETURNING id
      `);

      return { success: true, fileId: result.rows[0].id, action: "created" };
    }
  } catch (error) {
    console.error("[uploadFileToRevision] 오류:", error);
    throw new Error(
      error instanceof Error ? error.message : "파일 업로드 실패"
    );
  }
}

// ============================================================================
// 벤더 통계 조회
// ============================================================================

export async function fetchVendorSwpStats(projNo?: string) {
  try {
    const vendorInfo = await getVendorSessionInfo();
    
    if (!vendorInfo) {
      throw new Error("벤더 정보를 찾을 수 없습니다.");
    }

    const whereConditions = [
      sql`d."VNDR_CD" = ${vendorInfo.vendorCode}`,
    ];

    if (projNo) {
      whereConditions.push(sql`d."PROJ_NO" = ${projNo}`);
    }

    const stats = await db.execute<{
      total_documents: number;
      total_revisions: number;
      total_files: number;
      uploaded_files: number;
      last_sync: Date | null;
    }>(sql`
      SELECT 
        COUNT(DISTINCT d."DOC_NO")::int as total_documents,
        COUNT(DISTINCT r.id)::int as total_revisions,
        COUNT(f.id)::int as total_files,
        COUNT(CASE WHEN f."FLD_PATH" IS NOT NULL AND f."FLD_PATH" != '' THEN 1 END)::int as uploaded_files,
        MAX(d.last_synced_at) as last_sync
      FROM swp.swp_documents d
      LEFT JOIN swp.swp_document_revisions r ON d."DOC_NO" = r."DOC_NO"
      LEFT JOIN swp.swp_document_files f ON r.id = f.revision_id
      WHERE ${sql.join(whereConditions, sql` AND `)}
    `);

    return stats.rows[0] || {
      total_documents: 0,
      total_revisions: 0,
      total_files: 0,
      uploaded_files: 0,
      last_sync: null,
    };
  } catch (error) {
    console.error("[fetchVendorSwpStats] 오류:", error);
    return {
      total_documents: 0,
      total_revisions: 0,
      total_files: 0,
      uploaded_files: 0,
      last_sync: null,
    };
  }
}