stand2026_10_06

This commit is contained in:
marco committed 2026-10-06 13:46:04 +02:00
1 parent 73015386e9
commit 30292794ae
25 files changed
+664 -164

No files matched your search

+7 -1
View File
@@ -7,7 +7,13 @@ import { VisualizationsModule } from './visualizations/visualizations.module';
import { ComponentsModule } from './components/components.module';
@Module({
imports: [GroupsModule, UnitsModule, SignalsModule, IosModule, VisualizationsModule, ComponentsModule],
imports: [
GroupsModule,
UnitsModule,
SignalsModule,
IosModule,
VisualizationsModule,
ComponentsModule],
})
export class AnlagenModule {}
@@ -1,5 +1,6 @@
import { Controller, Get, Param } from '@nestjs/common';
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
import { ComponentsService } from './components.service';
import { ComponentCreateDto, ComponentUpdateDto } from 'src/_dto/anlagen/components';
@Controller('rk2/components')
export class ComponentsController {
@@ -10,4 +11,25 @@ export class ComponentsController {
return this.service.findOne(id);
}
@Post(':compId')
create(@Param('compId') compId: string, @Body() dto: ComponentCreateDto) {
console.log(dto)
return this.service.create(compId, dto);
}
@Patch(':compId')
update(@Param('compId') compId: string, @Body() dto: ComponentUpdateDto) {
console.log(dto)
return this.service.update(compId, dto);
}
@Delete(':compId')
remove(@Param('compId') compId: string) {
return this.service.delete(compId);
}
@Delete(':compId/deleteProp/:propName')
deleteProp(@Param('compId') compId: string,@Param('propName') propName: string) {
return this.service.deletePropertie(compId,propName);
}
}
+8 -3
View File
@@ -1,13 +1,18 @@
import { Module } from '@nestjs/common';
import { forwardRef, Module } from '@nestjs/common';
import { ComponentsService } from './components.service';
import { ComponentsController } from './components.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { Unit, UnitSchema } from 'src/_schemas/anlagen/units';
import { SignalsModule } from '../signals/signals.module';
@Module({
controllers: [ComponentsController],
providers: [ComponentsService],
imports: [MongooseModule.forFeature([{ name: Unit.name, schema: UnitSchema }], "anlagen")],
imports: [
MongooseModule.forFeature([{ name: Unit.name, schema: UnitSchema }], "anlagen"),
forwardRef(() => SignalsModule)
],
exports: [ComponentsService]
})
export class ComponentsModule { }
+127 -10
View File
@@ -1,23 +1,28 @@
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { forwardRef, Inject, Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import { ComponentOfUnitDto, ComponentResponseDto, ComponentTypes } from 'src/_dto/anlagen/components';
import { UnitResponseDto } from 'src/_dto/anlagen/units';
import { ComponentCreateDto, ComponentOfUnitDto, ComponentResponseDto, ComponentTypes, ComponentUpdateDto } from 'src/_dto/anlagen/components';
import { Component } from 'src/_schemas/anlagen/component';
import { Unit } from 'src/_schemas/anlagen/units';
import { SignalsService } from '../signals/signals.service';
@Injectable()
export class ComponentsService {
constructor(@InjectModel(Unit.name, "anlagen") private model: Model<Unit>) { }
constructor(
@InjectModel(Unit.name, "anlagen") private model: Model<Unit>,
@Inject(forwardRef(() => SignalsService))
private readonly signalsService: SignalsService,
) { }
//-------------------------------------------------------------------------
//--- Find BY component id
//-------------------------------------------------------------------------
async findOne(id: string): Promise<ComponentOfUnitDto> {
if(!Types.ObjectId.isValid(id)) throw new NotFoundException('id ist kein ObjectId')
if (!Types.ObjectId.isValid(id)) throw new NotFoundException('id ist kein ObjectId')
const item = await this.model.findOne({ 'components._id': id }).exec();
if (!item) throw new NotFoundException(`Keine Unit mit der Componenten Id: ${id} gefunden`)
let comp = item.components.find((c) => c._id?.toString() === id);
@@ -28,6 +33,119 @@ export class ComponentsService {
};
}
//-------------------------------------------------------------------------
// Create Component
//-------------------------------------------------------------------------
async create(unitId: string, dto: ComponentCreateDto): Promise<ComponentResponseDto> {
if (!Types.ObjectId.isValid(unitId)) throw new NotFoundException('id ist kein ObjectId')
const componentId = new Types.ObjectId();
const create = {
_id: componentId,
...dto,
name: dto.name ? dto.name : "unbenannt",
type: dto.type ? dto.type : ComponentTypes.UNKNOWN
}
const item = await this.model.findByIdAndUpdate(unitId,
{ $push: { components: create } },
{ returnDocument: 'after' }).exec();
if (!item) throw new NotFoundException(`After Push component: Keine Unit mit der Componenten Id: ${unitId} gefunden`)
const newComp = await this.findOne(componentId.toString())
if (!newComp || !newComp.component) throw new NotFoundException(`Componente wurde nicht erzeugt UnitId: ${unitId}`)
return newComp.component
}
async delete(compId: string): Promise<any> {
if (!Types.ObjectId.isValid(compId)) throw new NotFoundException('id ist kein ObjectId')
const item = await this.model.findOneAndUpdate({ 'components._id': compId },
{
$pull: {
components: { _id: compId }
}
},
{ returnDocument: 'after' }).exec();
if (!item) throw new NotFoundException(`Keine Unit mit der Componenten Id: ${compId} gefunden`)
let delSigResult = await this.signalsService.removeSignalsOfComponent(compId);
console.log(item._id,delSigResult)
return item
}
async update(compId: string, dto: ComponentUpdateDto): Promise<any> {
if (!Types.ObjectId.isValid(compId)) throw new NotFoundException('id ist kein ObjectId')
const update: Record<string, any> = {};
if (dto.name !== undefined) { update['components.$.name'] = dto.name };
if (dto.postfix !== undefined) { update['components.$.postfix'] = dto.postfix };
if (dto.prefix !== undefined) { update['components.$.prefix'] = dto.prefix };
if (dto.index !== undefined) { update['components.$.index'] = dto.index };
if (dto.properties) {
for (const [key, value] of Object.entries(dto.properties)) {
update[`components.$.properties.${key}`] = value;
}
}
console.log(update);
const item = await this.model.findOneAndUpdate({ 'components._id': compId },
{ $set: update },
{ returnDocument: 'after' }).exec();
return item
}
async deletePropertie(compId: string, propertieName: string): Promise<any> {
if (!Types.ObjectId.isValid(compId)) throw new NotFoundException('id ist kein ObjectId')
const deleteProp: Record<string, any> = {};
deleteProp[`components.$.properties.${propertieName}`] = '';
const item = await this.model.findOneAndUpdate(
{ 'components._id': compId },
{
$unset: deleteProp
},
{ returnDocument: 'after' }
).exec();;
return item
}
async createSignalRef(compId: string, signalId: string): Promise<ComponentOfUnitDto> {
if (!Types.ObjectId.isValid(compId)) throw new NotFoundException('Create Component id ist kein ObjectId')
const item = await this.model.findOneAndUpdate({ 'components._id': compId },
{ $addToSet: { 'components.$.signalIds': new Types.ObjectId(signalId) } },
{ returnDocument: 'after' }).exec();
if (!item) throw new NotFoundException(`Keine Unit mit der Componenten Id: ${compId} gefunden`)
let retValue = this.findOne(compId)
return retValue;
}
toDto(component: Component): ComponentResponseDto {
@@ -38,7 +156,6 @@ export class ComponentsService {
id: component._id!.toString(),
name: component.name,
type: this.getComponentType(component.type.toString()),
signalIds: component.signalIds.map((signal) => signal.toString()),
properties: component.properties
}
+1
View File
@@ -35,6 +35,7 @@ export class GroupsController {
@Delete(':id')
remove(@Param('id') id: string) {
console.log('Delete Group')
return this.service.remove(id);
}
}
+5 -1
View File
@@ -3,9 +3,13 @@ import { GroupsController } from './groups.controller';
import { GroupsService } from './groups.service';
import { MongooseModule } from '@nestjs/mongoose';
import { Group, GroupSchema } from 'src/_schemas/anlagen/groups';
import { AnlageModule } from 'src/rk-anlagen/anlage/anlage.module';
@Module({
imports: [MongooseModule.forFeature([{ name: Group.name, schema: GroupSchema }],"anlagen")],
imports: [
MongooseModule.forFeature([{ name: Group.name, schema: GroupSchema }],"anlagen"),
AnlageModule
],
controllers: [GroupsController],
providers: [GroupsService]
})
+12 -5
View File
@@ -3,14 +3,18 @@ import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import { GroupCreateDto, GroupResponseDto, GroupUpdateDto } from 'src/_dto/anlagen/groups';
import { Group, GroupDocument } from 'src/_schemas/anlagen/groups';
import { AnlageService } from 'src/rk-anlagen/anlage/anlage.service';
@Injectable()
export class GroupsService {
constructor(@InjectModel(Group.name,"anlagen") private model: Model<Group>) { }
constructor(
@InjectModel(Group.name,"anlagen") private model: Model<Group>,
private readonly anlageService: AnlageService) { }
async findAll(): Promise<GroupResponseDto[]> {
const responsData = await this.model.find().exec();
let anlageId = (await this.anlageService.getSelected()).id;
const responsData = await this.model.find({anlageId:new Types.ObjectId(anlageId)}).exec();
return responsData.map((item) => this.toDto(item));
}
@@ -28,14 +32,16 @@ export class GroupsService {
}
async create(dto: GroupCreateDto):Promise<GroupResponseDto> {
const newItem = new this.model(this.toDocument(dto));
let anlageId = (await this.anlageService.getSelected()).id;
const newItem = new this.model(this.toDocument(anlageId,dto));
const result = await newItem.save();
return this.toDto(result);
}
async update(id: string, dto: GroupUpdateDto) {
let update = this.toDocument(dto);
let anlageId = (await this.anlageService.getSelected()).id;
let update = this.toDocument(anlageId,dto);
console.log(update);
const changedItem = await this.model.findByIdAndUpdate(id, {
$set: {
@@ -68,9 +74,10 @@ export class GroupsService {
}
}
toDocument(dto: GroupCreateDto|GroupUpdateDto) {
toDocument(anlageid:string,dto: GroupCreateDto|GroupUpdateDto) {
return {
name: dto.name,
anlageId: new Types.ObjectId(anlageid),
container: dto.container,
nr: dto.nr,
parentId: dto.parentId ? new Types.ObjectId(dto.parentId) : null
+6 -3
View File
@@ -1,16 +1,19 @@
import { Module } from '@nestjs/common';
import { forwardRef, Module } from '@nestjs/common';
import { SignalsService } from './signals.service';
import { SignalsController } from './signals.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { Signal, SignalSchema } from 'src/_schemas/anlagen/singnals';
import { AnlageModule } from 'src/rk-anlagen/anlage/anlage.module';
import { ComponentsModule } from '../components/components.module';
@Module({
imports: [
MongooseModule.forFeature([{ name: Signal.name, schema: SignalSchema }], "anlagen"),
AnlageModule
AnlageModule,
forwardRef(() =>ComponentsModule)
],
providers: [SignalsService],
controllers: [SignalsController]
controllers: [SignalsController],
exports:[SignalsService]
})
export class SignalsModule { }
+25 -12
View File
@@ -1,15 +1,18 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { forwardRef, Inject, Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import { SignalCreateDto, SignalResponseDto, SignalUpdateDto } from 'src/_dto/anlagen/signals';
import { Signal, SignalDocument } from 'src/_schemas/anlagen/singnals';
import { AnlageService } from 'src/rk-anlagen/anlage/anlage.service';
import { ComponentsService } from '../components/components.service';
@Injectable()
export class SignalsService {
constructor(
@InjectModel(Signal.name, "anlagen") private model: Model<Signal>,
private readonly anlageService: AnlageService
private readonly anlageService: AnlageService,
@Inject(forwardRef(() => ComponentsService))
private readonly componentService: ComponentsService,
) { }
async findAll(): Promise<SignalResponseDto[]> {
@@ -24,8 +27,8 @@ export class SignalsService {
return this.toDto(item);
}
async findByComponent(componentId:string): Promise<SignalResponseDto[]> {
const responsData = await this.model.find({componentId:new Types.ObjectId(componentId)}).exec();
async findByComponent(componentId: string): Promise<SignalResponseDto[]> {
const responsData = await this.model.find({ componentId: new Types.ObjectId(componentId) }).exec();
return responsData.map((item) => this.toDto(item));
}
@@ -33,13 +36,16 @@ export class SignalsService {
async create(dto: SignalCreateDto): Promise<SignalResponseDto> {
let currentAnlage = await this.anlageService.getSelected();
if(!currentAnlage) throw new NotFoundException('Es ist keine Anlage ausgewählt');
if (!currentAnlage) throw new NotFoundException('Es ist keine Anlage ausgewählt');
const newItem = new this.model(this.toDocument(dto,currentAnlage.id));
const newItem = new this.model(this.toDocument(dto, currentAnlage.id));
const result = await newItem.save();
// Id in Componente anlagen
if (dto.componentId)
this.componentService.createSignalRef(dto.componentId?.toString(), newItem._id.toString())
return this.toDto(result);
}
@@ -47,9 +53,9 @@ export class SignalsService {
async update(id: string, dto: SignalUpdateDto) {
let currentAnlage = await this.anlageService.getSelected();
let anlage = dto.anlageId ? dto.anlageId : currentAnlage.id;
if(!anlage) throw new NotFoundException('Anlage Id muss gesetzt sein')
let update = this.toDocument(dto,anlage);
if (!anlage) throw new NotFoundException('Anlage Id muss gesetzt sein')
let update = this.toDocument(dto, anlage);
console.log(update);
const changedItem = await this.model.findByIdAndUpdate(id, {
$set: {
@@ -70,6 +76,13 @@ export class SignalsService {
return this.toDto(removedItem);
}
async removeSignalsOfComponent(compId: string) {
const removedItem = await this.model.deleteMany({ componentId: new Types.ObjectId(compId) });
return removedItem;
}
toDto(dbData: SignalDocument): SignalResponseDto {
@@ -84,7 +97,7 @@ export class SignalsService {
}
}
toDocument(dto: SignalCreateDto | SignalUpdateDto,anlage:string) {
toDocument(dto: SignalCreateDto | SignalUpdateDto, anlage: string) {
return {
anlageId: new Types.ObjectId(anlage),
name: dto.name,
+2 -10
View File
@@ -1,7 +1,7 @@
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 { ComponentCreateDto, ComponentUpdateDto } from 'src/_dto/anlagen/components';
@Controller('rk2/units')
@@ -31,18 +31,10 @@ export class UnitsController {
@Patch(':id')
update(@Param('id') id: string, @Body() dto: UnitUpdateBaseDto) {
return this.service.update(id, dto);
}
@Patch(':id/component/:compId')
updateComp(@Param('id') id: string,@Param('compId') compId: string, @Body() dto: ComponentUpdateDto) {
return this.service.updateComponent(id,compId, dto);
}
@Patch(':id/addComponent')
updateUnitAddComp(@Param('id') id: string, @Body() dto: ComponentCreateDto) {
return this.service.addComponent(id,dto);
}
@Delete(':id')
remove(@Param('id') id: string) {
+31 -92
View File
@@ -16,17 +16,13 @@ export class UnitsService {
async findAllExp(): Promise<any[]> {
let currentAnlage = await this.anlageService.getSelected();
let items = await this.model.aggregate<any>(Unit_query_get(currentAnlage.id));
return items;
}
//--- Find ALL
async findAll(): Promise<UnitResponseDto[]> {
const responsData = await this.model.find().exec();
return responsData.map((item) => this.toDto(item));
@@ -42,39 +38,39 @@ export class UnitsService {
//--- Create new Unit
async create(dto: UnitCreateDto): Promise<UnitResponseDto> {
let { components, ...base } = dto;
components = components.map((c) => ({ name: c.name, signalIds: c.signalIds, type: c.type, properties: c.properties, id: c.id == "" ? undefined : c.id }))
let cComponents = components.map((c) => this.handleComponents(c));
console.log(base);
const newItem = new this.model({ ...base, components: cComponents });
let anlageId = dto.anlageId ? new Types.ObjectId(dto.anlageId) : new Types.ObjectId((await this.anlageService.getSelected()).id);
let { components,groupId, ...base } = dto;
let newGroupId = groupId != undefined && Types.ObjectId.isValid(groupId) ? new Types.ObjectId(groupId) : undefined
// components = components ? components.map((c) => ({ name: c.name, type: c.type, properties: c.properties, id: c.id == "" ? undefined : c.id })):undefined
// let cComponents = components.map((c) => this.handleComponents(c));
const newItem = new this.model({ ...base, groupId:newGroupId, anlageId:anlageId });
const result = await newItem.save();
return this.toDto(result);
}
//--- Update a Unit
async update(id: string, dto: UnitUpdateBaseDto) {
let anlageId = dto.anlageId && Types.ObjectId.isValid(dto.anlageId) ? new Types.ObjectId(dto.anlageId) : new Types.ObjectId((await this.anlageService.getSelected()).id);
let update = {
...( { anlageId: anlageId }),
...(dto.name !== undefined && { name: dto.name }),
...(dto.groupId !== undefined && { groupId: dto.groupId }),
...(dto.groupId !== undefined && Types.ObjectId.isValid(dto.groupId) && { groupId: new Types.ObjectId(dto.groupId) }),
...(dto.prefix !== undefined && { prefix: dto.prefix }),
...(dto.postfix !== undefined && { postfix: dto.postfix }),
...(dto.bmk !== undefined && { bmk: dto.bmk }),
...(dto.type !== undefined && { type: dto.type.toString() }),
...(dto.components !== undefined && { components: dto.components.map((c) => this.handleComponents(c)) }),
...(dto.components !== undefined ? { components: dto.components.map((c) => this.handleComponents(c)) } : { components: [] }),
}
console.log(update.components);
const changedItem = await this.model.findByIdAndUpdate({ _id: id }, {
$set: {
...update,
const item = await this.model.findByIdAndUpdate({ _id: id }, { $set: { ...update } }, { returnDocument: 'after', runValidators: true });
if (!item) throw new NotFoundException('The updated Unit are not found');
}
}
, { returnDocument: 'after', runValidators: true });
const newUnit = await this.findOne(id);
if (!newUnit) throw new NotFoundException('The updated Unit are not found');
return newUnit;
return this.toDto(item);
}
//--- Remove
@@ -86,63 +82,8 @@ export class UnitsService {
}
//--- Update Components of a Unit
async updateComponent(unitId: string, componentId: string, dto: ComponentUpdateDto) {
const unit = await this.model
.findOneAndUpdate(
{
_id: unitId,
'components._id': componentId,
},
{
$set: {
'components.$.name': dto.name,
'components.$.type': dto.type,
'components.$.properties': dto.properties,
'components.$.signalIds': dto.signalIds,
},
},
{
new: true,
after: true,
},
)
.exec();
if (!unit) {
throw new NotFoundException(
`Unit oder Component nicht gefunden`,
);
}
return unit;
}
//--- Update Components of a Unit
async addComponent(unitId: string, dto: ComponentCreateDto) {
let currentUnit = await this.model.findById(unitId).exec();
if (!currentUnit) throw new NotFoundException('Unit id ncht gefunden');
let signale = dto.signalIds ? dto.signalIds.map((s) => new Types.ObjectId(s)) : [];
let pros = { ...dto.properties };
currentUnit.components = [...currentUnit.components, {
name: dto.name,
signalIds: [...signale], //dto.signalIds ? dto.signalIds.map((s)=>new ObjectId(s)): [],
type: dto.type,
properties: { ...pros }
}]
const unit = await this.model.findOneAndUpdate({ _id: unitId }, currentUnit, { new: true, runValidators: true, }).exec();
if (!unit) {
throw new NotFoundException(
`Unit oder Component nicht gefunden`,
);
}
return unit;
}
toDto(dbData: Unit): UnitResponseDto {
@@ -151,8 +92,10 @@ export class UnitsService {
{
id: c._id!.toString(),
name: c.name,
index: c.index,
prefix: c.prefix,
postfix: c.postfix,
type: this.getComponentType(c.type.toString()),
signalIds: c.signalIds.map((signal) => signal.toString()),
properties: c.properties
}))
@@ -161,17 +104,15 @@ export class UnitsService {
name: dbData.name,
bmk: dbData.bmk,
prefix: dbData.prefix,
postfix: dbData.postfix,
info: dbData.info,
anlageId: dbData.anlageId,
anlageId: dbData.anlageId ? dbData.anlageId.toString() : '',
type: dbData.type,
groupId: dbData.groupId.toString(),
groupId: dbData.groupId ? dbData.groupId.toString() : '',
components: comop
}
}
getToUpdateComponents(currentDbComp: Component[], updateComp: ComponentResponseDto[]) {
}
getComponentType(name: string) {
let retValue = Object.values(ComponentTypes).find((c) => c === name)
@@ -184,7 +125,6 @@ export class UnitsService {
_id: new Types.ObjectId(compDto.id),
name: compDto.name,
type: compDto.type?.toString(),
signalIds: compDto.signalIds.map((s) => new Types.ObjectId(s)),
properties: { ...compDto.properties }
}
}
@@ -207,21 +147,20 @@ export class UnitsService {
return comps.map((c) => ({
name: c.name,
type: c.type?.toString(),
signalIds: c.signalIds ? c.signalIds.map((s) => new Types.ObjectId(s)) : [],
properties: { ...c.properties }
}));
}
handleComponents(component: ComponentResponseDto): Component {
let { id, name, signalIds, type, properties } = component;
let { id, name, type, properties,postfix,prefix,index } = component;
const cid = (id && Types.ObjectId.isValid(id)) ? new Types.ObjectId(id) : undefined;
const cSignalIds = signalIds ? this.handleSignals(signalIds) : [];
const cProperties = properties ? this.handleProperties(properties) : {};
const cName = name;
const cType = type;
return { name: cName, type: cType, signalIds: cSignalIds, properties: cProperties, _id: cid }
return { name: cName,postfix,prefix,index, type, properties: cProperties, _id: cid }
}