v1.0.0
This commit is contained in:
506
ws/package-lock.json
generated
506
ws/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "WS Sheep Service",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.1",
|
||||
"main": "ws.js",
|
||||
"scripts": {
|
||||
"start": "node ws.js"
|
||||
@@ -12,6 +12,7 @@
|
||||
"dependencies": {
|
||||
"sqlite3": "^5.1.7",
|
||||
"url": "^0.11.4",
|
||||
"ws": "^8.18.0"
|
||||
"ws": "^8.18.0",
|
||||
"dotenv": "^17.2.0"
|
||||
}
|
||||
}
|
||||
|
||||
224
ws/ws.js
224
ws/ws.js
@@ -2,11 +2,12 @@ const WebSocket = require("ws");
|
||||
const { URL } = require('url');
|
||||
const sqlite3 = require('sqlite3');
|
||||
const path = require('path');
|
||||
require("dotenv").config();
|
||||
|
||||
const dbPath = process.env.DATABASE_PATH || '../';
|
||||
const db = new sqlite3.Database(path.join(dbPath, 'database.sqlite'));
|
||||
|
||||
const port = 4001;
|
||||
const port = process.env.WS_PORT || 4001;
|
||||
const api_version = '1.0.0';
|
||||
|
||||
const wss = new WebSocket.Server({
|
||||
@@ -20,13 +21,13 @@ wss.on('connection', async (ws, request) => {
|
||||
const uuid = params.uuid;
|
||||
|
||||
if (!uuid) return
|
||||
|
||||
|
||||
let check = await checkUUID(uuid);
|
||||
|
||||
console.log(check);
|
||||
|
||||
|
||||
if (!check && (check.can_view_territory || check.administrator.id || check.moderator.id)) return
|
||||
|
||||
if (!check && check.possibilities.can_view_territory) return
|
||||
|
||||
// Periodic ping to maintain a connection
|
||||
const pingInterval = setInterval(() => {
|
||||
@@ -49,7 +50,7 @@ wss.on('connection', async (ws, request) => {
|
||||
break;
|
||||
|
||||
case "message":
|
||||
updateDatabase(message, check.sheep_id);
|
||||
updateDatabase(message);
|
||||
broadcastMessage(message);
|
||||
break;
|
||||
};
|
||||
@@ -77,121 +78,146 @@ function broadcastMessage(message) {
|
||||
});
|
||||
}
|
||||
|
||||
function updateDatabase(message, sheep_id) {
|
||||
let sql = 'UPDATE apartments SET status = ?, description = ?, sheep_id = ?, updated_at = ? WHERE id = ?';
|
||||
function updateDatabase(message) {
|
||||
|
||||
db.run(sql, [
|
||||
Number(message.data.status),
|
||||
message.data.description,
|
||||
sheep_id,
|
||||
message.data.updated_at,
|
||||
message.data.id
|
||||
], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
} else if (this.changes === 0) {
|
||||
console.error('Product not found');
|
||||
} else {
|
||||
console.log({ "update": "ok", "id": message.data.id });
|
||||
console.log(message.type);
|
||||
|
||||
|
||||
let sql = `INSERT INTO apartments_history (apartments_id, status, description, sheep_id, created_at) VALUES (?, ?, ?, ?, ?)`;
|
||||
if (message.type === "apartment") {
|
||||
|
||||
db.run(sql, [
|
||||
Number(message.data.id),
|
||||
Number(message.data.status),
|
||||
message.data.description,
|
||||
sheep_id,
|
||||
message.data.updated_at
|
||||
], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
} else if (this.changes === 0) {
|
||||
console.error('Apartments not found');
|
||||
} else {
|
||||
console.log({ "insert": "ok", "id": this.lastID });
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
let sql = 'UPDATE apartments SET status = ?, description = ?, sheep_id = ?, updated_at = ? WHERE id = ?';
|
||||
|
||||
db.run(sql, [
|
||||
Number(message.data.status),
|
||||
message.data.description,
|
||||
message.data.sheep_id,
|
||||
message.data.updated_at,
|
||||
message.data.id
|
||||
], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
} else if (this.changes === 0) {
|
||||
console.error('Product not found');
|
||||
} else {
|
||||
console.log({ "update": "ok", "id": message.data.id });
|
||||
|
||||
let sql = `INSERT INTO apartments_history (apartments_id, status, description, sheep_id, created_at) VALUES (?, ?, ?, ?, ?)`;
|
||||
|
||||
db.run(sql, [
|
||||
Number(message.data.id),
|
||||
Number(message.data.status),
|
||||
message.data.description,
|
||||
message.data.sheep_id,
|
||||
message.data.updated_at
|
||||
], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
} else if (this.changes === 0) {
|
||||
console.error('Apartments not found');
|
||||
} else {
|
||||
console.log({ "insert": "ok", "id": this.lastID });
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else if(message.type === "building") {
|
||||
let sql = 'UPDATE buildings SET status = ?, description = ?, sheep_id = ?, updated_at = ? WHERE id = ?';
|
||||
|
||||
db.run(sql, [
|
||||
Number(message.data.status),
|
||||
message.data.description,
|
||||
message.data.sheep_id,
|
||||
message.data.updated_at,
|
||||
message.data.id
|
||||
], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
} else if (this.changes === 0) {
|
||||
console.error('Product not found');
|
||||
} else {
|
||||
console.log({ "update": "ok", "id": message.data.id });
|
||||
|
||||
let sql = `INSERT INTO buildings_history (buildings_id, status, description, sheep_id, created_at) VALUES (?, ?, ?, ?, ?)`;
|
||||
|
||||
db.run(sql, [
|
||||
Number(message.data.id),
|
||||
Number(message.data.status),
|
||||
message.data.description,
|
||||
message.data.sheep_id,
|
||||
message.data.updated_at
|
||||
], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
} else if (this.changes === 0) {
|
||||
console.error('Apartments not found');
|
||||
} else {
|
||||
console.log({ "insert": "ok", "id": this.lastID });
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function checkUUID(uuid) {
|
||||
return new Promise((res, rej) => {
|
||||
db.get(`
|
||||
SELECT sheeps.*, administrators.* FROM administrators JOIN sheeps ON sheeps.id = administrators.sheep_id WHERE administrators.uuid = ?`,
|
||||
SELECT
|
||||
sheeps.*,
|
||||
possibilities.can_view_territory AS can_view_territory
|
||||
FROM
|
||||
sheeps
|
||||
LEFT JOIN
|
||||
possibilities ON possibilities.sheep_id = sheeps.id
|
||||
WHERE
|
||||
sheeps.uuid_manager = ?`,
|
||||
[uuid],
|
||||
(err, administrator) => {
|
||||
if (administrator) {
|
||||
(err, moderator) => {
|
||||
if (moderator) {
|
||||
let data = {
|
||||
"id": Number(administrator.id),
|
||||
"group_id": Number(administrator.group_id),
|
||||
"name": administrator.name,
|
||||
"icon": administrator.icon,
|
||||
"uuid": administrator.uuid,
|
||||
"appointment": administrator.appointment,
|
||||
"can_view_stand": administrator.can_view_stand == 0 ? false : true,
|
||||
"can_view_schedule": administrator.can_view_schedule == 0 ? false : true,
|
||||
"can_view_territory": administrator.can_view_territory == 0 ? false : true,
|
||||
"administrator": {
|
||||
"id": administrator.administrators_id ? administrator.administrators_id : false
|
||||
},
|
||||
"moderator": {
|
||||
"id": administrator.administrators_id ? administrator.administrators_id : false
|
||||
id: moderator.sheep_id,
|
||||
group_id: moderator.group_id,
|
||||
name: moderator.name,
|
||||
icon: moderator.icon,
|
||||
uuid: moderator.uuid,
|
||||
appointment: moderator.appointment,
|
||||
sheepRole: moderator.mode_title,
|
||||
possibilities: {
|
||||
can_view_territory: moderator.can_view_territory == 1 ? true : false
|
||||
}
|
||||
}
|
||||
return res(data);
|
||||
}
|
||||
|
||||
|
||||
db.get(`
|
||||
SELECT sheeps.*, moderators.* FROM moderators JOIN sheeps ON sheeps.id = moderators.sheep_id WHERE moderators.uuid = ?`,
|
||||
SELECT
|
||||
sheeps.*,
|
||||
possibilities.can_view_territory AS can_view_territory
|
||||
FROM
|
||||
sheeps
|
||||
LEFT JOIN
|
||||
possibilities ON possibilities.sheep_id = sheeps.id
|
||||
WHERE
|
||||
sheeps.uuid = ?`,
|
||||
[uuid],
|
||||
(err, moderator) => {
|
||||
if (moderator) {
|
||||
(err, sheep) => {
|
||||
if (sheep) {
|
||||
let data = {
|
||||
"id": Number(moderator.id),
|
||||
"group_id": Number(moderator.group_id),
|
||||
"name": moderator.name,
|
||||
"icon": moderator.icon,
|
||||
"uuid": moderator.uuid,
|
||||
"appointment": moderator.appointment,
|
||||
"can_view_stand": moderator.can_view_stand == 0 ? false : true,
|
||||
"can_view_schedule": moderator.can_view_schedule == 0 ? false : true,
|
||||
"can_view_territory": moderator.can_view_territory == 0 ? false : true,
|
||||
"administrator": {
|
||||
"id": moderator.administrators_id ? moderator.administrators_id : false
|
||||
},
|
||||
"moderator": {
|
||||
"id": moderator.moderators_id ? moderator.moderators_id : false
|
||||
id: sheep.sheep_id,
|
||||
group_id: sheep.group_id,
|
||||
name: sheep.name,
|
||||
icon: sheep.icon,
|
||||
uuid: sheep.uuid,
|
||||
appointment: sheep.appointment,
|
||||
sheepRole: sheep.mode_title,
|
||||
possibilities: {
|
||||
can_view_territory: sheep.can_view_territory == 1 ? true : false
|
||||
}
|
||||
}
|
||||
return res(data);
|
||||
}
|
||||
|
||||
db.get(`SELECT sheeps.* FROM sheeps WHERE sheeps.uuid = ?`, [uuid],(err, sheep) => {
|
||||
if (sheep) {
|
||||
let data = {
|
||||
"id": Number(sheep.id),
|
||||
"group_id": Number(sheep.group_id),
|
||||
"name": sheep.name,
|
||||
"icon": sheep.icon,
|
||||
"uuid": sheep.uuid,
|
||||
"appointment": sheep.appointment,
|
||||
"can_view_stand": sheep.can_view_stand == 0 ? false : true,
|
||||
"can_view_schedule": sheep.can_view_schedule == 0 ? false : true,
|
||||
"can_view_territory": sheep.can_view_territory == 0 ? false : true,
|
||||
"administrator": {
|
||||
"id": sheep.administrators_id ? sheep.administrators_id : false
|
||||
},
|
||||
"moderator": {
|
||||
"id": sheep.moderators_id ? sheep.moderators_id : false
|
||||
}
|
||||
}
|
||||
return res(sheep);
|
||||
}
|
||||
|
||||
return res(false);
|
||||
}
|
||||
);
|
||||
|
||||
return res(false);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user