Initial content
This commit is contained in:
+76
@@ -0,0 +1,76 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createAdminCatalogRoutes = void 0;
|
||||
const models_1 = require("../../data/orm/models");
|
||||
const validation_1 = require("../../data/validation");
|
||||
const createAdminCatalogRoutes = (router) => {
|
||||
router.get("/table", async (req, resp) => {
|
||||
const products = await models_1.ProductModel.findAll({
|
||||
include: [
|
||||
{ model: models_1.SupplierModel, as: "supplier" },
|
||||
{ model: models_1.CategoryModel, as: "category" }
|
||||
],
|
||||
raw: true, nest: true
|
||||
});
|
||||
resp.render("admin/product_table", { products });
|
||||
});
|
||||
router.delete("/:id", async (req, resp) => {
|
||||
const id = req.params.id;
|
||||
const count = await models_1.ProductModel.destroy({ where: { id } });
|
||||
if (count == 1) {
|
||||
resp.end();
|
||||
}
|
||||
else {
|
||||
throw Error(`Unexpected deletion count result: ${count}`);
|
||||
}
|
||||
});
|
||||
router.get("/edit/:id", async (req, resp) => {
|
||||
const id = req.params.id;
|
||||
const data = {
|
||||
product: { id: { value: id },
|
||||
...await validation_1.ProductDTOValidator.validate(await models_1.ProductModel.findByPk(id, { raw: true })) },
|
||||
suppliers: await models_1.SupplierModel.findAll({ raw: true }),
|
||||
categories: await models_1.CategoryModel.findAll({ raw: true })
|
||||
};
|
||||
resp.render("admin/product_editor", data);
|
||||
});
|
||||
router.put("/:id", async (req, resp) => {
|
||||
const validation = await validation_1.ProductDTOValidator.validate(req.body);
|
||||
if ((0, validation_1.isValid)(validation)) {
|
||||
await models_1.ProductModel.update((0, validation_1.getData)(validation), { where: { id: req.params.id } });
|
||||
resp.redirect(303, "/api/products/table");
|
||||
}
|
||||
else {
|
||||
resp.render("admin/product_editor", {
|
||||
product: { id: { value: req.params.id }, ...validation },
|
||||
suppliers: await models_1.SupplierModel.findAll({ raw: true }),
|
||||
categories: await models_1.CategoryModel.findAll({ raw: true })
|
||||
});
|
||||
}
|
||||
});
|
||||
router.get("/create", async (req, resp) => {
|
||||
const data = {
|
||||
product: {},
|
||||
suppliers: await models_1.SupplierModel.findAll({ raw: true }),
|
||||
categories: await models_1.CategoryModel.findAll({ raw: true }),
|
||||
create: true
|
||||
};
|
||||
resp.render("admin/product_editor", data);
|
||||
});
|
||||
router.post("/create", async (req, resp) => {
|
||||
const validation = await validation_1.ProductDTOValidator.validate(req.body);
|
||||
if ((0, validation_1.isValid)(validation)) {
|
||||
await models_1.ProductModel.create((0, validation_1.getData)(validation));
|
||||
resp.redirect(303, "/api/products/table");
|
||||
}
|
||||
else {
|
||||
resp.render("admin/product_editor", {
|
||||
product: validation,
|
||||
suppliers: await models_1.SupplierModel.findAll({ raw: true }),
|
||||
categories: await models_1.CategoryModel.findAll({ raw: true }),
|
||||
create: true
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
exports.createAdminCatalogRoutes = createAdminCatalogRoutes;
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createAdminOrderRoutes = void 0;
|
||||
const order_models_1 = require("../../data/orm/models/order_models");
|
||||
const customer_models_1 = require("../../data/orm/models/customer_models");
|
||||
const models_1 = require("../../data/orm/models");
|
||||
const createAdminOrderRoutes = (router) => {
|
||||
router.get("/table", async (req, resp) => {
|
||||
const orders = (await order_models_1.OrderModel.findAll({
|
||||
include: [
|
||||
{ model: customer_models_1.CustomerModel, as: "customer" },
|
||||
{ model: order_models_1.AddressModel, as: "address" },
|
||||
{ model: order_models_1.ProductSelectionModel, as: "selections",
|
||||
include: [{ model: models_1.ProductModel, as: "product" }]
|
||||
}
|
||||
],
|
||||
order: ["shipped", "id"]
|
||||
})).map(o => o.toJSON());
|
||||
resp.render("admin/order_table", { orders });
|
||||
});
|
||||
router.post("/ship", async (req, resp) => {
|
||||
const { id, shipped } = req.body;
|
||||
const [rows] = await order_models_1.OrderModel.update({ shipped }, { where: { id } });
|
||||
if (rows === 1) {
|
||||
resp.redirect(303, "/api/orders/table");
|
||||
}
|
||||
else {
|
||||
throw new Error(`Expected 1 row updated, but got ${rows}`);
|
||||
}
|
||||
});
|
||||
};
|
||||
exports.createAdminOrderRoutes = createAdminOrderRoutes;
|
||||
@@ -0,0 +1,58 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createAdminRoutes = void 0;
|
||||
const express_1 = require("express");
|
||||
const admin_catalog_routes_1 = require("./admin_catalog_routes");
|
||||
const admin_order_routes_1 = require("./admin_order_routes");
|
||||
const passport_1 = __importDefault(require("passport"));
|
||||
const config_1 = require("../../config");
|
||||
const users = (0, config_1.getConfig)("admin:users", []);
|
||||
const createAdminRoutes = (app) => {
|
||||
app.use((req, resp, next) => {
|
||||
resp.locals.layout = false;
|
||||
resp.locals.user = req.user;
|
||||
next();
|
||||
});
|
||||
app.get("/admin/signin", (req, resp) => resp.render("admin/signin"));
|
||||
app.post("/admin/signout", (req, resp) => req.logOut(() => { resp.redirect("/admin/signin"); }));
|
||||
app.get("/admin/google", passport_1.default.authenticate("admin-auth"));
|
||||
app.get("/auth-signin-google", passport_1.default.authenticate("admin-auth", {
|
||||
successRedirect: "/admin/products", keepSessionInfo: true
|
||||
}));
|
||||
const authCheck = (r) => users.find(u => r.user?.email === u);
|
||||
const apiAuth = (req, resp, next) => {
|
||||
if (!authCheck(req)) {
|
||||
return resp.sendStatus(401);
|
||||
}
|
||||
next();
|
||||
};
|
||||
const cat_router = (0, express_1.Router)();
|
||||
(0, admin_catalog_routes_1.createAdminCatalogRoutes)(cat_router);
|
||||
app.use("/api/products", apiAuth, cat_router);
|
||||
const order_router = (0, express_1.Router)();
|
||||
(0, admin_order_routes_1.createAdminOrderRoutes)(order_router);
|
||||
app.use("/api/orders", apiAuth, order_router);
|
||||
const userAuth = (req, resp, next) => {
|
||||
if (!authCheck(req)) {
|
||||
return resp.redirect("/admin/signin");
|
||||
}
|
||||
next();
|
||||
};
|
||||
app.get("/admin", userAuth, (req, resp) => resp.redirect("/admin/products"));
|
||||
app.get("/admin/products", userAuth, (req, resp) => {
|
||||
resp.locals.content = "/api/products/table";
|
||||
resp.render("admin/admin_layout");
|
||||
});
|
||||
app.get("/admin/products/edit/:id", userAuth, (req, resp) => {
|
||||
resp.locals.content = `/api/products/edit/${req.params.id}`;
|
||||
resp.render("admin/admin_layout");
|
||||
});
|
||||
app.get("/admin/orders", userAuth, (req, resp) => {
|
||||
resp.locals.content = "/api/orders/table";
|
||||
resp.render("admin/admin_layout");
|
||||
});
|
||||
};
|
||||
exports.createAdminRoutes = createAdminRoutes;
|
||||
Reference in New Issue
Block a user