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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | import { Controller, Get, Post, Put, Body, Param, Query, UseGuards, Request, } from '@nestjs/common'; import { OpportunityService } from '../services/opportunity.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('crm/opportunities') @UseGuards(PermissionGuard) @ApiTags('Opportunity') export class OpportunityController { constructor(private readonly opportunityService: OpportunityService) {} @Post('pipelines') @RequirePermission('crm_config', 'update') @ApiOperation({ summary: 'Create pipeline operation' }) @ApiResponse({ status: 201, description: 'Operation successful' }) async createPipeline(@Request() req: any, @Body() body: any) { const tenantId = new Types.ObjectId(req.user.tenantId); return this.opportunityService.createPipeline(tenantId, body); } @Get('pipelines') @RequirePermission('crm_config', 'read') @ApiOperation({ summary: 'List pipelines operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async listPipelines(@Request() req: any) { const tenantId = new Types.ObjectId(req.user.tenantId); return this.opportunityService.listPipelines(tenantId); } @Post('pipelines/stages') @RequirePermission('crm_config', 'update') @ApiOperation({ summary: 'Create stage operation' }) @ApiResponse({ status: 201, description: 'Operation successful' }) async createStage(@Request() req: any, @Body() body: any) { const tenantId = new Types.ObjectId(req.user.tenantId); return this.opportunityService.createStage(tenantId, body); } @Get('pipelines/:pipelineId/stages') @RequirePermission('crm_config', 'read') @ApiOperation({ summary: 'Get stages operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async getStages( @Request() req: any, @Param('pipelineId') pipelineId: string, ) { const tenantId = new Types.ObjectId(req.user.tenantId); return this.opportunityService.getStages(tenantId, pipelineId); } @Post() @RequirePermission('crm_opportunity', 'create') @ApiOperation({ summary: 'Create opportunity operation' }) @ApiResponse({ status: 201, description: 'Operation successful' }) async createOpportunity(@Request() req: any, @Body() body: any) { const tenantId = new Types.ObjectId(req.user.tenantId); return this.opportunityService.createOpportunity(tenantId, body); } @Get() @RequirePermission('crm_opportunity', 'read') @ApiOperation({ summary: 'List opportunities operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async listOpportunities( @Request() req: any, @Query('status') status?: string, @Query('pipelineId') pipelineId?: string, @Query('stageId') stageId?: string, @Query('ownerId') ownerId?: string, @Query('accountId') accountId?: string, @Query('search') search?: string, @Query('page') page?: number, @Query('limit') limit?: number, ) { const tenantId = new Types.ObjectId(req.user.tenantId); return this.opportunityService.listOpportunities( tenantId, { status, pipelineId, stageId, ownerId, accountId, search }, page ? Number(page) : 1, limit ? Number(limit) : 25, ); } @Get(':id') @RequirePermission('crm_opportunity', 'read') @ApiOperation({ summary: 'Get opportunity by id operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async getOpportunityById(@Request() req: any, @Param('id') id: string) { const tenantId = new Types.ObjectId(req.user.tenantId); return this.opportunityService.getOpportunityById(tenantId, id); } @Put(':id') @RequirePermission('crm_opportunity', 'update') @ApiOperation({ summary: 'Update opportunity operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async updateOpportunity( @Request() req: any, @Param('id') id: string, @Body() body: any, ) { const tenantId = new Types.ObjectId(req.user.tenantId); return this.opportunityService.updateOpportunity(tenantId, id, body); } @Post(':id/move') @RequirePermission('crm_opportunity', 'update') @ApiOperation({ summary: 'Move to stage operation' }) @ApiResponse({ status: 201, description: 'Operation successful' }) async moveToStage( @Request() req: any, @Param('id') id: string, @Body('toStageId') toStageId: string, @Body('reason') reason?: string, ) { const tenantId = new Types.ObjectId(req.user.tenantId); const userId = new Types.ObjectId(req.user.userId); return this.opportunityService.moveToStage( tenantId, id, toStageId, userId, reason, ); } @Get(':id/stage-history') @RequirePermission('crm_opportunity', 'read') @ApiOperation({ summary: 'Get stage history operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async getStageHistory(@Request() req: any, @Param('id') id: string) { const tenantId = new Types.ObjectId(req.user.tenantId); return this.opportunityService.getStageHistory(tenantId, id); } @Get('forecast/calculate') @RequirePermission('crm_opportunity', 'read') @ApiOperation({ summary: 'Calculate forecast operation' }) @ApiResponse({ status: 200, description: 'Operation successful' }) async calculateForecast( @Request() req: any, @Query('period') period: string, @Query('start') start: string, @Query('end') end: string, @Query('ownerId') ownerId?: string, @Query('teamId') teamId?: string, ) { const tenantId = new Types.ObjectId(req.user.tenantId); return this.opportunityService.calculateForecast( tenantId, period, new Date(start), new Date(end), { ownerId, teamId }, ); } } |