mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-04 19:02:38 +00:00
Refactor and clean up "getBrew()"
Some redundant logic and sprawling formatting
This commit is contained in:
@@ -87,76 +87,66 @@ const api = {
|
|||||||
// Create middleware with the accessType passed in as part of the scope
|
// Create middleware with the accessType passed in as part of the scope
|
||||||
return async (req, res, next)=>{
|
return async (req, res, next)=>{
|
||||||
// Get relevant IDs for the brew
|
// Get relevant IDs for the brew
|
||||||
const { id, googleId } = api.getId(req);
|
let { id, googleId } = api.getId(req);
|
||||||
|
|
||||||
const accessMap = {
|
const accessMap = {
|
||||||
edit : { editId: id },
|
edit : { editId: id },
|
||||||
share : { shareId: id },
|
share : { shareId: id },
|
||||||
admin : {
|
admin : { $or : [{ editId: id }, { shareId: id }] }
|
||||||
$or : [
|
|
||||||
{ editId: id },
|
|
||||||
{ shareId: id },
|
|
||||||
] }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Try to find the document in the Homebrewery database -- if it doesn't exist, that's fine.
|
// Try to find the document in the Homebrewery database -- if it doesn't exist, that's fine.
|
||||||
let stub = await HomebrewModel.get(accessMap[accessType])
|
let stub = await HomebrewModel.get(accessMap[accessType])
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
if(googleId) {
|
if(googleId)
|
||||||
console.warn(`Unable to find document stub for ${accessType}Id ${id}`);
|
console.warn(`Unable to find document stub for ${accessType}Id ${id}`);
|
||||||
} else {
|
else
|
||||||
console.warn(err);
|
console.warn(err);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
stub = stub?.toObject();
|
stub = stub?.toObject();
|
||||||
|
googleId ??= stub?.googleId;
|
||||||
|
|
||||||
|
const isAuthor = stub?.authors?.includes(req.account?.username);
|
||||||
|
const isInvited = stub?.invitedAuthors?.includes(req.account?.username);
|
||||||
|
|
||||||
|
if(accessType === 'edit' && !(isOwner || isAuthor || isInvited)) {
|
||||||
|
const accessError = { name: 'Access Error', status: 401, authors: stub.authors, brewTitle: stub.title, shareId: stub.shareId };
|
||||||
|
if(req.account)
|
||||||
|
throw { ...accessError, message: 'User is not an Author', HBErrorCode: '03' };
|
||||||
|
else
|
||||||
|
throw { ...accessError, message: 'User is not logged in', HBErrorCode: '04' };
|
||||||
|
}
|
||||||
|
|
||||||
if(stub?.lock?.locked && accessType != 'edit') {
|
if(stub?.lock?.locked && accessType != 'edit') {
|
||||||
throw { HBErrorCode: '51', code: stub.lock.code, message: stub.lock.shareMessage, brewId: stub.shareId, brewTitle: stub.title };
|
throw { HBErrorCode: '51', code: stub.lock.code, message: stub.lock.shareMessage, brewId: stub.shareId, brewTitle: stub.title };
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there is a google id, try to find the google brew
|
// If there is a google id, try to find the google brew
|
||||||
if(!stubOnly && (googleId || stub?.googleId)) {
|
|
||||||
let googleError;
|
|
||||||
const googleBrew = await GoogleActions.getGoogleBrew(googleId || stub?.googleId, id, accessType)
|
const googleBrew = await GoogleActions.getGoogleBrew(googleId || stub?.googleId, id, accessType)
|
||||||
.catch((err)=>{
|
if(!stubOnly && googleId) {
|
||||||
googleError = err;
|
|
||||||
|
.catch((googleError)=>{
|
||||||
|
const reason = googleError.errors?.[0].reason;
|
||||||
|
if(reason == 'notFound')
|
||||||
|
throw { ...googleError, HBErrorCode: '02', authors: stub?.authors, account: req.account?.username };
|
||||||
|
else
|
||||||
|
throw { ...googleError, HBErrorCode: '01' };
|
||||||
});
|
});
|
||||||
// Throw any error caught while attempting to retrieve Google brew.
|
|
||||||
if(googleError) {
|
|
||||||
const reason = googleError.errors?.[0].reason;
|
|
||||||
if(reason == 'notFound') {
|
|
||||||
throw { ...googleError, HBErrorCode: '02', authors: stub?.authors, account: req.account?.username };
|
|
||||||
} else {
|
|
||||||
throw { ...googleError, HBErrorCode: '01' };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Combine the Homebrewery stub with the google brew, or if the stub doesn't exist just use the google brew
|
// Combine the Homebrewery stub with the google brew, or if the stub doesn't exist just use the google brew
|
||||||
stub = stub ? _.assign({ ...api.excludeStubProps(stub), stubbed: true }, api.excludeGoogleProps(googleBrew)) : googleBrew;
|
stub = stub ? _.assign({ ...api.excludeStubProps(stub), stubbed: true }, api.excludeGoogleProps(googleBrew)) : googleBrew;
|
||||||
}
|
}
|
||||||
const authorsExist = stub?.authors?.length > 0;
|
|
||||||
const isAuthor = stub?.authors?.includes(req.account?.username);
|
|
||||||
const isInvited = stub?.invitedAuthors?.includes(req.account?.username);
|
|
||||||
if(accessType === 'edit' && (authorsExist && !(isAuthor || isInvited))) {
|
|
||||||
const accessError = { name: 'Access Error', status: 401 };
|
|
||||||
if(req.account){
|
|
||||||
throw { ...accessError, message: 'User is not an Author', HBErrorCode: '03', authors: stub.authors, brewTitle: stub.title, shareId: stub.shareId };
|
|
||||||
}
|
|
||||||
throw { ...accessError, message: 'User is not logged in', HBErrorCode: '04', authors: stub.authors, brewTitle: stub.title, shareId: stub.shareId };
|
|
||||||
}
|
|
||||||
|
|
||||||
// If after all of that we still don't have a brew, throw an exception
|
// If after all of that we still don't have a brew, throw an exception
|
||||||
if(!stub && !stubOnly) {
|
if(!stub)
|
||||||
throw { name: 'BrewLoad Error', message: 'Brew not found', status: 404, HBErrorCode: '05', accessType: accessType, brewId: id };
|
throw { name: 'BrewLoad Error', message: 'Brew not found', status: 404, HBErrorCode: '05', accessType: accessType, brewId: id };
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up brew: fill in missing fields with defaults / fix old invalid values
|
// Clean up brew: fill in missing fields with defaults / fix old invalid values
|
||||||
if(stub) {
|
stub.tags = stub.tags || undefined; // Clear empty strings
|
||||||
stub.tags = stub.tags || undefined; // Clear empty strings
|
stub.renderer = stub.renderer || undefined; // Clear empty strings
|
||||||
stub.renderer = stub.renderer || undefined; // Clear empty strings
|
stub = _.defaults(stub, DEFAULT_BREW_LOAD); // Fill in blank fields
|
||||||
stub = _.defaults(stub, DEFAULT_BREW_LOAD); // Fill in blank fields
|
|
||||||
}
|
|
||||||
|
|
||||||
req.brew = stub ?? {};
|
req.brew = stub;
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user