add communities filter to devices by project API (#455)

This commit is contained in:
ZaydSkaff
2025-07-08 11:25:15 +03:00
committed by GitHub
parent 18b21d697c
commit 7a07f39f16
2 changed files with 41 additions and 6 deletions

View File

@ -1,7 +1,7 @@
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum'; import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { import {
IsArray,
IsEnum, IsEnum,
IsNotEmpty, IsNotEmpty,
IsOptional, IsOptional,
@ -74,13 +74,34 @@ export class GetDevicesFilterDto {
@IsEnum(DeviceTypeEnum) @IsEnum(DeviceTypeEnum)
@IsOptional() @IsOptional()
public deviceType: DeviceTypeEnum; public deviceType: DeviceTypeEnum;
@ApiProperty({ @ApiProperty({
description: 'List of Space IDs to filter devices', description: 'List of Space IDs to filter devices',
required: false, required: false,
example: ['60d21b4667d0d8992e610c85', '60d21b4967d0d8992e610c86'], example: ['60d21b4667d0d8992e610c85', '60d21b4967d0d8992e610c86'],
}) })
@IsOptional() @IsOptional()
@IsArray() @Transform(({ value }) => {
if (!Array.isArray(value)) {
return [value];
}
return value;
})
@IsUUID('4', { each: true }) @IsUUID('4', { each: true })
public spaces?: string[]; 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[];
} }

View File

@ -100,12 +100,15 @@ export class DeviceService {
async getAllDevices( async getAllDevices(
param: ProjectParam, param: ProjectParam,
{ deviceType, spaces }: GetDevicesFilterDto, { deviceType, spaces, communities }: GetDevicesFilterDto,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
try { try {
await this.validateProject(param.projectUuid); await this.validateProject(param.projectUuid);
if (deviceType === DeviceTypeEnum.DOOR_LOCK) { if (deviceType === DeviceTypeEnum.DOOR_LOCK) {
return await this.getDoorLockDevices(param.projectUuid, spaces); return await this.getDoorLockDevices(param.projectUuid, {
spaces,
communities,
});
} else if (!deviceType) { } else if (!deviceType) {
const devices = await this.deviceRepository.find({ const devices = await this.deviceRepository.find({
where: { where: {
@ -113,7 +116,13 @@ export class DeviceService {
spaceDevice: { spaceDevice: {
uuid: spaces && spaces.length ? In(spaces) : undefined, uuid: spaces && spaces.length ? In(spaces) : undefined,
spaceName: Not(ORPHAN_SPACE_NAME), spaceName: Not(ORPHAN_SPACE_NAME),
community: { project: { uuid: param.projectUuid } }, community: {
uuid:
communities && communities.length
? In(communities)
: undefined,
project: { uuid: param.projectUuid },
},
}, },
}, },
relations: [ relations: [
@ -1247,7 +1256,10 @@ export class DeviceService {
await this.deviceRepository.save(updatedDevices); 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); await this.validateProject(projectUuid);
const devices = await this.deviceRepository.find({ const devices = await this.deviceRepository.find({
@ -1259,6 +1271,8 @@ export class DeviceService {
spaceName: Not(ORPHAN_SPACE_NAME), spaceName: Not(ORPHAN_SPACE_NAME),
uuid: spaces && spaces.length ? In(spaces) : undefined, uuid: spaces && spaces.length ? In(spaces) : undefined,
community: { community: {
uuid:
communities && communities.length ? In(communities) : undefined,
project: { project: {
uuid: projectUuid, uuid: projectUuid,
}, },