copy unit

This commit is contained in:
marco committed 2026-10-09 16:50:07 +02:00
1 parent 90393f8e4f
commit bd850adcf5
7 files changed
+13837 -1151

No files matched your search

+12615 -13
View File
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+137 -100
View File
@@ -90,7 +90,7 @@
"$date": "2026-08-24T09:39:54.590Z"
},
"updatedAt": {
"$date": "2026-09-30T09:05:28.762Z"
"$date": "2026-10-09T10:03:46.616Z"
},
"__v": 0,
"prefix": "BA1",
@@ -123,8 +123,9 @@
"fu": false,
"rm": true,
"ms": true,
"ptc": false,
"ts": false
"ptc": true,
"ts": false,
"rmRtg2": true
},
"_id": {
"$oid": "6a9914eb9bce6777a1e11c1d"
@@ -143,7 +144,7 @@
"fu": false,
"rm": true,
"ms": true,
"ptc": false,
"ptc": true,
"ts": false
},
"_id": {
@@ -183,32 +184,6 @@
"$oid": "6a9919a1f7118560cd60c3cf"
}
},
{
"name": "Sl",
"prefix": "",
"postfix": "",
"index": 3,
"type": "Schieflaufwächter",
"properties": {
"warnung": false
},
"_id": {
"$oid": "6a9919a1f7118560cd60c400"
}
},
{
"name": "Sl",
"prefix": "",
"postfix": "",
"index": 4,
"type": "Schieflaufwächter",
"properties": {
"warnung": false
},
"_id": {
"$oid": "6a9919a2f7118560cd60c43a"
}
},
{
"name": "Rl",
"prefix": "",
@@ -223,16 +198,28 @@
}
},
{
"name": "Rl",
"prefix": "",
"postfix": "",
"index": 2,
"type": "Reißleine",
"name": "Dz",
"type": "Drehzahl",
"properties": {
"safty": true
"impuls": true,
"dauer": false
},
"_id": {
"$oid": "6a9919bef7118560cd60c4c9"
"$oid": "6ac899975d1f0632ad26a365"
}
},
{
"name": "Vorort",
"type": "Vorortbedienung",
"properties": {
"ss_anwahl": true,
"ta_ein": true,
"ta_ein_rtg2": false,
"ta_aus_no": false,
"ta_aus_nc": false
},
"_id": {
"$oid": "6ac8a3065d1f0632ad26acdb"
}
}
],
@@ -240,7 +227,7 @@
"$date": "2026-08-24T09:40:39.461Z"
},
"updatedAt": {
"$date": "2026-09-30T08:17:24.548Z"
"$date": "2026-10-09T08:19:18.135Z"
},
"__v": 0,
"anlageId": {
@@ -300,19 +287,6 @@
"$oid": "6a99238ff7118560cd60c639"
}
},
{
"name": "Sl",
"prefix": "",
"postfix": "",
"index": 2,
"type": "Schieflaufwächter",
"properties": {
"warnung": false
},
"_id": {
"$oid": "6a992392f7118560cd60c658"
}
},
{
"name": "Rl",
"prefix": "",
@@ -326,19 +300,6 @@
"$oid": "6a99239af7118560cd60c680"
}
},
{
"name": "Rl",
"prefix": "",
"postfix": "",
"index": 2,
"type": "Reißleine",
"properties": {
"safty": true
},
"_id": {
"$oid": "6a99239bf7118560cd60c6b1"
}
},
{
"name": "Dz",
"type": "Drehzahl",
@@ -369,13 +330,30 @@
"_id": {
"$oid": "6a9978c85a942d35b8ae7fc5"
}
},
{
"name": "Vorort",
"type": "Vorortbedienung",
"properties": {
"ss_anwahl": true,
"ta_ein": true,
"ta_ein_rtg2": true,
"ta_aus_no": false,
"ta_aus_nc": false
},
"_id": {
"$oid": "6ac8acb15d1f0632ad26b4ee"
},
"index": 0,
"postfix": "",
"prefix": ""
}
],
"createdAt": {
"$date": "2026-08-25T12:39:41.973Z"
},
"updatedAt": {
"$date": "2026-09-30T08:24:19.885Z"
"$date": "2026-10-09T09:00:04.023Z"
},
"__v": 0,
"bmk": "30",
@@ -408,7 +386,7 @@
"fu": false,
"rm": true,
"ms": true,
"ptc": false,
"ptc": true,
"ts": false
},
"_id": {
@@ -428,19 +406,6 @@
"$oid": "6a993d3940358b6c2375780a"
}
},
{
"name": "Rl",
"prefix": "",
"postfix": "",
"index": 2,
"type": "Reißleine",
"properties": {
"safty": true
},
"_id": {
"$oid": "6a993d3a40358b6c23757820"
}
},
{
"name": "Sl",
"prefix": "",
@@ -454,19 +419,6 @@
"$oid": "6a993d3c40358b6c2375783f"
}
},
{
"name": "Sl",
"prefix": "",
"postfix": "",
"index": 2,
"type": "Schieflaufwächter",
"properties": {
"warnung": false
},
"_id": {
"$oid": "6a993d3d40358b6c23757867"
}
},
{
"name": "Dz",
"type": "Drehzahl",
@@ -504,13 +456,27 @@
"_id": {
"$oid": "6a9a62285a942d35b8ae818d"
}
},
{
"name": "Vorort",
"type": "Vorortbedienung",
"properties": {
"ss_anwahl": true,
"ta_ein": true,
"ta_ein_rtg2": false,
"ta_aus_no": false,
"ta_aus_nc": false
},
"_id": {
"$oid": "6ac8ad565d1f0632ad26b767"
}
}
],
"createdAt": {
"$date": "2026-08-26T13:59:49.023Z"
},
"updatedAt": {
"$date": "2026-09-30T08:24:11.004Z"
"$date": "2026-10-09T09:04:29.927Z"
},
"__v": 0,
"anlageId": {
@@ -546,7 +512,7 @@
"fu": false,
"rm": true,
"ms": true,
"ptc": false,
"ptc": true,
"ts": false
},
"_id": {
@@ -566,7 +532,7 @@
"fu": false,
"rm": true,
"ms": true,
"ptc": false,
"ptc": true,
"ts": false
},
"_id": {
@@ -586,19 +552,64 @@
"fu": false,
"rm": true,
"ms": true,
"ptc": false,
"ptc": true,
"ts": false
},
"_id": {
"$oid": "6a9944e6a2f93050c6c9314b"
}
},
{
"name": "M",
"prefix": "",
"postfix": "",
"index": 4,
"type": "Antrieb",
"properties": {
"linkslauf": false,
"direkt": true,
"sternDreieck": false,
"fu": false,
"rm": true,
"rmRtg2": false,
"ms": true,
"ptc": true,
"ts": false
},
"_id": {
"$oid": "6ac89fa05d1f0632ad26aa8c"
}
},
{
"name": "Vorort",
"prefix": "",
"postfix": "",
"index": 0,
"type": "Vorortbedienung",
"properties": {
"ss_anwahl": true,
"ta_ein": true,
"ta_ein_rtg2": true,
"ta_aus_no": false,
"ta_aus_nc": false
},
"_id": {
"$oid": "6ac8aad25d1f0632ad26b1db"
}
},
{
"name": "Hs",
"type": "Reparaturschalter",
"_id": {
"$oid": "6ac8ab2a5d1f0632ad26b28b"
}
}
],
"createdAt": {
"$date": "2026-08-27T11:17:26.174Z"
},
"updatedAt": {
"$date": "2026-09-30T08:11:36.706Z"
"$date": "2026-10-09T08:52:00.489Z"
},
"__v": 0,
"anlageId": {
@@ -753,7 +764,7 @@
}
},
{
"name": "Box5",
"name": "Box5a",
"prefix": "Es",
"postfix": "Start",
"index": 0,
@@ -766,7 +777,7 @@
}
},
{
"name": "Box5",
"name": "Box5a",
"prefix": "Es",
"postfix": "Ende",
"index": 0,
@@ -777,13 +788,39 @@
"_id": {
"$oid": "6a9a68b75a942d35b8ae846f"
}
},
{
"name": "Box5b",
"type": "Endschalter",
"properties": {
"normallyClosed": false
},
"_id": {
"$oid": "6ac8a9d05d1f0632ad26b03f"
},
"index": 0,
"postfix": "Start",
"prefix": "Es"
},
{
"name": "Box5b",
"type": "Endschalter",
"properties": {
"normallyClosed": false
},
"_id": {
"$oid": "6ac8a9d15d1f0632ad26b090"
},
"index": 0,
"postfix": "Ende",
"prefix": "Es"
}
],
"createdAt": {
"$date": "2026-09-02T13:12:44.982Z"
},
"updatedAt": {
"$date": "2026-09-30T08:19:17.797Z"
"$date": "2026-10-09T08:49:39.356Z"
},
"__v": 0,
"postfix": ""
+8
View File
@@ -39,6 +39,14 @@ export class UnitCreateDto {
components?: ComponentResponseDto[];
}
export class UnitCopyDto {
name!: string;
groupId?: string | null;
prefix?: string;
bmk?: string;
postfix?: string;
}
export class UnitUpdateBaseDto {
name?: string;
anlageId?: string;
+6 -1
View File
@@ -1,6 +1,6 @@
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
import { UnitsService } from './units.service';
import { UnitCreateDto, UnitUpdateBaseDto } from 'src/_dto/anlagen/units';
import { UnitCopyDto, UnitCreateDto, UnitUpdateBaseDto } from 'src/_dto/anlagen/units';
@@ -35,6 +35,11 @@ export class UnitsController {
return this.service.update(id, dto);
}
@Post(':id/copy')
copy(@Param('id') id: string, @Body() dto: UnitCopyDto) {
return this.service.copy(id, dto);
}
@Delete(':id')
remove(@Param('id') id: string) {
+3 -1
View File
@@ -4,9 +4,11 @@ import { UnitsController } from './units.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { Unit, UnitSchema } from 'src/_schemas/anlagen/units';
import { AnlageModule } from 'src/rk-anlagen/anlage/anlage.module';
import { Signal, SignalSchema } from 'src/_schemas/anlagen/singnals';
import { Group, GroupSchema } from 'src/_schemas/anlagen/groups';
@Module({
imports: [MongooseModule.forFeature([{ name: Unit.name, schema: UnitSchema }], "anlagen"), AnlageModule],
imports: [MongooseModule.forFeature([{ name: Unit.name, schema: UnitSchema }, { name: Signal.name, schema: SignalSchema }, { name: Group.name, schema: GroupSchema }], "anlagen"), AnlageModule],
providers: [UnitsService],
controllers: [UnitsController]
+71 -5
View File
@@ -1,7 +1,9 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { BadRequestException, Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import { UnitCreateDto, UnitResponseDto, UnitUpdateBaseDto } from 'src/_dto/anlagen/units';
import { UnitCopyDto, UnitCreateDto, UnitResponseDto, UnitUpdateBaseDto } from 'src/_dto/anlagen/units';
import { Signal } from 'src/_schemas/anlagen/singnals';
import { Group } from 'src/_schemas/anlagen/groups';
import { Component } from 'src/_schemas/anlagen/component';
import { Unit, UnitDocument } from 'src/_schemas/anlagen/units';
import { ComponentCreateDto, ComponentResponseDto, ComponentTypes, ComponentUpdateDto, } from 'src/_dto/anlagen/components';
@@ -12,8 +14,64 @@ import { AnlageService } from 'src/rk-anlagen/anlage/anlage.service';
@Injectable()
export class UnitsService {
constructor(@InjectModel(Unit.name, "anlagen") private model: Model<Unit>,
@InjectModel(Signal.name, "anlagen") private signalModel: Model<Signal>,
@InjectModel(Group.name, "anlagen") private groupModel: Model<Group>,
private readonly anlageService: AnlageService) { }
async copy(id: string, dto: UnitCopyDto): Promise<UnitResponseDto> {
if (!Types.ObjectId.isValid(id)) throw new BadRequestException('Ungültige Unit-ID');
if (!dto || typeof dto.name !== 'string' || !dto.name.trim()) throw new BadRequestException('Name ist erforderlich');
for (const key of ['prefix', 'bmk', 'postfix'] as const) {
if (dto[key] !== undefined && typeof dto[key] !== 'string') throw new BadRequestException(`${key} muss Text sein`);
}
const source = await this.model.findById(id).exec();
if (!source) throw new NotFoundException('Unit nicht gefunden');
const anlage = await this.anlageService.getSelected();
if (source.anlageId.toString() !== anlage.id) throw new BadRequestException('Unit gehört nicht zur ausgewählten Anlage');
const groupId = dto.groupId === undefined ? source.groupId?.toString() : dto.groupId;
if (groupId) {
if (typeof groupId !== 'string' || !Types.ObjectId.isValid(groupId)) throw new BadRequestException('Ungültige Gruppen-ID');
const group = await this.groupModel.findById(groupId).exec();
if (!group || group.anlageId.toString() !== anlage.id) throw new BadRequestException('Gruppe gehört nicht zur ausgewählten Anlage');
} else if (groupId !== undefined && groupId !== null && groupId !== '') {
throw new BadRequestException('Ungültige Gruppen-ID');
}
const componentIds = new Map(source.components.map((component) => [component._id!.toString(), new Types.ObjectId()]));
const sourceSignals = await this.signalModel.find({
anlageId: source.anlageId,
componentId: { $in: source.components.map((component) => component._id!) },
}).exec();
const components = source.components.map((component) => ({
_id: componentIds.get(component._id!.toString()), name: component.name, type: component.type,
prefix: component.prefix, postfix: component.postfix, index: component.index, properties: component.properties,
}));
const signals = sourceSignals.map((signal) => new this.signalModel({
_id: new Types.ObjectId(), anlageId: source.anlageId, componentId: componentIds.get(signal.componentId!.toString()),
name: signal.name, type: signal.type, description: signal.description, ioId: null,
}));
const copy = new this.model({
_id: new Types.ObjectId(), anlageId: source.anlageId, type: source.type, info: source.info,
name: dto.name.trim(), groupId: groupId ? new Types.ObjectId(groupId) : undefined,
prefix: dto.prefix ?? source.prefix, bmk: dto.bmk ?? source.bmk, postfix: dto.postfix ?? source.postfix, components,
});
await copy.validate();
for (const signal of signals) await signal.validate();
try {
await copy.save();
if (signals.length) await this.signalModel.insertMany(signals, { ordered: true });
return this.toDto(copy);
} catch (error) {
// Auch bei teilweise erfolgreichem insertMany nur die neu erzeugten IDs entfernen.
try {
await this.signalModel.deleteMany({ _id: { $in: signals.map((signal) => signal._id) } }).exec();
await this.model.deleteOne({ _id: copy._id }).exec();
} catch {
throw new InternalServerErrorException(`Kopieren fehlgeschlagen; Bereinigung der Kopie ${copy._id} nicht vollständig. Bitte prüfen.`);
}
throw error;
}
}
async findAllExp(): Promise<any[]> {
let currentAnlage = await this.anlageService.getSelected();
let items = await this.model.aggregate<any>(Unit_query_get(currentAnlage.id));
@@ -75,9 +133,17 @@ export class UnitsService {
//--- Remove
async remove(id: string) {
const removedItem = await this.model.findByIdAndDelete(id, { returnDocument: 'after', runValidators: true });
if (!removedItem) throw new NotFoundException(`Group ${id} nicht gefunden`);
if (!Types.ObjectId.isValid(id)) throw new BadRequestException('Ungültige Unit-ID');
const unit = await this.model.findById(id).exec();
if (!unit) throw new NotFoundException(`Unit ${id} nicht gefunden`);
const anlage = await this.anlageService.getSelected();
if (unit.anlageId.toString() !== anlage.id) throw new BadRequestException('Unit gehört nicht zur ausgewählten Anlage');
// ObjectIds verwenden, da componentId im vorhandenen Schema als Mixed behandelt wird.
await this.signalModel.deleteMany({
anlageId: unit.anlageId, componentId: { $in: unit.components.map((component) => component._id!) },
}).exec();
const removedItem = await this.model.findByIdAndDelete(unit._id).exec();
if (!removedItem) throw new NotFoundException(`Unit ${id} nicht gefunden`);
return this.toDto(removedItem);
}