bookbrainz/bookbrainz-site

View on GitHub
src/server/helpers/middleware.ts

Summary

Maintainability
F
3 days
Test Coverage

File middleware.ts has 367 lines of code (exceeds 250 allowed). Consider refactoring.
Open

/*
 * Copyright (C) 2015       Ben Ockmore
 *               2015-2016  Sean Burke
 *               2021       Akash Gupta
 *               2022       Ansh Goyal
Severity: Minor
Found in src/server/helpers/middleware.ts - About 4 hrs to fix

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

    export function makeEntityLoader(modelName: string, additionalRels: Array<string>, errMessage: string) {
        const relations = [
            'aliasSet.aliases.language',
            'annotation.lastRevision',
            'defaultAlias',
    Severity: Minor
    Found in src/server/helpers/middleware.ts - About 1 hr to fix

      Function validateCollectionParams has 26 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

      export async function validateCollectionParams(req, res, next) {
          const {collaborators = [], name, entityType} = req.body;
          const {orm} = req.app.locals;
          const {Editor} = orm;
      
      
      Severity: Minor
      Found in src/server/helpers/middleware.ts - About 1 hr to fix

        Function loadSeriesItems has 26 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        export function loadSeriesItems(req: $Request, res: $Response, next: NextFunction) {
            try {
                const {entity} = res.locals;
                if (entity.dataId) {
                    const {relationships} = entity;
        Severity: Minor
        Found in src/server/helpers/middleware.ts - About 1 hr to fix

          Function validateBBIDsForCollectionAdd has 26 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

          export async function validateBBIDsForCollectionAdd(req, res, next) {
              const {Entity} = req.app.locals.orm;
              const {bbids = []} = req.body;
              if (!bbids.length) {
                  return next(new error.BadRequestError('BBIDs array is empty'));
          Severity: Minor
          Found in src/server/helpers/middleware.ts - About 1 hr to fix

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

            export async function validateBBIDsForCollectionAdd(req, res, next) {
                const {Entity} = req.app.locals.orm;
                const {bbids = []} = req.body;
                if (!bbids.length) {
                    return next(new error.BadRequestError('BBIDs array is empty'));
            Severity: Minor
            Found in src/server/helpers/middleware.ts - 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 validateCollectionParams has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
            Open

            export async function validateCollectionParams(req, res, next) {
                const {collaborators = [], name, entityType} = req.body;
                const {orm} = req.app.locals;
                const {Editor} = orm;
            
            
            Severity: Minor
            Found in src/server/helpers/middleware.ts - 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

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

            export function loadSeriesItems(req: $Request, res: $Response, next: NextFunction) {
                try {
                    const {entity} = res.locals;
                    if (entity.dataId) {
                        const {relationships} = entity;
            Severity: Minor
            Found in src/server/helpers/middleware.ts - 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 validateBBIDsForCollectionRemove has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
            Open

            export function validateBBIDsForCollectionRemove(req, res, next) {
                const {bbids = []} = req.body;
                if (!bbids.length) {
                    return next(new error.BadRequestError('BBIDs array is empty'));
                }
            Severity: Minor
            Found in src/server/helpers/middleware.ts - 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 next();
            Severity: Major
            Found in src/server/helpers/middleware.ts - About 30 mins to fix

              Avoid too many return statements within this function.
              Open

                  return next();
              Severity: Major
              Found in src/server/helpers/middleware.ts - About 30 mins to fix

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

                export async function loadWikipediaExtract(req: $Request, res: $Response, next: NextFunction) {
                    const {entity} = res.locals;
                    if (!entity) {
                        return next(new error.SiteError('Failed to load entity'));
                    }
                Severity: Minor
                Found in src/server/helpers/middleware.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

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

                    for (let i = 0; i < collaboratorIds.length; i++) {
                        const collaboratorId = collaboratorIds[i];
                        const isCollaborator = collection.collaborators.find(collaborator => collaborator.id === collaboratorId);
                        if (!isCollaborator) {
                            return next(new error.BadRequestError(`User ${collaboratorId} is not a collaborator of collection ${collection.id}`, req));
                Severity: Major
                Found in src/server/helpers/middleware.ts and 1 other location - About 3 hrs to fix
                src/server/helpers/middleware.ts on lines 427..433

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

                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

                    for (let i = 0; i < bbids.length; i++) {
                        const bbid = bbids[i];
                        const isBbidInCollection = collection.items.find(item => item.bbid === bbid);
                        if (!isBbidInCollection) {
                            return next(new error.BadRequestError(`Entity ${bbid} is not in collection ${collection.id}`, req));
                Severity: Major
                Found in src/server/helpers/middleware.ts and 1 other location - About 3 hrs to fix
                src/server/helpers/middleware.ts on lines 444..450

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

                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 relationshipPromises = entity.relationships.map(async (relationship) => {
                        const [source, target] = await Promise.all([getEntityWithAlias(relationship.source), getEntityWithAlias(relationship.target)]);
                        relationship.source = source.toJSON();
                        relationship.target = target.toJSON();
                
                
                Severity: Major
                Found in src/server/helpers/middleware.ts and 1 other location - About 3 hrs to fix
                src/server/routes/merge.ts on lines 211..220

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

                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

                    for (let i = 0; i < bbids.length; i++) {
                        const bbid = bbids[i];
                        if (!commonUtils.isValidBBID(bbid)) {
                            return next(new error.BadRequestError(`Invalid BBID ${bbid}`, req));
                        }
                Severity: Major
                Found in src/server/helpers/middleware.ts and 1 other location - About 2 hrs to fix
                src/server/helpers/middleware.ts on lines 421..426

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

                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

                    for (let i = 0; i < bbids.length; i++) {
                        const bbid = bbids[i];
                        if (!commonUtils.isValidBBID(bbid)) {
                            return next(new error.BadRequestError(`Invalid BBID ${bbid}`, req));
                        }
                Severity: Major
                Found in src/server/helpers/middleware.ts and 1 other location - About 2 hrs to fix
                src/server/helpers/middleware.ts on lines 393..398

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

                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 formattedSeriesItems = seriesItems.map((item) => (
                                {...item.source, displayNumber: true,
                                    number: item.number,
                                    position: item.position}
                            ));
                Severity: Minor
                Found in src/server/helpers/middleware.ts and 1 other location - About 45 mins to fix
                src/client/entity-editor/series-section/series-section-merge.tsx on lines 91..95

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

                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