All files / src/domains/crm/controllers sales-target.controller.ts

0% Statements 0/25
0% Branches 0/4
0% Functions 0/6
0% Lines 0/23

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                                                                                                                                                             
import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Query,
  UseGuards,
  Request,
} from '@nestjs/common';
import { SalesTargetService } from '../services/sales-target.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/sales-targets')
@UseGuards(PermissionGuard)
@ApiTags('SalesTarget')
export class SalesTargetController {
  constructor(private readonly targetService: SalesTargetService) {}
 
  @Post()
  @RequirePermission('crm_target', 'create')
  @ApiOperation({ summary: 'Create target operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async createTarget(@Request() req: any, @Body() body: any) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    return this.targetService.createTarget(tenantId, body);
  }
 
  @Get()
  @RequirePermission('crm_target', 'read')
  @ApiOperation({ summary: 'Get targets operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getTargets(@Request() req: any, @Query('userId') userId?: string) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    return this.targetService.getTargets(tenantId, userId);
  }
 
  @Post(':id/achievement')
  @RequirePermission('crm_target', 'update')
  @ApiOperation({ summary: 'Update achievement operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async updateAchievement(
    @Request() req: any,
    @Param('id') id: string,
    @Body('actualValueMinor') actualValueMinor: number,
  ) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    return this.targetService.calculateTargetAchievement(
      tenantId,
      id,
      actualValueMinor,
    );
  }
 
  @Post('commission-plans')
  @RequirePermission('crm_target', 'update')
  @ApiOperation({ summary: 'Create commission plan operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async createCommissionPlan(@Request() req: any, @Body() body: any) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    return this.targetService.createCommissionPlan(tenantId, body);
  }
 
  @Get('commissions')
  @RequirePermission('crm_target', 'read')
  @ApiOperation({ summary: 'Get commission logs operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getCommissionLogs(
    @Request() req: any,
    @Query('userId') userId: string,
  ) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    return this.targetService.getCommissionLogs(tenantId, userId);
  }
}