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 { 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[];
}

View File

@ -100,12 +100,15 @@ export class DeviceService {
async getAllDevices(
param: ProjectParam,
{ deviceType, spaces }: GetDevicesFilterDto,
{ deviceType, spaces, communities }: GetDevicesFilterDto,
): Promise<BaseResponseDto> {
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,
},