From 7a07f39f1692de10bc02c80a6c03eb0ab8f9868c Mon Sep 17 00:00:00 2001 From: ZaydSkaff Date: Tue, 8 Jul 2025 11:25:15 +0300 Subject: [PATCH] add communities filter to devices by project API (#455) --- src/device/dtos/get.device.dto.ts | 25 +++++++++++++++++++++++-- src/device/services/device.service.ts | 22 ++++++++++++++++++---- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/device/dtos/get.device.dto.ts b/src/device/dtos/get.device.dto.ts index e34a8b6..429273a 100644 --- a/src/device/dtos/get.device.dto.ts +++ b/src/device/dtos/get.device.dto.ts @@ -1,7 +1,7 @@ import { DeviceTypeEnum } from '@app/common/constants/device-type.enum'; import { ApiProperty } from '@nestjs/swagger'; +import { Transform } from 'class-transformer'; import { - IsArray, IsEnum, IsNotEmpty, IsOptional, @@ -74,13 +74,34 @@ export class GetDevicesFilterDto { @IsEnum(DeviceTypeEnum) @IsOptional() public deviceType: DeviceTypeEnum; + @ApiProperty({ description: 'List of Space IDs to filter devices', required: false, example: ['60d21b4667d0d8992e610c85', '60d21b4967d0d8992e610c86'], }) @IsOptional() - @IsArray() + @Transform(({ value }) => { + if (!Array.isArray(value)) { + return [value]; + } + return value; + }) @IsUUID('4', { each: true }) public spaces?: string[]; + + @ApiProperty({ + description: 'List of Community IDs to filter devices', + required: false, + example: ['60d21b4667d0d8992e610c85', '60d21b4967d0d8992e610c86'], + }) + @Transform(({ value }) => { + if (!Array.isArray(value)) { + return [value]; + } + return value; + }) + @IsOptional() + @IsUUID('4', { each: true }) + public communities?: string[]; } diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index e4c0da5..cc6f911 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -100,12 +100,15 @@ export class DeviceService { async getAllDevices( param: ProjectParam, - { deviceType, spaces }: GetDevicesFilterDto, + { deviceType, spaces, communities }: GetDevicesFilterDto, ): Promise { try { await this.validateProject(param.projectUuid); if (deviceType === DeviceTypeEnum.DOOR_LOCK) { - return await this.getDoorLockDevices(param.projectUuid, spaces); + return await this.getDoorLockDevices(param.projectUuid, { + spaces, + communities, + }); } else if (!deviceType) { const devices = await this.deviceRepository.find({ where: { @@ -113,7 +116,13 @@ export class DeviceService { spaceDevice: { uuid: spaces && spaces.length ? In(spaces) : undefined, spaceName: Not(ORPHAN_SPACE_NAME), - community: { project: { uuid: param.projectUuid } }, + community: { + uuid: + communities && communities.length + ? In(communities) + : undefined, + project: { uuid: param.projectUuid }, + }, }, }, relations: [ @@ -1247,7 +1256,10 @@ export class DeviceService { await this.deviceRepository.save(updatedDevices); } - private async getDoorLockDevices(projectUuid: string, spaces?: string[]) { + private async getDoorLockDevices( + projectUuid: string, + { communities, spaces }: { spaces?: string[]; communities?: string[] }, + ) { await this.validateProject(projectUuid); const devices = await this.deviceRepository.find({ @@ -1259,6 +1271,8 @@ export class DeviceService { spaceName: Not(ORPHAN_SPACE_NAME), uuid: spaces && spaces.length ? In(spaces) : undefined, community: { + uuid: + communities && communities.length ? In(communities) : undefined, project: { uuid: projectUuid, },