TryGhost/Ghost

View on GitHub
ghost/staff-service/lib/StaffServiceEmails.js

Summary

Maintainability
F
3 days
Test Coverage

File StaffServiceEmails.js has 400 lines of code (exceeds 250 allowed). Consider refactoring.
Open

const {promises: fs, readFileSync} = require('fs');
const path = require('path');
const moment = require('moment');
const glob = require('glob');
const {EmailAddressParser} = require('@tryghost/email-addresses');
Severity: Minor
Found in ghost/staff-service/lib/StaffServiceEmails.js - About 5 hrs to fix

    StaffServiceEmails has 23 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class StaffServiceEmails {
        constructor({logging, models, mailer, settingsHelpers, settingsCache, urlUtils, labs}) {
            this.logging = logging;
            this.models = models;
            this.mailer = mailer;
    Severity: Minor
    Found in ghost/staff-service/lib/StaffServiceEmails.js - About 2 hrs to fix

      Function notifyPaidSubscriptionStarted has 45 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          async notifyPaidSubscriptionStarted({member, subscription, offer, tier, attribution}, options = {}) {
              const users = await this.models.User.getEmailAlertUsers('paid-started', options);
      
              for (const user of users) {
                  const to = user.email;
      Severity: Minor
      Found in ghost/staff-service/lib/StaffServiceEmails.js - About 1 hr to fix

        Function notifyDonationReceived has 42 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            async notifyDonationReceived({donationPaymentEvent}) {
                const emailPromises = [];
                const users = await this.models.User.getEmailAlertUsers('donation');
                const formattedAmount = this.getFormattedAmount({currency: donationPaymentEvent.currency, amount: donationPaymentEvent.amount / 100});
        
        
        Severity: Minor
        Found in ghost/staff-service/lib/StaffServiceEmails.js - About 1 hr to fix

          Function notifyMilestoneReceived has 40 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              async notifyMilestoneReceived({milestone}) {
                  if (!milestone?.emailSentAt || milestone?.meta?.reason) {
                      // Do not send an email when no email was set to be sent or a reason
                      // not to send provided
                      return;
          Severity: Minor
          Found in ghost/staff-service/lib/StaffServiceEmails.js - About 1 hr to fix

            Function notifyPaidSubscriptionCanceled has 39 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                async notifyPaidSubscriptionCanceled({member, tier, subscription}, options = {}) {
                    const users = await this.models.User.getEmailAlertUsers('paid-canceled', options);
            
                    for (const user of users) {
                        const to = user.email;
            Severity: Minor
            Found in ghost/staff-service/lib/StaffServiceEmails.js - About 1 hr to fix

              Function notifyFreeMemberSignup has 31 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                  async notifyFreeMemberSignup({
                      member, attribution
                  }, options) {
                      const users = await this.models.User.getEmailAlertUsers('free-signup', options);
              
              
              Severity: Minor
              Found in ghost/staff-service/lib/StaffServiceEmails.js - About 1 hr to fix

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

                    getOfferData(offer) {
                        if (offer) {
                            let offAmount = '';
                            let offDuration = '';
                
                
                Severity: Minor
                Found in ghost/staff-service/lib/StaffServiceEmails.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 notifyMilestoneReceived has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                    async notifyMilestoneReceived({milestone}) {
                        if (!milestone?.emailSentAt || milestone?.meta?.reason) {
                            // Do not send an email when no email was set to be sent or a reason
                            // not to send provided
                            return;
                Severity: Minor
                Found in ghost/staff-service/lib/StaffServiceEmails.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 notifyDonationReceived has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                    async notifyDonationReceived({donationPaymentEvent}) {
                        const emailPromises = [];
                        const users = await this.models.User.getEmailAlertUsers('donation');
                        const formattedAmount = this.getFormattedAmount({currency: donationPaymentEvent.currency, amount: donationPaymentEvent.amount / 100});
                
                
                Severity: Minor
                Found in ghost/staff-service/lib/StaffServiceEmails.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

                    extractInitials(name = '') {
                        const names = name.split(' ');
                        const initials = names.length > 1 ? [names[0][0], names[names.length - 1][0]] : [names[0][0]];
                        return initials.join('').toUpperCase();
                    }
                Severity: Major
                Found in ghost/staff-service/lib/StaffServiceEmails.js and 1 other location - About 3 hrs to fix
                ghost/core/core/server/services/comments/CommentsServiceEmails.js on lines 174..178

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

                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

                    async sendMail(message) {
                        if (process.env.NODE_ENV !== 'production') {
                            this.logging.warn(message.text);
                        }
                
                
                Severity: Major
                Found in ghost/staff-service/lib/StaffServiceEmails.js and 1 other location - About 2 hrs to fix
                ghost/core/core/server/services/comments/CommentsServiceEmails.js on lines 180..191

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

                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 6 locations. Consider refactoring.
                Open

                            let staffUrl = this.urlUtils.urlJoin(this.urlUtils.urlFor('admin', true), '#', `/settings/staff/${user.slug}`);
                Severity: Major
                Found in ghost/staff-service/lib/StaffServiceEmails.js and 5 other locations - About 35 mins to fix
                ghost/staff-service/lib/StaffServiceEmails.js on lines 95..95
                ghost/staff-service/lib/StaffServiceEmails.js on lines 148..148
                ghost/staff-service/lib/StaffServiceEmails.js on lines 180..180
                ghost/staff-service/lib/StaffServiceEmails.js on lines 224..224
                ghost/staff-service/lib/StaffServiceEmails.js on lines 279..279

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

                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 6 locations. Consider refactoring.
                Open

                        let staffUrl = this.urlUtils.urlJoin(this.urlUtils.urlFor('admin', true), '#', `/settings/staff/${recipient.slug}`);
                Severity: Major
                Found in ghost/staff-service/lib/StaffServiceEmails.js and 5 other locations - About 35 mins to fix
                ghost/staff-service/lib/StaffServiceEmails.js on lines 39..39
                ghost/staff-service/lib/StaffServiceEmails.js on lines 95..95
                ghost/staff-service/lib/StaffServiceEmails.js on lines 148..148
                ghost/staff-service/lib/StaffServiceEmails.js on lines 224..224
                ghost/staff-service/lib/StaffServiceEmails.js on lines 279..279

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

                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 6 locations. Consider refactoring.
                Open

                            let staffUrl = this.urlUtils.urlJoin(this.urlUtils.urlFor('admin', true), '#', `/settings/staff/${user.slug}`);
                Severity: Major
                Found in ghost/staff-service/lib/StaffServiceEmails.js and 5 other locations - About 35 mins to fix
                ghost/staff-service/lib/StaffServiceEmails.js on lines 39..39
                ghost/staff-service/lib/StaffServiceEmails.js on lines 148..148
                ghost/staff-service/lib/StaffServiceEmails.js on lines 180..180
                ghost/staff-service/lib/StaffServiceEmails.js on lines 224..224
                ghost/staff-service/lib/StaffServiceEmails.js on lines 279..279

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

                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 6 locations. Consider refactoring.
                Open

                            let staffUrl = this.urlUtils.urlJoin(this.urlUtils.urlFor('admin', true), '#', `/settings/staff/${user.slug}`);
                Severity: Major
                Found in ghost/staff-service/lib/StaffServiceEmails.js and 5 other locations - About 35 mins to fix
                ghost/staff-service/lib/StaffServiceEmails.js on lines 39..39
                ghost/staff-service/lib/StaffServiceEmails.js on lines 95..95
                ghost/staff-service/lib/StaffServiceEmails.js on lines 180..180
                ghost/staff-service/lib/StaffServiceEmails.js on lines 224..224
                ghost/staff-service/lib/StaffServiceEmails.js on lines 279..279

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

                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 (const result of results) {
                            if (result.status === 'rejected') {
                                this.logging.warn(result?.reason);
                            }
                        }
                Severity: Minor
                Found in ghost/staff-service/lib/StaffServiceEmails.js and 1 other location - About 35 mins to fix
                ghost/staff-service/lib/StaffServiceEmails.js on lines 250..254

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

                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 6 locations. Consider refactoring.
                Open

                            let staffUrl = this.urlUtils.urlJoin(this.urlUtils.urlFor('admin', true), '#', `/settings/staff/${user.slug}`);
                Severity: Major
                Found in ghost/staff-service/lib/StaffServiceEmails.js and 5 other locations - About 35 mins to fix
                ghost/staff-service/lib/StaffServiceEmails.js on lines 39..39
                ghost/staff-service/lib/StaffServiceEmails.js on lines 95..95
                ghost/staff-service/lib/StaffServiceEmails.js on lines 148..148
                ghost/staff-service/lib/StaffServiceEmails.js on lines 180..180
                ghost/staff-service/lib/StaffServiceEmails.js on lines 224..224

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

                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 6 locations. Consider refactoring.
                Open

                            let staffUrl = this.urlUtils.urlJoin(this.urlUtils.urlFor('admin', true), '#', `/settings/staff/${user.slug}`);
                Severity: Major
                Found in ghost/staff-service/lib/StaffServiceEmails.js and 5 other locations - About 35 mins to fix
                ghost/staff-service/lib/StaffServiceEmails.js on lines 39..39
                ghost/staff-service/lib/StaffServiceEmails.js on lines 95..95
                ghost/staff-service/lib/StaffServiceEmails.js on lines 148..148
                ghost/staff-service/lib/StaffServiceEmails.js on lines 180..180
                ghost/staff-service/lib/StaffServiceEmails.js on lines 279..279

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

                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 (const result of results) {
                            if (result.status === 'rejected') {
                                this.logging.warn(result?.reason);
                            }
                        }
                Severity: Minor
                Found in ghost/staff-service/lib/StaffServiceEmails.js and 1 other location - About 35 mins to fix
                ghost/staff-service/lib/StaffServiceEmails.js on lines 310..314

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

                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