Skip to content

Commit

Permalink
refactor(server): change regex on search to be case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
NotExpectedYet committed Sep 24, 2022
1 parent fc2271b commit a78e20e
Showing 1 changed file with 66 additions and 65 deletions.
131 changes: 66 additions & 65 deletions server/routes/history.routes.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
const express = require("express");
const express = require('express');

const router = express.Router();
const _ = require("lodash");
const HistoryRoutes = require("../models/History.js");
const { ensureAuthenticated } = require("../middleware/auth");
const Printers = require("../models/Printer.js");
const Spools = require("../models/Filament.js");
const Profiles = require("../models/Profiles.js");
const { getHistoryCache } = require("../cache/history.cache");
const { sortOptions } = require("../constants/history-sort.constants");
const { generatePrinterStatistics } = require("../services/printer-statistics.service");
const { getPrinterStoreCache } = require("../cache/printer-store.cache");
const { validateParamsMiddleware } = require("../middleware/validators");
const M_VALID = require("../constants/validate-mongo.constants");

router.post("/update", ensureAuthenticated, async (req, res) => {
const _ = require('lodash');
const HistoryRoutes = require('../models/History.js');
const { ensureAuthenticated } = require('../middleware/auth');
const Printers = require('../models/Printer.js');
const Spools = require('../models/Filament.js');
const Profiles = require('../models/Profiles.js');
const { getHistoryCache } = require('../cache/history.cache');
const { sortOptions } = require('../constants/history-sort.constants');
const { generatePrinterStatistics } = require('../services/printer-statistics.service');
const { getPrinterStoreCache } = require('../cache/printer-store.cache');
const { validateParamsMiddleware } = require('../middleware/validators');
const M_VALID = require('../constants/validate-mongo.constants');

router.post('/update', ensureAuthenticated, async (req, res) => {
// Check required fields
const note = req.bodyString("note");
const note = req.bodyString('note');
const filamentId = req.body.filamentId;
const id = req.bodyString("id");
const id = req.bodyString('id');
const history = await HistoryRoutes.findById(id);
if (history.printHistory.notes !== note) {
history.printHistory.notes = note;
}
for (let f = 0; f < filamentId.length; f++) {
if (Array.isArray(history.printHistory.filamentSelection)) {
if (
typeof history.printHistory.filamentSelection[f] !== "undefined" &&
typeof history.printHistory.filamentSelection[f] !== 'undefined' &&
history.printHistory.filamentSelection[f] !== null &&
history.printHistory.filamentSelection[f]._id === filamentId
) {
Expand Down Expand Up @@ -60,26 +60,26 @@ router.post("/update", ensureAuthenticated, async (req, res) => {
}
}
}
history.markModified("printHistory");
history.markModified('printHistory');
history.save().then(() => {
getHistoryCache().initCache();
});
res.send("success");
res.send('success');
});
//Register Handle for Saving printers
router.post("/delete", ensureAuthenticated, async (req, res) => {
router.post('/delete', ensureAuthenticated, async (req, res) => {
//Check required fields
const deleteHistory = req.bodyString("id");
const deleteHistory = req.bodyString('id');
await HistoryRoutes.findOneAndDelete({ _id: deleteHistory }).then(() => {
getHistoryCache().initCache();
});
res.send("success");
res.send('success');
});

router.get("/get", ensureAuthenticated, async (req, res) => {
const page = req.queryString("page");
const limit = req.queryString("limit");
const sort = req.queryString("sort");
router.get('/get', ensureAuthenticated, async (req, res) => {
const page = req.queryString('page');
const limit = req.queryString('limit');
const sort = req.queryString('sort');

let paginationOptions = {};

Expand All @@ -89,61 +89,62 @@ router.get("/get", ensureAuthenticated, async (req, res) => {

paginationOptions.sort = sortOptions[sort];

const firstDate = req.queryString("firstDate");
const lastDate = req.queryString("lastDate");
const fileFilter = req.queryString("fileFilter");
const pathFilter = req.queryString("pathFilter");
const spoolManuFilter = req.queryString("spoolManuFilter");
const spoolMatFilter = req.queryString("spoolMatFilter");
const fileSearch = req.queryString("fileSearch");
const spoolSearch = req.queryString("spoolSearch");
const printerNameFilter = req.queryString("printerNameFilter");
const printerGroupFilter = req.queryString("printerGroupFilter");
const printerSearch = req.queryString("printerSearch");
const firstDate = req.queryString('firstDate');
const lastDate = req.queryString('lastDate');
const fileFilter = req.queryString('fileFilter');
const pathFilter = req.queryString('pathFilter');
const spoolManuFilter = req.queryString('spoolManuFilter');
const spoolMatFilter = req.queryString('spoolMatFilter');
const fileSearch = req.queryString('fileSearch');
const spoolSearch = req.queryString('spoolSearch');
const printerNameFilter = req.queryString('printerNameFilter');
const printerGroupFilter = req.queryString('printerGroupFilter');
const printerSearch = req.queryString('printerSearch');

const findOptions = {
"printHistory.endDate": { $gte: new Date(lastDate), $lte: new Date(firstDate) }
'printHistory.endDate': { $gte: new Date(lastDate), $lte: new Date(firstDate) },
};

if (fileFilter) {
findOptions["printHistory.fileName"] = fileFilter;
findOptions['printHistory.fileName'] = fileFilter;
}

if (printerNameFilter) {
findOptions["printHistory.printerName"] = printerNameFilter;
findOptions['printHistory.printerName'] = printerNameFilter;
}

if (printerGroupFilter) {
findOptions["printHistory.printerGroup"] = printerGroupFilter;
findOptions['printHistory.printerGroup'] = printerGroupFilter;
}

if (pathFilter) {
findOptions["printHistory.job.file.path"] = new RegExp(pathFilter, "g");
findOptions['printHistory.job.file.path'] = new RegExp(pathFilter, 'g');
}
if (spoolManuFilter) {
findOptions["printHistory.filamentSelection.spools.profile.manufacturer"] = new RegExp(
spoolManuFilter.replace(/_/g, " "),
"g"
findOptions['printHistory.filamentSelection.spools.profile.manufacturer'] = new RegExp(
spoolManuFilter.replace(/_/g, ' '),
'g'
);
}
if (spoolMatFilter) {
findOptions["printHistory.filamentSelection.spools.profile.material"] = new RegExp(
spoolMatFilter.replace(/_/g, " "),
"g"
findOptions['printHistory.filamentSelection.spools.profile.material'] = new RegExp(
spoolMatFilter.replace(/_/g, ' '),
'g'
);
}

if (fileSearch) {
findOptions["printHistory.fileName"] = new RegExp(fileSearch.replace(/_/g, " "), "g");
findOptions['printHistory.fileName'] = new RegExp(fileSearch.replace(/_/g, ' '), 'i');
}

if (printerSearch) {
findOptions["printHistory.printerName"] = new RegExp(printerSearch.replace(/_/g, " "), "g");
findOptions['printHistory.printerName'] = new RegExp(printerSearch.replace(/_/g, ' '), 'i');
}

if (spoolSearch) {
findOptions["printHistory.filamentSelection.spools.name"] = new RegExp(
spoolSearch.replace(/_/g, " "),
"g"
findOptions['printHistory.filamentSelection.spools.name'] = new RegExp(
spoolSearch.replace(/_/g, ' '),
'i'
);
}

Expand All @@ -161,18 +162,18 @@ router.get("/get", ensureAuthenticated, async (req, res) => {
statisticsClean,
pagination,
monthlyStatistics: historyCache.monthlyStatistics,
historyFilterData
historyFilterData,
});
});

router.get("/statisticsData", ensureAuthenticated, async (req, res) => {
router.get('/statisticsData', ensureAuthenticated, async (req, res) => {
const historyCache = getHistoryCache();
const stats = historyCache.generateStatistics();

res.send({ history: stats });
});
router.post("/updateCostMatch", ensureAuthenticated, async (req, res) => {
const latest = req.bodyString("id");
router.post('/updateCostMatch', ensureAuthenticated, async (req, res) => {
const latest = req.bodyString('id');

// Find history and matching printer ID
const historyEntity = await HistoryRoutes.findOne({ _id: latest });
Expand All @@ -182,12 +183,12 @@ router.post("/updateCostMatch", ensureAuthenticated, async (req, res) => {
});
if (printer > -1) {
historyEntity.printHistory.costSettings = printers[printer].costSettings;
historyEntity.markModified("printHistory");
historyEntity.markModified('printHistory');
historyEntity.save();
const send = {
status: 200,
printTime: historyEntity.printHistory.printTime,
costSettings: printers[printer].costSettings
costSettings: printers[printer].costSettings,
};
res.send(send);
} else {
Expand All @@ -196,14 +197,14 @@ router.post("/updateCostMatch", ensureAuthenticated, async (req, res) => {
electricityCosts: 0.15,
purchasePrice: 500,
estimateLifespan: 43800,
maintenanceCosts: 0.25
maintenanceCosts: 0.25,
};
const send = {
status: 400,
printTime: historyEntity.printHistory.printTime,
costSettings: historyEntity.printHistory.costSettings
costSettings: historyEntity.printHistory.costSettings,
};
historyEntity.markModified("printHistory");
historyEntity.markModified('printHistory');
historyEntity.save().then(() => {
getHistoryCache().initCache();
});
Expand All @@ -212,11 +213,11 @@ router.post("/updateCostMatch", ensureAuthenticated, async (req, res) => {
}
});
router.get(
"/statistics/:id",
'/statistics/:id',
ensureAuthenticated,
validateParamsMiddleware(M_VALID.MONGO_ID),
async function (req, res) {
const printerID = req.paramString("id");
const printerID = req.paramString('id');
let stats = getPrinterStoreCache().getPrinterStatistics(printerID);
if (!stats) {
stats = await generatePrinterStatistics(printerID);
Expand Down

0 comments on commit a78e20e

Please sign in to comment.