resource-watch/layer

View on GitHub
app/src/services/layer.service.js

Summary

Maintainability
F
3 days
Test Coverage
B
82%

File layer.service.js has 401 lines of code (exceeds 250 allowed). Consider refactoring.
Open

const logger = require('logger');
const Layer = require('models/layer.model');
const GraphService = require('services/graph.service');
const LayerNotFound = require('errors/layerNotFound.error');
const LayerProtected = require('errors/layerProtected.error');
Severity: Minor
Found in app/src/services/layer.service.js - About 5 hrs to fix

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

        static getFilteredQuery(query, ids = []) {
            const { collection } = query;
            const { favourite } = query;
            if (!query.application && query.app) {
                query.application = query.app;
    Severity: Major
    Found in app/src/services/layer.service.js - About 2 hrs to fix

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

          static async update(id, layer, apiKey) {
              logger.debug(`[LayerService - update]: Getting layer with id:  ${id}`);
              const currentLayer = await Layer.findById(id).exec() || await Layer.findOne({
                  slug: id
              }).exec();
      Severity: Minor
      Found in app/src/services/layer.service.js - About 1 hr to fix

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

            static async create(layer, dataset, user, apiKey) {
                logger.debug(`[LayerService - create]: Getting layer with name:  ${layer.name}`);
                const tempSlug = await LayerService.getSlug(layer.name);
        
                logger.debug(`[DBACCESS-SAVE]: layer.name: ${layer.name}`);
        Severity: Minor
        Found in app/src/services/layer.service.js - About 1 hr to fix

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

              static async deleteByDataset(id, apiKey) {
                  logger.debug(`[LayerService - deleteByDataset]: Getting layers of dataset with id:  ${id}`);
                  const layers = await Layer.find({
                      dataset: id
                  }).exec();
          Severity: Minor
          Found in app/src/services/layer.service.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 delete has 30 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              static async delete(layer, apiKey) {
                  logger.debug(`[LayerService - delete]: Getting layer with id: ${layer.id}`);
                  if (!layer) {
                      logger.error(`[LayerService]: Layer with id ${layer.id} doesn't exist`);
                      throw new LayerNotFound(`Layer with id '${layer.id}' doesn't exist`);
          Severity: Minor
          Found in app/src/services/layer.service.js - About 1 hr to fix

            Function deleteByDataset has 28 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                static async deleteByDataset(id, apiKey) {
                    logger.debug(`[LayerService - deleteByDataset]: Getting layers of dataset with id:  ${id}`);
                    const layers = await Layer.find({
                        dataset: id
                    }).exec();
            Severity: Minor
            Found in app/src/services/layer.service.js - About 1 hr to fix

              Function getByDataset has 27 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                  static async getByDataset(resource) {
                      logger.debug(`[LayerService - getByDataset] Getting layers for datasets with ids ${resource.ids}`);
                      if (resource.app) {
                          if (resource.app.indexOf('@') >= 0) {
                              resource.app = {
              Severity: Minor
              Found in app/src/services/layer.service.js - About 1 hr to fix

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

                    static getFilteredQuery(query, ids = []) {
                        const { collection } = query;
                        const { favourite } = query;
                        if (!query.application && query.app) {
                            query.application = query.app;
                Severity: Minor
                Found in app/src/services/layer.service.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

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

                    static async delete(layer, apiKey) {
                        logger.debug(`[LayerService - delete]: Getting layer with id: ${layer.id}`);
                        if (!layer) {
                            logger.error(`[LayerService]: Layer with id ${layer.id} doesn't exist`);
                            throw new LayerNotFound(`Layer with id '${layer.id}' doesn't exist`);
                Severity: Minor
                Found in app/src/services/layer.service.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

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

                    static async getAll(query = {}, dataset = null, user, apiKey) {
                        logger.debug(`[LayerService - getAll]: Getting all layers`);
                        const sort = query.sort || '';
                        const page = query['page[number]'] ? parseInt(query['page[number]'], 10) : 1;
                        const limit = query['page[size]'] ? parseInt(query['page[size]'], 10) : 10;
                Severity: Minor
                Found in app/src/services/layer.service.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 getByDataset has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                    static async getByDataset(resource) {
                        logger.debug(`[LayerService - getByDataset] Getting layers for datasets with ids ${resource.ids}`);
                        if (resource.app) {
                            if (resource.app.indexOf('@') >= 0) {
                                resource.app = {
                Severity: Minor
                Found in app/src/services/layer.service.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

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

                            if (resource.app.indexOf('@') >= 0) {
                                resource.app = {
                                    $all: resource.app.split('@').map((elem) => elem.trim())
                                };
                            } else {
                Severity: Major
                Found in app/src/services/layer.service.js and 1 other location - About 4 hrs to fix
                app/src/services/layer.service.js on lines 65..73

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

                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

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

                                        if (query[param].indexOf('@') >= 0) {
                                            query[param] = {
                                                $all: query[param].split('@').map((elem) => elem.trim())
                                            };
                                        } else {
                Severity: Major
                Found in app/src/services/layer.service.js and 1 other location - About 4 hrs to fix
                app/src/services/layer.service.js on lines 397..405

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

                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

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

                        const layer = await Layer.findById(id).exec() || await Layer.findOne({
                            slug: id
                        }).exec();
                Severity: Minor
                Found in app/src/services/layer.service.js and 1 other location - About 50 mins to fix
                app/src/services/layer.service.js on lines 190..192

                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

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

                        const currentLayer = await Layer.findById(id).exec() || await Layer.findOne({
                            slug: id
                        }).exec();
                Severity: Minor
                Found in app/src/services/layer.service.js and 1 other location - About 50 mins to fix
                app/src/services/layer.service.js on lines 131..133

                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

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

                        const ids = query.ids ? query.ids.split(',').map((elem) => elem.trim()) : [];
                Severity: Minor
                Found in app/src/services/layer.service.js and 1 other location - About 40 mins to fix
                app/src/services/layer.service.js on lines 373..373

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

                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

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

                        const includes = query.includes ? query.includes.split(',').map((elem) => elem.trim()) : [];
                Severity: Minor
                Found in app/src/services/layer.service.js and 1 other location - About 40 mins to fix
                app/src/services/layer.service.js on lines 372..372

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

                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

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

                        await RWAPIMicroservice.requestToMicroservice({
                            uri: `/v1/dataset/${datasetId}/layer/${layerId}/metadata`,
                            method: 'DELETE',
                            headers: {
                                'x-api-key': apiKey
                Severity: Minor
                Found in app/src/services/layer.service.js and 1 other location - About 35 mins to fix
                app/src/services/graph.service.js on lines 9..15

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

                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

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

                        if (!layer) {
                            logger.info(`[LayerService]: Layer with id ${id} doesn't exist`);
                            throw new LayerNotFound(`Layer with id '${id}' doesn't exist`);
                        }
                Severity: Minor
                Found in app/src/services/layer.service.js and 1 other location - About 30 mins to fix
                app/src/services/layer.service.js on lines 193..196

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

                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

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

                        if (!currentLayer) {
                            logger.error(`[LayerService]: Layer with id ${id} doesn't exist`);
                            throw new LayerNotFound(`Layer with id '${id}' doesn't exist`);
                        }
                Severity: Minor
                Found in app/src/services/layer.service.js and 1 other location - About 30 mins to fix
                app/src/services/layer.service.js on lines 134..137

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

                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