간단한 crud 부분 prisma orm으로 변경
This commit is contained in:
parent
cb88faa68e
commit
bde5f0884a
|
|
@ -547,61 +547,48 @@ export const getCompanyList = async (
|
||||||
user: req.user,
|
user: req.user,
|
||||||
});
|
});
|
||||||
|
|
||||||
// PostgreSQL 클라이언트 생성
|
// Prisma ORM을 사용한 회사 목록 조회
|
||||||
const client = new Client({
|
const companies = await prisma.company_mng.findMany({
|
||||||
connectionString:
|
where: {
|
||||||
process.env.DATABASE_URL ||
|
OR: [{ status: "active" }, { status: null }],
|
||||||
"postgresql://postgres:postgres@localhost:5432/ilshin",
|
},
|
||||||
|
orderBy: {
|
||||||
|
company_name: "asc",
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
company_code: true,
|
||||||
|
company_name: true,
|
||||||
|
status: true,
|
||||||
|
writer: true,
|
||||||
|
regdate: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await client.connect();
|
// 프론트엔드에서 기대하는 응답 형식으로 변환
|
||||||
|
const response = {
|
||||||
|
success: true,
|
||||||
|
data: companies.map((company) => ({
|
||||||
|
company_code: company.company_code,
|
||||||
|
company_name: company.company_name,
|
||||||
|
status: company.status || "active",
|
||||||
|
writer: company.writer,
|
||||||
|
regdate: company.regdate
|
||||||
|
? company.regdate.toISOString()
|
||||||
|
: new Date().toISOString(),
|
||||||
|
data_type: "company",
|
||||||
|
})),
|
||||||
|
message: "회사 목록 조회 성공",
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
logger.info("회사 목록 조회 성공", {
|
||||||
// 회사 목록 조회 쿼리
|
totalCount: companies.length,
|
||||||
const query = `
|
companies: companies.map((c) => ({
|
||||||
SELECT
|
code: c.company_code,
|
||||||
company_code,
|
name: c.company_name,
|
||||||
company_name,
|
})),
|
||||||
status,
|
});
|
||||||
writer,
|
|
||||||
regdate,
|
|
||||||
'company' as data_type
|
|
||||||
FROM company_mng
|
|
||||||
WHERE status = 'active' OR status IS NULL
|
|
||||||
ORDER BY company_name
|
|
||||||
`;
|
|
||||||
|
|
||||||
const result = await client.query(query);
|
res.status(200).json(response);
|
||||||
const companies = result.rows;
|
|
||||||
|
|
||||||
// 프론트엔드에서 기대하는 응답 형식으로 변환
|
|
||||||
const response = {
|
|
||||||
success: true,
|
|
||||||
data: companies.map((company) => ({
|
|
||||||
company_code: company.company_code,
|
|
||||||
company_name: company.company_name,
|
|
||||||
status: company.status || "active",
|
|
||||||
writer: company.writer,
|
|
||||||
regdate: company.regdate
|
|
||||||
? company.regdate.toISOString()
|
|
||||||
: new Date().toISOString(),
|
|
||||||
data_type: company.data_type,
|
|
||||||
})),
|
|
||||||
message: "회사 목록 조회 성공",
|
|
||||||
};
|
|
||||||
|
|
||||||
logger.info("회사 목록 조회 성공", {
|
|
||||||
totalCount: companies.length,
|
|
||||||
companies: companies.map((c) => ({
|
|
||||||
code: c.company_code,
|
|
||||||
name: c.company_name,
|
|
||||||
})),
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(200).json(response);
|
|
||||||
} finally {
|
|
||||||
await client.end();
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("회사 목록 조회 실패", { error });
|
logger.error("회사 목록 조회 실패", { error });
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
|
|
@ -1433,78 +1420,89 @@ export const getDepartmentList = async (
|
||||||
|
|
||||||
const { companyCode, status, search } = req.query;
|
const { companyCode, status, search } = req.query;
|
||||||
|
|
||||||
// PostgreSQL 클라이언트 생성
|
// Prisma ORM을 사용한 부서 목록 조회
|
||||||
const client = new Client({
|
const whereConditions: any = {};
|
||||||
connectionString:
|
|
||||||
process.env.DATABASE_URL ||
|
// 회사 코드 필터
|
||||||
"postgresql://postgres:postgres@localhost:5432/ilshin",
|
if (companyCode) {
|
||||||
|
whereConditions.company_name = companyCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 상태 필터
|
||||||
|
if (status) {
|
||||||
|
whereConditions.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 검색 조건
|
||||||
|
if (search) {
|
||||||
|
whereConditions.OR = [
|
||||||
|
{ dept_name: { contains: search as string, mode: "insensitive" } },
|
||||||
|
{ dept_code: { contains: search as string, mode: "insensitive" } },
|
||||||
|
{ location_name: { contains: search as string, mode: "insensitive" } },
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
const departments = await prisma.dept_info.findMany({
|
||||||
|
where: whereConditions,
|
||||||
|
orderBy: [{ parent_dept_code: "asc" }, { dept_name: "asc" }],
|
||||||
|
select: {
|
||||||
|
dept_code: true,
|
||||||
|
parent_dept_code: true,
|
||||||
|
dept_name: true,
|
||||||
|
master_sabun: true,
|
||||||
|
master_user_id: true,
|
||||||
|
location: true,
|
||||||
|
location_name: true,
|
||||||
|
regdate: true,
|
||||||
|
data_type: true,
|
||||||
|
status: true,
|
||||||
|
sales_yn: true,
|
||||||
|
company_name: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await client.connect();
|
// 부서 트리 구조 생성
|
||||||
|
const deptMap = new Map();
|
||||||
|
const rootDepartments: any[] = [];
|
||||||
|
|
||||||
try {
|
// 모든 부서를 맵에 저장
|
||||||
// 부서 목록 조회 쿼리
|
departments.forEach((dept) => {
|
||||||
let query = `
|
deptMap.set(dept.dept_code, {
|
||||||
SELECT
|
deptCode: dept.dept_code,
|
||||||
dept_code,
|
deptName: dept.dept_name,
|
||||||
parent_dept_code,
|
parentDeptCode: dept.parent_dept_code,
|
||||||
dept_name,
|
masterSabun: dept.master_sabun,
|
||||||
master_sabun,
|
masterUserId: dept.master_user_id,
|
||||||
master_user_id,
|
location: dept.location,
|
||||||
location,
|
locationName: dept.location_name,
|
||||||
location_name,
|
regdate: dept.regdate ? dept.regdate.toISOString() : null,
|
||||||
regdate,
|
dataType: dept.data_type,
|
||||||
data_type,
|
status: dept.status || "active",
|
||||||
status,
|
salesYn: dept.sales_yn,
|
||||||
sales_yn,
|
companyName: dept.company_name,
|
||||||
company_name
|
children: [],
|
||||||
FROM dept_info
|
});
|
||||||
WHERE 1=1
|
});
|
||||||
`;
|
|
||||||
|
|
||||||
const queryParams: any[] = [];
|
// 부서 트리 구조 생성
|
||||||
let paramIndex = 1;
|
departments.forEach((dept) => {
|
||||||
|
const deptNode = deptMap.get(dept.dept_code);
|
||||||
|
|
||||||
// 회사 코드 필터
|
if (dept.parent_dept_code && deptMap.has(dept.parent_dept_code)) {
|
||||||
if (companyCode) {
|
// 상위 부서가 있으면 children에 추가
|
||||||
query += ` AND company_name = $${paramIndex}`;
|
const parentDept = deptMap.get(dept.parent_dept_code);
|
||||||
queryParams.push(companyCode);
|
parentDept.children.push(deptNode);
|
||||||
paramIndex++;
|
} else {
|
||||||
|
// 상위 부서가 없으면 루트 부서로 추가
|
||||||
|
rootDepartments.push(deptNode);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// 상태 필터
|
const response = {
|
||||||
if (status) {
|
success: true,
|
||||||
query += ` AND status = $${paramIndex}`;
|
data: {
|
||||||
queryParams.push(status);
|
departments: rootDepartments,
|
||||||
paramIndex++;
|
flatList: departments.map((dept) => ({
|
||||||
}
|
|
||||||
|
|
||||||
// 검색 조건
|
|
||||||
if (search) {
|
|
||||||
query += ` AND (
|
|
||||||
dept_name ILIKE $${paramIndex} OR
|
|
||||||
dept_code ILIKE $${paramIndex} OR
|
|
||||||
location_name ILIKE $${paramIndex}
|
|
||||||
)`;
|
|
||||||
queryParams.push(`%${search}%`);
|
|
||||||
paramIndex++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 정렬 (상위 부서 먼저, 그 다음 부서명 순)
|
|
||||||
query += ` ORDER BY
|
|
||||||
CASE WHEN parent_dept_code IS NULL OR parent_dept_code = '' THEN 0 ELSE 1 END,
|
|
||||||
dept_name`;
|
|
||||||
|
|
||||||
const result = await client.query(query, queryParams);
|
|
||||||
const departments = result.rows;
|
|
||||||
|
|
||||||
// 부서 트리 구조 생성
|
|
||||||
const deptMap = new Map();
|
|
||||||
const rootDepartments: any[] = [];
|
|
||||||
|
|
||||||
// 모든 부서를 맵에 저장
|
|
||||||
departments.forEach((dept) => {
|
|
||||||
deptMap.set(dept.dept_code, {
|
|
||||||
deptCode: dept.dept_code,
|
deptCode: dept.dept_code,
|
||||||
deptName: dept.dept_name,
|
deptName: dept.dept_name,
|
||||||
parentDeptCode: dept.parent_dept_code,
|
parentDeptCode: dept.parent_dept_code,
|
||||||
|
|
@ -1517,56 +1515,18 @@ export const getDepartmentList = async (
|
||||||
status: dept.status || "active",
|
status: dept.status || "active",
|
||||||
salesYn: dept.sales_yn,
|
salesYn: dept.sales_yn,
|
||||||
companyName: dept.company_name,
|
companyName: dept.company_name,
|
||||||
children: [],
|
})),
|
||||||
});
|
},
|
||||||
});
|
message: "부서 목록 조회 성공",
|
||||||
|
total: departments.length,
|
||||||
|
};
|
||||||
|
|
||||||
// 부서 트리 구조 생성
|
logger.info("부서 목록 조회 성공", {
|
||||||
departments.forEach((dept) => {
|
totalCount: departments.length,
|
||||||
const deptNode = deptMap.get(dept.dept_code);
|
rootCount: rootDepartments.length,
|
||||||
|
});
|
||||||
|
|
||||||
if (dept.parent_dept_code && deptMap.has(dept.parent_dept_code)) {
|
res.status(200).json(response);
|
||||||
// 상위 부서가 있으면 children에 추가
|
|
||||||
const parentDept = deptMap.get(dept.parent_dept_code);
|
|
||||||
parentDept.children.push(deptNode);
|
|
||||||
} else {
|
|
||||||
// 상위 부서가 없으면 루트 부서로 추가
|
|
||||||
rootDepartments.push(deptNode);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const response = {
|
|
||||||
success: true,
|
|
||||||
data: {
|
|
||||||
departments: rootDepartments,
|
|
||||||
flatList: departments.map((dept) => ({
|
|
||||||
deptCode: dept.dept_code,
|
|
||||||
deptName: dept.dept_name,
|
|
||||||
parentDeptCode: dept.parent_dept_code,
|
|
||||||
masterSabun: dept.master_sabun,
|
|
||||||
masterUserId: dept.master_user_id,
|
|
||||||
location: dept.location,
|
|
||||||
locationName: dept.location_name,
|
|
||||||
regdate: dept.regdate ? dept.regdate.toISOString() : null,
|
|
||||||
dataType: dept.data_type,
|
|
||||||
status: dept.status || "active",
|
|
||||||
salesYn: dept.sales_yn,
|
|
||||||
companyName: dept.company_name,
|
|
||||||
})),
|
|
||||||
},
|
|
||||||
message: "부서 목록 조회 성공",
|
|
||||||
total: departments.length,
|
|
||||||
};
|
|
||||||
|
|
||||||
logger.info("부서 목록 조회 성공", {
|
|
||||||
totalCount: departments.length,
|
|
||||||
rootCount: rootDepartments.length,
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(200).json(response);
|
|
||||||
} finally {
|
|
||||||
await client.end();
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("부서 목록 조회 실패", { error });
|
logger.error("부서 목록 조회 실패", { error });
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
|
|
@ -2122,121 +2082,77 @@ export const saveUser = async (req: AuthenticatedRequest, res: Response) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostgreSQL 클라이언트 생성
|
// 비밀번호 암호화
|
||||||
const client = new Client({
|
const encryptedPassword = await EncryptUtil.encrypt(userData.userPassword);
|
||||||
connectionString:
|
|
||||||
process.env.DATABASE_URL ||
|
// Prisma ORM을 사용한 사용자 저장 (upsert)
|
||||||
"postgresql://postgres:postgres@localhost:5432/ilshin",
|
const savedUser = await prisma.user_info.upsert({
|
||||||
|
where: {
|
||||||
|
user_id: userData.userId,
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
user_id: userData.userId,
|
||||||
|
user_name: userData.userName,
|
||||||
|
user_name_eng: userData.userNameEng || null,
|
||||||
|
user_password: encryptedPassword,
|
||||||
|
dept_code: userData.deptCode || null,
|
||||||
|
dept_name: userData.deptName || null,
|
||||||
|
position_code: userData.positionCode || null,
|
||||||
|
position_name: userData.positionName || null,
|
||||||
|
email: userData.email || null,
|
||||||
|
tel: userData.tel || null,
|
||||||
|
cell_phone: userData.cellPhone || null,
|
||||||
|
user_type: userData.userType || null,
|
||||||
|
user_type_name: userData.userTypeName || null,
|
||||||
|
sabun: userData.sabun || null,
|
||||||
|
company_code: userData.companyCode || null,
|
||||||
|
status: userData.status || "active",
|
||||||
|
locale: userData.locale || null,
|
||||||
|
regdate: new Date(),
|
||||||
|
},
|
||||||
|
update: {
|
||||||
|
user_name: userData.userName,
|
||||||
|
user_name_eng: userData.userNameEng || null,
|
||||||
|
user_password: encryptedPassword,
|
||||||
|
dept_code: userData.deptCode || null,
|
||||||
|
dept_name: userData.deptName || null,
|
||||||
|
position_code: userData.positionCode || null,
|
||||||
|
position_name: userData.positionName || null,
|
||||||
|
email: userData.email || null,
|
||||||
|
tel: userData.tel || null,
|
||||||
|
cell_phone: userData.cellPhone || null,
|
||||||
|
user_type: userData.userType || null,
|
||||||
|
user_type_name: userData.userTypeName || null,
|
||||||
|
sabun: userData.sabun || null,
|
||||||
|
company_code: userData.companyCode || null,
|
||||||
|
status: userData.status || "active",
|
||||||
|
locale: userData.locale || null,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await client.connect();
|
// 기존 사용자인지 새 사용자인지 확인
|
||||||
|
const isUpdate =
|
||||||
|
(await prisma.user_info.count({
|
||||||
|
where: { user_id: userData.userId },
|
||||||
|
})) > 0;
|
||||||
|
|
||||||
try {
|
logger.info(isUpdate ? "사용자 정보 수정 완료" : "새 사용자 등록 완료", {
|
||||||
// 기존 사용자 확인
|
userId: userData.userId,
|
||||||
const checkQuery = `
|
});
|
||||||
SELECT user_id FROM user_info WHERE user_id = $1
|
|
||||||
`;
|
|
||||||
const checkResult = await client.query(checkQuery, [userData.userId]);
|
|
||||||
const isUpdate = checkResult.rows.length > 0;
|
|
||||||
|
|
||||||
if (isUpdate) {
|
const response = {
|
||||||
// 기존 사용자 수정
|
success: true,
|
||||||
const updateQuery = `
|
result: true,
|
||||||
UPDATE user_info SET
|
message: isUpdate
|
||||||
user_name = $1,
|
? "사용자 정보가 수정되었습니다."
|
||||||
user_name_eng = $2,
|
: "사용자가 등록되었습니다.",
|
||||||
user_password = $3,
|
data: {
|
||||||
dept_code = $4,
|
userId: userData.userId,
|
||||||
dept_name = $5,
|
isUpdate,
|
||||||
position_code = $6,
|
},
|
||||||
position_name = $7,
|
};
|
||||||
email = $8,
|
|
||||||
tel = $9,
|
|
||||||
cell_phone = $10,
|
|
||||||
user_type = $11,
|
|
||||||
user_type_name = $12,
|
|
||||||
sabun = $13,
|
|
||||||
company_code = $14,
|
|
||||||
status = $15,
|
|
||||||
locale = $16
|
|
||||||
WHERE user_id = $17
|
|
||||||
`;
|
|
||||||
|
|
||||||
const updateValues = [
|
res.status(200).json(response);
|
||||||
userData.userName,
|
|
||||||
userData.userNameEng || null,
|
|
||||||
await EncryptUtil.encrypt(userData.userPassword), // 비밀번호 암호화
|
|
||||||
userData.deptCode || null,
|
|
||||||
userData.deptName || null,
|
|
||||||
userData.positionCode || null,
|
|
||||||
userData.positionName || null,
|
|
||||||
userData.email || null,
|
|
||||||
userData.tel || null,
|
|
||||||
userData.cellPhone || null,
|
|
||||||
userData.userType || null,
|
|
||||||
userData.userTypeName || null,
|
|
||||||
userData.sabun || null,
|
|
||||||
userData.companyCode || null,
|
|
||||||
userData.status || "active",
|
|
||||||
userData.locale || null,
|
|
||||||
userData.userId,
|
|
||||||
];
|
|
||||||
|
|
||||||
await client.query(updateQuery, updateValues);
|
|
||||||
logger.info("사용자 정보 수정 완료", { userId: userData.userId });
|
|
||||||
} else {
|
|
||||||
// 새 사용자 등록
|
|
||||||
const insertQuery = `
|
|
||||||
INSERT INTO user_info (
|
|
||||||
user_id, user_name, user_name_eng, user_password, dept_code, dept_name,
|
|
||||||
position_code, position_name, email, tel, cell_phone, user_type,
|
|
||||||
user_type_name, sabun, company_code, status, locale, regdate
|
|
||||||
) VALUES (
|
|
||||||
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18
|
|
||||||
)
|
|
||||||
`;
|
|
||||||
|
|
||||||
const insertValues = [
|
|
||||||
userData.userId,
|
|
||||||
userData.userName,
|
|
||||||
userData.userNameEng || null,
|
|
||||||
await EncryptUtil.encrypt(userData.userPassword), // 비밀번호 암호화
|
|
||||||
userData.deptCode || null,
|
|
||||||
userData.deptName || null,
|
|
||||||
userData.positionCode || null,
|
|
||||||
userData.positionName || null,
|
|
||||||
userData.email || null,
|
|
||||||
userData.tel || null,
|
|
||||||
userData.cellPhone || null,
|
|
||||||
userData.userType || null,
|
|
||||||
userData.userTypeName || null,
|
|
||||||
userData.sabun || null,
|
|
||||||
userData.companyCode || null,
|
|
||||||
userData.status || "active",
|
|
||||||
userData.locale || null,
|
|
||||||
new Date(),
|
|
||||||
];
|
|
||||||
|
|
||||||
await client.query(insertQuery, insertValues);
|
|
||||||
logger.info("새 사용자 등록 완료", { userId: userData.userId });
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = {
|
|
||||||
success: true,
|
|
||||||
result: true,
|
|
||||||
message: isUpdate
|
|
||||||
? "사용자 정보가 수정되었습니다."
|
|
||||||
: "사용자가 등록되었습니다.",
|
|
||||||
data: {
|
|
||||||
userId: userData.userId,
|
|
||||||
isUpdate,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
res.status(200).json(response);
|
|
||||||
} finally {
|
|
||||||
await client.end();
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("사용자 저장 실패", { error, userData: req.body });
|
logger.error("사용자 저장 실패", { error, userData: req.body });
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue