All files / src/modules/agencies agencies.controller.ts

65.85% Statements 27/41
54.23% Branches 32/59
7.69% Functions 1/13
64.1% Lines 25/39

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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 12711x                       11x 11x 11x 11x 11x 11x 11x 11x 11x 11x           11x 11x       11x           11x                 11x                   11x             11x                 11x                 11x                 11x                 11x           11x           11x             11x              
import {
  Controller,
  Post,
  Body,
  UseGuards,
  Get,
  Param,
  ParseIntPipe,
  Patch,
  Delete,
  ForbiddenException,
} from '@nestjs/common';
import { AgenciesService } from '@modules/agencies/agencies.service';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { ApplyForAgencyDto } from '@modules/agencies/dto/apply-for-agency.dto';
import { JwtAuthGuard } from '@common/guards/jwt-auth.guard';
import { User, Role } from '@prisma/client';
import { Roles } from '@common/decorators/roles.decorator';
import { RolesGuard } from '@common/guards/roles.guard';
import { InviteAgentDto } from '@modules/agencies/dto/invite-agent.dto';
import { UpdateAgencyDto } from '@modules/agencies/dto/update-agency.dto';
import { CurrentUser } from '@common/decorators/current-user.decorator';
 
@ApiTags('Agencies')
@ApiBearerAuth()
@Controller('agencies')
@UseGuards(JwtAuthGuard)
export class AgenciesController {
  constructor(private readonly agenciesService: AgenciesService) {}
 
  @Get('applications/me')
  @ApiOperation({ summary: 'Get my agency applications', description: 'Requires authentication.' })
  getMyApplications(@CurrentUser() user: User) {
    return this.agenciesService.getMyApplications(user);
  }
 
  @Post('apply')
  @ApiOperation({ summary: 'Apply to create an agency', description: 'Requires authentication.' })
  applyForAgency(
    @Body() applyForAgencyDto: ApplyForAgencyDto,
    @CurrentUser() user: User,
  ) {
    return this.agenciesService.applyForAgency(applyForAgencyDto, user);
  }
 
  @Patch('me')
  @ApiOperation({ summary: 'Request update to my agency profile', description: 'Requires authentication.' })
  updateMyAgency(
    @CurrentUser() user: User,
    @Body() updateAgencyDto: UpdateAgencyDto,
  ) {
    return this.agenciesService.requestUpdateMyAgency(user, updateAgencyDto);
  }
 
  @Post('me/seats')
  @ApiOperation({ summary: 'Purchase agent seats for my agency', description: 'Creates a subscription and Click.uz payment intent.' })
  @Roles(Role.AGENCY_OWNER)
  createSeats(@CurrentUser() user: User, @Body() body: { seatCount: number }) {
    return (this.agenciesService as any).createSeatSubscription(user.agencyId, Number(body.seatCount || 1));
  }
 
  @Get('me/insights')
  @ApiOperation({ summary: 'Get agency analytics insights', description: 'Returns pre-aggregated analytics data for the agency.' })
  @Roles(Role.AGENCY_OWNER)
  async getInsights(@CurrentUser() user: User) {
    Iif (!user.agencyId) {
      throw new ForbiddenException('User is not associated with an agency');
    }
    return this.agenciesService.getInsights(user.agencyId);
  }
 
  @Post('invite')
  @ApiOperation({ summary: 'Invite a user to agency', description: 'Requires authentication.' })
  inviteAgent(
    @CurrentUser() user: User,
    @Body() inviteAgentDto: InviteAgentDto,
  ) {
    return this.agenciesService.inviteAgent(user, inviteAgentDto);
  }
 
  @Post('invitations/:token/accept')
  @ApiOperation({ summary: 'Accept an agency invitation' })
  acceptInvitation(
    @CurrentUser() user: User,
    @Param('token') token: string,
  ) {
    return this.agenciesService.acceptInvitation(user, token);
  }
 
  @Post('invitations/:token/decline')
  @ApiOperation({ summary: 'Decline an agency invitation' })
  declineInvitation(
    @CurrentUser() user: User,
    @Param('token') token: string,
  ) {
    return this.agenciesService.declineInvitation(user, token);
  }
 
  @Get('me')
  @ApiOperation({ summary: 'Get my agency profile', description: 'Requires authentication.' })
  getMyAgency(@CurrentUser() user: User) {
    return this.agenciesService.getMyAgency(user);
  }
 
  @Get(':id')
  @ApiOperation({ summary: 'Get an agency by ID' })
  getAgencyById(@Param('id', ParseIntPipe) id: number) {
    return this.agenciesService.getAgencyById(id);
  }
 
  @Get(':id/agents')
  @ApiOperation({ summary: 'List agents in an agency' })
  getAgencyAgents(@Param('id', ParseIntPipe) id: number) {
    return this.agenciesService.getAgencyAgents(id);
  }
 
  @Delete(':agencyId/agents/:userId')
  @Roles(Role.AGENT)
  @UseGuards(RolesGuard)
  removeAgent(
    @CurrentUser() remover: User,
    @Param('agencyId', ParseIntPipe) agencyId: number,
    @Param('userId', ParseIntPipe) userId: number,
  ) {
    return this.agenciesService.removeAgent(remover, agencyId, userId);
  }
}