Archived
1
0
Fork 0

Add list, set service and fix user list for service page

This commit is contained in:
Ethanell 2020-05-30 18:47:39 +02:00
parent d537c47cbf
commit 2650833a9b
8 changed files with 128 additions and 35 deletions

View file

@ -1,4 +1,5 @@
"use strict";
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
username: {
@ -6,7 +7,11 @@ module.exports = (sequelize, DataTypes) => {
primaryKey: true
},
passwordHash: {
type: DataTypes.STRING
type: DataTypes.STRING,
set(value) {
if (value)
this.setDataValue("passwordHash", require("crypto").createHash("sha256").update(this.username + value).digest("utf-8"));
}
},
firstName: {
type: DataTypes.STRING,

View file

@ -1,32 +1,40 @@
let socket = io();
let users = {};
let usersAdd = [];
document.querySelectorAll("input[type='text']").forEach(e => {
e.addEventListener("keyup", ev => {hinter(ev)})
});
function addUser(username) {
let firstName, lastName;
do {
firstName = prompt("First name for " + username);
} while (firstName === "");
if (firstName) {
do {
lastName = prompt("Last name for " + username);
} while (lastName === "");
if (lastName)
socket.emit("add user", {username: username, firstName: firstName, lastName: lastName});
}
if (!firstName|| !lastName)
alert("User creation aborted for " + username);
}
document.querySelector("button").addEventListener("click", ev => {
let users= {};
document.querySelectorAll("input[type='text']").forEach(e=> {
if (e.style.color)
socket.emit("add user", {"username": e.value, "firstname": prompt(`Prénom pour ${e.value}`), "lastname": prompt(`Nom pour ${e.value}`), "password": prompt(`MDP pour ${e.value}`)});
users[e.id] = e.value;
});
socket.emit("set service", users);
window.close();
});
function next() {
if (usersAdd.length)
addUser(usersAdd.pop());
else
socket.emit("set service", users);
}
function hinter(ev) {
let input = ev.target;
let min_characters = 0;
if (input.value.length < min_characters)
return;
socket.emit("list users");
socket.emit("list user", input.value);
}
socket.on("connect", data => {
if (data === "ok") {
socket.emit("list service");
}
socket.on("connected", () => {
socket.emit("list service");
});
socket.on("list service", data => {
@ -35,18 +43,50 @@ socket.on("list service", data => {
}
});
socket.on("list users", data => {
socket.on("list user", data => {
let user_list = document.getElementById("user_list");
user_list.innerHTML = "";
for (let u of data["list"]) {
for (let u of data) {
user_list.insertAdjacentHTML("beforeend", `<option value="${u}">`);
}
document.querySelectorAll("input[type='text']").forEach(e => {
if (data["list"].indexOf(e.value) === -1)
document.querySelectorAll("input[type='text']:focus").forEach(e => {
if (data.indexOf(e.value) === -1)
e.style.color = "red";
else {
e.style.color = "";
}
});
});
socket.on("add user", data => {
alert("User creation fail !");
addUser(data);
});
socket.on("set service", () => {
console.log("close !")
window.close();
})
socket.on("fail add user", data => {
alert("User creation fail !");
addUser(data);
});
socket.on("internal error", () => {
alert("An error occurred !");
})
document.querySelectorAll("input[type='text']").forEach(e => {
e.addEventListener("keyup", ev => {hinter(ev)})
});
document.querySelector("button").addEventListener("click", () => {
document.querySelectorAll("input[type='text']").forEach(e=> {
if (e.style.color)
usersAdd.push(e.value);
users[e.id] = e.value;
});
next();
});

View file

@ -3,22 +3,22 @@ const models = require("../models");
module.exports = socket => {
return async (data) => {
try {
let u;
try {
u = await models.User.create({
await models.User.create({
username: data.username,
firstName: data.firstName,
lastName: data.lastName
})
lastName: data.lastName,
passwordHash: data.password ? data.password : null
});
} catch (e) {
if (e instanceof models.Sequelize.ValidationError)
socket.emit("fail add user")
socket.emit("fail add user");
else
throw e;
}
socket.emit("add user", u.username);
socket.broadcast.emit("add user", u.username);
socket.emit("add user", data.username);
socket.broadcast.emit("add user", data.username);
} catch (e) {
socket.emit("internal error");
console.error(e);

View file

@ -5,8 +5,10 @@ const listSauce = require("./listSauce");
const listDrink = require("./listDrink");
const listDessert = require("./listDessert");
const listUser = require("./listUser");
const listService = require("./listService");
const addCommand = require("./addCommand");
const addUSer = require("./addUser")
const addUSer = require("./addUser");
const setService = require("./setService");
const giveCommand = require("./giveCommand");
const errorCommand = require("./errorCommand");
const clearCommand = require("./clearCommand");
@ -21,8 +23,10 @@ module.exports = socket => {
socket.on("list dessert", listDessert(socket));
socket.on("list dessert", listDessert(socket));
socket.on("list user", listUser(socket));
socket.on("list service", listService(socket));
socket.on("add command", addCommand(socket));
socket.on("add user", addUSer(socket));
socket.on("set service", setService(socket));
socket.on("give command", giveCommand(socket));
socket.on("error command", errorCommand(socket));
socket.on("clear command", clearCommand(socket));

24
sockets/listService.js Normal file
View file

@ -0,0 +1,24 @@
const models = require("../models");
module.exports = socket => {
return async () => {
let send = null;
let service = await models.Service.findOne({
where: {
date: {
[models.Sequelize.Op.eq]: new Date()
}
},
include: ["sandwich1", "sandwich2", "sandwich3", "commi1", "commi2"]
})
if (service)
send = {
sandwich1: service.sandwich1 ? service.sandwich1.username : null,
sandwich2: service.sandwich2 ? service.sandwich2.username : null,
sandwich3: service.sandwich3 ? service.sandwich3.username : null,
commi1: service.commi1 ? service.commi1.username: null,
commi2: service.commi2 ? service.commi2.username : null
}
socket.emit("list service", send);
}
}

20
sockets/setService.js Normal file
View file

@ -0,0 +1,20 @@
const models = require("../models");
module.exports = socket => {
return async (data) => {
try {
let s = await models.Service.findOne({where: {date: {[models.Sequelize.Op.eq]: new Date()}}, include: ["sandwich1", "sandwich2", "sandwich3", "commi1", "commi2"]})
if (!s)
s = await models.Service.create();
for (let u in data)
await s["set"+u.charAt(0).toUpperCase()+u.slice(1)](data[u]);
socket.emit("set service", data);
socket.broadcast.emit("set service", data);
} catch (e) {
socket.emit("internal error");
console.error(e);
}
}
}

View file

@ -26,7 +26,7 @@ block content
button.contact2-form-btn Logout
div#main-container.bg-contact2(style="background-image: url(/images/bg-01.jpg);height: 93vh")
form.container(action="#")
form.container(action="")
div#dish
h1 Dish
ul
@ -51,7 +51,7 @@ block content
p#p-dessert
input#user(list="user_list" type="text" placeholder="Username")
datalist#user_list
button.validation(type="submit") Confirm
button.validation(type="button") Confirm
h2 0€
div.list

View file

@ -7,7 +7,7 @@ block content
li Back to commands
div#main-container.bg-contact2(style="background-image: url(/images/bg-01.jpg); height: 93vh;")
form#stocks.container(action="#")
form#stocks.container(action="")
div#teams
div#sandwich
label(for="sandwich1") Sandwich 1
@ -25,7 +25,7 @@ block content
label(for="commi2") Commi 2
input#commi2(list="user_list" type="text")
div#okteam
button.contact2-form-btn Accept
button(type="button").contact2-form-btn Accept
datalist#user_list
script(src="/socket.io/socket.io.js")