summaryrefslogtreecommitdiff
path: root/lib/forms/sedp-actions.ts
blob: 24ae2e661c2076ba344d22014048391cd6710cbe (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
"use server";

import { getSEDPToken } from "@/lib/sedp/sedp-token";

interface SEDPTagData {
  [tableName: string]: Array<{
    TAG_NO: string;
    TAG_DESC: string;
    ATTRIBUTES: Array<{
      ATT_ID: string;
      VALUE: string;
    }>;
  }>;
}

interface SEDPTemplateData {
  templateId: string;
  content: string;
  projectNo: string;
  regTypeId: string;
  [key: string]: any;
}

/**
 * SEDP에서 태그 데이터를 가져오는 서버 액션
 */
export async function fetchTagDataFromSEDP(
  projectCode: string, 
  formCode: string
): Promise<SEDPTagData> {
  try {
    // Get the token
    const apiKey = await getSEDPToken();

    // Define the API base URL
    const SEDP_API_BASE_URL = process.env.SEDP_API_BASE_URL || 'http://sedpwebapi.ship.samsung.co.kr/api';

    // Make the API call
    const response = await fetch(
      `${SEDP_API_BASE_URL}/Data/GetPubData`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'accept': '*/*',
          'ApiKey': apiKey,
          'ProjectNo': projectCode
        },
        body: JSON.stringify({
          ProjectNo: projectCode,
          REG_TYPE_ID: formCode,
          ContainDeleted: false
        })
      }
    );

    if (!response.ok) {
      const errorText = await response.text();
      throw new Error(`SEDP API request failed: ${response.status} ${response.statusText} - ${errorText}`);
    }

    const data = await response.json();
    return data as SEDPTagData;
  } catch (error: unknown) {
    console.error('Error calling SEDP API:', error);
    const errorMessage = error instanceof Error ? error.message : 'Unknown error';
    throw new Error(`Failed to fetch data from SEDP API: ${errorMessage}`);
  }
}

/**
 * SEDP에서 템플릿 데이터를 가져오는 서버 액션
 */
export async function fetchTemplateFromSEDP(
  projectCode: string, 
  formCode: string
): Promise<SEDPTemplateData> {
  try {
    // Get the token
    const apiKey = await getSEDPToken();

    // Define the API base URL
    const SEDP_API_BASE_URL = process.env.SEDP_API_BASE_URL || 'http://sedpwebapi.ship.samsung.co.kr/api';

    // Make the API call
    const response = await fetch(
      `${SEDP_API_BASE_URL}/Template/GetByRegisterID`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'accept': '*/*',
          'ApiKey': apiKey,
          'ProjectNo': projectCode
        },
        body: JSON.stringify({
          WithContent: true,
          ProjectNo: projectCode,
          REG_TYPE_ID: formCode
        })
      }
    );

    if (!response.ok) {
      const errorText = await response.text();
      throw new Error(`SEDP Template API request failed: ${response.status} ${response.statusText} - ${errorText}`);
    }

    const data = await response.json();
    return data as SEDPTemplateData;
  } catch (error: unknown) {
    console.error('Error calling SEDP Template API:', error);
    const errorMessage = error instanceof Error ? error.message : 'Unknown error';
    throw new Error(`Failed to fetch template from SEDP API: ${errorMessage}`);
  }
}