타입스크립트 에러수정
This commit is contained in:
253
backend-node/src/services/collectionService.ts
Normal file
253
backend-node/src/services/collectionService.ts
Normal file
@@ -0,0 +1,253 @@
|
||||
// 수집 관리 서비스
|
||||
// 작성일: 2024-12-23
|
||||
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import {
|
||||
DataCollectionConfig,
|
||||
CollectionFilter,
|
||||
CollectionJob,
|
||||
CollectionHistory,
|
||||
} from "../types/collectionManagement";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export class CollectionService {
|
||||
/**
|
||||
* 수집 설정 목록 조회
|
||||
*/
|
||||
static async getCollectionConfigs(
|
||||
filter: CollectionFilter
|
||||
): Promise<DataCollectionConfig[]> {
|
||||
const whereCondition: any = {
|
||||
company_code: filter.company_code || "*",
|
||||
};
|
||||
|
||||
if (filter.config_name) {
|
||||
whereCondition.config_name = {
|
||||
contains: filter.config_name,
|
||||
mode: "insensitive",
|
||||
};
|
||||
}
|
||||
|
||||
if (filter.source_connection_id) {
|
||||
whereCondition.source_connection_id = filter.source_connection_id;
|
||||
}
|
||||
|
||||
if (filter.collection_type) {
|
||||
whereCondition.collection_type = filter.collection_type;
|
||||
}
|
||||
|
||||
if (filter.is_active) {
|
||||
whereCondition.is_active = filter.is_active === "Y";
|
||||
}
|
||||
|
||||
if (filter.search) {
|
||||
whereCondition.OR = [
|
||||
{ config_name: { contains: filter.search, mode: "insensitive" } },
|
||||
{ description: { contains: filter.search, mode: "insensitive" } },
|
||||
];
|
||||
}
|
||||
|
||||
const configs = await prisma.data_collection_configs.findMany({
|
||||
where: whereCondition,
|
||||
orderBy: { created_date: "desc" },
|
||||
});
|
||||
|
||||
return configs.map((config: any) => ({
|
||||
...config,
|
||||
is_active: config.is_active ? "Y" : "N",
|
||||
})) as DataCollectionConfig[];
|
||||
}
|
||||
|
||||
/**
|
||||
* 수집 설정 상세 조회
|
||||
*/
|
||||
static async getCollectionConfigById(
|
||||
id: number
|
||||
): Promise<DataCollectionConfig | null> {
|
||||
const config = await prisma.data_collection_configs.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!config) return null;
|
||||
|
||||
return {
|
||||
...config,
|
||||
is_active: config.is_active ? "Y" : "N",
|
||||
} as DataCollectionConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 수집 설정 생성
|
||||
*/
|
||||
static async createCollectionConfig(
|
||||
data: DataCollectionConfig
|
||||
): Promise<DataCollectionConfig> {
|
||||
const { id, collection_options, ...createData } = data;
|
||||
const config = await prisma.data_collection_configs.create({
|
||||
data: {
|
||||
...createData,
|
||||
is_active: data.is_active,
|
||||
collection_options: collection_options || undefined,
|
||||
created_date: new Date(),
|
||||
updated_date: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
...config,
|
||||
is_active: config.is_active ? "Y" : "N",
|
||||
} as DataCollectionConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 수집 설정 수정
|
||||
*/
|
||||
static async updateCollectionConfig(
|
||||
id: number,
|
||||
data: Partial<DataCollectionConfig>
|
||||
): Promise<DataCollectionConfig> {
|
||||
const updateData: any = {
|
||||
...data,
|
||||
updated_date: new Date(),
|
||||
};
|
||||
|
||||
if (data.is_active !== undefined) {
|
||||
updateData.is_active = data.is_active;
|
||||
}
|
||||
|
||||
const config = await prisma.data_collection_configs.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
return {
|
||||
...config,
|
||||
is_active: config.is_active ? "Y" : "N",
|
||||
} as DataCollectionConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 수집 설정 삭제
|
||||
*/
|
||||
static async deleteCollectionConfig(id: number): Promise<void> {
|
||||
await prisma.data_collection_configs.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 수집 작업 실행
|
||||
*/
|
||||
static async executeCollection(configId: number): Promise<CollectionJob> {
|
||||
const config = await prisma.data_collection_configs.findUnique({
|
||||
where: { id: configId },
|
||||
});
|
||||
|
||||
if (!config) {
|
||||
throw new Error("수집 설정을 찾을 수 없습니다.");
|
||||
}
|
||||
|
||||
if (!config.is_active) {
|
||||
throw new Error("비활성화된 수집 설정입니다.");
|
||||
}
|
||||
|
||||
// 수집 작업 기록 생성
|
||||
const job = await prisma.data_collection_jobs.create({
|
||||
data: {
|
||||
config_id: configId,
|
||||
job_status: "running",
|
||||
started_at: new Date(),
|
||||
created_date: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
// 실제 수집 작업 실행 로직은 여기에 구현
|
||||
// 현재는 시뮬레이션으로 처리
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
// 수집 작업 시뮬레이션
|
||||
await new Promise((resolve) => setTimeout(resolve, 3000));
|
||||
|
||||
const recordsCollected = Math.floor(Math.random() * 1000) + 100;
|
||||
|
||||
await prisma.data_collection_jobs.update({
|
||||
where: { id: job.id },
|
||||
data: {
|
||||
job_status: "completed",
|
||||
completed_at: new Date(),
|
||||
records_processed: recordsCollected,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
await prisma.data_collection_jobs.update({
|
||||
where: { id: job.id },
|
||||
data: {
|
||||
job_status: "failed",
|
||||
completed_at: new Date(),
|
||||
error_message:
|
||||
error instanceof Error ? error.message : "알 수 없는 오류",
|
||||
},
|
||||
});
|
||||
}
|
||||
}, 0);
|
||||
|
||||
return job as CollectionJob;
|
||||
}
|
||||
|
||||
/**
|
||||
* 수집 작업 목록 조회
|
||||
*/
|
||||
static async getCollectionJobs(configId?: number): Promise<CollectionJob[]> {
|
||||
const whereCondition: any = {};
|
||||
|
||||
if (configId) {
|
||||
whereCondition.config_id = configId;
|
||||
}
|
||||
|
||||
const jobs = await prisma.data_collection_jobs.findMany({
|
||||
where: whereCondition,
|
||||
orderBy: { started_at: "desc" },
|
||||
include: {
|
||||
config: {
|
||||
select: {
|
||||
config_name: true,
|
||||
collection_type: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return jobs as CollectionJob[];
|
||||
}
|
||||
|
||||
/**
|
||||
* 수집 이력 조회
|
||||
*/
|
||||
static async getCollectionHistory(
|
||||
configId: number
|
||||
): Promise<CollectionHistory[]> {
|
||||
const history = await prisma.data_collection_jobs.findMany({
|
||||
where: { config_id: configId },
|
||||
orderBy: { started_at: "desc" },
|
||||
take: 50, // 최근 50개 이력
|
||||
});
|
||||
|
||||
return history.map((item: any) => ({
|
||||
id: item.id,
|
||||
config_id: item.config_id,
|
||||
status: item.job_status,
|
||||
collection_date: item.started_at,
|
||||
started_at: item.started_at,
|
||||
completed_at: item.completed_at,
|
||||
execution_time_ms:
|
||||
item.completed_at && item.started_at
|
||||
? new Date(item.completed_at).getTime() -
|
||||
new Date(item.started_at).getTime()
|
||||
: null,
|
||||
records_collected: item.records_processed || 0,
|
||||
result_message: `${item.records_processed || 0}개의 레코드가 처리되었습니다.`,
|
||||
error_message: item.error_message,
|
||||
})) as CollectionHistory[];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user