diff --git a/src/device/dtos/add.device.dto.ts b/src/device/dtos/add.device.dto.ts index 564dc78..7a09b1c 100644 --- a/src/device/dtos/add.device.dto.ts +++ b/src/device/dtos/add.device.dto.ts @@ -18,6 +18,14 @@ export class AddDeviceDto { @IsNotEmpty() public spaceUuid: string; + @ApiProperty({ + description: 'tagUuid', + required: true, + }) + @IsString() + @IsNotEmpty() + public tagUuid: string; + @ApiProperty({ description: 'deviceName', required: true, diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index 793d854..a73664b 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -162,10 +162,24 @@ export class DeviceService { if (!device.productUuid) { throw new Error('Product UUID is missing for the device.'); } + const existingConflictingDevice = await this.deviceRepository.exists({ + where: { + spaceDevice: { uuid: addDeviceDto.spaceUuid }, + productDevice: { uuid: device.productUuid }, + tag: { uuid: addDeviceDto.tagUuid }, + }, + }); + if (existingConflictingDevice) { + throw new HttpException( + 'Device with the same product type and tag already exists in this space', + HttpStatus.BAD_REQUEST, + ); + } const deviceSaved = await this.deviceRepository.save({ deviceTuyaUuid: addDeviceDto.deviceTuyaUuid, productDevice: { uuid: device.productUuid }, spaceDevice: { uuid: addDeviceDto.spaceUuid }, + tag: { uuid: addDeviceDto.tagUuid }, name: addDeviceDto.deviceName, }); if (deviceSaved.uuid) {