diff --git a/server/vault.api.js b/server/vault.api.js index 6a5b58140..1406895a6 100644 --- a/server/vault.api.js +++ b/server/vault.api.js @@ -19,25 +19,8 @@ const buildAuthorConditions = (author) => { return { authors: author }; }; -//"$and": [ {"published": true}, {"$text": { "$search": "titleString", "$caseSensitive": false } }, { "authors" : "authorString"}] -//is a good example of a query constructed with this function - -const handleErrorResponse = (res, error, functionName) => { - const status = error.response?.status || 500; - const message = - status === 503 ? 'Service Unavailable' : 'Internal Server Error'; - - console.error(`Error in ${functionName}:`, error); - - return res.status(status).json({ - errorCode: status.toString(), - message: `Error in function ${functionName}: ${message}`, - }); -}; - -const buildBrewsQuery = (legacy, v3) => { - const brewsQuery = { published: true }; - if (legacy === 'true' && v3 === 'true') return { published: true }; +const buildRendererConditions = (legacy, v3) => { + const brewsQuery = {}; if (legacy === 'true' && v3 !== 'true') { brewsQuery.renderer = 'legacy'; @@ -48,81 +31,96 @@ const buildBrewsQuery = (legacy, v3) => { return brewsQuery; }; -const vault = { - findBrews: async (req, res) => { - try { - const title = req.query.title || ''; - const author = req.query.author || ''; - const page = Math.max(parseInt(req.query.page) || 1, 1); - const mincount = 10; - const count = Math.max(parseInt(req.query.count) || 20, mincount); - const skip = (page - 1) * count; +// Function to find brews +const findBrews = async (req, res) => { + const title = req.query.title || ''; + const author = req.query.author || ''; + const page = Math.max(parseInt(req.query.page) || 1, 1); + const mincount = 10; + const count = Math.max(parseInt(req.query.count) || 20, mincount); + const skip = (page - 1) * count; - const brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3); - const titleConditions = buildTitleConditions(title); - const authorConditions = buildAuthorConditions(author); + const brewsQuery = buildRendererConditions(req.query.legacy, req.query.v3); + const titleConditions = buildTitleConditions(title); + const authorConditions = buildAuthorConditions(author); - const combinedQuery = { - $and: [brewsQuery, titleConditions, authorConditions], - }; + const combinedQuery = { + $and: [ + { published: true }, + brewsQuery, + titleConditions, + authorConditions, + ], + }; - const projection = { - editId: 0, - googleId: 0, - text: 0, - textBin: 0, - version: 0, - thumbnail: 0, - }; - - const brews = await HomebrewModel.find(combinedQuery, projection) - .skip(skip) - .limit(count) - .maxTimeMS(5000) - .exec(); + const projection = { + editId: 0, + googleId: 0, + text: 0, + textBin: 0, + version: 0, + thumbnail: 0, + }; + await HomebrewModel.find(combinedQuery, projection) + .skip(skip) + .limit(count) + .maxTimeMS(5000) + .exec() + .then((brews) => { console.log( 'Query in findBrews: ', JSON.stringify(combinedQuery, null, 2) ); - return res.json({ brews, page }); - } catch (error) { + res.json({ brews, page }); + }) + .catch((error) => { console.error(error); - return handleErrorResponse(res, error, 'findBrews'); - } - }, + res.status(500).json({ + error: 'Error finding brews in Vault search', + HBErrorCode: '99', + }); + }); +}; - findTotal: async (req, res) => { - try { - const title = req.query.title || ''; - const author = req.query.author || ''; +// Function to find total brews +const findTotal = async (req, res) => { + const title = req.query.title || ''; + const author = req.query.author || ''; - const brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3); - const titleConditions = buildTitleConditions(title); - const authorConditions = buildAuthorConditions(author); + const brewsQuery = buildRendererConditions(req.query.legacy, req.query.v3); + const titleConditions = buildTitleConditions(title); + const authorConditions = buildAuthorConditions(author); - const combinedQuery = { - $and: [brewsQuery, titleConditions, authorConditions], - }; + const combinedQuery = { + $and: [ + { published: true }, + brewsQuery, + titleConditions, + authorConditions, + ], + }; - const totalBrews = await HomebrewModel.countDocuments( - combinedQuery - ); + await HomebrewModel.countDocuments(combinedQuery) + .then((totalBrews) => { console.log( 'when returning, the total of brews is ', totalBrews, 'for the query', JSON.stringify(combinedQuery) ); - return res.json({ totalBrews }); - } catch (error) { + res.json({ totalBrews }); + }) + .catch((error) => { console.error(error); - return handleErrorResponse(res, error, 'findTotal'); - } - }, + res.status(500).json({ + error: 'Error finding brews in Vault search find total search', + HBErrorCode: '99', + }); + }); }; -router.get('/api/vault/total', asyncHandler(vault.findTotal)); -router.get('/api/vault', asyncHandler(vault.findBrews)); +router.get('/api/vault/total', asyncHandler(findTotal)); +router.get('/api/vault', asyncHandler(findBrews)); module.exports = router;