Додана сторінка "Стенд"
Додане повідомлення про оновлення застосунку Оновлен Service Worker Перероблен WebSocket APІ
This commit is contained in:
@@ -1,26 +1,61 @@
|
||||
const db = require("../config/db");
|
||||
|
||||
function lockingStand(user, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const sheepId = Number(data.sheep_id) || null;
|
||||
|
||||
if (!user.possibilities.can_view_stand) {
|
||||
return reject(new Error("Forbidden: no rights to view stand"));
|
||||
}
|
||||
if ((sheepId !== user.id && sheepId !== null) && !user.possibilities.can_manager_stand) {
|
||||
return reject(new Error("Forbidden: no rights to view stand"));
|
||||
}
|
||||
|
||||
resolve({ update: "ok", id: data.id });
|
||||
});
|
||||
}
|
||||
|
||||
function unlockingStand(user, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const sheepId = Number(data.sheep_id) || null;
|
||||
|
||||
if (!user.possibilities.can_view_stand) {
|
||||
return reject(new Error("Forbidden: no rights to view stand"));
|
||||
}
|
||||
if ((sheepId !== user.id && sheepId !== null) && !user.possibilities.can_manager_stand) {
|
||||
return reject(new Error("Forbidden: no rights to view stand"));
|
||||
}
|
||||
|
||||
resolve({ update: "ok", id: data.id });
|
||||
});
|
||||
}
|
||||
|
||||
function updateStand(user, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!user.possibilities.can_manager_territory) {
|
||||
return reject(new Error("Forbidden: no rights to manage territory"));
|
||||
const sheepId = Number(data.sheep_id) || null;
|
||||
|
||||
if (!user.possibilities.can_view_stand) {
|
||||
return reject(new Error("Forbidden: no rights to view stand"));
|
||||
}
|
||||
if ((sheepId !== user.id && sheepId !== null) && !user.possibilities.can_manager_stand) {
|
||||
return reject(new Error("Forbidden: no rights to view stand"));
|
||||
}
|
||||
|
||||
const sql = `
|
||||
UPDATE buildings
|
||||
SET status = ?, description = ?, sheep_id = ?, updated_at = ?
|
||||
UPDATE stand_schedule
|
||||
SET sheep_id = ?, updated_at = ?
|
||||
WHERE id = ?`;
|
||||
|
||||
db.run(sql, [Number(data.status), data.description, data.sheep_id, data.updated_at, data.id], function (err) {
|
||||
db.run(sql, [sheepId, Date.now(), data.id], function (err) {
|
||||
if (err) return reject(err);
|
||||
if (this.changes === 0) return reject(new Error("Building not found"));
|
||||
if (this.changes === 0) return reject(new Error("Stand not found"));
|
||||
|
||||
const insertSql = `
|
||||
INSERT INTO buildings_history
|
||||
(buildings_id, status, description, sheep_id, created_at)
|
||||
VALUES (?, ?, ?, ?, ?)`;
|
||||
INSERT INTO stand_schedule_history
|
||||
(stand_schedule_id, sheep_id, created_at)
|
||||
VALUES (?, ?, ?)`;
|
||||
|
||||
db.run(insertSql, [Number(data.id), Number(data.status), data.description, data.sheep_id, Date.now()], function (err) {
|
||||
db.run(insertSql, [Number(data.id), sheepId, Date.now()], function (err) {
|
||||
if (err) return reject(err);
|
||||
resolve({ update: "ok", id: data.id, historyId: this.lastID });
|
||||
});
|
||||
@@ -28,4 +63,4 @@ function updateStand(user, data) {
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { updateStand };
|
||||
module.exports = { lockingStand, unlockingStand, updateStand };
|
||||
Reference in New Issue
Block a user