-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (106 loc) · 3.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const { app, BrowserWindow, ipcMain, Notification } = require("electron");
const isDev = require("electron-is-dev");
const url = require("url");
const path = require("path");
// * mongoDB url
const dotenv = require("dotenv");
dotenv.config();
const MONGODB_URL = process.env["MONGODB_URL"];
// ? for future ref:
// ? .env file => gitignore => KEY="VALUE" => npm i dotenv => import => process.env
// * db
const ingoingRecord = require("./models/ingoingRecord");
const outgoingRecord = require("./models/outgoingRecord");
const otRecord = require("./models/otRecord");
const mongoose = require("mongoose");
var mainWindow; // global
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1500,
height: 1200,
webPreferences: {
nodeIntegration: true,
},
});
// mainWindow.webContents.openDevTools();
// console.log("bawa ji"); //vscode terminal
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, "src/build/index.html"),
protocol: "file:",
slashes: true,
})
);
mainWindow.setMenu(null);
}
app.whenReady().then(() => {
createWindow();
app.on("activate", function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on("window-all-closed", function () {
if (process.platform !== "darwin") app.quit();
});
// CREATE
ipcMain.on("message", (event, arg) => {
console.log("EVENT FIRED...");
console.log(arg.variant);
insertRecords(arg).then(() =>
new Notification({
title: "Notification",
body: "New Patient Record Inserted!",
}).show()
);
});
async function insertRecords(arg) {
mongoose.connect(MONGODB_URL, { useUnifiedTopology: true });
if (arg.variant === "ingoing") {
await ingoingRecord.create(arg);
console.log("added records to ingoing");
} else if (arg.variant === "outgoing") {
await outgoingRecord.create(arg);
console.log("added records to outgoing");
} else if (arg.variant === "ot") {
await otRecord.create(arg);
console.log("added records to ot");
}
}
/// END CREATE
/// READ
ipcMain.on("fetchIngoing", (event, arg) => {
getRecords("ingoingRecord").then((allRecords) => {
console.log("SENDING THIS:");
console.log(allRecords);
mainWindow.webContents.send("sendIngoing", allRecords);
});
});
ipcMain.on("fetchOutgoing", (event, arg) => {
getRecords("outgoingRecord").then((allRecords) => {
console.log("SENDING THIS:");
console.log(allRecords);
mainWindow.webContents.send("sendOutgoing", allRecords);
});
});
ipcMain.on("fetchOt", (event, arg) => {
getRecords("otRecord").then((allRecords) => {
// console.log("SENDING THIS:");
// console.log(allRecords);
mainWindow.webContents.send("sendOt", allRecords);
});
});
async function getRecords(variant) {
mongoose.connect(MONGODB_URL, { useUnifiedTopology: true });
if (variant == "ingoingRecord") {
return await ingoingRecord.find({});
// console.log(records);
} else if (variant == "outgoingRecord") {
console.log("HERE");
return await outgoingRecord.find({});
// console.log(records);
} else if (variant == "otRecord") {
return await otRecord.find({});
}
}
/// END READ