betagouv/service-national-universel

View on GitHub
packages/lib/src/roles.ts

Summary

Maintainability
D
2 days
Test Coverage

File roles.ts has 982 lines of code (exceeds 700 allowed). Consider refactoring.
Open

import { ReferentDto, UserDto } from "./dto";
import { region2department } from "./region-and-departments";
import { isNowBetweenDates } from "./utils/date";
import { LIMIT_DATE_ESTIMATED_SEATS, LIMIT_DATE_TOTAL_SEATS, STATUS_CLASSE } from "./constants/constants";
import { ClasseType, SessionPhase1Type } from "./mongoSchema";
Severity: Major
Found in packages/lib/src/roles.ts - About 1 day to fix

    Function canSearchInElasticSearch has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
    Open

    function canSearchInElasticSearch(actor, index) {
      if (index === "mission") {
        return [ROLES.ADMIN, ROLES.REFERENT_REGION, ROLES.REFERENT_DEPARTMENT, ROLES.RESPONSIBLE, ROLES.SUPERVISOR].includes(actor.role);
      } else if (index === "school" || index === "schoolramses") {
        return [ROLES.ADMIN, ROLES.REFERENT_REGION, ROLES.REFERENT_DEPARTMENT, ROLES.RESPONSIBLE, ROLES.SUPERVISOR, ROLES.HEAD_CENTER, ROLES.VISITOR].includes(actor.role);
    Severity: Minor
    Found in packages/lib/src/roles.ts - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function canUpdateReferent has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
    Open

    function canUpdateReferent({ actor, originalTarget, modifiedTarget = null, structure }: CanUpdateReferent) {
      const isMe = actor._id?.toString() === originalTarget._id?.toString();
      const isAdmin = actor.role === ROLES.ADMIN;
      const withoutChangingRole = modifiedTarget === null || !("role" in modifiedTarget) || modifiedTarget.role === originalTarget.role;
      const isResponsibleModifyingResponsibleWithoutChangingRole =
    Severity: Minor
    Found in packages/lib/src/roles.ts - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function canSigninAs has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
    Open

    const canSigninAs = (actor, target, source) => {
      if (isAdmin(actor)) return true;
      if (!isReferentRegDep(actor)) return false;
    
      if (source === "referent") {
    Severity: Minor
    Found in packages/lib/src/roles.ts - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Avoid too many return statements within this function.
    Open

        return [ROLES.ADMIN, ROLES.REFERENT_REGION, ROLES.REFERENT_DEPARTMENT, ROLES.TRANSPORTER, ROLES.ADMINISTRATEUR_CLE, ROLES.REFERENT_CLASSE].includes(actor.role);
    Severity: Major
    Found in packages/lib/src/roles.ts - About 30 mins to fix

      Avoid too many return statements within this function.
      Open

        return false;
      Severity: Major
      Found in packages/lib/src/roles.ts - About 30 mins to fix

        Avoid too many return statements within this function.
        Open

            return [ROLES.ADMINISTRATEUR_CLE, ROLES.REFERENT_CLASSE].includes(actor.role);
        Severity: Major
        Found in packages/lib/src/roles.ts - About 30 mins to fix

          Avoid too many return statements within this function.
          Open

              return [ROLES.ADMIN, ROLES.REFERENT_REGION, ROLES.REFERENT_DEPARTMENT, ROLES.ADMINISTRATEUR_CLE, ROLES.REFERENT_CLASSE, ROLES.TRANSPORTER].includes(actor.role);
          Severity: Major
          Found in packages/lib/src/roles.ts - About 30 mins to fix

            Avoid too many return statements within this function.
            Open

                return [ROLES.ADMIN, ROLES.REFERENT_REGION, ROLES.REFERENT_DEPARTMENT].includes(actor.role);
            Severity: Major
            Found in packages/lib/src/roles.ts - About 30 mins to fix

              Function canDeleteReferent has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
              Open

              function canDeleteReferent({ actor, originalTarget, structure }) {
                // TODO: we must handle rights more precisely.
                // See: https://trello.com/c/Wv2TrQnQ/383-admin-ajouter-onglet-utilisateurs-pour-les-r%C3%A9f%C3%A9rents
                const isMe = actor._id === originalTarget._id;
              
              
              Severity: Minor
              Found in packages/lib/src/roles.ts - About 25 mins to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Function canAssignManually has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
              Open

              function canAssignManually(actor, young, cohort) {
                if (!cohort) return false;
                return (
                  (actor.role === ROLES.ADMIN && cohort.manualAffectionOpenForAdmin) ||
                  (actor.role === ROLES.REFERENT_REGION && actor.region === young.region && cohort.manualAffectionOpenForReferentRegion) ||
              Severity: Minor
              Found in packages/lib/src/roles.ts - About 25 mins to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              There are no issues that match your filters.

              Category
              Status