-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2f1c649
Showing
54 changed files
with
14,822 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// npm install express, ejs, mongoose, body-parser | ||
// use yarn to go fast | ||
|
||
var bodyParser = require('body-parser'); | ||
const express = require('express'); | ||
const mongoose = require('mongoose'); | ||
const dotenv = require('dotenv'); | ||
const cookieParser = require('cookie-parser'); | ||
|
||
|
||
dotenv.config(); | ||
const app = express(); | ||
|
||
|
||
app.set('view engine', 'ejs'); | ||
|
||
|
||
let connect_uri = process.env.MONGO_DB; | ||
mongoose.connect(connect_uri, | ||
{ useNewUrlParser: true}, | ||
() => { | ||
console.log("DB Address --> " + connect_uri); | ||
} | ||
); | ||
|
||
|
||
// Import Routes | ||
const IndexRoute = require('./routes/index'); | ||
const AccountRoute = require('./routes/account'); | ||
const MenuRoute = require('./routes/menu'); | ||
const ApiRoute = require('./routes/api'); | ||
const AuthRoute = require('./routes/auth'); | ||
|
||
|
||
// Middlewares | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
app.use(cookieParser()); | ||
|
||
|
||
// Route Middlewares | ||
app.use('/', IndexRoute); | ||
app.use('/account', AccountRoute); | ||
app.use('/menu', MenuRoute); | ||
app.use('/api', ApiRoute); | ||
app.use('/auth', AuthRoute); | ||
|
||
|
||
// set owr public directory | ||
app.use(express.static('public')); | ||
|
||
|
||
const port = process.env.PORT; | ||
app.listen(port, () => { | ||
console.log("Server is running."); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// il tool dei cookie jwt | ||
|
||
|
||
const jwt = require('jsonwebtoken'); | ||
const dotenv = require('dotenv'); | ||
|
||
dotenv.config(); | ||
|
||
|
||
function generateAccessToken(data) { | ||
// scadenza 1800s = 30min | ||
return jwt.sign(data, process.env.TOKEN_SECRET, { expiresIn: '4800s' }); | ||
} | ||
|
||
function autorization(req, res, next) { | ||
const token = req.cookies.access_token; | ||
if (!token) { | ||
return res.redirect("/auth?token=not_found"); | ||
} | ||
try { | ||
// proviamo a verificare il token | ||
const data = jwt.verify(token, process.env.TOKEN_SECRET); | ||
req.user_id = data.user_id; | ||
return next(); | ||
} catch { | ||
// se è sbagliato vuol dire che non era valido | ||
return res.redirect("/auth?token=not_valid"); | ||
} | ||
} | ||
|
||
module.exports = {generateAccessToken, autorization} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const fetch = require('node-fetch'); | ||
|
||
async function VerifyRecaptcha(req, res, next) { | ||
|
||
const recaptcha_response = req.body["g-recaptcha-response"]; | ||
const validateRecaptcha = await fetch( | ||
'https://www.google.com/recaptcha/api/siteverify?secret=' + process.env.RECAPTCHA_PRIVATE_KEY + '&response=' + recaptcha_response, | ||
{ | ||
method: "POST", | ||
} | ||
).then(_res => _res.json()); | ||
|
||
if(validateRecaptcha.success === true) { return next(); } | ||
else { return res.redirect("/auth?google-recaptcha=invalid"); } | ||
|
||
} | ||
|
||
module.exports = {VerifyRecaptcha} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const sharp = require('sharp'); | ||
const { v4: uuidv4 } = require('uuid'); | ||
const path = require('path'); | ||
|
||
class Resizer { | ||
|
||
constructor(folder) { | ||
this.folder = folder; | ||
} | ||
|
||
async save(buffer) { | ||
const filename = Resizer.filename(); | ||
const filepath = this.filepath(filename); | ||
|
||
await sharp(buffer) | ||
// il resizer salva l'immagine già ritagliata | ||
.resize(600, 600, { | ||
fit: sharp.fit.inside, | ||
withoutEnlargement: true | ||
}) | ||
.toFile(filepath); | ||
|
||
return filename; | ||
} | ||
|
||
static filename() { | ||
return `${uuidv4()}.png`; | ||
} | ||
|
||
filepath(filename) { | ||
return path.resolve(`${this.folder}/${filename}`) | ||
} | ||
|
||
} | ||
module.exports = Resizer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function array_move(arr, old_index, new_index) { | ||
if (new_index >= arr.length) { | ||
var k = new_index - arr.length + 1; | ||
while (k--) { | ||
arr.push(undefined); | ||
} | ||
} | ||
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]); | ||
return arr; // for testing | ||
}; | ||
|
||
module.exports = { array_move } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
var days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']; | ||
|
||
|
||
/* | ||
const dayInfo = { | ||
day: "", | ||
dayOpen: true, | ||
morning: true, | ||
morningStart: "", | ||
morningEnd: "", | ||
afternoon: true, | ||
afternoonStart: "", | ||
afternoonEnd: "", | ||
allDay: true, | ||
allDayStart: "", | ||
allDayEnd: "", | ||
} | ||
*/ | ||
|
||
|
||
function checkOrari(orariObj) { | ||
var nDay = 0; | ||
try { | ||
for(orariSingle of orariObj) { | ||
// controllo nome corretto | ||
if(orariSingle.day != days[nDay]) { return false; } | ||
nDay++; | ||
if(checkDay(orariSingle) == false) { return false; } | ||
// controllo validità orari | ||
} | ||
} catch(e) { | ||
return false; | ||
} | ||
|
||
// missione compiuta | ||
return true; | ||
} | ||
|
||
|
||
function checkDay(day) { | ||
// valid parte true e se arriva false vuol dire che qualcuno la ha modificata! | ||
var valid = true; | ||
|
||
try { | ||
if(day.allDay == true) { | ||
if(day.morning || day.afternoon) { valid = false; } | ||
const allDayStart = setHoursAndMinutes(day.allDayStart); | ||
const allDayEnd = setHoursAndMinutes(day.allDayEnd); | ||
if (allDayStart >= allDayEnd) { | ||
valid = false; | ||
} | ||
} else { | ||
const morningStart = setHoursAndMinutes(day.morningStart); | ||
const morningEnd = setHoursAndMinutes(day.morningEnd); | ||
const afternoonStart = setHoursAndMinutes(day.afternoonStart); | ||
const afternoonEnd = setHoursAndMinutes(day.afternoonEnd); | ||
|
||
if(day.morning == true) { | ||
if (morningStart >= morningEnd) { | ||
valid = false; | ||
} | ||
} | ||
if(day.afternoon == true) { | ||
if (afternoonStart >= afternoonEnd) { | ||
valid = false; | ||
} | ||
} | ||
|
||
if( day.morning == true && day.afternoon == true ) { | ||
if (morningEnd >= afternoonStart) { | ||
valid = false; | ||
} | ||
} | ||
} | ||
} catch(e) { | ||
valid = false; | ||
} | ||
|
||
// missione compiuta | ||
return valid; | ||
} | ||
|
||
|
||
|
||
function setHoursAndMinutes(timeString) { | ||
var date = new Date(); | ||
var timeParts = timeString.split(':'); | ||
var hours = parseInt(timeParts[0], 10); | ||
var minutes = parseInt(timeParts[1], 10); | ||
|
||
date.setHours(hours); | ||
date.setMinutes(minutes); | ||
return date; | ||
} | ||
|
||
|
||
|
||
module.exports = {checkOrari} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const crypto = require('crypto'); | ||
const randomstring = require('randomstring'); | ||
|
||
|
||
function verifyCodeGenerator() { | ||
let hash = crypto.createHash('md5').update(randomstring.generate(16)).digest("hex"); | ||
return hash; | ||
} | ||
|
||
module.exports = {verifyCodeGenerator}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const orariSchema = mongoose.Schema({ | ||
enable: { | ||
// indica se il prenotatore è attivo oppure no | ||
type: Boolean, | ||
required: true, | ||
default: false, | ||
}, | ||
orariObjString: { | ||
type: String, | ||
required: true, | ||
default: '[{"day":"monday","dayOpen":true,"morning":true,"afternoon":true,"allDay":false,"morningStart":"08:30","morningEnd":"13:00","afternoonStart":"13:30","afternoonEnd":"16:30","allDayStart":"13:30","allDayEnd":"16:30"},{"day":"tuesday","dayOpen":true,"morning":true,"afternoon":true,"allDay":false,"morningStart":"08:30","morningEnd":"13:00","afternoonStart":"13:30","afternoonEnd":"16:30","allDayStart":"13:30","allDayEnd":"16:30"},{"day":"wednesday","dayOpen":true,"morning":true,"afternoon":true,"allDay":false,"morningStart":"08:30","morningEnd":"13:00","afternoonStart":"13:30","afternoonEnd":"16:30","allDayStart":"13:30","allDayEnd":"16:30"},{"day":"thursday","dayOpen":true,"morning":true,"afternoon":true,"allDay":false,"morningStart":"08:30","morningEnd":"13:00","afternoonStart":"13:30","afternoonEnd":"16:30","allDayStart":"13:30","allDayEnd":"16:30"},{"day":"friday","dayOpen":true,"morning":true,"afternoon":true,"allDay":false,"morningStart":"08:30","morningEnd":"13:00","afternoonStart":"13:30","afternoonEnd":"16:30","allDayStart":"13:30","allDayEnd":"16:30"},{"day":"saturday","dayOpen":true,"morning":true,"afternoon":true,"allDay":false,"morningStart":"08:30","morningEnd":"13:00","afternoonStart":"13:30","afternoonEnd":"16:30","allDayStart":"13:30","allDayEnd":"16:30"},{"day":"sunday","dayOpen":true,"morning":true,"afternoon":true,"allDay":false,"morningStart":"08:30","morningEnd":"13:00","afternoonStart":"13:30","afternoonEnd":"16:30","allDayStart":"13:30","allDayEnd":"16:30"}]', | ||
max: 2500, | ||
}, | ||
user_id: { | ||
// collegamento con l'user_id dell'utente a cui appartiene | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: true, | ||
default: null, | ||
}, | ||
menu_id: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Menu', | ||
required: true, | ||
}, | ||
}, {timestamps: true}); | ||
|
||
const Orari = mongoose.model('Orari', orariSchema, 'Orari'); | ||
module.exports = Orari; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const mongoose = require('mongoose'); | ||
mongoose instanceof mongoose.Mongoose; | ||
|
||
const piattoSchema = mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true, | ||
min: 5, | ||
max: 50 | ||
}, | ||
price: { | ||
type: String, | ||
max: 8, | ||
required: true | ||
}, | ||
description: { | ||
type: String, | ||
max: 200, | ||
required: false, | ||
default: "" | ||
}, | ||
owner_id: { | ||
// questo campo conterrà l'id del proprietario, USER | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: false, | ||
default: null, | ||
}, | ||
}, {timestamps: true}); | ||
|
||
const Piatto = mongoose.model('Piatto', piattoSchema, 'Piatto'); | ||
module.exports = Piatto; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const prenotatoreSchema = mongoose.Schema({ | ||
enable: { | ||
// indica se il prenotatore è attivo oppure no | ||
type: Boolean, | ||
required: true, | ||
default: false | ||
}, | ||
call: { | ||
// indica se il pulsante chiamata è attivo oppure no | ||
type: Boolean, | ||
required: true, | ||
default: false | ||
}, | ||
call_number: { | ||
type: String, | ||
required: false, | ||
min: 5, | ||
max: 100 | ||
}, | ||
whatsapp:{ | ||
// indica se il tasto whatsapp è attivo oppure no | ||
type: Boolean, | ||
required: true, | ||
default: false | ||
}, | ||
whatsapp_number: { | ||
type: String, | ||
required: false, | ||
min: 5, | ||
max: 100 | ||
}, | ||
user_id: { | ||
// collegamento con l'user_id dell'utente a cui appartiene | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: true, | ||
default: null, | ||
}, | ||
menu_id: { | ||
// collegamento con l'user_id dell'utente a cui appartiene | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Menu', | ||
required: true, | ||
}, | ||
}, {timestamps: true}); | ||
|
||
const Prenotatore = mongoose.model('Prenotatore', prenotatoreSchema, 'Prenotatore'); | ||
module.exports = Prenotatore; |
Oops, something went wrong.