Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring the function for listing flagged content in src/controllers/mods.js #12

Closed
wants to merge 2 commits into from

Conversation

mayTkhin24
Copy link

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.

@mayTkhin24 mayTkhin24 closed this Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants