All files / src/interfaces/web-api/client crm-client.controller.ts

0% Statements 0/36
0% Branches 0/20
0% Functions 0/4
0% Lines 0/34

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 96 97                                                                                                                                                                                                 
import {
  Controller,
  Get,
  Param,
  UseGuards,
  Request,
  NotFoundException,
} from '@nestjs/common';
import { QuotationContractService } from '../../../domains/crm/services/quotation-contract.service';
import { OnboardingService } from '../../../domains/crm/services/onboarding.service';
import { AccountContactService } from '../../../domains/crm/services/account-contact.service';
import { PermissionGuard } from '../../../platform/permissions/permission.guard';
import { RequirePermission } from '../../../platform/permissions/permission.decorator';
import { Types } from 'mongoose';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('web-api/client/crm')
@UseGuards(PermissionGuard)
@ApiTags('CrmClient')
export class CrmClientController {
  constructor(
    private readonly quotationContractService: QuotationContractService,
    private readonly onboardingService: OnboardingService,
    private readonly accountContactService: AccountContactService,
  ) {}
 
  @Get('quotes')
  @RequirePermission('crm_client_portal', 'read')
  @ApiOperation({ summary: 'Get my quotes operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getMyQuotes(@Request() req: any) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    // Find client's mapped accounts and contacts
    const contact = await this.accountContactService.getContactById(
      tenantId,
      req.user.contactId,
    );
    if (!contact?.accountId) {
      return { data: [], total: 0 };
    }
    return this.quotationContractService.listQuotations(
      tenantId,
      { accountId: contact.accountId.toString() },
      1,
      100,
    );
  }
 
  @Get('contracts')
  @RequirePermission('crm_client_portal', 'read')
  @ApiOperation({ summary: 'Get my contracts operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getMyContracts(@Request() req: any) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    const contact = await this.accountContactService.getContactById(
      tenantId,
      req.user.contactId,
    );
    if (!contact?.accountId) {
      return { data: [], total: 0 };
    }
    return this.quotationContractService.listContracts(
      tenantId,
      { accountId: contact.accountId.toString() },
      1,
      100,
    );
  }
 
  @Get('onboarding')
  @RequirePermission('crm_client_portal', 'read')
  @ApiOperation({ summary: 'Get my onboarding operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getMyOnboarding(@Request() req: any) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    const contact = await this.accountContactService.getContactById(
      tenantId,
      req.user.contactId,
    );
    if (!contact?.accountId) {
      throw new NotFoundException('Client account not found');
    }
    const onboarding = await this.onboardingService.getOnboardingByAccount(
      tenantId,
      contact.accountId.toString(),
    );
    if (!onboarding) {
      throw new NotFoundException('No active onboarding checklist found');
    }
    const checklist = await this.onboardingService.getChecklistItems(
      tenantId,
      onboarding._id.toString(),
    );
    return { onboarding, checklist };
  }
}