All files / src/repositories document.repository.ts

55.88% Statements 19/34
0% Branches 0/14
20% Functions 1/5
53.13% Lines 17/32

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 931x 1x   1x 1x 1x   1x 1x       1x                 84x   84x   84x   84x   84x   84x   84x   84x   84x                                                                                                              
import { Getter, inject } from '@loopback/core';
import { BelongsToAccessor, repository } from '@loopback/repository';
import { CompanyRepository, FlowActionRepository, FlowRepository } from '.';
import { LogChainDataSource } from '../datasources';
import { Company, Document, DocumentRelations, Flow, FlowAction } from '../models';
import { BaseCrudRepository } from './base-crud.repository.base';
import { AnyObject } from '@logchain/types';
import moment from 'moment';
import { LogChainConfigs } from '@logchain/configs';
import { UserView } from '@logchain/models/views';
import { UserViewRepository } from './user.view.repository';
 
export class DocumentRepository extends BaseCrudRepository<Document, typeof Document.prototype.id, DocumentRelations> {
  public readonly company: BelongsToAccessor<Company, typeof Document.prototype.id>;
  public readonly flow: BelongsToAccessor<Flow, typeof Document.prototype.id>;
  public readonly action: BelongsToAccessor<FlowAction, typeof Document.prototype.id>;
  public readonly modifier: BelongsToAccessor<UserView, typeof UserView.prototype.id>;
 
  constructor(
    @inject('datasources.logchain') dataSource: LogChainDataSource,
    @repository.getter('CompanyRepository')
    protected comp_repo_getter: Getter<CompanyRepository>,
    @repository.getter('UserViewRepository')
    protected user_repo_getter: Getter<UserViewRepository>,
    @repository.getter('FlowRepository')
    protected flow_repo_getter: Getter<FlowRepository>,
    @repository.getter('FlowActionRepository')
    protected flow_action_repo_getter: Getter<FlowActionRepository>,
  ) {
    super(Document, dataSource);
 
    this.company = this.createBelongsToAccessor('company', comp_repo_getter);
 
    this.flow = this.createBelongsToAccessor('flow', flow_repo_getter);
 
    this.action = this.createBelongsToAccessor('flow_action', flow_action_repo_getter);
 
    this.modifier = this.createBelongsToAccessor('modifier', user_repo_getter);
  }
 
  /**
   * Get action information by etag
   * @param  {string} etag
   */
  async getDocumentByETag(etag: string) {
    const sql = `
      SELECT get_documents_uploaded(adv.flow_action_id, adv.document_name) as data
      FROM actions_data_view adv
      WHERE hash = $1::text`;
    const resl = await this.execute(sql, [etag]);
 
    // If action's data have many records we should filter by hash
    if (resl.length && resl[0].data?.length) {
      const matchedRecords = resl[0].data.filter((record: AnyObject) => {
        return record.hash === etag;
      });
 
      if (matchedRecords.length) {
        matchedRecords[0].consensus = matchedRecords[0].consensus.filter((record: AnyObject) => {
          return record.company_id === this.current_user?.company_id;
        });
 
        return matchedRecords[0];
      }
 
      return false;
    }
 
    return false;
  }
 
  /**
   * Retrieves a list of failed PDF documents.
   *
   * @returns {Promise<Array<AnyObject>>} Returns an array of failed PDF documents.
   */
  async getFailedPDFDocuments(): Promise<Array<AnyObject>> {
    const now = moment();
    const nowAnd10MinutesLater = now.subtract(10, 'minute').toDate();
    const sql = `
      SELECT * 
      FROM actions_data_view adv
      WHERE status > 1
      AND modified_by IS NOT NULL
      AND actual_date IS NOT NULL
      AND actual_date < $1::timestamp
      AND (is_pdf_created IS NULL OR is_pdf_created = false)
      AND pdf_attempt <= $2::int`;
 
    return (await this.execute(sql, [nowAnd10MinutesLater, LogChainConfigs.MaxPdfAttempts])) as Array<AnyObject>;
  }
}