2022-09-07 10:36:30 +00:00
|
|
|
const bodyParser = require('body-parser');
|
2022-09-07 17:08:49 +00:00
|
|
|
const child_process = require('child_process');
|
|
|
|
const cors = require('cors');
|
2022-09-07 10:36:30 +00:00
|
|
|
const express = require('express');
|
|
|
|
require('express-async-errors');
|
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
const path = require('path');
|
|
|
|
const sqlite3 = require('sqlite3').verbose();
|
|
|
|
|
2022-09-11 10:45:48 +00:00
|
|
|
const {authPassword, accessTokenSecret, dbFile, catclient} = require('./config');
|
2022-09-07 10:36:30 +00:00
|
|
|
|
2022-09-11 10:45:48 +00:00
|
|
|
const db = new sqlite3.Database(dbFile, err => {
|
2022-09-07 10:36:30 +00:00
|
|
|
if (err) {
|
|
|
|
return console.error(err.message);
|
|
|
|
}
|
|
|
|
console.log('Connected to the database.');
|
|
|
|
});
|
|
|
|
|
|
|
|
function makeTable(table) {
|
|
|
|
db.run(table, err => {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
makeTable(`
|
|
|
|
CREATE TABLE IF NOT EXISTS Chikinz (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
bandId INTEGER NOT NULL,
|
|
|
|
weight REAL NOT NULL,
|
2022-09-07 17:08:49 +00:00
|
|
|
killDate DATE,
|
|
|
|
wrapDate DATE NOT NULL,
|
|
|
|
sellDate DATE,
|
2022-09-07 10:36:30 +00:00
|
|
|
misc TEXT
|
|
|
|
);`);
|
|
|
|
|
|
|
|
makeTable(`
|
|
|
|
CREATE TABLE IF NOT EXISTS Bandz (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
name TEXT,
|
2022-09-07 17:08:49 +00:00
|
|
|
receiveDate DATE,
|
2022-09-07 10:36:30 +00:00
|
|
|
misc TEXT
|
|
|
|
);`);
|
|
|
|
|
|
|
|
function addChikin(db, chikin) {
|
2022-09-11 13:33:51 +00:00
|
|
|
const sql_insert = 'INSERT INTO Chikinz '+
|
|
|
|
'(bandId, weight, killDate, wrapDate, wounded) ' +
|
|
|
|
'VALUES (?, ?, ?, ?, ?);';
|
|
|
|
const {bandId, weight, killDate, wrapDate, wounded} = chikin;
|
2022-09-07 10:36:30 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-09-11 13:33:51 +00:00
|
|
|
db.run(sql_insert,
|
|
|
|
[bandId, weight, killDate, wrapDate, wounded],
|
|
|
|
function(err) {
|
2022-09-07 10:36:30 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve(this);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBandz(db) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const bandz = [];
|
|
|
|
db.each('SELECT id, name from Bandz', (err, row) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
bandz.push(row);
|
|
|
|
}, (err, rows) => {
|
|
|
|
if (bandz.length !== rows) {
|
|
|
|
return reject('did not store all rows');
|
|
|
|
}
|
|
|
|
resolve(bandz);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-07 17:08:49 +00:00
|
|
|
function getChikin(db, chikinId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.get('SELECT * from Chikinz where id=?', chikinId, (err, row) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve(row);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-07 10:36:30 +00:00
|
|
|
const authenticateJWT = (req, res, next) => {
|
|
|
|
const authHeader = req.headers.authorization;
|
|
|
|
|
|
|
|
if (authHeader) {
|
|
|
|
const token = authHeader.split(' ')[1];
|
|
|
|
|
|
|
|
jwt.verify(token, accessTokenSecret, (err, payload) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
return res.sendStatus(403);
|
|
|
|
}
|
|
|
|
if (new Date().getTime() > payload.expiration) {
|
|
|
|
console.log('expired token');
|
|
|
|
return res.sendStatus(403);
|
|
|
|
}
|
|
|
|
|
|
|
|
req.auth = payload;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.sendStatus(401);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-07 17:08:49 +00:00
|
|
|
const app = express();
|
|
|
|
|
2022-09-07 10:36:30 +00:00
|
|
|
app.use(bodyParser.json());
|
2022-09-07 17:08:49 +00:00
|
|
|
app.use(cors());
|
2022-09-07 10:36:30 +00:00
|
|
|
|
|
|
|
app.listen(3000, () => {
|
|
|
|
console.log('Server started (http://localhost:3000/)!');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// all access points below
|
|
|
|
|
|
|
|
app.get("/", (req, res) => {
|
|
|
|
res.send("This is not the way.");
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/bandz', authenticateJWT, async (req, res) => {
|
|
|
|
const bandz = await getBandz(db);
|
|
|
|
res.json({message: 'This is the way.', bandz});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/toktok', (req, res) => {
|
|
|
|
const {password} = req.body;
|
|
|
|
|
2022-09-11 10:45:48 +00:00
|
|
|
if (password != authPassword) {
|
2022-09-07 10:36:30 +00:00
|
|
|
return res.json({message: 'This is not the way.'});
|
|
|
|
}
|
|
|
|
|
2022-09-10 06:32:42 +00:00
|
|
|
// 20-hour tokens
|
|
|
|
const expiration = new Date().getTime() + 20 * 3600 * 1000;
|
2022-09-07 10:36:30 +00:00
|
|
|
const token = jwt.sign({expiration}, accessTokenSecret);
|
|
|
|
|
|
|
|
res.json({message: 'This is the way.', token});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/wrap', authenticateJWT, async (req, res) => {
|
|
|
|
const chikin = req.body;
|
|
|
|
console.log(chikin);
|
|
|
|
const {lastID} = await addChikin(db, chikin);
|
2022-09-07 17:08:49 +00:00
|
|
|
res.json({message: 'This is the way.', chikinId: lastID});
|
2022-09-07 10:36:30 +00:00
|
|
|
});
|
|
|
|
|
2022-09-07 17:08:49 +00:00
|
|
|
app.post('/print', authenticateJWT, async (req, res) => {
|
|
|
|
const {chikinId} = req.body;
|
|
|
|
|
|
|
|
const qr_url = `http://lafermedumalpas.fr/chikinz/${chikinId}`;
|
|
|
|
const chikin = await getChikin(db, chikinId);
|
2022-09-10 06:32:42 +00:00
|
|
|
const weight = chikin.weight / 1000;
|
2022-09-07 17:08:49 +00:00
|
|
|
const line0 = 'Poulet fermier bio';
|
2022-09-10 06:32:42 +00:00
|
|
|
const line1 = `${weight.toFixed(2)} kg`;
|
2022-09-07 17:08:49 +00:00
|
|
|
|
|
|
|
const pricePerKg = 12;
|
2022-09-10 06:32:42 +00:00
|
|
|
const price = weight * pricePerKg;
|
|
|
|
const line2 = `${pricePerKg.toFixed(2)} €/kg - ${price.toFixed(2)} €`;
|
|
|
|
|
2022-09-11 10:45:48 +00:00
|
|
|
const args = [catclient,
|
2022-09-10 06:32:42 +00:00
|
|
|
'--template0', [qr_url, line0, line1, line2].join(';'),
|
|
|
|
'--feed',
|
|
|
|
];
|
2022-09-07 17:08:49 +00:00
|
|
|
|
|
|
|
const proc = child_process.spawn('python', args);
|
|
|
|
proc.stdout.on('data', data => console.log(`stdout: ${data}`));
|
|
|
|
proc.stderr.on('data', data => console.log(`stderr: ${data}`));
|
|
|
|
|
|
|
|
res.json({message: 'This is the way.'});
|
|
|
|
});
|
2022-09-07 10:36:30 +00:00
|
|
|
|
|
|
|
// catch errors
|
|
|
|
|
|
|
|
app.use((err, req, res, next) => {
|
|
|
|
console.error(err);
|
|
|
|
res.status(500).json({
|
|
|
|
message: 'This is not the way.',
|
|
|
|
error: err.message,
|
|
|
|
});
|
|
|
|
});
|