119 lines
3.8 KiB
TypeScript
119 lines
3.8 KiB
TypeScript
import { HardwareAsset, SoftwareAsset, SWUser, HardwareLog } from './excelHandler';
|
|
|
|
// --- State Definitions ---
|
|
export interface MasterAssetData {
|
|
pc: HardwareAsset[];
|
|
server: HardwareAsset[];
|
|
storage: HardwareAsset[];
|
|
equip: HardwareAsset[];
|
|
mobile: HardwareAsset[];
|
|
subSw: SoftwareAsset[];
|
|
permSw: SoftwareAsset[];
|
|
swUsers: SWUser[];
|
|
logs: HardwareLog[];
|
|
}
|
|
|
|
export interface AppState {
|
|
activeCategory: 'dashboard' | 'hw' | 'sw';
|
|
activeSubTab: string; // '대시보드', '개인PC', '서버', '스토리지', '전산비품', '구독SW', '영구SW'
|
|
masterData: MasterAssetData;
|
|
}
|
|
|
|
// 초기 상태
|
|
export const state: AppState = {
|
|
activeCategory: 'dashboard',
|
|
activeSubTab: '대시보드',
|
|
masterData: {
|
|
pc: [],
|
|
server: [],
|
|
storage: [],
|
|
equip: [],
|
|
mobile: [],
|
|
subSw: [],
|
|
permSw: [],
|
|
swUsers: [],
|
|
logs: []
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 전용 API 엔드포인트들로부터 데이터 로드
|
|
*/
|
|
export async function loadMasterDataFromDB() {
|
|
try {
|
|
const endpoints = [
|
|
{ key: 'pc', url: 'http://localhost:3000/api/pc' },
|
|
{ key: 'server', url: 'http://localhost:3000/api/server' },
|
|
{ key: 'storage', url: 'http://localhost:3000/api/storage' },
|
|
{ key: 'equip', url: 'http://localhost:3000/api/equip' },
|
|
{ key: 'mobile', url: 'http://localhost:3000/api/mobile' },
|
|
{ key: 'subSw', url: 'http://localhost:3000/api/sw/sub' },
|
|
{ key: 'permSw', url: 'http://localhost:3000/api/sw/perm' },
|
|
{ key: 'swUsers', url: 'http://localhost:3000/api/sw-users' }
|
|
];
|
|
|
|
const results = await Promise.all(endpoints.map(e => fetch(e.url)));
|
|
|
|
for (let i = 0; i < endpoints.length; i++) {
|
|
if (results[i].ok) {
|
|
const data = await results[i].json();
|
|
(state.masterData as any)[endpoints[i].key] = data || [];
|
|
}
|
|
}
|
|
|
|
console.log('✅ 6개 테이블 데이터 로드 완료');
|
|
return true;
|
|
} catch (err) {
|
|
console.warn('⚠️ 백엔드 서버 연결 실패. 로컬 데이터를 유지합니다.');
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// --- State Helpers ---
|
|
export function updateState(newState: Partial<AppState>) {
|
|
Object.assign(state, newState);
|
|
}
|
|
|
|
/**
|
|
* 하드웨어 자산 통합 저장 (자동 카테고리 분류)
|
|
*/
|
|
export function saveHardwareAsset(updatedAsset: HardwareAsset) {
|
|
const { type } = updatedAsset;
|
|
const detailPurpose = (updatedAsset as any).상세용도 || '';
|
|
|
|
// 1. 타겟 카테고리 결정
|
|
let targetKey: keyof MasterAssetData = 'equip';
|
|
if (type === '서버' || (type === 'PC' && detailPurpose === '서버')) targetKey = 'server';
|
|
else if (['NAS', 'DAS', '스토리지'].includes(type)) targetKey = 'storage';
|
|
else if (['CPU', 'GPU', 'RAM', 'HDD'].includes(type)) targetKey = 'equip';
|
|
else if (['모바일', '태블릿', '노트북'].includes(type)) targetKey = 'mobile';
|
|
else if (type === 'PC' && detailPurpose === '개인PC') targetKey = 'pc';
|
|
|
|
// 2. 모든 카테고리에서 기존 ID 자산 삭제 (이동 가능성 대비)
|
|
const hwKeys: (keyof MasterAssetData)[] = ['pc', 'server', 'storage', 'equip', 'mobile'];
|
|
hwKeys.forEach(key => {
|
|
const arr = state.masterData[key] as HardwareAsset[];
|
|
if (Array.isArray(arr)) {
|
|
const idx = arr.findIndex(a => a.id === updatedAsset.id);
|
|
if (idx > -1) arr.splice(idx, 1);
|
|
}
|
|
});
|
|
|
|
// 3. 새로운 타겟 카테고리에 추가
|
|
(state.masterData[targetKey] as HardwareAsset[]).push(updatedAsset);
|
|
}
|
|
|
|
/**
|
|
* 하드웨어 자산 통합 삭제
|
|
*/
|
|
export function deleteHardwareAsset(assetId: string) {
|
|
const hwKeys: (keyof MasterAssetData)[] = ['pc', 'server', 'storage', 'equip', 'mobile'];
|
|
hwKeys.forEach(key => {
|
|
const arr = state.masterData[key] as HardwareAsset[];
|
|
if (Array.isArray(arr)) {
|
|
const idx = arr.findIndex(a => a.id === assetId);
|
|
if (idx > -1) arr.splice(idx, 1);
|
|
}
|
|
});
|
|
}
|