All files / src/interfaces/public-api crm-public.controller.ts

0% Statements 0/17
0% Branches 0/12
0% Functions 0/2
0% Lines 0/15

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                                                                                                               
import {
  Controller,
  Post,
  Body,
  Param,
  Query,
  Headers,
  BadRequestException,
} from '@nestjs/common';
import { LeadCaptureService } from '../../domains/crm/services/lead-capture.service';
import { Types } from 'mongoose';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('public-api/crm')
@ApiTags('CrmPublic')
export class CrmPublicController {
  constructor(private readonly leadCaptureService: LeadCaptureService) {}
 
  @Post('capture/:formCode')
  @ApiOperation({ summary: 'Capture lead operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async captureLead(
    @Param('formCode') formCode: string,
    @Query('tenantId') queryTenantId: string,
    @Body() payload: any,
    @Headers('x-forwarded-for') xForwardedFor?: string,
    @Headers('user-agent') userAgent?: string,
    @Headers('referer') referrer?: string,
  ) {
    const tenantIdStr = queryTenantId || payload.tenantId;
    if (!tenantIdStr) {
      throw new BadRequestException(
        'Missing tenantId in query parameters or request body',
      );
    }
 
    if (!Types.ObjectId.isValid(tenantIdStr)) {
      throw new BadRequestException('Invalid tenantId format');
    }
 
    const tenantId = new Types.ObjectId(tenantIdStr);
    const ipAddress = xForwardedFor
      ? xForwardedFor.split(',')[0].trim()
      : undefined;
 
    return this.leadCaptureService.submitLead(
      tenantId,
      formCode,
      payload,
      ipAddress,
      userAgent,
      referrer,
    );
  }
}