All files / src/platform/tenants ticket.controller.ts

0% Statements 0/24
0% Branches 0/19
0% Functions 0/7
0% Lines 0/22

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                                                                                                                                                                                                   
import {
  Controller,
  Get,
  Post,
  Put,
  Param,
  Body,
  Req,
  UseGuards,
} from '@nestjs/common';
import { TenantService } from './tenant.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('api/v1/support/tickets')
@ApiTags('SupportTickets')
@UseGuards(JwtAuthGuard)
export class TicketController {
  constructor(private readonly tenantService: TenantService) {}
 
  @Get()
  @ApiOperation({ summary: 'List support tickets for active tenant' })
  @ApiResponse({ status: 200, description: 'List of tickets' })
  async listTickets(@Req() req: any) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    return this.tenantService.listTickets(tenantId);
  }
 
  @Get('admin')
  @ApiOperation({
    summary: 'List all support tickets across all tenants for admins',
  })
  @ApiResponse({ status: 200, description: 'Cross-tenant list of tickets' })
  async listAllTickets() {
    return this.tenantService.listAllTicketsCrossTenant();
  }
 
  @Get(':id')
  @ApiOperation({ summary: 'Retrieve specific support ticket details' })
  @ApiResponse({ status: 200, description: 'Ticket details object' })
  async findTicketById(@Param('id') id: string) {
    return this.tenantService.findTicketById(id);
  }
 
  @Post()
  @ApiOperation({ summary: 'Create a new support ticket' })
  @ApiResponse({ status: 201, description: 'Created ticket object' })
  async createTicket(
    @Req() req: any,
    @Body()
    body: {
      subject: string;
      description: string;
      priority?: string;
      category?: string;
    },
  ) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    return this.tenantService.createTicket(
      tenantId,
      body.subject,
      body.description,
      body.priority || 'MEDIUM',
      body.category || 'General',
    );
  }
 
  @Post(':id/messages')
  @ApiOperation({ summary: 'Add message to support ticket message log thread' })
  @ApiResponse({ status: 201, description: 'Updated ticket details' })
  async addMessage(
    @Param('id') id: string,
    @Req() req: any,
    @Body() body: { content: string },
  ) {
    const sender = req.user?.name || req.user?.email || 'User';
    const role = req.user?.role || 'CLIENT';
    return this.tenantService.addMessageToTicket(
      id,
      sender,
      role,
      body.content,
    );
  }
 
  @Put(':id/status')
  @ApiOperation({ summary: 'Change state of support ticket' })
  @ApiResponse({ status: 200, description: 'Updated ticket details' })
  async updateStatus(
    @Param('id') id: string,
    @Body() body: { status: string },
  ) {
    return this.tenantService.updateTicketStatus(id, body.status);
  }
}