First commit

This commit is contained in:
Ninjdai 2023-12-06 08:46:58 +01:00
commit 5a8e65a471
9 changed files with 270 additions and 0 deletions

55
index.js Normal file
View File

@ -0,0 +1,55 @@
const { launchWeb } = require('./web.js');
const { EventEmitter } = require('events');
const Sequelize = require('sequelize');
const sequelize = new Sequelize("database", "user", "password", {
host: "localhost",
dialect: "sqlite",
logging: false,
storage: "database.sqlite",
});
const contactsDB = sequelize.define("contacts", {
phone: {
type: Sequelize.STRING,
primaryKey: true,
},
firstName: {
type: Sequelize.STRING,
},
lastName: {
type: Sequelize.STRING,
},
called: {
type: Sequelize.INTEGER,// 0: not called - 1: called - 2: ongoing call (- 3: no response ?)
defaultValue: false,
},
vote: {
type: Sequelize.BOOLEAN,
defaultValue: false,
}
});
contactsDB.sync();
global.database = {};
global.database.contacts = contactsDB
const submitEvent = new EventEmitter();
launchWeb(submitEvent);
submitEvent.on('call', async (call) => {
let vote;
if(call.vote) {
console.log(`${call.phone} va voter`);
vote = 1;
} else {
console.log(`${call.phone} ne va pas voter`);
vote = 0;
}
await global.database.contacts.update({ vote: vote }, { where: { phone: call.phone } })
});
submitEvent.on('add', (request) => {
console.log(request);
});

16
package.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "autocallmenu",
"version": "1.0.0",
"description": "Website for automatic phone number displaying from a database",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ninjdai",
"license": "MIT",
"dependencies": {
"express": "^4.18.2",
"sequelize": "^6.35.1",
"sqlite3": "^5.1.6"
}
}

0
src/database/addUser.js Normal file
View File

13
src/database/getUser.js Normal file
View File

@ -0,0 +1,13 @@
async function getRandomUser() {
const user = await global.database.contacts.findOne({ where: { called: 0}});
if (!user || user == 0) return false;
await global.database.contacts.update({ called: 2 }, { where: { phone: user.dataValues.phone}});
const count = await global.database.contacts.count({ where: {called: 0} });
return {
name: `${user.dataValues.firstName} ${user.dataValues.lastName}`,
phone: user.dataValues.phone,
count: count,
};
}
module.exports = { getRandomUser };

19
src/html/callHTML.js Normal file
View File

@ -0,0 +1,19 @@
function generateCallHTML(phoneNumber, name, callcount) {
const head = `<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><link rel="stylesheet" type="text/css" href="/assets/css/form.css" /><title>AutoCallMenu</title></head>`;
const form = `
<form action="/inputs/call" method="post">
<input type="hidden" name="phone" value="${phoneNumber}">
<label class="switch">
<input type="checkbox" name="vote">
<span class="slider round"></span>
</label>
<label for="vote">Vote ?</label>
<br>
<button class="submit" type="submit">CONFIRMER</button>
</form>
`;
const body = `<body><h1>Bienvenue :3</h1><h2>Appel: ${name} - ${phoneNumber}</h2><p>${callcount} appels restants</p>${form}</body>`;
return `<!DOCTYPE html><html>${head}${body}</html>`;
}
module.exports = { generateCallHTML };

41
web.js Normal file
View File

@ -0,0 +1,41 @@
const express = require('express');
const { readFile } = require('fs').promises;
const { generateCallHTML } = require('./src/html/callHTML.js');
const { getRandomUser } = require('./src/database/getUser.js');
function launchWeb(submitEvent){
const app = express();
app.use(express.json()); // Used to parse JSON bodies
app.use(express.urlencoded({ extended: false })); //Parse URL-encoded bodies
app.use('/assets', express.static('./www/assets'))
app.get('/', async (request, response) => {
response.send(await readFile('./www/index.html', 'utf8'));
});
app.get('/call', async (request, response) => {
response.send(await generateCallResponse());
});
app.post('/inputs/call', async (request, response) => {
submitEvent.emit('call', request.body);
response.send(await generateCallResponse());
});
app.post('/inputs/add', async (request, response) => {
submitEvent.emit('add', request);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => { console.log(`App available at http://localhost:${PORT}`) });
};
async function generateCallResponse(){
const user = await getRandomUser();
let callHTML;
if(user) callHTML = generateCallHTML( user.phone, user.name, user.count );
else callHTML = `Plus d'utilisateur à appeler !`;
return callHTML;
}
module.exports = { launchWeb };

73
www/assets/css/form.css Normal file
View File

@ -0,0 +1,73 @@
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
.submit {
background-color: #04AA6D;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
}
.submit:hover {
opacity: 0.8;
}

40
www/assets/js/form.js Normal file
View File

@ -0,0 +1,40 @@
//Get the form element by id
const callForm = document.getElementById("call-form");
callForm.addEventListener("submit", async (e) => {
e.preventDefault();
let form = e.currentTarget;
let url = form.action;
try {
let formData = new FormData(form);
console.log(form)
console.log(formData)
let responseData = await postFormFieldsAsJson({ url, formData });
let { serverDataResponse } = responseData;
console.log(serverDataResponse);
} catch (error) {
console.error(error);
}
});
async function postFormFieldsAsJson({ url, formData }) {
let formDataObject = Object.fromEntries(formData.entries());
let formDataJsonString = JSON.stringify(formDataObject);
let fetchOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: formDataJsonString,
};
console.log(fetchOptions);
let res = await fetch(url, fetchOptions);
if (!res.ok) {
let error = await res.text();
throw new Error(error);
}
return res.json();
}

13
www/index.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>AutoCallMenu</title>
</head>
<body>
<h1>Bienvenue :3</h1>
<a href="/call"><p>Accéder aux téléphones</p></a>
</body>
</html>