HabitRPG/habitrpg

View on GitHub
website/server/libs/cron.js

Summary

Maintainability
D
3 days
Test Coverage

Function cron has 180 lines of code (exceeds 25 allowed). Consider refactoring.
Open

export async function cron (options = {}) {
  const {
    user, tasksByType, analytics, now = new Date(), daysMissed, timezoneUtcOffsetFromUserPrefs,
  } = options;
  let _progress = { down: 0, up: 0, collectedItems: 0 };
Severity: Major
Found in website/server/libs/cron.js - About 7 hrs to fix

    File cron.js has 367 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    import moment from 'moment';
    import _ from 'lodash';
    import cloneDeep from 'lodash/cloneDeep';
    import nconf from 'nconf';
    import { model as User } from '../models/user';
    Severity: Minor
    Found in website/server/libs/cron.js - About 4 hrs to fix

      Function cron has a Cognitive Complexity of 22 (exceeds 5 allowed). Consider refactoring.
      Open

      export async function cron (options = {}) {
        const {
          user, tasksByType, analytics, now = new Date(), daysMissed, timezoneUtcOffsetFromUserPrefs,
        } = options;
        let _progress = { down: 0, up: 0, collectedItems: 0 };
      Severity: Minor
      Found in website/server/libs/cron.js - About 3 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 awardLoginIncentives has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
      Open

      function awardLoginIncentives (user) {
        if (user.loginIncentives > MAX_INCENTIVES) return;
      
        //  Remove old notifications if they exists
        user.notifications.forEach((notif, index) => {
      Severity: Minor
      Found in website/server/libs/cron.js - 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 grantEndOfTheMonthPerks has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
      Open

      async function grantEndOfTheMonthPerks (user, now) {
        const { plan, elapsedMonths } = getPlanContext(user, now);
      
        if (elapsedMonths > 0) {
          plan.dateUpdated = now;
      Severity: Minor
      Found in website/server/libs/cron.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 awardLoginIncentives has 32 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

      function awardLoginIncentives (user) {
        if (user.loginIncentives > MAX_INCENTIVES) return;
      
        //  Remove old notifications if they exists
        user.notifications.forEach((notif, index) => {
      Severity: Minor
      Found in website/server/libs/cron.js - About 1 hr to fix

        Function resetHabitCounters has 30 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        function resetHabitCounters (user, tasksByType, now, daysMissed) {
          // check if we've passed a day on which we should reset the habit counters, including today
          let resetWeekly = false;
          let resetMonthly = false;
          for (let i = 0; i < daysMissed; i += 1) {
        Severity: Minor
        Found in website/server/libs/cron.js - About 1 hr to fix

          Function resetHabitCounters has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
          Open

          function resetHabitCounters (user, tasksByType, now, daysMissed) {
            // check if we've passed a day on which we should reset the habit counters, including today
            let resetWeekly = false;
            let resetMonthly = false;
            for (let i = 0; i < daysMissed; i += 1) {
          Severity: Minor
          Found in website/server/libs/cron.js - About 45 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 deeply nested control flow statements.
          Open

                      if (!CRON_SEMI_SAFE_MODE) {
                        // Apply damage from a boss, less damage for Trivial priority (difficulty)
                        user.party.quest.progress.down += delta * (task.priority < 1 ? task.priority : 1);
                        // NB: Medium and Hard priorities do not increase damage from boss.
                        // This was by accident
          Severity: Major
          Found in website/server/libs/cron.js - About 45 mins to fix

            Consider simplifying this complex logical expression.
            Open

              if (
                user.party && user.party.quest && !user.party.quest.RSVPNeeded
                && !user.party.quest.completed && user.party.quest.key && !user.preferences.sleep
              ) {
                analytics.track('quest participation', {
            Severity: Major
            Found in website/server/libs/cron.js - About 40 mins to fix

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

              export async function recoverCron (status, locals) {
                const { user } = locals;
              
                await sleep(0.3);
              
              
              Severity: Minor
              Found in website/server/libs/cron.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

              There are no issues that match your filters.

              Category
              Status