File size: 4,061 Bytes
639bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
171
172
173
174
175
176
177
178
179
180
181
182
import {
  Controller,
  Get,
  Query,
  UseGuards,
  Request,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { SearchService } from './search.service';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { SearchDto, SearchType } from './dto';

interface RequestWithUser extends Request {
  user: {
    id: string;
    email: string;
  };
}

@ApiTags('Search')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@Controller('search')
export class SearchController {
  constructor(private readonly searchService: SearchService) {}

  /**
   * GET /search
   * Global search across messages, users, spaces, and files
   */
  @Get()
  @HttpCode(HttpStatus.OK)
  async search(
    @Query() dto: SearchDto,
    @Request() req: RequestWithUser,
  ): Promise<any> {
    const result = await this.searchService.search(req.user.id, dto);
    
    // Record search for analytics
    await this.searchService.recordSearch(
      req.user.id,
      dto.query,
      dto.type || SearchType.ALL,
    );

    return {
      success: true,
      data: result.results,
      meta: {
        query: result.query,
        total: result.total,
        hasMore: result.hasMore,
        page: result.page,
        limit: result.limit,
        byType: result.byType,
      },
    };
  }

  /**
   * GET /search/popular
   * Get popular search queries
   */
  @Get('popular')
  @HttpCode(HttpStatus.OK)
  async getPopularSearches(
    @Query('type') type?: string,
  ): Promise<any> {
    const searches = await this.searchService.getPopularSearches(type);
    return {
      success: true,
      data: searches,
    };
  }

  /**
   * GET /search/messages
   * Search messages only
   */
  @Get('messages')
  @HttpCode(HttpStatus.OK)
  async searchMessages(
    @Query() dto: SearchDto,
    @Request() req: RequestWithUser,
  ): Promise<any> {
    const messageDto = { ...dto, type: SearchType.MESSAGES };
    const result = await this.searchService.search(req.user.id, messageDto);
    
    return {
      success: true,
      data: result.results,
      meta: {
        query: result.query,
        total: result.total,
        hasMore: result.hasMore,
        page: result.page,
        limit: result.limit,
      },
    };
  }

  /**
   * GET /search/users
   * Search users only
   */
  @Get('users')
  @HttpCode(HttpStatus.OK)
  async searchUsers(
    @Query() dto: SearchDto,
    @Request() req: RequestWithUser,
  ): Promise<any> {
    const userDto = { ...dto, type: SearchType.USERS };
    const result = await this.searchService.search(req.user.id, userDto);
    
    return {
      success: true,
      data: result.results,
      meta: {
        query: result.query,
        total: result.total,
        hasMore: result.hasMore,
        page: result.page,
        limit: result.limit,
      },
    };
  }

  /**
   * GET /search/spaces
   * Search spaces only
   */
  @Get('spaces')
  @HttpCode(HttpStatus.OK)
  async searchSpaces(
    @Query() dto: SearchDto,
    @Request() req: RequestWithUser,
  ): Promise<any> {
    const spaceDto = { ...dto, type: SearchType.SPACES };
    const result = await this.searchService.search(req.user.id, spaceDto);
    
    return {
      success: true,
      data: result.results,
      meta: {
        query: result.query,
        total: result.total,
        hasMore: result.hasMore,
        page: result.page,
        limit: result.limit,
      },
    };
  }

  /**
   * GET /search/files
   * Search files only
   */
  @Get('files')
  @HttpCode(HttpStatus.OK)
  async searchFiles(
    @Query() dto: SearchDto,
    @Request() req: RequestWithUser,
  ): Promise<any> {
    const fileDto = { ...dto, type: SearchType.FILES };
    const result = await this.searchService.search(req.user.id, fileDto);
    
    return {
      success: true,
      data: result.results,
      meta: {
        query: result.query,
        total: result.total,
        hasMore: result.hasMore,
        page: result.page,
        limit: result.limit,
      },
    };
  }
}