mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
feat: update device and space services to use productType instead of deviceType and add query support for device retrieval by product type
This commit is contained in:
@ -58,7 +58,7 @@ export class GetDevicesBySpaceOrCommunityDto {
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public deviceType: string;
|
||||
public productType: string;
|
||||
@IsUUID('4', { message: 'Invalid space UUID format' })
|
||||
@IsOptional()
|
||||
spaceUuid?: string;
|
||||
|
@ -1729,7 +1729,7 @@ export class DeviceService {
|
||||
query: GetDevicesBySpaceOrCommunityDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
try {
|
||||
const { spaceUuid, communityUuid, deviceType } = query;
|
||||
const { spaceUuid, communityUuid, productType } = query;
|
||||
if (!spaceUuid && !communityUuid) {
|
||||
throw new BadRequestException(
|
||||
'Either spaceUuid or communityUuid must be provided',
|
||||
@ -1750,12 +1750,12 @@ export class DeviceService {
|
||||
}
|
||||
|
||||
const devicesFilterd = devices.filter(
|
||||
(device) => device.productDevice?.prodType === deviceType,
|
||||
(device) => device.productDevice?.prodType === productType,
|
||||
);
|
||||
|
||||
if (devicesFilterd.length === 0) {
|
||||
return new SuccessResponseDto({
|
||||
message: `No ${deviceType} devices found for ${spaceUuid ? 'space' : 'community'}`,
|
||||
message: `No ${productType} devices found for ${spaceUuid ? 'space' : 'community'}`,
|
||||
data: [],
|
||||
statusCode: HttpStatus.CREATED,
|
||||
});
|
||||
|
@ -1,11 +1,12 @@
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import { Controller, Get, Param, UseGuards } from '@nestjs/common';
|
||||
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { GetSpaceParam } from '../dtos';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SpaceDeviceService } from '../services';
|
||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||
import { GetDevicesBySpaceDto } from '../dtos/device.space.dto';
|
||||
|
||||
@ApiTags('Space Module')
|
||||
@Controller({
|
||||
@ -26,7 +27,8 @@ export class SpaceDeviceController {
|
||||
@Get()
|
||||
async listDevicesInSpace(
|
||||
@Param() params: GetSpaceParam,
|
||||
@Query() query: GetDevicesBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.spaceDeviceService.listDevicesInSpace(params);
|
||||
return await this.spaceDeviceService.listDevicesInSpace(params, query);
|
||||
}
|
||||
}
|
||||
|
13
src/space/dtos/device.space.dto.ts
Normal file
13
src/space/dtos/device.space.dto.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsOptional, IsString } from 'class-validator';
|
||||
|
||||
export class GetDevicesBySpaceDto {
|
||||
@ApiProperty({
|
||||
description: 'Device Product Type',
|
||||
example: 'PC',
|
||||
required: false,
|
||||
})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
public productType?: string;
|
||||
}
|
@ -17,6 +17,7 @@ import { DeviceService } from 'src/device/services';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { DeviceEntity } from '@app/common/modules/device/entities';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { GetDevicesBySpaceDto } from '../dtos/device.space.dto';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceDeviceService {
|
||||
@ -27,9 +28,12 @@ export class SpaceDeviceService {
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
) {}
|
||||
|
||||
async listDevicesInSpace(params: GetSpaceParam): Promise<BaseResponseDto> {
|
||||
async listDevicesInSpace(
|
||||
params: GetSpaceParam,
|
||||
query: GetDevicesBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { spaceUuid, communityUuid, projectUuid } = params;
|
||||
|
||||
const { productType } = query;
|
||||
try {
|
||||
// Validate community, project, and fetch space including devices in a single query
|
||||
const space = await this.validationService.fetchSpaceDevices(spaceUuid);
|
||||
@ -51,7 +55,23 @@ export class SpaceDeviceService {
|
||||
const detailedDevices = (await Promise.allSettled(deviceDetailsPromises))
|
||||
.filter((result) => result.status === 'fulfilled' && result.value)
|
||||
.map((result) => (result as PromiseFulfilledResult<any>).value);
|
||||
|
||||
console.log('detailedDevices', detailedDevices);
|
||||
if (productType) {
|
||||
const devicesFilterd = detailedDevices.filter(
|
||||
(device) => device.productType === productType,
|
||||
);
|
||||
if (devicesFilterd.length === 0) {
|
||||
return new SuccessResponseDto({
|
||||
message: `No ${productType} devices found for ${spaceUuid ? 'space' : 'community'}`,
|
||||
data: [],
|
||||
statusCode: HttpStatus.CREATED,
|
||||
});
|
||||
}
|
||||
return new SuccessResponseDto({
|
||||
data: devicesFilterd,
|
||||
message: 'Successfully retrieved list of devices.',
|
||||
});
|
||||
}
|
||||
return new SuccessResponseDto({
|
||||
data: detailedDevices,
|
||||
message: 'Successfully retrieved list of devices.',
|
||||
|
Reference in New Issue
Block a user