ERP-node/backend-node/src/controllers/tableManagementController.ts

446 lines
12 KiB
TypeScript
Raw Normal View History

2025-08-25 14:08:08 +09:00
import { Request, Response } from "express";
import { logger } from "../utils/logger";
import { AuthenticatedRequest } from "../types/auth";
import { ApiResponse } from "../types/common";
import { Client } from "pg";
import { TableManagementService } from "../services/tableManagementService";
import {
TableInfo,
ColumnTypeInfo,
ColumnSettings,
TableListResponse,
ColumnListResponse,
ColumnSettingsResponse,
} from "../types/tableManagement";
/**
*
*/
export async function getTableList(
req: AuthenticatedRequest,
res: Response
): Promise<void> {
try {
logger.info("=== 테이블 목록 조회 시작 ===");
// PostgreSQL 클라이언트 생성
const client = new Client({
connectionString: process.env.DATABASE_URL,
});
await client.connect();
try {
const tableManagementService = new TableManagementService(client);
const tableList = await tableManagementService.getTableList();
logger.info(`테이블 목록 조회 결과: ${tableList.length}`);
const response: ApiResponse<TableInfo[]> = {
success: true,
message: "테이블 목록을 성공적으로 조회했습니다.",
data: tableList,
};
res.status(200).json(response);
} finally {
await client.end();
}
} catch (error) {
logger.error("테이블 목록 조회 중 오류 발생:", error);
const response: ApiResponse<null> = {
success: false,
message: "테이블 목록 조회 중 오류가 발생했습니다.",
error: {
code: "TABLE_LIST_ERROR",
details: error instanceof Error ? error.message : "Unknown error",
},
};
res.status(500).json(response);
}
}
/**
*
*/
export async function getColumnList(
req: AuthenticatedRequest,
res: Response
): Promise<void> {
try {
const { tableName } = req.params;
logger.info(`=== 컬럼 정보 조회 시작: ${tableName} ===`);
if (!tableName) {
const response: ApiResponse<null> = {
success: false,
message: "테이블명이 필요합니다.",
error: {
code: "MISSING_TABLE_NAME",
details: "테이블명 파라미터가 누락되었습니다.",
},
};
res.status(400).json(response);
return;
}
// PostgreSQL 클라이언트 생성
const client = new Client({
connectionString: process.env.DATABASE_URL,
});
await client.connect();
try {
const tableManagementService = new TableManagementService(client);
const columnList = await tableManagementService.getColumnList(tableName);
logger.info(`컬럼 정보 조회 결과: ${tableName}, ${columnList.length}`);
const response: ApiResponse<ColumnTypeInfo[]> = {
success: true,
message: "컬럼 목록을 성공적으로 조회했습니다.",
data: columnList,
};
res.status(200).json(response);
} finally {
await client.end();
}
} catch (error) {
logger.error("컬럼 정보 조회 중 오류 발생:", error);
const response: ApiResponse<null> = {
success: false,
message: "컬럼 목록 조회 중 오류가 발생했습니다.",
error: {
code: "COLUMN_LIST_ERROR",
details: error instanceof Error ? error.message : "Unknown error",
},
};
res.status(500).json(response);
}
}
/**
*
*/
export async function updateColumnSettings(
req: AuthenticatedRequest,
res: Response
): Promise<void> {
try {
const { tableName, columnName } = req.params;
const settings: ColumnSettings = req.body;
logger.info(`=== 컬럼 설정 업데이트 시작: ${tableName}.${columnName} ===`);
if (!tableName || !columnName) {
const response: ApiResponse<null> = {
success: false,
message: "테이블명과 컬럼명이 필요합니다.",
error: {
code: "MISSING_PARAMETERS",
details: "테이블명 또는 컬럼명 파라미터가 누락되었습니다.",
},
};
res.status(400).json(response);
return;
}
if (!settings) {
const response: ApiResponse<null> = {
success: false,
message: "컬럼 설정 정보가 필요합니다.",
error: {
code: "MISSING_SETTINGS",
details: "요청 본문에 컬럼 설정 정보가 누락되었습니다.",
},
};
res.status(400).json(response);
return;
}
// PostgreSQL 클라이언트 생성
const client = new Client({
connectionString: process.env.DATABASE_URL,
});
await client.connect();
try {
const tableManagementService = new TableManagementService(client);
await tableManagementService.updateColumnSettings(
tableName,
columnName,
settings
);
logger.info(`컬럼 설정 업데이트 완료: ${tableName}.${columnName}`);
const response: ApiResponse<null> = {
success: true,
message: "컬럼 설정을 성공적으로 저장했습니다.",
};
res.status(200).json(response);
} finally {
await client.end();
}
} catch (error) {
logger.error("컬럼 설정 업데이트 중 오류 발생:", error);
const response: ApiResponse<null> = {
success: false,
message: "컬럼 설정 저장 중 오류가 발생했습니다.",
error: {
code: "COLUMN_SETTINGS_UPDATE_ERROR",
details: error instanceof Error ? error.message : "Unknown error",
},
};
res.status(500).json(response);
}
}
/**
*
*/
export async function updateAllColumnSettings(
req: AuthenticatedRequest,
res: Response
): Promise<void> {
try {
const { tableName } = req.params;
const columnSettings: ColumnSettings[] = req.body;
logger.info(`=== 전체 컬럼 설정 일괄 업데이트 시작: ${tableName} ===`);
if (!tableName) {
const response: ApiResponse<null> = {
success: false,
message: "테이블명이 필요합니다.",
error: {
code: "MISSING_TABLE_NAME",
details: "테이블명 파라미터가 누락되었습니다.",
},
};
res.status(400).json(response);
return;
}
if (!Array.isArray(columnSettings) || columnSettings.length === 0) {
const response: ApiResponse<null> = {
success: false,
message: "컬럼 설정 목록이 필요합니다.",
error: {
code: "MISSING_COLUMN_SETTINGS",
details: "요청 본문에 컬럼 설정 목록이 누락되었습니다.",
},
};
res.status(400).json(response);
return;
}
// PostgreSQL 클라이언트 생성
const client = new Client({
connectionString: process.env.DATABASE_URL,
});
await client.connect();
try {
const tableManagementService = new TableManagementService(client);
await tableManagementService.updateAllColumnSettings(
tableName,
columnSettings
);
logger.info(
`전체 컬럼 설정 일괄 업데이트 완료: ${tableName}, ${columnSettings.length}`
);
const response: ApiResponse<null> = {
success: true,
message: "모든 컬럼 설정을 성공적으로 저장했습니다.",
};
res.status(200).json(response);
} finally {
await client.end();
}
} catch (error) {
logger.error("전체 컬럼 설정 일괄 업데이트 중 오류 발생:", error);
const response: ApiResponse<null> = {
success: false,
message: "컬럼 설정 저장 중 오류가 발생했습니다.",
error: {
code: "ALL_COLUMN_SETTINGS_UPDATE_ERROR",
details: error instanceof Error ? error.message : "Unknown error",
},
};
res.status(500).json(response);
}
}
/**
*
*/
export async function getTableLabels(
req: AuthenticatedRequest,
res: Response
): Promise<void> {
try {
const { tableName } = req.params;
logger.info(`=== 테이블 라벨 정보 조회 시작: ${tableName} ===`);
if (!tableName) {
const response: ApiResponse<null> = {
success: false,
message: "테이블명이 필요합니다.",
error: {
code: "MISSING_TABLE_NAME",
details: "테이블명 파라미터가 누락되었습니다.",
},
};
res.status(400).json(response);
return;
}
// PostgreSQL 클라이언트 생성
const client = new Client({
connectionString: process.env.DATABASE_URL,
});
await client.connect();
try {
const tableManagementService = new TableManagementService(client);
const tableLabels =
await tableManagementService.getTableLabels(tableName);
if (!tableLabels) {
const response: ApiResponse<null> = {
success: false,
message: "테이블 라벨 정보를 찾을 수 없습니다.",
error: {
code: "TABLE_LABELS_NOT_FOUND",
details: `테이블 ${tableName}의 라벨 정보가 존재하지 않습니다.`,
},
};
res.status(404).json(response);
return;
}
logger.info(`테이블 라벨 정보 조회 완료: ${tableName}`);
const response: ApiResponse<any> = {
success: true,
message: "테이블 라벨 정보를 성공적으로 조회했습니다.",
data: tableLabels,
};
res.status(200).json(response);
} finally {
await client.end();
}
} catch (error) {
logger.error("테이블 라벨 정보 조회 중 오류 발생:", error);
const response: ApiResponse<null> = {
success: false,
message: "테이블 라벨 정보 조회 중 오류가 발생했습니다.",
error: {
code: "TABLE_LABELS_ERROR",
details: error instanceof Error ? error.message : "Unknown error",
},
};
res.status(500).json(response);
}
}
/**
*
*/
export async function getColumnLabels(
req: AuthenticatedRequest,
res: Response
): Promise<void> {
try {
const { tableName, columnName } = req.params;
logger.info(`=== 컬럼 라벨 정보 조회 시작: ${tableName}.${columnName} ===`);
if (!tableName || !columnName) {
const response: ApiResponse<null> = {
success: false,
message: "테이블명과 컬럼명이 필요합니다.",
error: {
code: "MISSING_PARAMETERS",
details: "테이블명 또는 컬럼명 파라미터가 누락되었습니다.",
},
};
res.status(400).json(response);
return;
}
// PostgreSQL 클라이언트 생성
const client = new Client({
connectionString: process.env.DATABASE_URL,
});
await client.connect();
try {
const tableManagementService = new TableManagementService(client);
const columnLabels = await tableManagementService.getColumnLabels(
tableName,
columnName
);
if (!columnLabels) {
const response: ApiResponse<null> = {
success: false,
message: "컬럼 라벨 정보를 찾을 수 없습니다.",
error: {
code: "COLUMN_LABELS_NOT_FOUND",
details: `컬럼 ${tableName}.${columnName}의 라벨 정보가 존재하지 않습니다.`,
},
};
res.status(404).json(response);
return;
}
logger.info(`컬럼 라벨 정보 조회 완료: ${tableName}.${columnName}`);
const response: ApiResponse<any> = {
success: true,
message: "컬럼 라벨 정보를 성공적으로 조회했습니다.",
data: columnLabels,
};
res.status(200).json(response);
} finally {
await client.end();
}
} catch (error) {
logger.error("컬럼 라벨 정보 조회 중 오류 발생:", error);
const response: ApiResponse<null> = {
success: false,
message: "컬럼 라벨 정보 조회 중 오류가 발생했습니다.",
error: {
code: "COLUMN_LABELS_ERROR",
details: error instanceof Error ? error.message : "Unknown error",
},
};
res.status(500).json(response);
}
}