All files / src application.ts

100% Statements 75/75
64% Branches 16/25
100% Functions 10/10
100% Lines 75/75

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 2421x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x         1x 1x 1x           1x     1x 1x 1x 1x 1x 1x 1x                           1x       1x                 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                             1x 1x 1x                       1x 1x                       1x 1x 1x     1x                           1x     1x         1x                               1x     1x 1x                     1x 1x 1x 1x 1x 1x                         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                       1x 1x                           1x                  
import { AuthenticationComponent, registerAuthenticationStrategy } from '@loopback/authentication';
import { AuthorizationComponent } from '@loopback/authorization';
import { BootMixin } from '@loopback/boot';
import { ApplicationConfig, createBindingFromClass } from '@loopback/core';
import { RepositoryMixin } from '@loopback/repository';
import { RestApplication } from '@loopback/rest';
import {
  RestExplorerBindings,
  RestExplorerComponent,
} from '@loopback/rest-explorer';
import { ServiceMixin } from '@loopback/service-proxy';
import { SQS } from 'aws-sdk';
import * as dotenv from 'dotenv';
import {
  SqsConsumerBindings,
  SqsConsumerComponent,
  SqsConsumerConfig,
} from 'loopback4-sqs-consumer';
import path from 'path';
import 'reflect-metadata';
import { 
  AuthenticationStrategyBindings, 
  CognitoAuthenticationStrategy, 
  ExternalAuthenticationStrategy, 
  HmacAuthenticationStrategy 
} from '@logchain/auth-strategies';
import {
  AwsComponent,
} from '@logchain/components/aws';
import { CasbinAuthorizationComponent } from '@logchain/components/casbin';
import { FabricComponent } from '@logchain/components/fabric';
import { I18NextComponent } from '@logchain/components/i18next';
import { InfoSpecEnhancer, SecuritySpecEnhancer } from '@logchain/enhancers';
import { log } from '@logchain/middleware';
import { CronComponent } from '@loopback/cron';
import {
  DueActionsNotificationCronJob,
  ExpectedActionsNotificationCronJob,
  ExpireRegulatoryFilingCronJob,
  ExpiryNotificationCronJob,
  ImportPmoCronJob,
  ImportApCalendarCronJob,
  PrecomputeDataForReportMasterCronJob,
  PreExpiryNotificationCronJob,
  ProcessDeletedAccountsCronJob,
  ProcessXMLFromSuttonCronJob,
  RetryFailedPdfGenerationCronJob,
  UpdateFlowStatusAfterPendingTimeCronJob
} from '@logchain/cronjobs';
import { CacheService } from './services/cache.service';
// import { HybridAuthStrategy } from './auth-strategies/hybrid.auth.strategy';
export { ApplicationConfig };
 
export class LogChainApplication extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  /**
   * Constructor for LogChainApplication.
   *
   * @param options - Application configuration.
   */
  constructor(options: ApplicationConfig = {}) {
    super(options);
    dotenv.config();
    this.setupAuthenticationStrategies();
    this.setupSpecEnhancers();
    this.setupHomePage();
    this.setupAuthenticationBindings();
    this.setupSqsConsumer();
    this.setupComponents();
    this.setupCronJobs();
    this.projectRoot = __dirname;
    this.setupMiddleware();
    this.setupBootOptions();
    this.service(CacheService);
  }
 
  /**
   * Setup authentication strategies.
   *
   * @remarks
   * Currently, it sets up the following strategies:
   *
   * - {@link CognitoAuthenticationStrategy}
   * - {@link HmacAuthenticationStrategy}
   * - {@link ExternalAuthenticationStrategy}
   *
   */
  private setupAuthenticationStrategies() {
    registerAuthenticationStrategy(this, CognitoAuthenticationStrategy);
    registerAuthenticationStrategy(this, HmacAuthenticationStrategy);
    registerAuthenticationStrategy(this, ExternalAuthenticationStrategy);
    // registerAuthenticationStrategy(this, HybridAuthStrategy);
  }
 
  /**
   * Setup specification enhancers.
   *
   * @remarks
   * This method adds custom OpenAPI specification enhancers to the application,
   * including information and security spec enhancers.
   */
  private setupSpecEnhancers() {
    this.add(createBindingFromClass(InfoSpecEnhancer));
    this.add(createBindingFromClass(SecuritySpecEnhancer));
  }
 
  /**
   * Set up the home page.
   *
   * @remarks
   * This method sets up the home page by serving the static content in the
   * `public` directory and enables the OpenAPI explorer at the `/explorer`
   * path. This is only done in non-production environments.
   */
  private setupHomePage() {
    Eif (process.env.NODE_ENV !== 'production') {
      this.static('/', path.join(__dirname, '../public'));
      this.configure(RestExplorerBindings.COMPONENT).to({
        path: '/explorer',
      });
      this.component(RestExplorerComponent);
    }
  }
 
  /**
   * Set up authentication bindings.
   *
   * @remarks
   * This method binds the default options for various authentication strategies used in the application.
   * It configures the default options for Cognito, HMAC, and External authentication strategies.
   * Each strategy is provided with specific settings such as token use, access key ID, secret access key, 
   * and maximum interval for request validation.
   */
  private setupAuthenticationBindings() {
    this.bind(AuthenticationStrategyBindings.COGNITO_DEFAULT_OPTIONS).to({
      token_use: 'access',
    });
    this.bind(AuthenticationStrategyBindings.HMAC_DEFAULT_OPTIONS).to({
      accessKeyId: process.env.CALISTA_ACCESS_KEY_ID ?? '',
      secretAccessKey: process.env.CALISTA_SECRET_ACCESS_KEY ?? '',
      maxInterval: 60 * 15
    });
    this.bind(AuthenticationStrategyBindings.EXTERNAL_DEFAULT_OPTIONS).to({
      accessKeyId: process.env.EXTERNAL_ACCESS_KEY_ID ?? '',
      secretAccessKey: process.env.EXTERNAL_ACCESS_KEY ?? '',
      maxInterval: 60 * 15
    });
  }
 
  /**
   * Set up the SQS consumer configuration.
   *
   * @remarks
   * This method configures the SQS consumer component by creating a new SQS instance
   * with the region specified in the environment variables. It then binds the configuration
   * to the SQS consumer component, enabling message consumption from AWS SQS.
   */
  private setupSqsConsumer() {
    const sqsConfig: SqsConsumerConfig = {
      sqs: new SQS({ region: process.env.AWS_REGION }),
    };
    this.configure(SqsConsumerBindings.COMPONENT).to(sqsConfig);
    this.component(SqsConsumerComponent);
  }
 
  /**
   * Registers all the components used in the application.
   *
   * @remarks
   * This method registers the components for authentication, authorization, casbin authorization,
   * AWS services, Fabric services and i18next translation.
   */
  private setupComponents() {
    this.component(AuthenticationComponent);
    this.component(AuthorizationComponent);
    this.component(CasbinAuthorizationComponent);
    this.component(AwsComponent);
    this.component(FabricComponent);
    this.component(I18NextComponent);
  }
 
  /**
   * Sets up the cron jobs for the application.
   *
   * @remarks
   * If the RUN_MODE environment variable is set to 0 or 2, the cron jobs component is registered
   * and the cron jobs for expiring regulatory filings, retrying failed PDF generation, due actions
   * notification, expected actions notification, updating flow status after pending time, processing
   * XML from Sutton, precomputing data for report master and processing deleted accounts are added.
   */
  private setupCronJobs() {
    Eif (process.env.RUN_MODE === '0' || process.env.RUN_MODE === '2') {
      this.component(CronComponent);
      this.add(createBindingFromClass(ExpireRegulatoryFilingCronJob));
      this.add(createBindingFromClass(RetryFailedPdfGenerationCronJob));
      this.add(createBindingFromClass(DueActionsNotificationCronJob));
      this.add(createBindingFromClass(ExpectedActionsNotificationCronJob));
      this.add(createBindingFromClass(UpdateFlowStatusAfterPendingTimeCronJob));
      this.add(createBindingFromClass(ProcessXMLFromSuttonCronJob));
      this.add(createBindingFromClass(PrecomputeDataForReportMasterCronJob));
      this.add(createBindingFromClass(ProcessDeletedAccountsCronJob));
      this.add(createBindingFromClass(PreExpiryNotificationCronJob));
      this.add(createBindingFromClass(ExpiryNotificationCronJob));
      this.add(createBindingFromClass(ImportPmoCronJob));
      this.add(createBindingFromClass(ImportApCalendarCronJob));
    }
  }
 
  /**
   * Set up the middleware.
   *
   * @remarks
   * If the environment is not production, this method adds the log middleware
   * to the application. The log middleware is used for logging HTTP requests.
   */
  private setupMiddleware() {
    Eif (process.env.NODE_ENV !== 'production') {
      this.middleware(log);
    }
  }
 
  /**
   * Sets up the boot options for the application.
   *
   * @remarks
   * The boot options are used to configure the application boot process.
   * The controllers are looked up in the "controllers" directory and its subdirectories.
   * The controllers are expected to have the extension ".controller.js".
   * The controllers are looked up in a nested fashion, meaning that subdirectories are also searched.
   */
  private setupBootOptions() {
    this.bootOptions = {
      controllers: {
        dirs: ['controllers'],
        extensions: ['.controller.js'],
        nested: true,
      },
    };
  }
}