Skip to content

Commit

Permalink
tidy: invert negated calls to Option.isEmpty()/isDefined() (#1289)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn authored Nov 25, 2024
1 parent 746ff05 commit 1c7ea7a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/http/preprocessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const emptyAuthInjector = ({ Auth }, context) => context.with({ auth: Auth.by(nu
const authHandler = ({ Sessions, Users, Auth }, context) => {
const authBySessionToken = (token, onFailure = noop) => Sessions.getByBearerToken(token)
.then((session) => {
if (!session.isDefined()) return onFailure();
if (session.isEmpty()) return onFailure();
return context.with({ auth: Auth.by(session.get()) });
});

Expand Down Expand Up @@ -120,7 +120,7 @@ const authHandler = ({ Sessions, Users, Auth }, context) => {
// if non-GET run authentication as usual but we'll have to check CSRF afterwards.
return maybeSession.then((cxt) => { // we have to use cxt rather than context for the linter
// if authentication failed anyway, just do nothing.
if ((cxt == null) || !cxt.auth.session.isDefined()) return;
if ((cxt == null) || cxt.auth.session.isEmpty()) return;

const csrf = urlDecode(cxt.body.__csrf);
if (csrf.isEmpty() || isBlank(csrf.get()) || (cxt.auth.session.get().csrf !== csrf.get())) {
Expand Down
10 changes: 5 additions & 5 deletions lib/model/query/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ const _updateEntity = (dataset, entityData, submissionId, submissionDef, submiss

// Get version of entity on the server
let serverEntity = await Entities.getById(dataset.id, clientEntity.uuid, QueryOptions.forUpdate);
if (!serverEntity.isDefined()) {
if (serverEntity.isEmpty()) {
// We probably never get into this case because computeBaseVersion also checks for the existence of the entity
// and returns an entity def associated with a top level entity.
throw Problem.user.entityNotFound({ entityUuid: clientEntity.uuid, datasetName: dataset.name });
Expand Down Expand Up @@ -380,7 +380,7 @@ const _computeBaseVersion = (eventId, dataset, clientEntity, submissionDef, forc
const condition = { version: clientEntity.def.baseVersion };
const maybeDef = await Entities.getDef(dataset.id, clientEntity.uuid, new QueryOptions({ condition }));

if (!maybeDef.isDefined()) {
if (maybeDef.isEmpty()) {
// If the def doesn't exist, check if the version doesnt exist or the whole entity doesnt exist
// There are different problems for each case
const maybeEntity = await Entities.getById(dataset.id, clientEntity.uuid);
Expand All @@ -405,7 +405,7 @@ const _computeBaseVersion = (eventId, dataset, clientEntity, submissionDef, forc

const baseEntityVersion = await Entities.getDef(dataset.id, clientEntity.uuid, new QueryOptions({ condition }));

if (!baseEntityVersion.isDefined()) {
if (baseEntityVersion.isEmpty()) {
if (forceOutOfOrderProcessing) {
// If the base version doesn't exist but we forcing the update anyway, use the latest version on the server as the base.
// If that can't be found, throw an error for _processSubmissionEvent to catch so it can try create instead of update.
Expand Down Expand Up @@ -441,7 +441,7 @@ const _processSubmissionEvent = (event, parentEvent) => async ({ Audits, Dataset

const form = await Forms.getByActeeId(event.acteeId);
// If form is deleted/purged then submission won't be there either.
if (!form.isDefined())
if (form.isEmpty())
return null;

const existingEntity = await Entities.getDefBySubmissionId(submissionId);
Expand All @@ -459,7 +459,7 @@ const _processSubmissionEvent = (event, parentEvent) => async ({ Audits, Dataset
const submission = await Submissions.getSubAndDefById(submissionDefId);

// If Submission is deleted/purged - Safety check, will not be true at this line
if (!submission.isDefined())
if (submission.isEmpty())
return null;

// Don't process draft submissions
Expand Down
2 changes: 1 addition & 1 deletion lib/resources/oidc.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function errorToFrontend(req, res, errorCode) {

async function getUserByEmail({ Users }, email) {
const userOption = await Users.getByEmail(email);
if (!userOption.isDefined()) return;
if (userOption.isEmpty()) return;

const user = userOption.get();

Expand Down
3 changes: 1 addition & 2 deletions lib/task/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// except according to the terms contained in the LICENSE file.

const config = require('config');
const { isEmpty } = require('ramda');

const { task } = require('./task');
const { buildSubmission } = require('../data/analytics');
Expand All @@ -25,7 +24,7 @@ const runAnalytics = task.withContainer(({ Analytics, Audits, odkAnalytics }) =>
.then((configuration) => ((configuration.value.enabled === false)
? { sent: false, message: 'Analytics disabled in config' }
: Analytics.getLatestAudit()
.then((au) => ((!isEmpty(au) && au.get().details.success === true && !force)
.then((au) => ((au.isDefined() && au.get().details.success === true && !force)
? { sent: false, message: `Analytics sent recently: ${au.get().loggedAt}` }
: Analytics.previewMetrics()
.then((data) => {
Expand Down

0 comments on commit 1c7ea7a

Please sign in to comment.