58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import { BadRequestException, 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 { Component } from 'src/_schemas/anlagen/component';
|
|
import { Unit } from 'src/_schemas/anlagen/units';
|
|
|
|
@Injectable()
|
|
export class ComponentsService {
|
|
constructor(@InjectModel(Unit.name, "anlagen") private model: Model<Unit>) { }
|
|
|
|
|
|
//--- Find BY component id
|
|
async findOne(id: string): Promise<ComponentOfUnitDto> {
|
|
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);
|
|
if (!comp) throw new NotFoundException(`Keine Unit mit der Componenten Id: ${id} gefunden`)
|
|
return {
|
|
unitId: item._id.toString(),
|
|
component: this.toDto(comp)
|
|
};
|
|
}
|
|
|
|
|
|
|
|
toDto(component: Component): ComponentResponseDto {
|
|
|
|
|
|
let compRet =
|
|
{
|
|
id: component._id!.toString(),
|
|
name: component.name,
|
|
type: this.getComponentType(component.type.toString()),
|
|
signalIds: component.signalIds.map((signal) => signal.toString()),
|
|
properties: component.properties
|
|
}
|
|
|
|
return compRet;
|
|
}
|
|
|
|
|
|
|
|
getComponentType(name: string) {
|
|
let retValue = Object.values(ComponentTypes).find((c) => c === name)
|
|
if (!retValue) return ComponentTypes.UNKNOWN;
|
|
return retValue
|
|
}
|
|
|
|
|
|
}
|