From 110ed4157a2456aae523b15e274fa3f872dd46c8 Mon Sep 17 00:00:00 2001 From: ZaydSkaff Date: Mon, 23 Jun 2025 09:34:59 +0300 Subject: [PATCH] task: add spaces filter to get devices by project (#422) --- .../controllers/device-project.controller.ts | 14 ++++---- src/device/dtos/get.device.dto.ts | 32 +++++++++++++------ src/device/services/device.service.ts | 16 ++++++---- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/device/controllers/device-project.controller.ts b/src/device/controllers/device-project.controller.ts index e5181dd..1585415 100644 --- a/src/device/controllers/device-project.controller.ts +++ b/src/device/controllers/device-project.controller.ts @@ -1,11 +1,11 @@ -import { DeviceService } from '../services/device.service'; -import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common'; -import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; -import { EnableDisableStatusEnum } from '@app/common/constants/days.enum'; import { ControllerRoute } from '@app/common/constants/controller-route'; -import { PermissionsGuard } from 'src/guards/permissions.guard'; +import { EnableDisableStatusEnum } from '@app/common/constants/days.enum'; +import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common'; +import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'; import { Permissions } from 'src/decorators/permissions.decorator'; -import { GetDoorLockDevices, ProjectParam } from '../dtos'; +import { PermissionsGuard } from 'src/guards/permissions.guard'; +import { GetDevicesFilterDto, ProjectParam } from '../dtos'; +import { DeviceService } from '../services/device.service'; @ApiTags('Device Module') @Controller({ @@ -25,7 +25,7 @@ export class DeviceProjectController { }) async getAllDevices( @Param() param: ProjectParam, - @Query() query: GetDoorLockDevices, + @Query() query: GetDevicesFilterDto, ) { return await this.deviceService.getAllDevices(param, query); } diff --git a/src/device/dtos/get.device.dto.ts b/src/device/dtos/get.device.dto.ts index 84c9d64..e34a8b6 100644 --- a/src/device/dtos/get.device.dto.ts +++ b/src/device/dtos/get.device.dto.ts @@ -1,6 +1,7 @@ import { DeviceTypeEnum } from '@app/common/constants/device-type.enum'; import { ApiProperty } from '@nestjs/swagger'; import { + IsArray, IsEnum, IsNotEmpty, IsOptional, @@ -41,16 +42,7 @@ export class GetDeviceLogsDto { @IsOptional() public endTime: string; } -export class GetDoorLockDevices { - @ApiProperty({ - description: 'Device Type', - enum: DeviceTypeEnum, - required: false, - }) - @IsEnum(DeviceTypeEnum) - @IsOptional() - public deviceType: DeviceTypeEnum; -} + export class GetDevicesBySpaceOrCommunityDto { @ApiProperty({ description: 'Device Product Type', @@ -72,3 +64,23 @@ export class GetDevicesBySpaceOrCommunityDto { @IsNotEmpty({ message: 'Either spaceUuid or communityUuid must be provided' }) requireEither?: never; // This ensures at least one of them is provided } + +export class GetDevicesFilterDto { + @ApiProperty({ + description: 'Device Type', + enum: DeviceTypeEnum, + required: false, + }) + @IsEnum(DeviceTypeEnum) + @IsOptional() + public deviceType: DeviceTypeEnum; + @ApiProperty({ + description: 'List of Space IDs to filter devices', + required: false, + example: ['60d21b4667d0d8992e610c85', '60d21b4967d0d8992e610c86'], + }) + @IsOptional() + @IsArray() + @IsUUID('4', { each: true }) + public spaces?: string[]; +} diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index d2ac4e7..793d854 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -53,7 +53,7 @@ import { DeviceSceneParamDto } from '../dtos/device.param.dto'; import { GetDeviceLogsDto, GetDevicesBySpaceOrCommunityDto, - GetDoorLockDevices, + GetDevicesFilterDto, } from '../dtos/get.device.dto'; import { controlDeviceInterface, @@ -955,19 +955,20 @@ export class DeviceService { async getAllDevices( param: ProjectParam, - query: GetDoorLockDevices, + { deviceType, spaces }: GetDevicesFilterDto, ): Promise { try { await this.validateProject(param.projectUuid); - if (query.deviceType === DeviceTypeEnum.DOOR_LOCK) { - return await this.getDoorLockDevices(param.projectUuid); - } else if (!query.deviceType) { + if (deviceType === DeviceTypeEnum.DOOR_LOCK) { + return await this.getDoorLockDevices(param.projectUuid, spaces); + } else if (!deviceType) { const devices = await this.deviceRepository.find({ where: { isActive: true, spaceDevice: { - community: { project: { uuid: param.projectUuid } }, + uuid: spaces && spaces.length ? In(spaces) : undefined, spaceName: Not(ORPHAN_SPACE_NAME), + community: { project: { uuid: param.projectUuid } }, }, }, relations: [ @@ -1563,7 +1564,7 @@ export class DeviceService { } } - async getDoorLockDevices(projectUuid: string) { + async getDoorLockDevices(projectUuid: string, spaces?: string[]) { await this.validateProject(projectUuid); const devices = await this.deviceRepository.find({ @@ -1573,6 +1574,7 @@ export class DeviceService { }, spaceDevice: { spaceName: Not(ORPHAN_SPACE_NAME), + uuid: spaces && spaces.length ? In(spaces) : undefined, community: { project: { uuid: projectUuid,