Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { IoTDeviceService } from "@services/device-management/iot-device.service
import { AuditLog } from "@services/audit-log.service";
import { ActionType } from "@entities/audit-log-entry";
import { ApiAuth } from "@auth/swagger-auth-decorator";
import { AppendCopiedDeviceDto } from "@dto/append-copied-device.dto";

@ApiTags("IoT-Device, PayloadDecoder and DataTarget Connection")
@Controller("iot-device-payload-decoder-data-target-connection")
Expand Down Expand Up @@ -205,6 +206,33 @@ export class IoTDevicePayloadDecoderDataTargetConnectionController {
}
}

@Put("appendCopiedDevice/:id")
@ApplicationAdmin()
@ApiNotFoundResponse({
description: "If the id of the entity doesn't exist",
})
@ApiBadRequestResponse({
description: "If one or more of the id's are invalid references.",
})
async appendCopiedDevice(
@Req() req: AuthenticatedRequest,
@Param("id", new ParseIntPipe()) id: number,
@Body() dto: AppendCopiedDeviceDto
): Promise<IoTDevicePayloadDecoderDataTargetConnection> {
try {
const newIotDevice = await this.iotDeviceService.findOne(dto.deviceId);
checkIfUserHasAccessToApplication(req, newIotDevice.application.id, ApplicationAccessScope.Write);

const result = await this.service.appendCopiedDevice(id, newIotDevice, req.user.userId);

AuditLog.success(ActionType.UPDATE, IoTDevicePayloadDecoderDataTargetConnection.name, req.user.userId, result.id);
return result;
} catch (err) {
AuditLog.fail(ActionType.UPDATE, IoTDevicePayloadDecoderDataTargetConnection.name, req.user.userId, id);
throw err;
}
}

@Delete(":id")
@ApplicationAdmin()
@ApiNotFoundResponse({
Expand Down
8 changes: 8 additions & 0 deletions src/entities/dto/append-copied-device.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsNumber } from "class-validator";

export class AppendCopiedDeviceDto {
@ApiProperty({ required: true })
@IsNumber()
deviceId: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { PayloadDecoderService } from "@services/data-management/payload-decoder
import { DataTargetService } from "@services/data-targets/data-target.service";

import { IoTDeviceService } from "./iot-device.service";
import { IoTDevice } from "@entities/iot-device.entity";

@Injectable()
export class IoTDevicePayloadDecoderDataTargetConnectionService {
Expand Down Expand Up @@ -173,6 +174,27 @@ export class IoTDevicePayloadDecoderDataTargetConnectionService {
return await this.repository.save(mapped);
}

async appendCopiedDevice(
id: number,
iotDevice: IoTDevice,
userId: number
): Promise<IoTDevicePayloadDecoderDataTargetConnection> {
let connection;
try {
connection = await this.repository.findOneOrFail({
where: { id },
relations: ["iotDevices"],
});
} catch (err) {
throw new NotFoundException(`Could not find IoTDevicePayloadDecoderDataTargetConnection by id: ${id}`);
}

connection.iotDevices.push(iotDevice);
connection.updatedBy = userId;

return await this.repository.save(connection);
}

async delete(id: number): Promise<DeleteResult> {
return await this.repository.delete(id);
}
Expand Down