Skip to content

Commit 5d0d837

Browse files
committed
some fixes from code review
1 parent d0c0837 commit 5d0d837

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

lib/helpers.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ async function resolveAuthLevel(schema, options, doc) {
44
// Look into options the options and try to find authLevels. Always prefer to take
55
// authLevels from the direct authLevel option as opposed to the computed
66
// ones from getAuthLevel in the schema object.
7-
let authLevelsIn = [];
7+
let authLevels = [];
88
if (options) {
99
if (options.authLevel) {
10-
authLevelsIn = options.authLevel;
10+
authLevels = _.castArray(options.authLevel);
1111
} else if (typeof schema.getAuthLevel === 'function') {
12-
authLevelsIn = schema.getAuthLevel(options.authPayload, doc);
12+
authLevels = _.castArray(await schema.getAuthLevel(options.authPayload, doc));
1313
}
1414
}
1515

16-
const authLevels = _.castArray(await Promise.resolve(authLevelsIn));
17-
1816
// Add `defaults` to the list of levels since you should always be able to do what's specified
1917
// in defaults.
2018
authLevels.push('defaults');
@@ -117,18 +115,18 @@ async function sanitizeDocument(schema, options, doc) {
117115
async function sanitizeDocumentList(schema, options, docs) {
118116
const multi = _.isArrayLike(docs);
119117
const docList = _.castArray(docs);
118+
const sanitizeAndAddOptions = (doc) => {
119+
const upgradedOptions = _.isEmpty(schema.pathsWithPermissionedSchemas)
120+
? options
121+
: _.merge({}, options, { authPayload: { originalDoc: doc } });
122+
return sanitizeDocument(schema, upgradedOptions, doc);
123+
};
120124

121-
const filteredResult = _.chain(await Promise.all(_.map(docList, async (doc) => {
122-
const upgradedOptions = _.isEmpty(schema.pathsWithPermissionedSchemas)
123-
? options
124-
: _.merge({}, options, { authPayload: { originalDoc: doc } });
125-
126-
return await sanitizeDocument(schema, upgradedOptions, doc);
127-
})))
128-
.filter(docList)
129-
.value();
125+
const filteredResult = (
126+
await Promise.all(docList.map(sanitizeAndAddOptions))
127+
).filter(doc => !doc);
130128

131-
return multi ? Promise.all(filteredResult) : filteredResult[0];
129+
return multi ? (filteredResult) : filteredResult[0];
132130
}
133131

134132
function getUpdatePaths(updates) {

0 commit comments

Comments
 (0)