All files / src/cronjobs process-xml-from-sutton.cronjob.ts

48.39% Statements 15/31
0% Branches 0/4
25% Functions 1/4
44.83% Lines 13/29

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 661x 1x 1x 1x 1x 1x 1x     1x 1x     1x   1x   1x   1x                                                                                            
import { UserRepository } from '@logchain/repositories';
import { ExternalService, FlowService } from '@logchain/services';
import { service } from '@loopback/core';
import { CronJob, cronJob } from '@loopback/cron';
import { repository } from '@loopback/repository';
import moment from 'moment';
import { exec } from 'child_process';
 
@cronJob()
export class ProcessXMLFromSuttonCronJob extends CronJob {
  private readonly lock_key = '1715049397';
  constructor(
    @repository(UserRepository)
    public userRepository: UserRepository,
    @service(ExternalService)
    public externalService: ExternalService,
    @service(FlowService)
    public flowService: FlowService,
  ) {
    super({
      name: 'Process XML from Sutton',
      onTick: async () => {
        await this.performCronJob();
      },
      cronTime: '* * * * *', // Run every minute
      start: true,
    });
  }
 
  async performCronJob() {
    const isUnLocked = await this.userRepository.execute(`SELECT pg_try_advisory_lock(${this.lock_key});`);
    if (!isUnLocked[0].pg_try_advisory_lock) {
      return;
    }
 
    await this.userRepository.execute(`SELECT pg_advisory_lock(${this.lock_key});`);
    const now = moment();
    
    const startAt = now.toDate();
    console.log(`*** Cronjob: ${this.name} started at ${startAt}`);
    // Your code start here
    
    // Start call service from here
    // Define the URL, headers, and options for the curl command
    const url = `${process.env.API_URL}/external/${process.env.EXTERNAL_ACCESS_KEY_ID}/ftp/actions/generate`;
 
    // Construct the curl command with headers
    const curlCommand = `curl -X POST "${url}" -H  "accept: */*" -H  "x-api-key: ${process.env.EXTERNAL_ACCESS_KEY}" -d ""`;
 
    // Execute the curl command
    exec(curlCommand, (error, _stdout, _stderr) => {
      if (error) {
        console.error(`Error executing curl: ${error.message}`);
      }
    });
    // ------------------------------
 
    // Your code end here
    const finishedAt = new Date();
    console.log(`*** Cronjob: ${this.name} finished at ${finishedAt}.`);
 
    await this.userRepository.execute(`SELECT pg_advisory_unlock(${this.lock_key});`);
  }
 
}