From 666c2273a62bb08eeac582ee4177d15d2fb5e467 Mon Sep 17 00:00:00 2001 From: Linda Paiste Date: Wed, 2 Aug 2023 22:10:15 -0500 Subject: [PATCH] Convert server object exists functions to async/await. --- .../collectionForUserExists.js | 39 +++------ server/controllers/project.controller.js | 39 +++++---- server/controllers/user.controller.js | 11 ++- server/routes/server.routes.js | 84 ++++++++++--------- server/views/index.js | 17 ++++ 5 files changed, 99 insertions(+), 91 deletions(-) diff --git a/server/controllers/collection.controller/collectionForUserExists.js b/server/controllers/collection.controller/collectionForUserExists.js index 61893c6224..8b6bb05f87 100644 --- a/server/controllers/collection.controller/collectionForUserExists.js +++ b/server/controllers/collection.controller/collectionForUserExists.js @@ -1,30 +1,17 @@ import Collection from '../../models/collection'; import User from '../../models/user'; -export default function collectionForUserExists( - username, - collectionId, - callback -) { - function sendFailure() { - callback(false); - } - - function sendSuccess(collection) { - callback(collection != null); - } - - function findUser() { - return User.findByUsername(username); - } - - function findCollection(owner) { - if (owner == null) { - throw new Error('User not found'); - } - - return Collection.findOne({ _id: collectionId, owner }); - } - - return findUser().then(findCollection).then(sendSuccess).catch(sendFailure); +/** + * @param {string} username + * @param {string} collectionId + * @return {Promise} + */ +export default async function collectionForUserExists(username, collectionId) { + const user = await User.findByUsername(username); + if (!user) return false; + const collection = await Collection.findOne({ + _id: collectionId, + owner: user + }).exec(); + return collection != null; } diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index fd48b7558d..88b1cb2701 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -169,29 +169,28 @@ export function getProjects(req, res) { } } -export function projectExists(projectId, callback) { - Project.findById(projectId, (err, project) => - project ? callback(true) : callback(false) - ); +/** + * @param {string} projectId + * @return {Promise} + */ +export async function projectExists(projectId) { + const project = await Project.findById(projectId); + return project != null; } -export function projectForUserExists(username, projectId, callback) { - User.findByUsername(username, (err, user) => { - if (!user) { - callback(false); - return; - } - Project.findOne( - { user: user._id, $or: [{ _id: projectId }, { slug: projectId }] }, - (innerErr, project) => { - if (!project) { - callback(false); - return; - } - callback(true); - } - ); +/** + * @param {string} username + * @param {string} projectId + * @return {Promise} + */ +export async function projectForUserExists(username, projectId) { + const user = await User.findByUsername(username); + if (!user) return false; + const project = await Project.findOne({ + user: user._id, + $or: [{ _id: projectId }, { slug: projectId }] }); + return project != null; } function bundleExternalLibs(project) { diff --git a/server/controllers/user.controller.js b/server/controllers/user.controller.js index bc3e179662..f3f66b9e5e 100644 --- a/server/controllers/user.controller.js +++ b/server/controllers/user.controller.js @@ -314,10 +314,13 @@ export function updatePassword(req, res) { // eventually send email that the password has been reset } -export function userExists(username, callback) { - User.findByUsername(username, (err, user) => - user ? callback(true) : callback(false) - ); +/** + * @param {string} username + * @return {Promise} + */ +export async function userExists(username) { + const user = await User.findByUsername(username); + return user != null; } export function saveUser(res, user) { diff --git a/server/routes/server.routes.js b/server/routes/server.routes.js index eda9464eba..eed2ea87da 100644 --- a/server/routes/server.routes.js +++ b/server/routes/server.routes.js @@ -1,6 +1,5 @@ import { Router } from 'express'; -import { renderIndex } from '../views/index'; -import { get404Sketch } from '../views/404Page'; +import sendHtml, { renderIndex } from '../views/index'; import { userExists } from '../controllers/user.controller'; import { projectExists, @@ -27,40 +26,46 @@ router.get('/signup', (req, res) => { return res.send(renderIndex()); }); -router.get('/projects/:project_id', (req, res) => { - projectExists(req.params.project_id, (exists) => - exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html)) - ); +router.get('/projects/:project_id', async (req, res) => { + const exists = await projectExists(req.params.project_id); + sendHtml(req, res, exists); }); -router.get('/:username/sketches/:project_id/add-to-collection', (req, res) => { - projectForUserExists(req.params.username, req.params.project_id, (exists) => - exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html)) - ); -}); +router.get( + '/:username/sketches/:project_id/add-to-collection', + async (req, res) => { + const exists = await projectForUserExists( + req.params.username, + req.params.project_id + ); + sendHtml(req, res, exists); + } +); -router.get('/:username/sketches/:project_id', (req, res) => { - projectForUserExists(req.params.username, req.params.project_id, (exists) => - exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html)) +router.get('/:username/sketches/:project_id', async (req, res) => { + const exists = await projectForUserExists( + req.params.username, + req.params.project_id ); + sendHtml(req, res, exists); }); -router.get('/:username/sketches', (req, res) => { - userExists(req.params.username, (exists) => - exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html)) - ); +router.get('/:username/sketches', async (req, res) => { + const exists = await userExists(req.params.username); + sendHtml(req, res, exists); }); -router.get('/:username/full/:project_id', (req, res) => { - projectForUserExists(req.params.username, req.params.project_id, (exists) => - exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html)) +router.get('/:username/full/:project_id', async (req, res) => { + const exists = await projectForUserExists( + req.params.username, + req.params.project_id ); + sendHtml(req, res, exists); }); -router.get('/full/:project_id', (req, res) => { - projectExists(req.params.project_id, (exists) => - exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html)) - ); +router.get('/full/:project_id', async (req, res) => { + const exists = await projectExists(req.params.project_id); + sendHtml(req, res, exists); }); router.get('/login', (req, res) => { @@ -98,15 +103,11 @@ router.get('/assets', (req, res) => { } }); -router.get('/:username/assets', (req, res) => { - userExists(req.params.username, (exists) => { - const isLoggedInUser = - req.user && req.user.username === req.params.username; - const canAccess = exists && isLoggedInUser; - return canAccess - ? res.send(renderIndex()) - : get404Sketch((html) => res.send(html)); - }); +router.get('/:username/assets', async (req, res) => { + const exists = await userExists(req.params.username); + const isLoggedInUser = req.user && req.user.username === req.params.username; + const canAccess = exists && isLoggedInUser; + sendHtml(req, res, canAccess); }); router.get('/account', (req, res) => { @@ -121,16 +122,17 @@ router.get('/about', (req, res) => { res.send(renderIndex()); }); -router.get('/:username/collections/:id', (req, res) => { - collectionForUserExists(req.params.username, req.params.id, (exists) => - exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html)) +router.get('/:username/collections/:id', async (req, res) => { + const exists = await collectionForUserExists( + req.params.username, + req.params.id ); + sendHtml(req, res, exists); }); -router.get('/:username/collections', (req, res) => { - userExists(req.params.username, (exists) => - exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html)) - ); +router.get('/:username/collections', async (req, res) => { + const exists = await userExists(req.params.username); + sendHtml(req, res, exists); }); router.get('/privacy-policy', (req, res) => { diff --git a/server/views/index.js b/server/views/index.js index 3bf23e32c6..19498ad4e7 100644 --- a/server/views/index.js +++ b/server/views/index.js @@ -1,3 +1,5 @@ +import get404Sketch from './404Page'; + export function renderIndex() { const assetsManifest = process.env.webpackAssets && JSON.parse(process.env.webpackAssets); return ` @@ -46,3 +48,18 @@ export function renderIndex() { `; } + +/** + * Send a 404 page if `exists` is false. + * @param {import('express').e.Request} req + * @param {import('express').e.Response} res + * @param {boolean} [exists] + */ +export default function sendHtml(req, res, exists = true) { + if (!exists) { + res.status(404); + get404Sketch((html) => res.send(html)); + } else { + res.send(renderIndex()); + } +};