HabitRPG/habitrpg

View on GitHub
website/server/models/challenge.js

Summary

Maintainability
D
2 days
Test Coverage

File challenge.js has 321 lines of code (exceeds 250 allowed). Consider refactoring.
Open

import mongoose from 'mongoose';
import validator from 'validator';
import _ from 'lodash';
import { TaskQueue } from 'cwait';
import baseModel from '../libs/baseModel';
Severity: Minor
Found in website/server/models/challenge.js - About 3 hrs to fix

    Function closeChal has 62 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    schema.methods.closeChal = async function closeChal (broken = {}) {
      const challenge = this;
    
      const { winner } = broken;
      const brokenReason = broken.broken;
    Severity: Major
    Found in website/server/models/challenge.js - About 2 hrs to fix

      Function syncChallengeTasksToUser has 55 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

      schema.methods.syncTasksToUser = async function syncChallengeTasksToUser (user) {
        const challenge = this;
        challenge.shortName = challenge.shortName || challenge.name;
      
        // Sync tags
      Severity: Major
      Found in website/server/models/challenge.js - About 2 hrs to fix

        Function _addTaskFn has 37 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        async function _addTaskFn (challenge, tasks, memberId) {
          const updateTasksOrderQ = { $push: {} };
          const toSave = [];
        
          tasks.forEach(chalTask => {
        Severity: Minor
        Found in website/server/models/challenge.js - About 1 hr to fix

          Function closeChal has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
          Open

          schema.methods.closeChal = async function closeChal (broken = {}) {
            const challenge = this;
          
            const { winner } = broken;
            const brokenReason = broken.broken;
          Severity: Minor
          Found in website/server/models/challenge.js - 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 challengeUnlinkTasks has 26 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

          schema.methods.unlinkTasks = async function challengeUnlinkTasks (user, keep, saveUser = true) {
            const challengeId = this._id;
            const findQuery = {
              userId: user._id,
              'challenge.id': challengeId,
          Severity: Minor
          Found in website/server/models/challenge.js - About 1 hr to fix

            Function canJoinChallenge has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
            Open

            schema.methods.canJoin = function canJoinChallenge (user, group) {
              // for when leader has left private group that contains the challenge
              if (this.isLeader(user)) return true;
              if (!group) return false;
              if (group.type === 'guild' && group.privacy === 'public') {
            Severity: Minor
            Found in website/server/models/challenge.js - About 35 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

            Avoid too many return statements within this function.
            Open

              return user.getGroups().indexOf(this.group) !== -1;
            Severity: Major
            Found in website/server/models/challenge.js - About 30 mins to fix

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

              schema.methods.unlinkTasks = async function challengeUnlinkTasks (user, keep, saveUser = true) {
                const challengeId = this._id;
                const findQuery = {
                  userId: user._id,
                  'challenge.id': challengeId,
              Severity: Minor
              Found in website/server/models/challenge.js - 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

              Identical blocks of code found in 3 locations. Consider refactoring.
              Open

                tasksOrder: {
                  habits: [{ $type: String, ref: 'Task' }],
                  dailys: [{ $type: String, ref: 'Task' }],
                  todos: [{ $type: String, ref: 'Task' }],
                  rewards: [{ $type: String, ref: 'Task' }],
              Severity: Major
              Found in website/server/models/challenge.js and 2 other locations - About 2 hrs to fix
              website/server/models/group.js on lines 125..130
              website/server/models/user/schema.js on lines 688..693

              Duplicated Code

              Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

              Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

              When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

              Tuning

              This issue has a mass of 78.

              We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

              The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

              If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

              See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

              Refactorings

              Further Reading

              Identical blocks of code found in 2 locations. Consider refactoring.
              Open

                  if (!winnerCanGetGems) {
                    await User.updateOne(
                      { _id: challenge.leader },
                      { $inc: { balance: challenge.prize / 4 } },
                    ).exec();
              Severity: Major
              Found in website/server/models/challenge.js and 1 other location - About 1 hr to fix
              website/server/models/challenge.js on lines 367..370

              Duplicated Code

              Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

              Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

              When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

              Tuning

              This issue has a mass of 55.

              We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

              The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

              If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

              See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

              Refactorings

              Further Reading

              Identical blocks of code found in 2 locations. Consider refactoring.
              Open

                if (brokenReason === 'CHALLENGE_DELETED') {
                  await User.updateOne({ _id: challenge.leader }, { $inc: { balance: challenge.prize / 4 } })
                    .exec();
                }
              Severity: Major
              Found in website/server/models/challenge.js and 1 other location - About 1 hr to fix
              website/server/models/challenge.js on lines 382..389

              Duplicated Code

              Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

              Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

              When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

              Tuning

              This issue has a mass of 55.

              We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

              The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

              If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

              See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

              Refactorings

              Further Reading

              There are no issues that match your filters.

              Category
              Status