Skip to content

Commit

Permalink
Merge pull request #1111 from bluewave-labs/fix/be/db-module-refactor
Browse files Browse the repository at this point in the history
Minor refactoring in DB modules for readability
  • Loading branch information
ajhollid authored Nov 5, 2024
2 parents 2fe44e2 + e36abfb commit de8e9a9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
24 changes: 12 additions & 12 deletions Server/db/mongo/modules/monitorModule.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Monitor from "../../models/Monitor.js";
import Check from "../../models/Check.js";
import PageSpeedCheck from "../../models/PageSpeedCheck.js";
import HardwareCheck from "../../models/HardwareCheck.js";
import { errorMessages } from "../../../utils/messages.js";
import Notification from "../../models/Notification.js";
import { NormalizeData } from "../../../utils/dataUtils.js";
Expand All @@ -16,6 +17,13 @@ const demoMonitors = JSON.parse(fs.readFileSync(demoMonitorsPath, "utf8"));

const SERVICE_NAME = "monitorModule";

const CHECK_MODEL_LOOKUP = {
http: Check,
ping: Check,
pageSpeed: PageSpeedCheck,
hardware: HardwareCheck,
};

/**
* Get all monitors
* @async
Expand Down Expand Up @@ -163,8 +171,7 @@ const getMonitorStatsById = async (req) => {
// Default sort order is newest -> oldest
sortOrder = sortOrder === "asc" ? 1 : -1;

let model =
monitor.type === "http" || monitor.type === "ping" ? Check : PageSpeedCheck;
let model = CHECK_MODEL_LOOKUP[monitor.type];

const monitorStats = {
...monitor.toObject(),
Expand Down Expand Up @@ -325,13 +332,9 @@ const getMonitorsAndSummaryByTeamId = async (teamId, type) => {
(acc, monitor) => {
if (monitor.status === true) {
acc.up += 1;
}

if (monitor.status === false) {
} else if (monitor.status === false) {
acc.down += 1;
}

if (monitor.isActive === false) {
} else if (monitor.isActive === false) {
acc.paused += 1;
}
return acc;
Expand Down Expand Up @@ -397,8 +400,6 @@ const getMonitorsByTeamId = async (req, res) => {
// Default sort order is newest -> oldest
if (checkOrder === "asc") {
checkOrder = 1;
} else if (checkOrder === "desc") {
checkOrder = -1;
} else checkOrder = -1;

// Sort order for monitors
Expand Down Expand Up @@ -428,8 +429,7 @@ const getMonitorsByTeamId = async (req, res) => {
checksQuery.status = status;
}

let model =
monitor.type === "http" || monitor.type === "ping" ? Check : PageSpeedCheck;
let model = CHECK_MODEL_LOOKUP[monitor.type];

// Checks are order newest -> oldest
let checks = await model
Expand Down
28 changes: 14 additions & 14 deletions Server/db/mongo/modules/recoveryModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,25 @@ const resetPassword = async (req, res) => {
const recoveryToken = await validateRecoveryToken(req, res);
const user = await UserModel.findOne({ email: recoveryToken.email });

if (user === null) {
throw new Error(errorMessages.DB_USER_NOT_FOUND);
}

const match = await user.comparePassword(newPassword);
if (match === true) {
throw new Error(errorMessages.DB_RESET_PASSWORD_BAD_MATCH);
}

if (user !== null) {
user.password = newPassword;
await user.save();
await RecoveryToken.deleteMany({ email: recoveryToken.email });
// Fetch the user again without the password
const userWithoutPassword = await UserModel.findOne({
email: recoveryToken.email,
})
.select("-password")
.select("-profileImage");
return userWithoutPassword;
} else {
throw new Error(errorMessages.DB_USER_NOT_FOUND);
}
user.password = newPassword;
await user.save();
await RecoveryToken.deleteMany({ email: recoveryToken.email });
// Fetch the user again without the password
const userWithoutPassword = await UserModel.findOne({
email: recoveryToken.email,
})
.select("-password")
.select("-profileImage");
return userWithoutPassword;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "resetPassword";
Expand Down
5 changes: 2 additions & 3 deletions Server/db/mongo/modules/userModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ const getUserByEmail = async (email) => {
// Need the password to be able to compare, removed .select()
// We can strip the hash before returning the user
const user = await UserModel.findOne({ email: email }).select("-profileImage");
if (user) {
return user;
} else {
if (!user) {
throw new Error(errorMessages.DB_USER_NOT_FOUND);
}
return user;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getUserByEmail";
Expand Down

0 comments on commit de8e9a9

Please sign in to comment.