v0.0.1
This commit is contained in:
396
api/services/sheeps.service.js
Normal file
396
api/services/sheeps.service.js
Normal file
@@ -0,0 +1,396 @@
|
||||
const crypto = require('crypto');
|
||||
const db = require("../config/db");
|
||||
|
||||
class SheepService {
|
||||
getSheep(uuid, sheepRole) {
|
||||
return new Promise((res, rej) => {
|
||||
let sql = `
|
||||
SELECT
|
||||
sheeps.*,
|
||||
groups.group_number AS group_id,
|
||||
administrators.id AS administrators_id,
|
||||
administrators.uuid AS administrators_uuid,
|
||||
moderators.id AS moderators_id,
|
||||
moderators.uuid AS moderators_uuid,
|
||||
moderators.can_add_sheeps,
|
||||
moderators.can_add_territory,
|
||||
moderators.can_manager_territory,
|
||||
moderators.can_add_stand,
|
||||
moderators.can_manager_stand,
|
||||
moderators.can_add_schedule
|
||||
FROM
|
||||
sheeps
|
||||
LEFT JOIN
|
||||
groups ON groups.group_number = sheeps.group_id
|
||||
LEFT JOIN
|
||||
administrators ON administrators.sheep_id = sheeps.id
|
||||
LEFT JOIN
|
||||
moderators ON moderators.sheep_id = sheeps.id
|
||||
WHERE
|
||||
sheeps.uuid = ?
|
||||
LIMIT 1;
|
||||
`
|
||||
db.get(sql, [uuid], (err, sheep) => {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
return res(false);
|
||||
} else if (!sheep) {
|
||||
console.log({ "error": "uuid not found" });
|
||||
return res(false);
|
||||
} else {
|
||||
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,
|
||||
"uuid": null
|
||||
},
|
||||
"moderator": {
|
||||
"id": sheep.moderators_id ? sheep.moderators_id : false,
|
||||
"uuid": null,
|
||||
"can_add_sheeps": sheep.can_add_sheeps == 1 ? true : false,
|
||||
"can_add_territory": sheep.can_add_territory == 1 ? true : false,
|
||||
"can_manager_territory": sheep.can_manager_territory == 1 ? true : false,
|
||||
"can_add_stand": sheep.can_add_stand == 1 ? true : false,
|
||||
"can_manager_stand": sheep.can_manager_stand == 1 ? true : false,
|
||||
"can_add_schedule": sheep.can_add_schedule == 1 ? true : false
|
||||
}
|
||||
}
|
||||
|
||||
if (sheepRole == "administrator") {
|
||||
if (sheep.administrators_id) {
|
||||
data.administrator.uuid = sheep.administrators_uuid;
|
||||
}
|
||||
if (sheep.moderators_id) {
|
||||
data.moderator.uuid = sheep.moderators_uuid;
|
||||
}
|
||||
}
|
||||
|
||||
return res(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
getList(sheepRole) {
|
||||
return new Promise((res, rej) => {
|
||||
let sql = `
|
||||
SELECT
|
||||
sheeps.*,
|
||||
groups.group_number AS group_id,
|
||||
administrators.id AS administrators_id,
|
||||
administrators.uuid AS administrators_uuid,
|
||||
moderators.id AS moderators_id,
|
||||
moderators.uuid AS moderators_uuid,
|
||||
moderators.can_add_sheeps,
|
||||
moderators.can_add_territory,
|
||||
moderators.can_manager_territory,
|
||||
moderators.can_add_stand,
|
||||
moderators.can_manager_stand,
|
||||
moderators.can_add_schedule
|
||||
FROM
|
||||
sheeps
|
||||
LEFT JOIN
|
||||
groups ON groups.group_number = sheeps.group_id
|
||||
LEFT JOIN
|
||||
administrators ON administrators.sheep_id = sheeps.id
|
||||
LEFT JOIN
|
||||
moderators ON moderators.sheep_id = sheeps.id
|
||||
ORDER BY
|
||||
id
|
||||
`;
|
||||
|
||||
db.all(sql, (err, sheeps) => {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
return res(false);
|
||||
} else {
|
||||
let result = sheeps.map((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,
|
||||
"uuid": null
|
||||
},
|
||||
"moderator": {
|
||||
"id": sheep.moderators_id ? sheep.moderators_id : false,
|
||||
"uuid": null,
|
||||
"can_add_sheeps": sheep.can_add_sheeps == 1 ? true : false,
|
||||
"can_add_territory": sheep.can_add_territory == 1 ? true : false,
|
||||
"can_manager_territory": sheep.can_manager_territory == 1 ? true : false,
|
||||
"can_add_stand": sheep.can_add_stand == 1 ? true : false,
|
||||
"can_manager_stand": sheep.can_manager_stand == 1 ? true : false,
|
||||
"can_add_schedule": sheep.can_add_schedule == 1 ? true : false
|
||||
}
|
||||
}
|
||||
|
||||
if (sheepRole == "administrator") {
|
||||
if (sheep.administrators_id) {
|
||||
data.administrator.uuid = sheep.administrators_uuid;
|
||||
}
|
||||
if (sheep.moderators_id) {
|
||||
data.moderator.uuid = sheep.moderators_uuid;
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
})
|
||||
|
||||
return res(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
getAdministrator(uuid) {
|
||||
return new Promise((res, rej) => {
|
||||
let sql = `
|
||||
SELECT
|
||||
sheeps.*,
|
||||
groups.group_number AS group_id,
|
||||
administrators.id AS administrators_id,
|
||||
administrators.uuid AS administrators_uuid
|
||||
FROM
|
||||
sheeps
|
||||
JOIN
|
||||
administrators ON sheeps.id = administrators.sheep_id
|
||||
LEFT JOIN
|
||||
groups ON groups.group_number = sheeps.group_id
|
||||
WHERE
|
||||
administrators.uuid = ?
|
||||
LIMIT 1;
|
||||
`
|
||||
db.get(sql, [uuid], (err, sheep) => {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
return res(false);
|
||||
} else if (!sheep) {
|
||||
console.log({ "error": "uuid not found" });
|
||||
return res(false);
|
||||
} else {
|
||||
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,
|
||||
"uuid": sheep.administrators_uuid
|
||||
},
|
||||
"moderator": false
|
||||
}
|
||||
|
||||
return res(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
getModerator(uuid) {
|
||||
return new Promise((res, rej) => {
|
||||
let sql = `
|
||||
SELECT
|
||||
sheeps.*,
|
||||
groups.group_number AS group_id,
|
||||
moderators.id AS moderators_id,
|
||||
moderators.uuid AS moderators_uuid,
|
||||
moderators.can_add_sheeps AS can_add_sheeps,
|
||||
moderators.can_add_territory AS can_add_territory,
|
||||
moderators.can_manager_territory AS can_manager_territory,
|
||||
moderators.can_add_stand AS can_add_stand,
|
||||
moderators.can_manager_stand AS can_manager_stand,
|
||||
moderators.can_add_schedule AS can_add_schedule
|
||||
FROM
|
||||
sheeps
|
||||
JOIN
|
||||
moderators ON sheeps.id = moderators.sheep_id
|
||||
LEFT JOIN
|
||||
groups ON groups.group_number = sheeps.group_id
|
||||
WHERE
|
||||
moderators.uuid = ?
|
||||
LIMIT 1;
|
||||
`
|
||||
db.get(sql, [uuid], (err, sheep) => {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
return res(false);
|
||||
} else if (!sheep) {
|
||||
console.log({ "error": "uuid not found" });
|
||||
return res(false);
|
||||
} else {
|
||||
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": false,
|
||||
"moderator": {
|
||||
"id": sheep.moderators_id,
|
||||
"uuid": sheep.moderators_uuid,
|
||||
"can_add_sheeps": sheep.can_add_sheeps == 0 ? false : true,
|
||||
"can_add_territory": sheep.can_add_territory == 0 ? false : true,
|
||||
"can_manager_territory": sheep.can_manager_territory == 0 ? false : true,
|
||||
"can_add_stand": sheep.can_add_stand == 0 ? false : true,
|
||||
"can_manager_stand": sheep.can_manager_stand == 0 ? false : true,
|
||||
"can_add_schedule": sheep.can_add_schedule == 0 ? false : true
|
||||
}
|
||||
}
|
||||
|
||||
return res(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
createSheep(data) {
|
||||
return new Promise((res, rej) => {
|
||||
let sql = 'INSERT INTO sheeps(name, group_id, appointment, uuid) VALUES (?, ?, ?, ?)';
|
||||
|
||||
let uuid = crypto.randomUUID();
|
||||
|
||||
db.run(sql, [
|
||||
data.name,
|
||||
Number(data.group_id),
|
||||
data.appointment,
|
||||
uuid
|
||||
], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
return res(false);
|
||||
} else if (this.changes === 0) {
|
||||
return res(false);
|
||||
} else {
|
||||
res({ "status": "ok", "id": this.lastID, "uuid": uuid });
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
updateSheep(data) {
|
||||
return new Promise(async (res, rej) => {
|
||||
try {
|
||||
let sql = `
|
||||
UPDATE sheeps
|
||||
SET name = ?, group_id = ?, appointment = ?,
|
||||
can_view_stand = ?, can_view_schedule = ?, can_view_territory = ?
|
||||
WHERE uuid = ?
|
||||
`;
|
||||
|
||||
await db.run(sql, [
|
||||
data.name,
|
||||
Number(data.group_id),
|
||||
data.appointment,
|
||||
data.can_view_stand ? 1 : 0,
|
||||
data.can_view_schedule ? 1 : 0,
|
||||
data.can_view_territory ? 1 : 0,
|
||||
data.uuid
|
||||
]);
|
||||
|
||||
if (data.role === "administrator") {
|
||||
if (!data.administrator?.id) {
|
||||
await db.run(
|
||||
'INSERT INTO administrators(sheep_id, uuid) VALUES (?, ?)',
|
||||
[data.id, crypto.randomUUID()]
|
||||
);
|
||||
console.log({ insert: "ok" });
|
||||
}
|
||||
if (data.moderator?.id) {
|
||||
await db.run('DELETE FROM moderators WHERE id = ?', [data.moderator.id]);
|
||||
console.log({ delete: "ok" });
|
||||
}
|
||||
} else if (data.role === "moderator") {
|
||||
if (!data.moderator?.id) {
|
||||
await db.run(
|
||||
`INSERT INTO moderators(sheep_id, can_add_sheeps, can_add_territory,
|
||||
can_manager_territory, can_add_stand, can_manager_stand,
|
||||
can_add_schedule, uuid) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[
|
||||
data.id,
|
||||
data.moderator.can_add_sheeps ? 1 : 0,
|
||||
data.moderator.can_add_territory ? 1 : 0,
|
||||
data.moderator.can_manager_territory ? 1 : 0,
|
||||
data.moderator.can_add_stand ? 1 : 0,
|
||||
data.moderator.can_manager_stand ? 1 : 0,
|
||||
data.moderator.can_add_schedule ? 1 : 0,
|
||||
crypto.randomUUID()
|
||||
]
|
||||
);
|
||||
console.log({ insert: "ok" });
|
||||
} else {
|
||||
await db.run(
|
||||
`UPDATE moderators
|
||||
SET can_add_sheeps = ?, can_add_territory = ?,
|
||||
can_manager_territory = ?, can_add_stand = ?,
|
||||
can_manager_stand = ?, can_add_schedule = ?
|
||||
WHERE id = ?`,
|
||||
[
|
||||
data.moderator.can_add_sheeps ? 1 : 0,
|
||||
data.moderator.can_add_territory ? 1 : 0,
|
||||
data.moderator.can_manager_territory ? 1 : 0,
|
||||
data.moderator.can_add_stand ? 1 : 0,
|
||||
data.moderator.can_manager_stand ? 1 : 0,
|
||||
data.moderator.can_add_schedule ? 1 : 0,
|
||||
data.moderator.id
|
||||
]
|
||||
);
|
||||
console.log({ update: "ok" });
|
||||
}
|
||||
if (data.administrator?.id) {
|
||||
await db.run('DELETE FROM administrators WHERE id = ?', [data.administrator.id]);
|
||||
console.log({ delete: "ok" });
|
||||
}
|
||||
} else if (data.role === "sheep") {
|
||||
if (data.moderator?.id) {
|
||||
await db.run('DELETE FROM moderators WHERE id = ?', [data.moderator.id]);
|
||||
console.log({ delete: "ok" });
|
||||
}
|
||||
if (data.administrator?.id) {
|
||||
await db.run('DELETE FROM administrators WHERE id = ?', [data.administrator.id]);
|
||||
console.log({ delete: "ok" });
|
||||
}
|
||||
}
|
||||
|
||||
res({ status: "ok", id: data.id });
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
rej(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
deleteSheep(data) {
|
||||
return new Promise((res, rej) => {
|
||||
db.run('DELETE FROM sheeps WHERE uuid = ?', [data.uuid], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
return res(false);
|
||||
} else if (this.changes === 0) {
|
||||
return res(false);
|
||||
} else {
|
||||
res({ "dellete": "ok" });
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new SheepService();
|
||||
Reference in New Issue
Block a user