- added csv import

- added somewhat real stats to dashboard
- basic search logic
This commit is contained in:
2023-05-02 18:23:47 +02:00
parent e869615ec7
commit d1d717a988
13 changed files with 270 additions and 29 deletions

View File

@ -15,8 +15,8 @@
</div>
<div class="card col m-2">
<div class="card-body">
<h5 class="card-title">10</h5>
<p class="card-text">Uncategorized items</p>
<h5 class="card-title"><%= it.stats.total %></h5>
<p class="card-text">Items in total</p>
</div>
</div>
<div class="card col m-2">
@ -44,27 +44,16 @@
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Samsung 65W USB-C Ladegerät</td>
<td><span class="badge text-bg-success">Normal</span></td>
<td>DB8031E8</td>
<td><a href="#" class="btn btn-primary">Edit</a></td>
</tr>
<tr>
<th scope="row">2</th>
<td>2.5mm Klinken Verlängerung 40mm</td>
<td><span class="badge text-bg-success">Normal</span></td>
<td>25A1E9DE</td>
<td><a href="#" class="btn btn-primary">Edit</a></td>
</tr>
<tr>
<th scope="row">3</th>
<td>Elgato Streamdeck XL</td>
<td><span class="badge text-bg-danger">Stolen</span></td>
<td>9AF2388E</td>
<td><a href="#" class="btn btn-primary">Edit</a></td>
</tr>
<% it.recents.forEach(function(user){ %>
<tr>
<th scope="row"><%= user.id %></th>
<td><%= user.name %></td>
<td><span class="badge text-bg-success"><%= user.status %></span></td>
<td><%= user.SKU %></td>
<td><a href="#" class="btn btn-primary">Edit</a></td>
</tr>
<% }) %>
</tbody>
</table>
</div>

View File

@ -0,0 +1,28 @@
<%~ E.includeFile("../partials/head.eta.html", {"title": "Bootstrap Demo Page" }) %>
<%~ E.includeFile("../partials/controls.eta.html", {"active": "Orders" }) %>
<h1>Import A CSV File</h1>
Upload a CSV file to import into the database. The CSV file must have the following columns:
<ul>
<li> Name</li>
<li> Amount</li>
<li> Manufacturer</li>
<li> Category</li>
</ul>
The following columns are optional:
<ul>
<li> SKU</li>
<li> Comment</li>
<li> StorageLocation (import currently not supported)</li>
</ul>
<form method="post" encType="multipart/form-data">
<label for="formFile" class="form-label">CSV Inventory File Upload</label>
<input class="form-control" type="file" id="formFile" name="formFile"><br>
<input type="submit" value="Run import" class="btn btn-primary">
</form>
<%~ E.includeFile("../partials/controlsFoot.eta.html") %>
<%~ E.includeFile("../partials/foot.eta.html") %>

View File

@ -10,14 +10,13 @@
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<input class="form-control form-control-dark w-100 bg-secondary" type="text" placeholder="Search" aria-label="Search" />
<input class="form-control form-control-dark w-100 bg-secondary" type="text" placeholder="Search" aria-label="Search" id="SearchBox" />
<div class="navbar-nav">
<div class="nav-item text-nowrap">
<a class="nav-link px-3" href="#">Sign out</a>
</div>
</div>
</header>
<div class="container-fluid">
<div class="row">
<nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">

View File

@ -1,3 +1,4 @@
</main>
</div>
</div>
<script src="/js/searchBox.js"></script>

View File

@ -15,7 +15,7 @@
<script src="/static/jquery/dist/jquery.min.js"></script>
<link href="/static/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="/static/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
<link href="css/dashboard.css" rel="stylesheet">
<link href="/css/dashboard.css" rel="stylesheet">
</head>
<body>

View File

@ -1,6 +1,7 @@
import { Signale } from 'signale';
import ConfigHandler from './assets/configHandler';
import express, { Request, Response } from 'express';
import fileUpload from 'express-fileupload';
import { PrismaClient } from '@prisma/client';
import { Status, Category } from '@prisma/client';
import * as eta from 'eta';
@ -43,6 +44,7 @@ export const prisma = new PrismaClient({
export const app = express();
app.engine("html", eta.renderFile)
app.use(fileUpload());
// Configure static https://expressjs.com/de/starter/static-files.html
// app.use('/static', express.static('public'));
app.use(express.static(__path + '/static'));

View File

@ -2,5 +2,17 @@ import express, { Request, Response } from 'express';
import { prisma, __path } from '../../index.js';
export default (req: Request, res: Response) => {
res.render(__path + '/src/frontend/demopage.eta.html');
prisma.item.findMany({
orderBy: {
updatedAt: 'desc'
},
// Limit to 10 items
take: 10
}).then((items) => {
// Count amount of total items
prisma.item.count().then((count) => {
res.render(__path + '/src/frontend/demopage.eta.html', { recents: items, stats: { total: count } });
});
});
// res.render(__path + '/src/frontend/demopage.eta.html');
};

View File

@ -0,0 +1,106 @@
import express, { Request, Response } from 'express';
import { prisma, __path, log } from '../../../index.js';
import { UploadedFile } from 'express-fileupload';
import { parse, transform } from 'csv';
import { Status, Category, PrismaPromise } from '@prisma/client';
export default (req: Request, res: Response) => {
// Decide wether its post or get
if (req.method === 'POST') {
// Handle file upload and import
console.log(req.files)
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send('No files were uploaded.');
}
const file: UploadedFile = req.files.formFile as UploadedFile;
const csv = file.data.toString();
parse(csv, { columns: true }, function (err, records) {
if (err) {
res.send(err);
return;
}
// Find all categories and save them into a set
const categories = new Set<string>();
records.forEach((record: any) => {
categories.add(record.category);
});
// Remove categories that already exists in the database
prisma.category.findMany({
where: {
name: {
in: Array.from(categories)
}
}
}).then((values) => {
values.forEach((value) => {
categories.delete(value.name);
});
// Log categories
log.web.debug(categories);
const categoryPromises: PrismaPromise<Category>[] = [];
categories.forEach((category: string) => {
const promise = prisma.category.create({
data: {
name: category
}
})
categoryPromises.push(promise);
});
Promise.all(categoryPromises).then((values) => {
// Create items
const listOfPromises = [];
for (let i = 0; i < records.length; i++) {
const record = records[i];
const promise = prisma.item.create({
data: {
name: record.name,
Amount: parseInt(record.amount),
Comment: record.comment,
category: {
connect: {
name: record.category
}
},
SKU: record.sku,
manufacturer: record.manufacturer,
status: Status.normal,
importedBy: "CSV Import"
}
});
listOfPromises.push(promise);
}
Promise.all(listOfPromises).then((values) => {
console.log(values);
res.send('ok');
}).catch((err) => {
res.send('failed to create items');
log.db.error(err);
return;
});
}).catch((err) => {
// res.send('failed to create categories');
log.db.error(err);
});
}).catch((err) => {
res.send('failed to find categories');
log.db.error(err);
return;
});
});
} else {
// Render page
res.render(__path + '/src/frontend/imports/csvImport.eta.html');
}
};

View File

@ -4,6 +4,7 @@ import express from 'express';
import skuRoute from './:id.js';
import testRoute from './test.js';
import etaTestRoute from './etaTest.js';
import csvImportRoute from './import/csvImport.js';
// Router base is '/'
const Router = express.Router();
@ -11,6 +12,7 @@ const Router = express.Router();
Router.use('/etaTest', etaTestRoute);
Router.use('/:id(\\w{8})', skuRoute);
Router.use('/test', testRoute);
Router.use('/import/csv', csvImportRoute);
export default Router;