1
- import { HttpException , HttpStatus , Injectable } from '@nestjs/common' ;
1
+ import { HttpException , HttpStatus , Injectable , Logger } from '@nestjs/common' ;
2
2
import { InjectModel } from '@nestjs/mongoose' ;
3
3
import { PageQueryDTO } from '@shared/validation/common/dto/PageQuery.dto' ;
4
4
import { SearchQueryDTO } from '@shared/validation/common/dto/SearchQuery.dto' ;
@@ -13,6 +13,7 @@ import { User, UserDocument } from './entity/user.entity';
13
13
14
14
@Injectable ( )
15
15
export class UserService {
16
+ private readonly logger = new Logger ( UserService . name ) ;
16
17
constructor ( @InjectModel ( User . name ) private userModel : Model < User > ) { }
17
18
18
19
public async create ( user_registered : CreateUser ) {
@@ -73,14 +74,26 @@ export class UserService {
73
74
74
75
const users = await this . userModel
75
76
. find ( {
76
- $text : { $search : query } ,
77
+ $or : [
78
+ { username : { $regex : query , $options : 'i' } } ,
79
+ { publicName : { $regex : query , $options : 'i' } } ,
80
+ { description : { $regex : query , $options : 'i' } } ,
81
+ ] ,
77
82
} )
78
83
. select ( 'username publicName email profileImage' )
79
84
. sort ( { [ sort ] : sortOrder } )
80
85
. skip ( skip )
81
86
. limit ( limit ) ;
82
87
83
- const total = await this . userModel . countDocuments ( ) ;
88
+ const total = await this . userModel . countDocuments ( {
89
+ username : { $regex : query , $options : 'i' } ,
90
+ publicName : { $regex : query , $options : 'i' } ,
91
+ description : { $regex : query , $options : 'i' } ,
92
+ } ) ;
93
+
94
+ this . logger . debug (
95
+ `Retrived users: ${ users . length } documents, with total: ${ total } ` ,
96
+ ) ;
84
97
85
98
return {
86
99
users,
@@ -196,7 +209,7 @@ export class UserService {
196
209
}
197
210
198
211
public async createSearchIndexes ( ) {
199
- await this . userModel . collection . createIndex (
212
+ return await this . userModel . collection . createIndex (
200
213
{
201
214
username : 'text' ,
202
215
publicName : 'text' ,
0 commit comments