mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 07:07:21 +00:00
add communities filter to devices by project API (#455)
This commit is contained in:
@ -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[];
|
||||
}
|
||||
|
@ -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,
|
||||
},
|
||||
|
Reference in New Issue
Block a user