All files / src application.ts

85.71% Statements 54/63
42.85% Branches 3/7
57.14% Functions 4/7
87.09% Lines 54/62

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 1321x   1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   1x         9x   9x     9x   9x 9x     9x     9x 9x     9x       9x 9x   9x   9x                       9x 9x   9x   9x 9x 9x 9x 9x 9x 9x 9x 9x 9x       9x                               9x                 9x 9x 9x                           9x      
import { AuthenticationComponent, registerAuthenticationStrategy } from '@loopback/authentication';
// import {SECURITY_SCHEME_SPEC} from './utils/security-spec';
import { SECURITY_SCHEME_SPEC } from '@loopback/authentication-jwt';
import { BootMixin } from '@loopback/boot';
import { ApplicationConfig } from '@loopback/core';
import { RepositoryMixin } from '@loopback/repository';
import { RestApplication, RestBindings } from '@loopback/rest';
import { RestExplorerBindings, RestExplorerComponent } from '@loopback/rest-explorer';
import { ServiceMixin } from '@loopback/service-proxy';
import moment from "moment";
import multer from "multer";
import path from 'path';
import { JWTStrategy } from './auth-stratgies/jwt-stratgies';
import {  ASSETS_DIRECTORY, ControllerKey, FILE_UPLOAD_SERVICE, LoggerServiceBindings, PasswordHasherBindings, STORAGE_DIRECTORY, TokenServiceBindings, TokenServiceConstants, UserServiceBindings } from './keys';
import { MySequence } from './sequence';
import { BcryptHasher } from './services/hash.password';
import { JWTService } from './services/jwt-service';
import { Logger } from './services/logger.services';
import { MyUserService } from './services/user-service';
import { AuthController, PaymentsController } from "./controllers";
import { CloseIntegration } from "./controllers/components/closeIntegration";
// import { AccountsController } from './controllers/accounts.controller';
import errorMessages from './utils/errorMessages';
let { imageOnlyError } = errorMessages
export { ApplicationConfig };
export class SawLoopbackApplication extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  constructor(options: ApplicationConfig = {}) {
 
    super(options);
    // setup binding
    this.setupBinding();
 
    // Add security spec
    this.addSecuritySpec();
 
    this.component(AuthenticationComponent);
    registerAuthenticationStrategy(this, JWTStrategy)
 
    // Set up the custom sequence
    this.sequence(MySequence);
 
    // Set up default home page
    let publicDir = path.join(__dirname, '../public')
    this.static('/', publicDir);
 
    // Customize @loopback/rest-explorer configuration here
    this.configure(RestExplorerBindings.COMPONENT).to({
      path: '/explorer',
    });
 
    this.component(RestExplorerComponent);
    this.configureFileUpload(options.fileStorageDirectory);
 
    this.projectRoot = __dirname;
    // Customize @loopback/boot Booter Conventions here
    this.bootOptions = {
      controllers: {
        // Customize ControllerBooter Conventions here
        dirs: ['controllers'],
        extensions: ['.controller.js'],
        nested: true,
      },
    };
 
  }
  setupBinding(): void {
 
    this.bind(ControllerKey.AUTH_CONTROLLER).toClass(AuthController);
    this.bind(ControllerKey.CLOSE_COMPONENT).toClass(CloseIntegration);
    // this.bind(ControllerKey.ACCOUNTS_CONTROLLER).toClass(AccountsController);
    this.bind(ControllerKey.PAYMENT_CONTROLLER).toClass(PaymentsController);
 
    this.bind(LoggerServiceBindings.LOGGER_SERVICE).toClass(Logger);
    this.bind(RestBindings.ERROR_WRITER_OPTIONS).to({ safeFields: ['errorCode'] });
    this.bind(PasswordHasherBindings.PASSWORD_HASHER).toClass(BcryptHasher);
    this.bind(PasswordHasherBindings.ROUNDS).to(10)
    this.bind(UserServiceBindings.USER_SERVICE).toClass(MyUserService);
    this.bind(TokenServiceBindings.TOKEN_SERVICE).toClass(JWTService);
    this.bind(TokenServiceBindings.TOKEN_SECRET).to(TokenServiceConstants.TOKEN_SECRET_VALUE)
    this.bind(TokenServiceBindings.TOKEN_EXPIRES_IN).to(TokenServiceConstants.TOKEN_EXPIRES_IN_VALUE);
    let assetsDir = path.join(__dirname, '../assets/img');
    this.bind(ASSETS_DIRECTORY).to(assetsDir);
  }
 
  addSecuritySpec(): void {
    this.api({
      openapi: '3.0.0',
      info: {
        title: 'Saw-Buyer-Broker',
        version: '1.0.0',
      },
      paths: {},
      components: { securitySchemes: SECURITY_SCHEME_SPEC },
      security: [
        {
          jwt: [],
        },
      ],
      servers: [{ url: '/' }],
    });
  }
  getUniqueFileName = (name: string) => {
    const ext = name.split('.').pop()
    const fileName = name.split('.').shift()
    const uniqueFileName = fileName + "-" + moment().format('DD_MM_YYYY HH_mm_ss.SSS') + "." + ext
    // uniqueFileName = name
    return uniqueFileName
  }
  public configureFileUpload(destination?: string) {
 
    destination = destination ?? path.join(__dirname, '../.sawfiles');
    this.bind(STORAGE_DIRECTORY).to(destination);
    const multerOptions: multer.Options = {
      storage: multer.diskStorage({
        destination,
        // Use the original file name as is
        filename: (req, file, cb) => {
          const acceptFileTypes = ['png', 'jpg', 'jpeg']
          if (acceptFileTypes.some((type: string) => file.mimetype.includes(type))) {
            cb(null, this.getUniqueFileName(file.originalname));
          } else {
            cb(new Error(imageOnlyError), "")
          }
        },
      }),
    };
    this.configure(FILE_UPLOAD_SERVICE).to(multerOptions);
  }
}