All files / src/domains/crm/controllers crm-config.controller.ts

0% Statements 0/33
0% Branches 0/8
0% Functions 0/8
0% Lines 0/31

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                                                                                                                                                                                                                         
import {
  Controller,
  Get,
  Post,
  Body,
  Put,
  Param,
  UseGuards,
  Request,
  Query,
} from '@nestjs/common';
import { CrmConfigService } from '../services/crm-config.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/config')
@UseGuards(PermissionGuard)
@ApiTags('CrmConfig')
export class CrmConfigController {
  constructor(private readonly configService: CrmConfigService) {}
 
  @Get()
  @RequirePermission('crm_config', 'read')
  @ApiOperation({ summary: 'Get configuration operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getConfiguration(@Request() req: any) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    return this.configService.getConfiguration(tenantId);
  }
 
  @Put()
  @RequirePermission('crm_config', 'update')
  @ApiOperation({ summary: 'Update configuration operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async updateConfiguration(@Request() req: any, @Body() updates: any) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    return this.configService.updateConfiguration(tenantId, updates);
  }
 
  @Post('publish')
  @RequirePermission('crm_config', 'update')
  @ApiOperation({ summary: 'Publish configuration operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async publishConfiguration(@Request() req: any, @Body() body: any) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    const userId = new Types.ObjectId(req.user.userId);
    return this.configService.publishConfiguration(
      tenantId,
      userId,
      body.description,
    );
  }
 
  @Get('versions')
  @RequirePermission('crm_config', 'read')
  @ApiOperation({ summary: 'Get configuration versions operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getConfigurationVersions(
    @Request() req: any,
    @Query('page') page?: number,
    @Query('limit') limit?: number,
  ) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    return this.configService.getConfigurationVersions(
      tenantId,
      page ? Number(page) : 1,
      limit ? Number(limit) : 20,
    );
  }
 
  @Get('numbering-rules')
  @RequirePermission('crm_config', 'read')
  @ApiOperation({ summary: 'List numbering rules operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async listNumberingRules(@Request() req: any) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    return this.configService.listNumberingRules(tenantId);
  }
 
  @Post('numbering-rules')
  @RequirePermission('crm_config', 'update')
  @ApiOperation({ summary: 'Upsert numbering rule operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async upsertNumberingRule(@Request() req: any, @Body() body: any) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    return this.configService.upsertNumberingRule(tenantId, body.entityType, {
      prefix: body.prefix,
      paddingLength: body.paddingLength,
      startNumber: body.startNumber,
    });
  }
 
  @Post('sequences/reset')
  @RequirePermission('crm_config', 'update')
  @ApiOperation({ summary: 'Reset sequence operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async resetSequence(@Request() req: any, @Body() body: any) {
    const tenantId = new Types.ObjectId(req.user.tenantId);
    await this.configService.resetSequence(
      tenantId,
      body.entityType,
      Number(body.newStart),
    );
    return { success: true };
  }
}