Skip to content

Commit 345f143

Browse files
Merge pull request #16 from CropWatchDevelopment/develop
implemented filter by group, location in latest-primary-data route
2 parents d342e64 + 20730d4 commit 345f143

2 files changed

Lines changed: 41 additions & 10 deletions

File tree

src/v1/devices/devices.controller.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ export class DevicesController {
111111
@UseGuards(JwtAuthGuard)
112112
@ApiQuery({ name: 'skip', description: 'Number of records to skip for pagination', required: false })
113113
@ApiQuery({ name: 'take', description: 'Number of records to take for pagination', required: false })
114+
@ApiQuery({ name: 'group', description: 'Filter by device group', required: false })
115+
@ApiQuery({ name: 'name', description: 'Filter by device name', required: false })
116+
@ApiQuery({ name: 'location', description: 'Filter by device location', required: false })
114117
@ApiOperation({
115118
summary: 'Get the latest primary data for all devices (paginated)',
116119
description: `
@@ -119,7 +122,10 @@ export class DevicesController {
119122
allLatestPrimaryData(@Req() req) {
120123
const skip = parseInt(req.query.skip, 10) || 0;
121124
const take = parseInt(req.query.take, 10) || 10;
122-
return this.devicesService.findAllLatestData(req.user, skip, take, req.headers.authorization);
125+
const searchGroup = req.query.group ? String(req.query.group) : undefined;
126+
const searchName = req.query.name ? String(req.query.name) : undefined;
127+
const searchLocation = req.query.location ? String(req.query.location) : undefined;
128+
return this.devicesService.findAllLatestData(req.user, skip, take, req.headers.authorization, searchGroup, searchName, searchLocation);
123129
}
124130

125131
@Get('location/:location_id')

src/v1/devices/devices.service.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)