@@ -399,32 +399,57 @@ export class DevicesService {
399399 skip : number = 0 ,
400400 take : number = 10 ,
401401 authHeader : string ,
402+ searchGroup ?: string ,
403+ searchName ?: string ,
404+ searchLocation ?: string ,
402405 ) : Promise < PagedDevicesResponse < any > > {
403406 const accessToken = getAccessToken ( authHeader ) ;
404407 const client = this . supabaseService . getClient ( accessToken ) ;
405408 const userId = getUserId ( jwtPayload ) ;
406409
407- const { count , error : countError } = await client
410+ let countQuery = client
408411 . from ( 'cw_devices' )
409- . select ( '*, owner_match:cw_device_owners()' , { count : 'exact' , head : true } )
412+ . select ( 'owner_match:cw_device_owners(), cw_locations(name )' , { count : 'exact' , head : true } )
410413 . eq ( 'owner_match.user_id' , userId )
411414 . gt ( 'owner_match.permission_level' , 4 )
412- . or ( `user_id.eq.${ userId } ,owner_match.not.is.null` )
413- . order ( 'name' , { ascending : true } ) ;
415+ . or ( `user_id.eq.${ userId } ,owner_match.not.is.null` ) ;
416+
417+ if ( searchGroup ) {
418+ countQuery = countQuery . ilike ( 'group' , `%${ searchGroup } %` ) ;
419+ }
420+ if ( searchName ) {
421+ countQuery = countQuery . ilike ( 'name' , `%${ searchName } %` ) ;
422+ }
423+ if ( searchLocation ) {
424+ countQuery = countQuery . ilike ( 'cw_locations.name' , `%${ searchLocation } %` ) ;
425+ }
426+
427+ const { count, error : countError } = await countQuery ;
414428
415429 if ( countError ) {
416430 throw new InternalServerErrorException ( 'Failed to fetch device' ) ;
417431 }
418432
419- const { data : device , error : deviceError } = await client
433+ let devicesQuery = client
420434 . from ( 'cw_devices' )
421435 . select ( '*, cw_device_type(*), cw_locations(name), owner_match:cw_device_owners()' )
422436 . eq ( 'owner_match.user_id' , userId )
423437 . gt ( 'owner_match.permission_level' , 4 )
424- . or ( `user_id.eq.${ userId } ,owner_match.not.is.null` )
425- . range ( skip , skip + take - 1 )
426- . limit ( take )
427- . order ( 'name' , { ascending : false } ) ;
438+ . or ( `user_id.eq.${ userId } ,owner_match.not.is.null` ) ;
439+
440+ if ( searchGroup ) {
441+ devicesQuery = devicesQuery . ilike ( 'group' , `%${ searchGroup } %` ) ;
442+ }
443+ if ( searchName ) {
444+ devicesQuery = devicesQuery . ilike ( 'name' , `%${ searchName } %` ) ;
445+ }
446+ if ( searchLocation ) {
447+ devicesQuery = devicesQuery . ilike ( 'cw_locations.name' , `%${ searchLocation } %` ) ;
448+ }
449+
450+ const { data : device , error : deviceError } = await devicesQuery
451+ . order ( 'name' , { ascending : false } )
452+ . range ( skip , skip + take - 1 ) ;
428453
429454 if ( deviceError ) {
430455 throw new InternalServerErrorException ( 'Failed to fetch device' ) ;
0 commit comments