0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-06 18:42:40 +00:00

Cleanup; added more fields to DEFAULT_BREW

This commit is contained in:
Trevor Buckner
2023-01-04 17:33:23 -05:00
parent e1765cad41
commit e02bde5eed
3 changed files with 25 additions and 20 deletions

View File

@@ -407,7 +407,10 @@ if(isLocalEnvironment){
//Render the page //Render the page
const templateFn = require('./../client/template.js'); const templateFn = require('./../client/template.js');
app.use(asyncHandler(async (req, res, next)=>{ app.use(asyncHandler(async (req, res, next)=>{
// Assuming we have not received a brew yet, populate with default values
const brew = _.defaults(req.brew, DEFAULT_BREW); const brew = _.defaults(req.brew, DEFAULT_BREW);
// Create configuration object // Create configuration object
const configuration = { const configuration = {
local : isLocalEnvironment, local : isLocalEnvironment,

View File

@@ -1,30 +1,36 @@
const _ = require('lodash'); const _ = require('lodash');
// Default brew properties in most cases // Default properties for newly-created brews
const DEFAULT_BREW = { const DEFAULT_BREW = {
text : '',
editId : null,
shareId : null,
title : 'Untitled Brew', title : 'Untitled Brew',
text : '',
style : undefined,
description : '', description : '',
editId : undefined,
shareId : undefined,
createdAt : undefined,
updatedAt : undefined,
renderer : 'V3', renderer : 'V3',
theme : '5ePHB',
authors : [],
tags : [], tags : [],
systems : [], systems : [],
thumbnail : '', thumbnail : '',
published : false, published : false,
pageCount : 1, pageCount : 1,
theme : '5ePHB' gDrive : false,
trashed : false
}; };
// Default brew properties for loading // Default values for older brews with missing properties
const DEFAULT_BREW_LOAD = {}; // e.g., missing "renderer" is assumed to be "legacy"
_.defaults(DEFAULT_BREW_LOAD, const DEFAULT_BREW_LOAD = _.defaults(
{ {
renderer : 'legacy', renderer : 'legacy',
published : true
}, },
DEFAULT_BREW); DEFAULT_BREW);
module.exports = { module.exports = {
DEFAULT_BREW, DEFAULT_BREW,
DEFAULT_BREW_LOAD DEFAULT_BREW_LOAD
}; };

View File

@@ -73,17 +73,13 @@ If you believe you should have access to this brew, ask the file owner to invite
throw 'Brew not found in Homebrewery database or Google Drive'; throw 'Brew not found in Homebrewery database or Google Drive';
} }
if(typeof stub?.tags === 'string') { // Clean up brew: fill in missing fields with defaults / fix old invalid values
stub.tags = []; stub.tags = stub.tags || undefined; // Clear empty strings
} stub.renderer = stub.renderer || undefined; // Clear empty strings
stub = _.defaults(stub, DEFAULT_BREW_LOAD); // Fill in blank fields
// Use _.assignWith instead of _.defaults - does this need to be replicated at all other uses of _.defaults???
_.assignWith(stub, DEFAULT_BREW_LOAD, (objValue, srcValue)=>{
if(typeof objValue === 'boolean') return objValue;
return objValue || srcValue;
});
req.brew = stub; req.brew = stub;
next(); next();
}; };
}; };