resource-watch/dataset

View on GitHub
src/routes/api/v1/dataset.router.js

Summary

Maintainability
F
5 days
Test Coverage

File dataset.router.js has 575 lines of code (exceeds 250 allowed). Consider refactoring.
Open

const Router = require('koa-router');
const koaMulter = require('koa-multer');
const { default: logger } = require('logger');
const xor = require('lodash/xor');
const DatasetService = require('services/dataset.service');
Severity: Major
Found in src/routes/api/v1/dataset.router.js - About 1 day to fix

    Function getAll has a Cognitive Complexity of 47 (exceeds 5 allowed). Consider refactoring.
    Open

        static async getAll(ctx) {
            logger.info(`[DatasetRouter] Getting all datasets`);
            const user = DatasetRouter.getUser(ctx);
            const { query } = ctx;
            const { search } = query;
    Severity: Minor
    Found in src/routes/api/v1/dataset.router.js - About 7 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 getAll has 117 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        static async getAll(ctx) {
            logger.info(`[DatasetRouter] Getting all datasets`);
            const user = DatasetRouter.getUser(ctx);
            const { query } = ctx;
            const { search } = query;
    Severity: Major
    Found in src/routes/api/v1/dataset.router.js - About 4 hrs to fix

      Function authorizationMiddleware has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
      Open

      const authorizationMiddleware = async (ctx, next) => {
          logger.info(`[DatasetRouter] Checking authorization`);
          // Get user from query (delete) or body (post-patch)
          const newDatasetCreation = ctx.request.path === '/dataset' && ctx.request.method === 'POST';
          const uploadDataset = ctx.request.path.indexOf('upload') >= 0 && ctx.request.method === 'POST';
      Severity: Minor
      Found in src/routes/api/v1/dataset.router.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 notifyAdapter has 41 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          static async notifyAdapter(ctx, dataset) {
              const { connectorType, provider } = dataset;
              const clonedDataset = { ...dataset.toObject() };
              clonedDataset.id = dataset._id;
              clonedDataset.connector_url = dataset.connectorUrl;
      Severity: Minor
      Found in src/routes/api/v1/dataset.router.js - About 1 hr to fix

        Function authorizationMiddleware has 36 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        const authorizationMiddleware = async (ctx, next) => {
            logger.info(`[DatasetRouter] Checking authorization`);
            // Get user from query (delete) or body (post-patch)
            const newDatasetCreation = ctx.request.path === '/dataset' && ctx.request.method === 'POST';
            const uploadDataset = ctx.request.path.indexOf('upload') >= 0 && ctx.request.method === 'POST';
        Severity: Minor
        Found in src/routes/api/v1/dataset.router.js - About 1 hr to fix

          Function update has 34 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              static async update(ctx) {
                  const id = ctx.params.dataset;
                  logger.info(`[DatasetRouter] Updating dataset with id: ${id}`);
                  try {
                      const user = DatasetRouter.getUser(ctx);
          Severity: Minor
          Found in src/routes/api/v1/dataset.router.js - About 1 hr to fix

            Consider simplifying this complex logical expression.
            Open

                        if (
                            search
                            || serializeObjToQuery(query).indexOf('concepts[0][0]') >= 0
                            || sort.indexOf('most-favorited') >= 0
                            || sort.indexOf('most-viewed') >= 0
            Severity: Critical
            Found in src/routes/api/v1/dataset.router.js - About 1 hr to fix

              Function notifyAdapterCreate has 33 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                  static async notifyAdapterCreate(ctx, dataset) {
                      const { connectorType, provider } = dataset;
                      const clonedDataset = { ...dataset.toObject() };
              
                      clonedDataset.id = dataset._id;
              Severity: Minor
              Found in src/routes/api/v1/dataset.router.js - About 1 hr to fix

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

                    static async update(ctx) {
                        const id = ctx.params.dataset;
                        logger.info(`[DatasetRouter] Updating dataset with id: ${id}`);
                        try {
                            const user = DatasetRouter.getUser(ctx);
                Severity: Minor
                Found in src/routes/api/v1/dataset.router.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 validationMiddleware has 26 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                const validationMiddleware = async (ctx, next) => {
                    logger.info(`[DatasetRouter] Validating`);
                    if (ctx.request.body.dataset) {
                        ctx.request.body = Object.assign(ctx.request.body, ctx.request.body.dataset);
                        delete ctx.request.body.dataset;
                Severity: Minor
                Found in src/routes/api/v1/dataset.router.js - About 1 hr to fix

                  Function validationMiddleware has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                  Open

                  const validationMiddleware = async (ctx, next) => {
                      logger.info(`[DatasetRouter] Validating`);
                      if (ctx.request.body.dataset) {
                          ctx.request.body = Object.assign(ctx.request.body, ctx.request.body.dataset);
                          delete ctx.request.body.dataset;
                  Severity: Minor
                  Found in src/routes/api/v1/dataset.router.js - About 55 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 notifyAdapterCreate has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                  Open

                      static async notifyAdapterCreate(ctx, dataset) {
                          const { connectorType, provider } = dataset;
                          const clonedDataset = { ...dataset.toObject() };
                  
                          clonedDataset.id = dataset._id;
                  Severity: Minor
                  Found in src/routes/api/v1/dataset.router.js - About 55 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 notifyAdapter has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                  Open

                      static async notifyAdapter(ctx, dataset) {
                          const { connectorType, provider } = dataset;
                          const clonedDataset = { ...dataset.toObject() };
                          clonedDataset.id = dataset._id;
                          clonedDataset.connector_url = dataset.connectorUrl;
                  Severity: Minor
                  Found in src/routes/api/v1/dataset.router.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;
                  Severity: Major
                  Found in src/routes/api/v1/dataset.router.js - About 30 mins to fix

                    Avoid too many return statements within this function.
                    Open

                            return;
                    Severity: Major
                    Found in src/routes/api/v1/dataset.router.js - About 30 mins to fix

                      Avoid too many return statements within this function.
                      Open

                                  return;
                      Severity: Major
                      Found in src/routes/api/v1/dataset.router.js - About 30 mins to fix

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

                            static async create(ctx) {
                                logger.info(`[DatasetRouter] Creating dataset with name: ${ctx.request.body.name}`);
                                try {
                                    const user = DatasetRouter.getUser(ctx);
                                    const dataset = await DatasetService.create(ctx.request.body, user, ctx.request.headers['x-api-key']);
                        Severity: Minor
                        Found in src/routes/api/v1/dataset.router.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

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

                            static async get(ctx) {
                                const id = ctx.params.dataset;
                                logger.info(`[DatasetRouter] Getting dataset with id: ${id}`);
                                const user = DatasetRouter.getUser(ctx);
                                const { query } = ctx;
                        Severity: Minor
                        Found in src/routes/api/v1/dataset.router.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 2 locations. Consider refactoring.
                        Open

                        const serializeObjToQuery = (obj) => Object.keys(obj).reduce((a, k) => {
                            a.push(`${k}=${encodeURIComponent(obj[k])}`);
                            return a;
                        }, []).join('&');
                        Severity: Major
                        Found in src/routes/api/v1/dataset.router.js and 1 other location - About 2 hrs to fix
                        src/services/relationships.service.js on lines 7..10

                        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 79.

                        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

                                        ctx.query.ids = ctx.query.ids.length > 0 ? ctx.query.ids.join(',') : '';
                        Severity: Minor
                        Found in src/routes/api/v1/dataset.router.js and 1 other location - About 55 mins to fix
                        src/routes/api/v1/dataset.router.js on lines 392..392

                        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 53.

                        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

                                        ctx.query.ids = ctx.query.ids.length > 0 ? ctx.query.ids.join(',') : '';
                        Severity: Minor
                        Found in src/routes/api/v1/dataset.router.js and 1 other location - About 55 mins to fix
                        src/routes/api/v1/dataset.router.js on lines 382..382

                        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 53.

                        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

                                return RWAPIMicroservice.requestToMicroservice({
                                    uri,
                                    method: 'POST',
                                    json: true,
                                    body: { connector: clonedDataset },
                        Severity: Minor
                        Found in src/routes/api/v1/dataset.router.js and 1 other location - About 50 mins to fix
                        src/routes/api/v1/dataset.router.js on lines 104..112

                        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 51.

                        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

                                const request = {
                                    uri,
                                    method: 'POST',
                                    json: true,
                                    body: { connector: clonedDataset },
                        Severity: Minor
                        Found in src/routes/api/v1/dataset.router.js and 1 other location - About 50 mins to fix
                        src/routes/api/v1/dataset.router.js on lines 156..164

                        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 51.

                        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