Refactoring the function for listing flagged content in src/controllers/mods.js #12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The function for listing flagged content, "modsController.flags.list", was broken down into helper functions to reduce cognitive complexity.
The helper functions are as follows:
initialize(req): Fetches necessary data like user permissions, moderated categories, and valid filter/sort options. It returns an array of these values for use in other functions.
checkAccess(isAdminOrGlobalMod, moderatedCids, req, res): Ensures the user has permission to view flags. If they don’t, it denies access; otherwise, it sets the user's moderated categories in res.locals.
parseFilters(req, filters): Extracts and validates filters from the request query. Adds filters that are present and non-empty in the query to a new filters object.
adjustFiltersForMod(filters, res): Adjusts the cid (category ID) filter to ensure the user only sees categories they moderate. If no cid is provided, it defaults to their moderated categories.
determineHasFilter(filters): Checks if any filters, other than pagination, are applied. Returns true if there are active filters.
parseSort(req, sorts): Parses the sort parameter from the request. If valid, it returns the specified sort option, or returns undefined if the sort is 'newest'.
fetchData(req, filters, sort): Fetches flags, analytics, and selected category data based on the filters and sort parameters. Uses Promise.all to handle multiple asynchronous calls.
getSelectedUsers(filters): Fetches detailed user information (username, picture) for users associated with certain filters (assignee, reporter, target user). Returns an object with user details for these filters.