mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
task: add spaces filter to get devices by project (#422)
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
@ -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[];
|
||||
}
|
||||
|
@ -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<BaseResponseDto> {
|
||||
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,
|
||||
|
Reference in New Issue
Block a user