refactor(ipc): harden channels and unify IPC contracts
This commit is contained in:
306
src/preload/index.d.ts
vendored
306
src/preload/index.d.ts
vendored
@@ -15,277 +15,105 @@ import type {
|
||||
MaterialTypeBatchRequest
|
||||
} from '../main/types/validation.types'
|
||||
import type {
|
||||
SettingsData,
|
||||
UserType,
|
||||
ConnectionTestResult,
|
||||
SaveSettingsResult
|
||||
} from '../main/types/settings.types'
|
||||
import type { IpcResult } from '../main/ipc'
|
||||
|
||||
/**
|
||||
* Order number resolver API
|
||||
*/
|
||||
export interface ResolverAPI {
|
||||
/**
|
||||
* Resolve productionIDs and 生产订单号 to production order numbers
|
||||
* @param input - Resolver input with list of inputs
|
||||
*/
|
||||
resolve: (input: ResolverInput) => Promise<ResolverResponse>
|
||||
/**
|
||||
* Validate input format only (without database lookup)
|
||||
* @param inputs - List of inputs to validate
|
||||
*/
|
||||
validateFormat: (inputs: string[]) => Promise<{
|
||||
success: boolean
|
||||
results?: Array<{ input: string; type: 'productionId' | 'orderNumber' | 'unknown' }>
|
||||
error?: string
|
||||
}>
|
||||
resolve: (input: ResolverInput) => Promise<IpcResult<ResolverResponse>>
|
||||
validateFormat: (inputs: string[]) => Promise<
|
||||
IpcResult<Array<{ input: string; type: 'productionId' | 'orderNumber' | 'unknown' }>>
|
||||
>
|
||||
}
|
||||
|
||||
/**
|
||||
* Authentication API
|
||||
*/
|
||||
export interface AuthAPI {
|
||||
/**
|
||||
* Get computer name
|
||||
*/
|
||||
getComputerName: () => Promise<string>
|
||||
/**
|
||||
* Silent login by computer name
|
||||
*/
|
||||
silentLogin: () => Promise<SilentLoginResponse>
|
||||
/**
|
||||
* Login with username and password
|
||||
* @param request - Login request with username and password
|
||||
*/
|
||||
login: (request: LoginRequest) => Promise<LoginResponse>
|
||||
/**
|
||||
* Logout
|
||||
*/
|
||||
logout: () => Promise<void>
|
||||
/**
|
||||
* Get current user
|
||||
*/
|
||||
getCurrentUser: () => Promise<CurrentUserResponse>
|
||||
/**
|
||||
* Get all users (for admin user selection)
|
||||
*/
|
||||
getAllUsers: () => Promise<UserInfo[]>
|
||||
/**
|
||||
* Switch user (admin only)
|
||||
* @param userInfo - User info to switch to
|
||||
*/
|
||||
switchUser: (userInfo: UserInfo) => Promise<UserSelectionResponse>
|
||||
/**
|
||||
* Check if current user is admin
|
||||
*/
|
||||
isAdmin: () => Promise<boolean>
|
||||
getComputerName: () => Promise<IpcResult<string>>
|
||||
silentLogin: () => Promise<IpcResult<SilentLoginResponse>>
|
||||
login: (request: LoginRequest) => Promise<IpcResult<LoginResponse>>
|
||||
logout: () => Promise<IpcResult<void>>
|
||||
getCurrentUser: () => Promise<IpcResult<CurrentUserResponse>>
|
||||
getAllUsers: () => Promise<IpcResult<UserInfo[]>>
|
||||
switchUser: (userInfo: UserInfo) => Promise<IpcResult<UserSelectionResponse>>
|
||||
isAdmin: () => Promise<IpcResult<boolean>>
|
||||
}
|
||||
|
||||
/**
|
||||
* Validation API
|
||||
*/
|
||||
export interface ValidationAPI {
|
||||
/**
|
||||
* Run material validation
|
||||
* @param request - Validation request
|
||||
*/
|
||||
validate: (request: ValidationRequest) => Promise<ValidationResponse>
|
||||
/**
|
||||
* Set shared Production IDs from extractor page
|
||||
* @param productionIds - List of Production IDs
|
||||
*/
|
||||
setSharedProductionIds: (productionIds: string[]) => Promise<void>
|
||||
/**
|
||||
* Get shared Production IDs
|
||||
*/
|
||||
getSharedProductionIds: () => Promise<{ productionIds: string[] }>
|
||||
/**
|
||||
* Get cleaner data (order numbers from shared Production IDs + material codes from MaterialsToBeDeleted)
|
||||
* Filters materials by current user (admin sees all, regular users see only their own)
|
||||
*/
|
||||
getCleanerData: () => Promise<{
|
||||
success: boolean
|
||||
orderNumbers?: string[]
|
||||
materialCodes?: string[]
|
||||
error?: string
|
||||
}>
|
||||
validate: (request: ValidationRequest) => Promise<IpcResult<ValidationResponse>>
|
||||
setSharedProductionIds: (productionIds: string[]) => Promise<IpcResult<void>>
|
||||
getSharedProductionIds: () => Promise<IpcResult<{ productionIds: string[] }>>
|
||||
getCleanerData: () => Promise<
|
||||
IpcResult<{
|
||||
orderNumbers: string[]
|
||||
materialCodes: string[]
|
||||
}>
|
||||
>
|
||||
}
|
||||
|
||||
/**
|
||||
* Materials API
|
||||
*/
|
||||
export interface MaterialsAPI {
|
||||
/**
|
||||
* Upsert batch materials to MaterialsToBeDeleted
|
||||
* @param materials - List of materials with materialCode and managerName
|
||||
*/
|
||||
upsertBatch: (materials: { materialCode: string; managerName: string }[]) => Promise<{
|
||||
success: boolean
|
||||
stats?: { total: number; success: number; failed: number }
|
||||
error?: string
|
||||
}>
|
||||
/**
|
||||
* Delete materials by material codes
|
||||
* @param materialCodes - List of material codes to delete
|
||||
*/
|
||||
delete: (materialCodes: string[]) => Promise<{
|
||||
success: boolean
|
||||
count?: number
|
||||
error?: string
|
||||
}>
|
||||
/**
|
||||
* Get unique manager names
|
||||
*/
|
||||
getManagers: () => Promise<{ managers: string[] }>
|
||||
/**
|
||||
* Get materials by manager
|
||||
* @param managerName - Manager name
|
||||
*/
|
||||
getByManager: (managerName: string) => Promise<{ materials: unknown[] }>
|
||||
/**
|
||||
* Get all material records
|
||||
*/
|
||||
getAll: () => Promise<{ materials: unknown[] }>
|
||||
/**
|
||||
* Get statistics
|
||||
*/
|
||||
getStatistics: () => Promise<{ stats: unknown }>
|
||||
/**
|
||||
* Update manager for a single material
|
||||
* @param materialCode - Material code
|
||||
* @param managerName - New manager name
|
||||
*/
|
||||
upsertBatch: (materials: { materialCode: string; managerName: string }[]) => Promise<
|
||||
IpcResult<{ stats: { total: number; success: number; failed: number } }>
|
||||
>
|
||||
delete: (materialCodes: string[]) => Promise<IpcResult<{ count: number }>>
|
||||
getManagers: () => Promise<IpcResult<{ managers: string[] }>>
|
||||
getByManager: (managerName: string) => Promise<IpcResult<{ materials: unknown[] }>>
|
||||
getAll: () => Promise<IpcResult<{ materials: unknown[] }>>
|
||||
getStatistics: () => Promise<IpcResult<{ stats: unknown }>>
|
||||
updateManager: (
|
||||
materialCode: string,
|
||||
managerName: string
|
||||
) => Promise<{ success: boolean; error?: string }>
|
||||
) => Promise<IpcResult<{ updated: boolean }>>
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings API
|
||||
*/
|
||||
export interface SettingsAPI {
|
||||
/**
|
||||
* Get current user type
|
||||
*/
|
||||
getUserType: () => Promise<UserType>
|
||||
/**
|
||||
* Get settings (filtered by user type)
|
||||
*/
|
||||
getSettings: () => Promise<SettingsData>
|
||||
/**
|
||||
* Save settings
|
||||
* @param settings - Settings data to save
|
||||
*/
|
||||
saveSettings: (settings: SettingsData) => Promise<SaveSettingsResult>
|
||||
/**
|
||||
* Reset to defaults (Admin only)
|
||||
*/
|
||||
resetDefaults: () => Promise<SaveSettingsResult>
|
||||
/**
|
||||
* Test ERP connection
|
||||
*/
|
||||
testErpConnection: () => Promise<ConnectionTestResult>
|
||||
/**
|
||||
* Test database connection
|
||||
*/
|
||||
testDbConnection: () => Promise<ConnectionTestResult>
|
||||
getUserType: () => Promise<IpcResult<UserType>>
|
||||
getSettings: () => Promise<IpcResult<{ erp: { username: string; password: string } }>>
|
||||
saveSettings: (settings: { erp?: { username?: string; password?: string } }) => Promise<IpcResult<SaveSettingsResult>>
|
||||
resetDefaults: () => Promise<IpcResult<SaveSettingsResult>>
|
||||
testDbConnection: () => Promise<IpcResult<ConnectionTestResult>>
|
||||
}
|
||||
|
||||
/**
|
||||
* Material Type API
|
||||
*/
|
||||
export interface MaterialTypeAPI {
|
||||
/**
|
||||
* Get all material type records
|
||||
*/
|
||||
getAll: () => Promise<{ success: boolean; data?: MaterialTypeRecord[]; error?: string }>
|
||||
/**
|
||||
* Get material types by manager
|
||||
* @param managerName - Manager name
|
||||
*/
|
||||
getByManager: (
|
||||
managerName: string
|
||||
) => Promise<{ success: boolean; data?: MaterialTypeRecord[]; error?: string }>
|
||||
/**
|
||||
* Get list of managers
|
||||
*/
|
||||
getManagers: () => Promise<{ success: boolean; data?: string[]; error?: string }>
|
||||
/**
|
||||
* Upsert (insert or update) a material type record
|
||||
*/
|
||||
upsert: (
|
||||
materialName: string,
|
||||
managerName: string
|
||||
) => Promise<{ success: boolean; error?: string }>
|
||||
/**
|
||||
* Delete a material type record
|
||||
*/
|
||||
delete: (
|
||||
materialName: string,
|
||||
managerName: string
|
||||
) => Promise<{ success: boolean; error?: string }>
|
||||
/**
|
||||
* Batch operation for material types
|
||||
*/
|
||||
upsertBatch: (request: MaterialTypeBatchRequest) => Promise<{
|
||||
success: boolean
|
||||
stats?: { total: number; success: number; failed: number }
|
||||
error?: string
|
||||
}>
|
||||
getAll: () => Promise<IpcResult<MaterialTypeRecord[]>>
|
||||
getByManager: (managerName: string) => Promise<IpcResult<MaterialTypeRecord[]>>
|
||||
getManagers: () => Promise<IpcResult<string[]>>
|
||||
upsert: (materialName: string, managerName: string) => Promise<IpcResult<{ updated: boolean }>>
|
||||
delete: (materialName: string, managerName: string) => Promise<IpcResult<{ deleted: boolean }>>
|
||||
upsertBatch: (request: MaterialTypeBatchRequest) => Promise<
|
||||
IpcResult<{ stats: { total: number; success: number; failed: number } }>
|
||||
>
|
||||
}
|
||||
|
||||
/**
|
||||
* User ERP Configuration API
|
||||
*/
|
||||
export interface UserErpConfigAPI {
|
||||
/**
|
||||
* Get current user's ERP configuration
|
||||
*/
|
||||
getCurrent: () => Promise<{
|
||||
success: boolean
|
||||
config?: { url: string; username: string; password: string }
|
||||
error?: string
|
||||
}>
|
||||
/**
|
||||
* Update current user's ERP configuration
|
||||
*/
|
||||
update: (config: { url: string; username: string; password: string }) => Promise<{
|
||||
success: boolean
|
||||
config?: { url: string; username: string; password: string }
|
||||
error?: string
|
||||
}>
|
||||
/**
|
||||
* Test ERP connection with provided credentials
|
||||
*/
|
||||
testConnection: (config: { url: string; username: string; password: string }) => Promise<{
|
||||
success: boolean
|
||||
message?: string
|
||||
}>
|
||||
/**
|
||||
* Get all users' ERP configurations (admin only)
|
||||
*/
|
||||
getAll: () => Promise<Array<{ username: string; erpUrl: string; erpUsername: string }>>
|
||||
getCurrent: () => Promise<
|
||||
IpcResult<{
|
||||
config: { url: string; username: string; password: string }
|
||||
}>
|
||||
>
|
||||
update: (config: { url: string; username: string; password: string }) => Promise<
|
||||
IpcResult<{
|
||||
config: { url: string; username: string; password: string }
|
||||
}>
|
||||
>
|
||||
testConnection: (config: { url: string; username: string; password: string }) => Promise<
|
||||
IpcResult<{ message: string }>
|
||||
>
|
||||
getAll: () => Promise<IpcResult<Array<{ username: string; erpUrl: string; erpUsername: string }>>>
|
||||
}
|
||||
|
||||
export interface ProcessAPI {
|
||||
versions: {
|
||||
electron: string
|
||||
chrome: string
|
||||
node: string
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
electron: {
|
||||
ipcRenderer: {
|
||||
send: (channel: string, ...args: unknown[]) => void
|
||||
on: (channel: string, func: (...args: unknown[]) => void) => void
|
||||
once: (channel: string, func: (...args: unknown[]) => void) => void
|
||||
removeListener: (channel: string, func: (...args: unknown[]) => void) => void
|
||||
removeAllListeners: (channel: string) => void
|
||||
invoke: (channel: string, ...args: unknown[]) => Promise<unknown>
|
||||
}
|
||||
process: {
|
||||
versions: {
|
||||
electron: string
|
||||
chrome: string
|
||||
node: string
|
||||
}
|
||||
}
|
||||
process: ProcessAPI
|
||||
file: FileAPI
|
||||
extractor: ExtractorAPI
|
||||
cleaner: CleanerAPI
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { contextBridge, ipcRenderer } from 'electron'
|
||||
import { electronAPI } from '@electron-toolkit/preload'
|
||||
import type { MySqlConfig, SqlServerConfig } from '../main/types/ipc-api.types'
|
||||
import type { ExtractorInput, ExtractionProgress } from '../main/types/extractor.types'
|
||||
import type { CleanerInput, CleanerProgress, ExportResultItem } from '../main/types/cleaner.types'
|
||||
@@ -11,158 +10,202 @@ import type {
|
||||
MaterialTypeRecord,
|
||||
MaterialTypeBatchRequest
|
||||
} from '../main/types/validation.types'
|
||||
import type { SettingsData } from '../main/types/settings.types'
|
||||
import type { IpcResult } from '../main/ipc'
|
||||
import { IPC_CHANNELS } from '../shared/ipc-channels'
|
||||
|
||||
type ErpSettingsPayload = {
|
||||
erp?: {
|
||||
username?: string
|
||||
password?: string
|
||||
}
|
||||
}
|
||||
|
||||
const processApi = {
|
||||
versions: {
|
||||
electron: process.versions.electron,
|
||||
chrome: process.versions.chrome,
|
||||
node: process.versions.node
|
||||
}
|
||||
}
|
||||
|
||||
function invokeIpc<T = unknown>(channel: string, ...args: unknown[]): Promise<IpcResult<T>> {
|
||||
return ipcRenderer.invoke(channel, ...args).then((result: unknown) => {
|
||||
if (result && typeof result === 'object' && 'success' in (result as Record<string, unknown>)) {
|
||||
const typed = result as Record<string, unknown>
|
||||
const hasIpcShape = 'data' in typed || 'code' in typed || 'error' in typed
|
||||
|
||||
if (hasIpcShape) {
|
||||
return typed as unknown as IpcResult<T>
|
||||
}
|
||||
|
||||
if (typed.success === true) {
|
||||
return { success: true, data: result as T }
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
error: typeof typed.error === 'string' ? typed.error : 'IPC operation failed'
|
||||
}
|
||||
}
|
||||
|
||||
return { success: true, data: result as T }
|
||||
})
|
||||
}
|
||||
|
||||
// Custom APIs for renderer
|
||||
const api = {
|
||||
// File operations
|
||||
process: processApi,
|
||||
|
||||
file: {
|
||||
readFile: (filePath: string) => ipcRenderer.invoke('file:read', filePath),
|
||||
readFile: (filePath: string) => invokeIpc(IPC_CHANNELS.FILE_READ, filePath),
|
||||
writeFile: (filePath: string, content: string) =>
|
||||
ipcRenderer.invoke('file:write', filePath, content),
|
||||
fileExists: (filePath: string) => ipcRenderer.invoke('file:exists', filePath),
|
||||
listFiles: (dirPath: string) => ipcRenderer.invoke('file:list', dirPath),
|
||||
openPath: (filePath: string) => ipcRenderer.invoke('file:openPath', filePath)
|
||||
invokeIpc(IPC_CHANNELS.FILE_WRITE, filePath, content),
|
||||
fileExists: (filePath: string) => invokeIpc(IPC_CHANNELS.FILE_EXISTS, filePath),
|
||||
listFiles: (dirPath: string) => invokeIpc(IPC_CHANNELS.FILE_LIST, dirPath),
|
||||
openPath: (filePath: string) => invokeIpc(IPC_CHANNELS.FILE_OPEN_PATH, filePath)
|
||||
},
|
||||
|
||||
// Extractor service
|
||||
extractor: {
|
||||
runExtractor: (input: ExtractorInput) => ipcRenderer.invoke('extractor:run', input),
|
||||
runExtractor: (input: ExtractorInput): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.EXTRACTOR_RUN, input),
|
||||
onProgress: (callback: (data: ExtractionProgress) => void) => {
|
||||
const subscription = (_event: Electron.IpcRendererEvent, data: ExtractionProgress) =>
|
||||
callback(data)
|
||||
ipcRenderer.on('extractor:progress', subscription)
|
||||
return () => ipcRenderer.removeListener('extractor:progress', subscription)
|
||||
ipcRenderer.on(IPC_CHANNELS.EXTRACTOR_PROGRESS, subscription)
|
||||
return () => ipcRenderer.removeListener(IPC_CHANNELS.EXTRACTOR_PROGRESS, subscription)
|
||||
},
|
||||
onLog: (callback: (data: { level: string; message: string }) => void) => {
|
||||
const subscription = (
|
||||
_event: Electron.IpcRendererEvent,
|
||||
data: { level: string; message: string }
|
||||
) => callback(data)
|
||||
ipcRenderer.on('extractor:log', subscription)
|
||||
return () => ipcRenderer.removeListener('extractor:log', subscription)
|
||||
ipcRenderer.on(IPC_CHANNELS.EXTRACTOR_LOG, subscription)
|
||||
return () => ipcRenderer.removeListener(IPC_CHANNELS.EXTRACTOR_LOG, subscription)
|
||||
}
|
||||
},
|
||||
|
||||
// Cleaner service
|
||||
cleaner: {
|
||||
runCleaner: (input: CleanerInput) => ipcRenderer.invoke('cleaner:run', input),
|
||||
exportResults: (items: ExportResultItem[]) =>
|
||||
ipcRenderer.invoke('cleaner:exportResults', items),
|
||||
runCleaner: (input: CleanerInput): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.CLEANER_RUN, input),
|
||||
exportResults: (items: ExportResultItem[]): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.CLEANER_EXPORT_RESULTS, items),
|
||||
onProgress: (callback: (data: CleanerProgress) => void) => {
|
||||
const subscription = (_event: Electron.IpcRendererEvent, data: CleanerProgress) =>
|
||||
callback(data)
|
||||
ipcRenderer.on('cleaner:progress', subscription)
|
||||
return () => ipcRenderer.removeListener('cleaner:progress', subscription)
|
||||
ipcRenderer.on(IPC_CHANNELS.CLEANER_PROGRESS, subscription)
|
||||
return () => ipcRenderer.removeListener(IPC_CHANNELS.CLEANER_PROGRESS, subscription)
|
||||
}
|
||||
},
|
||||
|
||||
// Order number resolver
|
||||
resolver: {
|
||||
resolve: (input: ResolverInput) => ipcRenderer.invoke('resolver:resolve', input),
|
||||
validateFormat: (inputs: string[]) => ipcRenderer.invoke('resolver:validateFormat', inputs)
|
||||
resolve: (input: ResolverInput): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.RESOLVER_RESOLVE, input),
|
||||
validateFormat: (inputs: string[]): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.RESOLVER_VALIDATE_FORMAT, inputs)
|
||||
},
|
||||
|
||||
// Authentication service
|
||||
auth: {
|
||||
getComputerName: () => ipcRenderer.invoke('auth:getComputerName'),
|
||||
silentLogin: () => ipcRenderer.invoke('auth:silentLogin'),
|
||||
login: (request: LoginRequest) => ipcRenderer.invoke('auth:login', request),
|
||||
logout: () => ipcRenderer.invoke('auth:logout'),
|
||||
getCurrentUser: () => ipcRenderer.invoke('auth:getCurrentUser'),
|
||||
getAllUsers: () => ipcRenderer.invoke('auth:getAllUsers'),
|
||||
switchUser: (userInfo: UserInfo) => ipcRenderer.invoke('auth:switchUser', userInfo),
|
||||
isAdmin: () => ipcRenderer.invoke('auth:isAdmin')
|
||||
getComputerName: (): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.AUTH_GET_COMPUTER_NAME),
|
||||
silentLogin: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.AUTH_SILENT_LOGIN),
|
||||
login: (request: LoginRequest): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.AUTH_LOGIN, request),
|
||||
logout: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.AUTH_LOGOUT),
|
||||
getCurrentUser: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.AUTH_GET_CURRENT_USER),
|
||||
getAllUsers: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.AUTH_GET_ALL_USERS),
|
||||
switchUser: (userInfo: UserInfo): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.AUTH_SWITCH_USER, userInfo),
|
||||
isAdmin: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.AUTH_IS_ADMIN)
|
||||
},
|
||||
|
||||
// Database service
|
||||
database: {
|
||||
connectMySql: (config: MySqlConfig) => ipcRenderer.invoke('database:mysql:connect', config),
|
||||
disconnectMySql: () => ipcRenderer.invoke('database:mysql:disconnect'),
|
||||
isMySqlConnected: () => ipcRenderer.invoke('database:mysql:isConnected'),
|
||||
queryMySql: (sql: string, params?: any[]) =>
|
||||
ipcRenderer.invoke('database:mysql:query', sql, params),
|
||||
|
||||
// SQL Server
|
||||
connectSqlServer: (config: SqlServerConfig) =>
|
||||
ipcRenderer.invoke('database:sqlserver:connect', config),
|
||||
disconnectSqlServer: () => ipcRenderer.invoke('database:sqlserver:disconnect'),
|
||||
isSqlServerConnected: () => ipcRenderer.invoke('database:sqlserver:isConnected'),
|
||||
querySqlServer: (sql: string, params?: Record<string, unknown>) =>
|
||||
ipcRenderer.invoke('database:sqlserver:query', sql, params)
|
||||
connectMySql: (config: MySqlConfig): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.DATABASE_MYSQL_CONNECT, config),
|
||||
disconnectMySql: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.DATABASE_MYSQL_DISCONNECT),
|
||||
isMySqlConnected: (): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.DATABASE_MYSQL_IS_CONNECTED),
|
||||
queryMySql: (sql: string, params?: any[]): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.DATABASE_MYSQL_QUERY, sql, params),
|
||||
connectSqlServer: (config: SqlServerConfig): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.DATABASE_SQLSERVER_CONNECT, config),
|
||||
disconnectSqlServer: (): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.DATABASE_SQLSERVER_DISCONNECT),
|
||||
isSqlServerConnected: (): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.DATABASE_SQLSERVER_IS_CONNECTED),
|
||||
querySqlServer: (sql: string, params?: Record<string, unknown>): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.DATABASE_SQLSERVER_QUERY, sql, params)
|
||||
},
|
||||
|
||||
// Validation service
|
||||
validation: {
|
||||
validate: (request: ValidationRequest) => ipcRenderer.invoke('validation:validate', request),
|
||||
setSharedProductionIds: (productionIds: string[]) =>
|
||||
ipcRenderer.invoke('validation:setSharedProductionIds', productionIds),
|
||||
getSharedProductionIds: () => ipcRenderer.invoke('validation:getSharedProductionIds'),
|
||||
getCleanerData: () => ipcRenderer.invoke('validation:getCleanerData')
|
||||
validate: (request: ValidationRequest): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.VALIDATION_VALIDATE, request),
|
||||
setSharedProductionIds: (productionIds: string[]): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.VALIDATION_SET_SHARED_PRODUCTION_IDS, productionIds),
|
||||
getSharedProductionIds: (): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.VALIDATION_GET_SHARED_PRODUCTION_IDS),
|
||||
getCleanerData: (): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.VALIDATION_GET_CLEANER_DATA)
|
||||
},
|
||||
|
||||
// Materials service
|
||||
materials: {
|
||||
upsertBatch: (materials: { materialCode: string; managerName: string }[]) =>
|
||||
ipcRenderer.invoke('materials:upsertBatch', { materials }),
|
||||
delete: (materialCodes: string[]) => ipcRenderer.invoke('materials:delete', { materialCodes }),
|
||||
getManagers: () => ipcRenderer.invoke('materials:getManagers'),
|
||||
getByManager: (managerName: string) =>
|
||||
ipcRenderer.invoke('materials:getByManager', managerName),
|
||||
getAll: () => ipcRenderer.invoke('materials:getAll'),
|
||||
getStatistics: () => ipcRenderer.invoke('materials:getStatistics'),
|
||||
updateManager: (materialCode: string, managerName: string) =>
|
||||
ipcRenderer.invoke('materials:updateManager', { materialCode, managerName })
|
||||
upsertBatch: (materials: { materialCode: string; managerName: string }[]): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.MATERIALS_UPSERT_BATCH, { materials }),
|
||||
delete: (materialCodes: string[]): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.MATERIALS_DELETE, { materialCodes }),
|
||||
getManagers: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.MATERIALS_GET_MANAGERS),
|
||||
getByManager: (managerName: string): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.MATERIALS_GET_BY_MANAGER, managerName),
|
||||
getAll: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.MATERIALS_GET_ALL),
|
||||
getStatistics: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.MATERIALS_GET_STATISTICS),
|
||||
updateManager: (materialCode: string, managerName: string): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.MATERIALS_UPDATE_MANAGER, { materialCode, managerName })
|
||||
},
|
||||
|
||||
// Settings service
|
||||
settings: {
|
||||
getUserType: () => ipcRenderer.invoke('settings:getUserType'),
|
||||
getSettings: () => ipcRenderer.invoke('settings:getSettings'),
|
||||
saveSettings: (settings: SettingsData) => ipcRenderer.invoke('settings:saveSettings', settings),
|
||||
resetDefaults: () => ipcRenderer.invoke('settings:resetDefaults'),
|
||||
testErpConnection: () => ipcRenderer.invoke('settings:testErpConnection'),
|
||||
testDbConnection: () => ipcRenderer.invoke('settings:testDbConnection')
|
||||
getUserType: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.SETTINGS_GET_USER_TYPE),
|
||||
getSettings: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.SETTINGS_GET_SETTINGS),
|
||||
saveSettings: (settings: ErpSettingsPayload): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.SETTINGS_SAVE_SETTINGS, settings),
|
||||
resetDefaults: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.SETTINGS_RESET_DEFAULTS),
|
||||
testDbConnection: (): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.SETTINGS_TEST_DB_CONNECTION)
|
||||
},
|
||||
|
||||
// Material Type service
|
||||
materialType: {
|
||||
getAll: () => ipcRenderer.invoke('materialType:getAll'),
|
||||
getByManager: (managerName: string) =>
|
||||
ipcRenderer.invoke('materialType:getByManager', managerName),
|
||||
getManagers: () => ipcRenderer.invoke('materialType:getManagers'),
|
||||
upsert: (materialName: string, managerName: string) =>
|
||||
ipcRenderer.invoke('materialType:upsert', { materialName, managerName }),
|
||||
delete: (materialName: string, managerName: string) =>
|
||||
ipcRenderer.invoke('materialType:delete', { materialName, managerName }),
|
||||
upsertBatch: (request: MaterialTypeBatchRequest) =>
|
||||
ipcRenderer.invoke('materialType:upsertBatch', request)
|
||||
getAll: (): Promise<IpcResult<MaterialTypeRecord[]>> =>
|
||||
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_GET_ALL),
|
||||
getByManager: (managerName: string): Promise<IpcResult<MaterialTypeRecord[]>> =>
|
||||
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_GET_BY_MANAGER, managerName),
|
||||
getManagers: (): Promise<IpcResult<string[]>> =>
|
||||
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_GET_MANAGERS),
|
||||
upsert: (materialName: string, managerName: string): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_UPSERT, { materialName, managerName }),
|
||||
delete: (materialName: string, managerName: string): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_DELETE, { materialName, managerName }),
|
||||
upsertBatch: (request: MaterialTypeBatchRequest): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.MATERIAL_TYPE_UPSERT_BATCH, request)
|
||||
},
|
||||
|
||||
// User ERP Configuration service
|
||||
userErpConfig: {
|
||||
getCurrent: () => ipcRenderer.invoke('user-erp-config:getCurrent'),
|
||||
update: (config: { url: string; username: string; password: string }) =>
|
||||
ipcRenderer.invoke('user-erp-config:update', config),
|
||||
testConnection: (config: { url: string; username: string; password: string }) =>
|
||||
ipcRenderer.invoke('user-erp-config:testConnection', config),
|
||||
getAll: () => ipcRenderer.invoke('user-erp-config:getAll')
|
||||
getCurrent: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.USER_ERP_CONFIG_GET_CURRENT),
|
||||
update: (config: { url: string; username: string; password: string }): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.USER_ERP_CONFIG_UPDATE, config),
|
||||
testConnection: (config: { url: string; username: string; password: string }): Promise<IpcResult> =>
|
||||
invokeIpc(IPC_CHANNELS.USER_ERP_CONFIG_TEST_CONNECTION, config),
|
||||
getAll: (): Promise<IpcResult> => invokeIpc(IPC_CHANNELS.USER_ERP_CONFIG_GET_ALL)
|
||||
}
|
||||
} as const
|
||||
|
||||
// Use `contextBridge` APIs to expose Electron APIs to
|
||||
// renderer only if context isolation is enabled, otherwise
|
||||
// just add to the DOM global.
|
||||
if (process.contextIsolated) {
|
||||
try {
|
||||
contextBridge.exposeInMainWorld('electron', { ...electronAPI, ...api })
|
||||
contextBridge.exposeInMainWorld('electron', api)
|
||||
contextBridge.exposeInMainWorld('api', api)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
} else {
|
||||
// @ts-ignore (define in dts)
|
||||
window.electron = { ...electronAPI, ...api }
|
||||
window.electron = api
|
||||
// @ts-ignore (define in dts)
|
||||
window.api = api
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user