current state
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import express, { Request, Response } from 'express';
|
||||
import { Request, Response } from 'express';
|
||||
import { prisma, __path, log } from '../../../index.js';
|
||||
|
||||
function get(req: Request, res: Response) {
|
||||
@ -15,53 +15,4 @@ function get(req: Request, res: Response) {
|
||||
});
|
||||
}
|
||||
|
||||
function post(req: Request, res: Response) {
|
||||
console.log(req.body);
|
||||
// Check if required fields are filled in
|
||||
if (!req.body.name) {
|
||||
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!req.body.editCategoryModalIsEdit) {
|
||||
console.log('is not edit');
|
||||
// Save data to category table
|
||||
prisma.itemCategory
|
||||
.create({
|
||||
data: {
|
||||
name: req.body.name,
|
||||
description: req.body.description
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
res.redirect('categories');
|
||||
})
|
||||
.catch((err) => {
|
||||
// TODO Catch if is a duplicate error and show a message to the user
|
||||
log.db.error(err);
|
||||
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
|
||||
});
|
||||
} else {
|
||||
// Save data to category table
|
||||
prisma.itemCategory
|
||||
.update({
|
||||
where: {
|
||||
id: parseInt(req.body.editCategoryModalId)
|
||||
},
|
||||
data: {
|
||||
name: req.body.name,
|
||||
description: req.body.description
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
res.redirect('categories');
|
||||
})
|
||||
.catch((err) => {
|
||||
// TODO Catch if is a duplicate error and show a message to the user
|
||||
log.db.error(err);
|
||||
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default { get, post };
|
||||
export default { get };
|
||||
|
102
src/routes/frontend/manage/import/jsonImport.ts
Normal file
102
src/routes/frontend/manage/import/jsonImport.ts
Normal file
@ -0,0 +1,102 @@
|
||||
import express, { Request, Response } from 'express';
|
||||
import { prisma, __path, log } from '../../../../index.js';
|
||||
import { UploadedFile } from 'express-fileupload';
|
||||
import { itemStatus, itemCategory, PrismaPromise } from '@prisma/client';
|
||||
|
||||
function post(req: Request, res: Response) {
|
||||
// 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 jsonRaw = file.data.toString();
|
||||
const json = JSON.parse(jsonRaw);
|
||||
// Get all unqiue categories
|
||||
const categories = new Set<string>();
|
||||
json.forEach((item: any) => {
|
||||
categories.add(item.category);
|
||||
});
|
||||
log.db.debug(categories);
|
||||
|
||||
prisma.itemCategory
|
||||
.findMany({
|
||||
where: {
|
||||
name: {
|
||||
in: Array.from(categories)
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((values) => {
|
||||
values.forEach((value) => {
|
||||
categories.delete(value.name);
|
||||
});
|
||||
log.db.debug(categories);
|
||||
const categoryPromises: PrismaPromise<itemCategory>[] = [];
|
||||
categories.forEach((category: string) => {
|
||||
const promise = prisma.itemCategory.create({
|
||||
data: {
|
||||
name: category,
|
||||
description: ''
|
||||
}
|
||||
});
|
||||
categoryPromises.push(promise);
|
||||
});
|
||||
Promise.all(categoryPromises)
|
||||
.then((values) => {
|
||||
// Create items
|
||||
const listOfPromises = [];
|
||||
|
||||
for (let i = 0; i < json.length; i++) {
|
||||
const record = json[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: itemStatus.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;
|
||||
});
|
||||
|
||||
// res.status(501).end("Not implemented yet");
|
||||
}
|
||||
|
||||
function get(req: Request, res: Response) {
|
||||
// Render page
|
||||
res.render(__path + '/src/frontend/manage/imports/jsonImport.eta.html');
|
||||
}
|
||||
|
||||
export default { get, post };
|
@ -3,6 +3,7 @@ import express from 'express';
|
||||
// Route imports
|
||||
import testRoute from './test.js';
|
||||
import csvImportRoute from './import/csvImport.js';
|
||||
import jsonImportRoute from './import/jsonImport.js';
|
||||
import categoryManager from './categoryManager.js';
|
||||
import storageManager from './storageManager.js';
|
||||
import startpageRoute from './startpage.js';
|
||||
@ -11,9 +12,10 @@ import startpageRoute from './startpage.js';
|
||||
const Router = express.Router({ strict: false });
|
||||
|
||||
Router.route('/test').get(testRoute.get);
|
||||
Router.route('/categories').get(categoryManager.get).post(categoryManager.post);
|
||||
Router.route('/categories').get(categoryManager.get);
|
||||
Router.route('/storages').get(storageManager.get);
|
||||
Router.route('/import/csv').get(csvImportRoute.get).post(csvImportRoute.post);
|
||||
Router.route('/import/json').get(jsonImportRoute.get).post(jsonImportRoute.post);
|
||||
Router.route('/').get(startpageRoute.get);
|
||||
|
||||
export default Router;
|
||||
|
Reference in New Issue
Block a user