Skip to content

Commit

Permalink
Merge pull request #1 from fdounis/refactoring-feature-branch
Browse files Browse the repository at this point in the history
Refactor Flags.getFlagIdsWithFilters Function to Reduce Cognitive Complexity
  • Loading branch information
fdounis authored Sep 2, 2024
2 parents 09ad0fe + 6f6b003 commit 99b68a2
Showing 1 changed file with 47 additions and 28 deletions.
75 changes: 47 additions & 28 deletions src/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,34 @@ Flags.getCount = async function ({ uid, filters, query }) {
};

Flags.getFlagIdsWithFilters = async function ({ filters, uid, query }) {
let sets = [];
const orSets = [];
initializeFilters(filters);

const { sets, orSets } = buildSets(filters, uid);

let flagIds = await getFlagIdsFromSets(sets);

if (orSets.length) {
const _flagIds = await getFlagIdsFromOrSets(orSets);
flagIds = mergeFlagIds(flagIds, _flagIds, sets.length > 0);
}

const result = await plugins.hooks.fire('filter:flags.getFlagIdsWithFilters', {
filters,
uid,
query,
flagIds,
});
return result.flagIds;
};

// Default filter
function initializeFilters(filters) {
filters.page = filters.hasOwnProperty('page') ? Math.abs(parseInt(filters.page, 10) || 1) : 1;
filters.perPage = filters.hasOwnProperty('perPage') ? Math.abs(parseInt(filters.perPage, 10) || 20) : 20;
}

function buildSets(filters, uid) {
let sets = [];
const orSets = [];

for (const type of Object.keys(filters)) {
if (Flags._filters.hasOwnProperty(type)) {
Expand All @@ -149,39 +171,36 @@ Flags.getFlagIdsWithFilters = async function ({ filters, uid, query }) {
winston.warn(`[flags/list] No flag filter type found: ${type}`);
}
}
sets = (sets.length || orSets.length) ? sets : ['flags:datetime']; // No filter default

let flagIds = [];
if (!(sets.length || orSets.length)) {
sets = ['flags:datetime']; // No filter default
}

return { sets, orSets };
}

async function getFlagIdsFromSets(sets) {
if (sets.length === 1) {
flagIds = await db.getSortedSetRevRange(sets[0], 0, -1);
return await db.getSortedSetRevRange(sets[0], 0, -1);
} else if (sets.length > 1) {
flagIds = await db.getSortedSetRevIntersect({ sets: sets, start: 0, stop: -1, aggregate: 'MAX' });
return await db.getSortedSetRevIntersect({ sets: sets, start: 0, stop: -1, aggregate: 'MAX' });
}
return [];
}

if (orSets.length) {
let _flagIds = await Promise.all(orSets.map(async orSet => await db.getSortedSetRevUnion({ sets: orSet, start: 0, stop: -1, aggregate: 'MAX' })));

// Each individual orSet is ANDed together to construct the final list of flagIds
_flagIds = _.intersection(..._flagIds);
async function getFlagIdsFromOrSets(orSets) {
const _flagIds = await Promise.all(orSets.map(async orSet => db.getSortedSetRevUnion({ sets: orSet, start: 0, stop: -1, aggregate: 'MAX' })));
return _.intersection(..._flagIds);
}

// Merge with flagIds returned by sets
if (sets.length) {
// If flag ids are already present, return a subset of flags that are in both sets
flagIds = _.intersection(flagIds, _flagIds);
} else {
// Otherwise, return all flags returned via orSets
flagIds = _.union(flagIds, _flagIds);
}
function mergeFlagIds(flagIds, _flagIds, hasSets) {
if (hasSets) {
return _.intersection(flagIds, _flagIds);
}
return _.union(flagIds, _flagIds);
}


const result = await plugins.hooks.fire('filter:flags.getFlagIdsWithFilters', {
filters,
uid,
query,
flagIds,
});
return result.flagIds;
};

Flags.list = async function (data) {
const filters = data.filters || {};
Expand Down

0 comments on commit 99b68a2

Please sign in to comment.