Untitled

 avatar
unknown
typescript
21 days ago
2.8 kB
5
Indexable
import { Controller, Post, Body, Headers, Req, Get, Query, Param } from '@nestjs/common';
import { Request } from 'express';
import { TransactionService } from './transaction.service';
import { PrometheusService } from '../prometheus/prometheus.service';
import { WinstonLoggerService } from '../../common/logger/logger.service';


@Controller('transaction')
export class TransactionController {
  constructor(
    private readonly transactionService: TransactionService,
    private readonly logger: WinstonLoggerService,
    private readonly metricsService: PrometheusService
  ) { }

  @Post()
async processTransaction(
  @Headers('x-request-id') uuid: string,
  @Body() transaction: any,
  @Req() req: Request
) {
  this.logger.log('Headers: ' + JSON.stringify(req.headers));

  if (!uuid) {
    this.logger.error('Missing UUID in request headers');
    return { error: 'Missing UUID in request headers' };
  }

  transaction.id = uuid;

  const isRoomOccupied = transaction.isRoomOccupied;
  if (isRoomOccupied) {
    const message = `Transaction [${transaction.transaction_id}] skipped due to occupied room`;
    this.logger.warn(message);
    return { info: message };
  }

  try {
    const processed = await this.transactionService.processTransaction(transaction);
    if (!processed) {
      this.logger.error('Processing failed silently');
      return { error: 'Transaction could not be processed' };
    }

    const status = await this.transactionService.checkTransactionStatus(transaction.transaction_id);
    
    const transactionId = transaction?.data?.transaction_id || transaction.transaction_id;
    const transactionData = transaction?.data || transaction;

    const trnxService = {
      message: `Transaction [${transactionId}] was sent for processing!`,
      data: transactionData,
    };

    return {
      trnxService,
      status
    };
  } catch (error) {
    this.logger.error(`Transaction Error [${transaction.transaction_id}]`, error.stack || error.message);

    return {
      error: 'Internal error occurred while processing transaction',
      details: error.message || 'Unknown error'
    };
  }
}

  @Get('status')
  async getTransactionStatus(@Query('transactionId') transactionId: string): Promise<any> {
      const status = await this.transactionService.checkTransactionStatus(transactionId);
      this.logger.log(`Transaction status query: ${JSON.stringify(status)}`);
      return { ...status };
  }

  @Get('task')
  async getTaskResult(@Query('taskId') taskId: string): Promise<any> {
    const result = await this.transactionService.getSpecificTaskResult(taskId);
    this.logger.log(`Task result query: ${JSON.stringify(result)}`);
    return result;
  }
}


Editor is loading...
Leave a Comment