184 lines
6.8 KiB
JavaScript
184 lines
6.8 KiB
JavaScript
import { app, BrowserWindow, shell, ipcMain } from 'electron'
|
|
import { fileURLToPath } from 'node:url'
|
|
import path from 'node:path'
|
|
import {
|
|
fetchEntries,
|
|
fetchTravellers,
|
|
createEntry,
|
|
updateEntry,
|
|
deleteEntry,
|
|
fetchVaccines,
|
|
fetchVaccineRecords,
|
|
saveVaccineRecord,
|
|
updateVaccineRecord,
|
|
} from './pocketbase.js'
|
|
import { JUVONNO_API_URL, JUVONNO_API_KEY } from './juvonno-config.js'
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
|
|
// ─── Paths ──────────────────────────────────────────────────────────────────
|
|
// Compiled main lives in dist-electron/main/ — go up two levels to reach dist/
|
|
const RENDERER_DIST = path.join(__dirname, '../../dist')
|
|
// Preload is compiled to dist-electron/preload/index.cjs (explicitly CJS)
|
|
const PRELOAD_PATH = path.join(__dirname, '../preload/index.cjs')
|
|
|
|
// ─── Dev server URL ─────────────────────────────────────────────────────────
|
|
// vite-plugin-electron sets this env var when running in development mode.
|
|
const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL
|
|
|
|
// ─── Window reference ────────────────────────────────────────────────────────
|
|
let mainWindow
|
|
|
|
function createWindow() {
|
|
mainWindow = new BrowserWindow({
|
|
width: 1280,
|
|
height: 800,
|
|
minWidth: 900,
|
|
minHeight: 600,
|
|
title: 'CTM Concierge',
|
|
show: false, // show after ready-to-show to prevent visual flash
|
|
backgroundColor: '#0f1117',
|
|
webPreferences: {
|
|
// ── Security defaults ──────────────────────────────────────────────
|
|
contextIsolation: true, // Isolates renderer from Electron internals
|
|
nodeIntegration: false, // Renderer cannot access Node.js APIs directly
|
|
sandbox: false, // Must be false so preload can use require('electron') for contextBridge
|
|
webSecurity: true,
|
|
// ── Preload ────────────────────────────────────────────────────────
|
|
preload: PRELOAD_PATH,
|
|
},
|
|
})
|
|
|
|
// Gracefully show window once the DOM is ready to avoid white flash
|
|
mainWindow.once('ready-to-show', () => {
|
|
mainWindow.show()
|
|
})
|
|
|
|
// Open external links in the system browser, not Electron
|
|
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
|
|
if (url.startsWith('https:') || url.startsWith('http:')) {
|
|
shell.openExternal(url)
|
|
}
|
|
return { action: 'deny' }
|
|
})
|
|
|
|
// ── Load the app ─────────────────────────────────────────────────────────
|
|
if (VITE_DEV_SERVER_URL) {
|
|
// Development: load from Vite HMR dev server
|
|
mainWindow.loadURL(VITE_DEV_SERVER_URL)
|
|
mainWindow.webContents.openDevTools()
|
|
} else {
|
|
// Production: load compiled static files
|
|
mainWindow.loadFile(path.join(RENDERER_DIST, 'index.html'))
|
|
}
|
|
}
|
|
|
|
// ─── App lifecycle ───────────────────────────────────────────────────────────
|
|
app.whenReady().then(() => {
|
|
createWindow()
|
|
|
|
// macOS: re-create window when dock icon is clicked and no windows are open
|
|
app.on('activate', () => {
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
createWindow()
|
|
}
|
|
})
|
|
})
|
|
|
|
// Quit when all windows are closed (except on macOS)
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit()
|
|
mainWindow = null
|
|
}
|
|
})
|
|
|
|
// ─── IPC Handlers ────────────────────────────────────────────────────────────
|
|
// Register main-process IPC handlers here.
|
|
// Example: ipcMain.handle('channel-name', async (event, ...args) => { ... })
|
|
|
|
ipcMain.handle('app:get-version', () => app.getVersion())
|
|
ipcMain.handle('pb:fetch-entries', async () => {
|
|
return await fetchEntries()
|
|
})
|
|
ipcMain.handle('pb:fetch-travellers', async () => {
|
|
return await fetchTravellers()
|
|
})
|
|
ipcMain.handle('pb:create-entry', async (_event, trip) => {
|
|
return await createEntry(trip)
|
|
})
|
|
ipcMain.handle('pb:update-entry', async (_event, id, trip) => {
|
|
return await updateEntry(id, trip)
|
|
})
|
|
ipcMain.handle('pb:delete-entry', async (_event, id) => {
|
|
return await deleteEntry(id)
|
|
})
|
|
|
|
ipcMain.handle('pb:fetch-vaccines', async () => {
|
|
return await fetchVaccines()
|
|
})
|
|
|
|
ipcMain.handle('pb:fetch-vaccine-records', async () => {
|
|
return await fetchVaccineRecords()
|
|
})
|
|
|
|
ipcMain.handle('pb:save-vaccine-record', async (_event, recordData) => {
|
|
return await saveVaccineRecord(recordData)
|
|
})
|
|
|
|
ipcMain.handle('pb:update-vaccine-record', async (_event, id, recordData) => {
|
|
return await updateVaccineRecord(id, recordData)
|
|
})
|
|
|
|
// ─── Health Risk Audit IPC Handler (Node-side to avoid CORS) ────────────────
|
|
ipcMain.handle('audit:run-health-audit', async (_event, payload) => {
|
|
const AUDIT_WEBHOOK_URL = 'https://n8n.wirediv.dev/webhook/travel-health-audit'
|
|
const AUDIT_API_KEY = '63350077-643e-40d8-8d34-378495c81203'
|
|
|
|
const response = await fetch(AUDIT_WEBHOOK_URL, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-API-Key': AUDIT_API_KEY,
|
|
},
|
|
body: JSON.stringify(payload),
|
|
})
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Audit webhook returned HTTP ${response.status} ${response.statusText}`)
|
|
}
|
|
|
|
return await response.json()
|
|
})
|
|
|
|
// ─── Juvonno EMR IPC Handler (Node-side to avoid CORS) ──────────────────────
|
|
ipcMain.handle('juvonno:fetch-chart', async (_event, emrId) => {
|
|
if (!emrId) {
|
|
throw new Error('EMR ID is required.')
|
|
}
|
|
|
|
const url = `${JUVONNO_API_URL}/customers/chart/${encodeURIComponent(emrId)}`
|
|
const response = await fetch(url, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'X-API-Key': JUVONNO_API_KEY,
|
|
},
|
|
})
|
|
|
|
if (!response.ok) {
|
|
if (response.status === 404) {
|
|
throw new Error(`Customer chart with EMR ID "${emrId}" not found in Juvonno.`)
|
|
}
|
|
if (response.status === 403 || response.status === 401) {
|
|
throw new Error(`Juvonno authentication failed (HTTP ${response.status}). Please verify the X-API-Key in juvonno-config.js.`)
|
|
}
|
|
throw new Error(`Juvonno API error: HTTP ${response.status} ${response.statusText}`)
|
|
}
|
|
|
|
return await response.json()
|
|
})
|
|
|
|
|
|
|