All files / src/controllers/public aws.controller.ts

39.13% Statements 9/23
0% Branches 0/4
0% Functions 0/4
33.33% Lines 7/21

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 961x 1x   1x 1x     1x                                     1x                                                                               1x                                                          
import { inject, service } from '@loopback/core';
import { oas, param, post, Request, requestBody, RestBindings, } from '@loopback/rest';
 
import { AwsConfigs, CONTENT_TYPE } from '@logchain/configs';
import { CompanyService, DocumentService, UserService } from '@logchain/services';
 
@oas.tags('Aws')
export class AWSController {
  constructor(
    @inject(RestBindings.Http.REQUEST)
    public req: Request,
    @service(UserService)
    public user_service: UserService,
    @service(CompanyService)
    public comp_service: CompanyService,
    @service(DocumentService)
    public doc_service: DocumentService,
  ) { }
 
  @post('/aws/webhook', {
    responses: {
      '204': {
        description: 'Update status successfully.',
      },
    },
  })
  async get(
    @param.header.string('x-api-key') api_key: string,
    @requestBody({
      content: {
        [CONTENT_TYPE.JSON]: {
          schema: {
            type: 'object',
            properties: {
              sub: {
                type: 'string',
              },
              company_id: {
                type: 'string',
              },
            },
          },
        },
      },
    })
    data: { sub: string; company_id: string },
  ) {
    if (api_key !== AwsConfigs.lambdaApiKey) {
      console.log(`API_KEY invalid ${api_key}`);
      return;
    }
 
    // eslint-disable-next-line @typescript-eslint/no-misused-promises
    setImmediate(async () => {
      await this.comp_service.activateCompany(data.company_id);
      await this.user_service.activateUser(data.sub);
    });
  }
 
  @post('/pdf/parse', {
    responses: {
      '200': {
        description: 'Read the pdf file and extract text from it.',
      },
    },
  })
  async parsePDF(
    @param.header.string('x-api-key') api_key: string,
    @requestBody({
      content: {
        [CONTENT_TYPE.JSON]: {
          schema: {
            type: 'object',
            properties: {
              pdf_id: {
                type: 'number',
              },
              s3_key: {
                type: 'string',
              },
            },
          },
        },
      },
    })
    data: { pdf_id: number; s3_key: string },
  ) {
    if (api_key !== AwsConfigs.lambdaApiKey) {
      console.log(`API_KEY invalid ${api_key}`);
      return;
    }
 
    return this.doc_service.parsePDF(data);
  }
}