Multi tenant DB, registratie per huishouden V1.2
This commit is contained in:
@@ -8,7 +8,7 @@ class CategoryController {
|
||||
|
||||
res.render("categories/index", {
|
||||
title: "Categorieën",
|
||||
categories: CategoryService.getCategories()
|
||||
categories: CategoryService.getCategories(req.user.household_id)
|
||||
});
|
||||
|
||||
}
|
||||
@@ -17,7 +17,7 @@ class CategoryController {
|
||||
editForm(req, res) {
|
||||
const categoryId = Number.parseInt(req.params.id, 10);
|
||||
const category = Number.isInteger(categoryId)
|
||||
? CategoryService.getCategory(categoryId)
|
||||
? CategoryService.getCategory(categoryId, req.user.household_id)
|
||||
: null;
|
||||
|
||||
if (!category) {
|
||||
@@ -41,7 +41,7 @@ class CategoryController {
|
||||
return res.redirect("/categories");
|
||||
}
|
||||
|
||||
CategoryService.createCategory({ name, icon });
|
||||
CategoryService.createCategory({ name, icon, household_id: req.user.household_id });
|
||||
req.flash("success", "Categorie toegevoegd.");
|
||||
res.redirect("/categories");
|
||||
}
|
||||
@@ -57,7 +57,7 @@ class CategoryController {
|
||||
return res.redirect("/categories");
|
||||
}
|
||||
|
||||
const result = CategoryService.updateCategory(categoryId, { name, icon });
|
||||
const result = CategoryService.updateCategory(categoryId, req.user.household_id, { name, icon });
|
||||
|
||||
if (result.changes === 0) {
|
||||
req.flash("error", "Categorie niet gevonden.");
|
||||
@@ -77,7 +77,7 @@ class CategoryController {
|
||||
return res.redirect("/categories");
|
||||
}
|
||||
|
||||
if (CategoryService.categoryHasProducts(categoryId)) {
|
||||
if (CategoryService.categoryHasProducts(categoryId, req.user.household_id)) {
|
||||
req.flash(
|
||||
"error",
|
||||
"Deze categorie is nog aan producten gekoppeld en kan niet worden verwijderd."
|
||||
@@ -85,7 +85,7 @@ class CategoryController {
|
||||
return res.redirect("/categories");
|
||||
}
|
||||
|
||||
const result = CategoryService.deleteCategory(categoryId);
|
||||
const result = CategoryService.deleteCategory(categoryId, req.user.household_id);
|
||||
|
||||
if (result.changes === 0) {
|
||||
req.flash("error", "Categorie niet gevonden.");
|
||||
|
||||
@@ -25,7 +25,7 @@ class MenuController {
|
||||
res.render("menu/preview", {
|
||||
title: "Weekmenu controleren",
|
||||
menu,
|
||||
newProducts: MenuService.getNewProducts(menu),
|
||||
newProducts: MenuService.getNewProducts(menu, req.user.household_id),
|
||||
listName: "Weekboodschappen"
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
const ProductService =
|
||||
require("../services/ProductService");
|
||||
|
||||
const db = require("../config/database");
|
||||
const CategoryService = require("../services/CategoryService");
|
||||
|
||||
const parseCsv = require("../utils/parseCsv");
|
||||
|
||||
class ProductController {
|
||||
@@ -10,11 +11,9 @@ class ProductController {
|
||||
index(req, res) {
|
||||
|
||||
const products =
|
||||
ProductService.getProducts();
|
||||
ProductService.getProducts(req.user.household_id);
|
||||
|
||||
const categories = db.prepare(
|
||||
"SELECT * FROM categories ORDER BY name"
|
||||
).all();
|
||||
const categories = CategoryService.getCategories(req.user.household_id);
|
||||
|
||||
res.render(
|
||||
"products/index",
|
||||
@@ -39,9 +38,15 @@ class ProductController {
|
||||
return res.redirect("/products");
|
||||
}
|
||||
|
||||
if (categoryId !== null && !CategoryService.getCategory(categoryId, req.user.household_id)) {
|
||||
req.flash("error", "De gekozen categorie bestaat niet.");
|
||||
return res.redirect("/products");
|
||||
}
|
||||
|
||||
ProductService.createProduct({
|
||||
name,
|
||||
category_id: categoryId
|
||||
category_id: categoryId,
|
||||
household_id: req.user.household_id
|
||||
});
|
||||
|
||||
req.flash("success", "Product toegevoegd.");
|
||||
@@ -80,7 +85,7 @@ class ProductController {
|
||||
return res.redirect("/products");
|
||||
}
|
||||
|
||||
const categories = db.prepare("SELECT id, name FROM categories").all();
|
||||
const categories = CategoryService.getCategories(req.user.household_id);
|
||||
const categoryIds = new Map(
|
||||
categories.map(category => [category.name.trim().toLowerCase(), category.id])
|
||||
);
|
||||
@@ -105,7 +110,8 @@ class ProductController {
|
||||
products.push({
|
||||
name,
|
||||
category_id: categoryId,
|
||||
barcode: barcodeIndex === -1 ? "" : (row[barcodeIndex] || "").trim()
|
||||
barcode: barcodeIndex === -1 ? "" : (row[barcodeIndex] || "").trim(),
|
||||
household_id: req.user.household_id
|
||||
});
|
||||
});
|
||||
|
||||
@@ -123,7 +129,7 @@ class ProductController {
|
||||
editForm(req, res) {
|
||||
const productId = Number.parseInt(req.params.id, 10);
|
||||
const product = Number.isInteger(productId)
|
||||
? ProductService.getProduct(productId)
|
||||
? ProductService.getProduct(productId, req.user.household_id)
|
||||
: null;
|
||||
|
||||
if (!product) {
|
||||
@@ -131,9 +137,7 @@ class ProductController {
|
||||
return res.redirect("/products");
|
||||
}
|
||||
|
||||
const categories = db.prepare(
|
||||
"SELECT * FROM categories ORDER BY name"
|
||||
).all();
|
||||
const categories = CategoryService.getCategories(req.user.household_id);
|
||||
|
||||
res.render("products/edit", {
|
||||
title: "Product wijzigen",
|
||||
@@ -156,7 +160,12 @@ class ProductController {
|
||||
return res.redirect("/products");
|
||||
}
|
||||
|
||||
const result = ProductService.updateProduct(productId, {
|
||||
if (categoryId !== null && !CategoryService.getCategory(categoryId, req.user.household_id)) {
|
||||
req.flash("error", "De gekozen categorie bestaat niet.");
|
||||
return res.redirect(`/products/${productId}/edit`);
|
||||
}
|
||||
|
||||
const result = ProductService.updateProduct(productId, req.user.household_id, {
|
||||
name,
|
||||
category_id: categoryId
|
||||
});
|
||||
@@ -179,7 +188,7 @@ class ProductController {
|
||||
return res.redirect("/products");
|
||||
}
|
||||
|
||||
if (ProductService.productHasListItems(productId)) {
|
||||
if (ProductService.productHasListItems(productId, req.user.household_id)) {
|
||||
req.flash(
|
||||
"error",
|
||||
"Dit product staat nog op een boodschappenlijst en kan niet worden verwijderd."
|
||||
@@ -187,7 +196,7 @@ class ProductController {
|
||||
return res.redirect(`/products/${productId}/edit`);
|
||||
}
|
||||
|
||||
const result = ProductService.deleteProduct(productId);
|
||||
const result = ProductService.deleteProduct(productId, req.user.household_id);
|
||||
|
||||
if (result.changes === 0) {
|
||||
req.flash("error", "Product niet gevonden.");
|
||||
|
||||
@@ -25,8 +25,8 @@ class ShoppingListController {
|
||||
res.locals.title = list.name;
|
||||
res.render("lists/show", {
|
||||
list,
|
||||
items: ShoppingListRepository.getItems(list.id),
|
||||
products: ProductRepository.getAll(),
|
||||
items: ShoppingListRepository.getItems(list.id, req.user.household_id),
|
||||
products: ProductRepository.getAll(req.user.household_id),
|
||||
user: req.user
|
||||
});
|
||||
}
|
||||
@@ -35,7 +35,9 @@ class ShoppingListController {
|
||||
const list = ShoppingListRepository.findById(req.params.id, req.user.household_id);
|
||||
const productId = Number.parseInt(req.body.product_id, 10);
|
||||
const amount = Number.parseFloat(req.body.amount || "1");
|
||||
const product = Number.isInteger(productId) ? ProductRepository.findById(productId) : null;
|
||||
const product = Number.isInteger(productId)
|
||||
? ProductRepository.findById(productId, req.user.household_id)
|
||||
: null;
|
||||
if (!list || !product || !Number.isFinite(amount) || amount <= 0) {
|
||||
req.flash("error", "Ongeldig product of ongeldige hoeveelheid");
|
||||
return res.redirect(`/lists/${req.params.id}`);
|
||||
|
||||
Reference in New Issue
Block a user