All files / src/platform/search search.controller.ts

0% Statements 0/37
0% Branches 0/22
0% Functions 0/9
0% Lines 0/35

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                                                                                                                                                                                                                                               
import {
  Controller,
  Get,
  Post,
  Delete,
  Body,
  Query,
  Req,
  Param,
} from '@nestjs/common';
import { GlobalSearchService } from './search.service';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('api/v1/search')
@ApiTags('GlobalSearch')
export class GlobalSearchController {
  constructor(private readonly searchService: GlobalSearchService) {}
 
  private getContext(req: any) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    const userId = req.user?.id || req.headers['x-user-id'] || 'system-user-id';
    return { tenantId, userId };
  }
 
  @Get()
  @ApiOperation({ summary: 'Search operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async search(
    @Query('q') q: string,
    @Query('entityTypes') entityTypes: string,
    @Query('page') page: string,
    @Query('limit') limit: string,
    @Query('filters') filters: string,
    @Req() req: any,
  ) {
    const ctx = this.getContext(req);
    const parsedTypes = entityTypes ? entityTypes.split(',') : undefined;
    let parsedFilters: any = undefined;
    if (filters) {
      try {
        parsedFilters = JSON.parse(filters);
      } catch {
        parsedFilters = undefined;
      }
    }
 
    return this.searchService.search({
      query: q || '',
      entityTypes: parsedTypes,
      tenantId: ctx.tenantId,
      userId: ctx.userId,
      filters: parsedFilters,
      page: page ? parseInt(page) : 1,
      limit: limit ? parseInt(limit) : 20,
    });
  }
 
  @Get('suggestions')
  @ApiOperation({ summary: 'Suggestions operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async suggestions(@Query('q') q: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.searchService.suggest(q || '', ctx.tenantId);
  }
 
  @Get('recent')
  @ApiOperation({ summary: 'Get recent operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getRecent(@Req() req: any) {
    const ctx = this.getContext(req);
    return this.searchService.getRecent(ctx.tenantId, ctx.userId);
  }
 
  @Delete('recent')
  @ApiOperation({ summary: 'Clear recent operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async clearRecent(@Req() req: any) {
    const ctx = this.getContext(req);
    return this.searchService.clearRecent(ctx.tenantId, ctx.userId);
  }
 
  @Get('saved')
  @ApiOperation({ summary: 'Get saved operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getSaved(@Req() req: any) {
    const ctx = this.getContext(req);
    return this.searchService.getSaved(ctx.tenantId, ctx.userId);
  }
 
  @Post('saved')
  @ApiOperation({ summary: 'Save search operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async saveSearch(
    @Body()
    body: {
      name: string;
      query: string;
      filters?: any;
      entityTypes?: string[];
    },
    @Req() req: any,
  ) {
    const ctx = this.getContext(req);
    return this.searchService.saveSearch({
      tenantId: ctx.tenantId,
      userId: ctx.userId,
      ...body,
    });
  }
 
  @Delete('saved/:id')
  @ApiOperation({ summary: 'Delete saved operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async deleteSaved(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.searchService.deleteSaved(id, ctx.tenantId, ctx.userId);
  }
}