procon aggregate

This commit is contained in:
marco committed 2026-10-22 07:57:46 +02:00
1 parent e284fae7b4
commit d0655f3638
32 files changed
+1520 -1058

No files matched your search

@@ -0,0 +1,87 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument, Schema as MongooseSchema, Types } from 'mongoose';
import { VisuAggregate } from './visu-aggregate';
export interface XamlVariante {
zustand: string;
xamlBase64: string;
}
export interface ViewElementBasis {
_id?: Types.ObjectId;
key: string;
name: string;
width: number;
height: number;
posX: number;
posY: number;
zIndex: number;
show: boolean;
}
export type ProConViewElement =
| (ViewElementBasis & { darstellung: 'xaml'; typ: string; varianten: XamlVariante[] })
| (ViewElementBasis & { darstellung: 'standard'; typ: 'anfahrzeit' | 'betriebsart' | 'text' });
const standardTypen = ['anfahrzeit', 'betriebsart', 'text'];
const dateiTeil = /^[a-zA-Z0-9_-]+$/;
const endlich = { validator: Number.isFinite, message: 'Wert muss eine endliche Zahl sein' };
const VarianteSchema = new MongooseSchema<XamlVariante>({
zustand: { type: String, required: true, match: dateiTeil },
xamlBase64: { type: String, default: '' },
}, { _id: false, strict: 'throw' });
const ElementSchema = new MongooseSchema<ViewElementBasis>({
key: { type: String, required: true, match: dateiTeil },
name: { type: String, default: '' },
width: { type: Number, required: true, min: 0, validate: endlich },
height: { type: Number, required: true, min: 0, validate: endlich },
posX: { type: Number, required: true, validate: endlich },
posY: { type: Number, required: true, validate: endlich },
zIndex: { type: Number, required: true, validate: { validator: Number.isSafeInteger, message: 'zIndex muss ganzzahlig sein' } },
show: { type: Boolean, default: true },
}, { discriminatorKey: 'darstellung', strict: 'throw' });
@Schema({ collection: 'proConViewAggregate', timestamps: true, strict: 'throw' })
export class ProConViewAggregate {
_id!: Types.ObjectId;
@Prop({ type: MongooseSchema.Types.ObjectId, ref: VisuAggregate.name, required: true, immutable: true })
visuAggregateId!: Types.ObjectId;
@Prop({
type: [ElementSchema],
default: [],
validate: {
validator: (elemente: ProConViewElement[]) =>
new Set(elemente.map(element => element.key)).size === elemente.length &&
elemente.every(element => element.darstellung === 'xaml' || element.darstellung === 'standard'),
message: 'Element-Keys müssen eindeutig sein und die Darstellung muss xaml oder standard sein',
},
})
elemente!: ProConViewElement[];
}
export type ProConViewAggregateDocument = HydratedDocument<ProConViewAggregate>;
export const ProConViewAggregateSchema = SchemaFactory.createForClass(ProConViewAggregate);
ProConViewAggregateSchema.index({ visuAggregateId: 1 }, { unique: true });
const elemente = ProConViewAggregateSchema.path<MongooseSchema.Types.DocumentArray>('elemente');
elemente.discriminator('xaml', new MongooseSchema({
typ: {
type: String, required: true,
validate: { validator: (typ: string) => !standardTypen.includes(typ), message: 'Dieser Elementtyp verwendet keine XAML-Grafiken' },
},
varianten: {
type: [VarianteSchema], default: [],
validate: {
validator: (varianten: XamlVariante[]) => new Set(varianten.map(variante => variante.zustand)).size === varianten.length,
message: 'Zustände müssen pro Element eindeutig sein',
},
},
}, { _id: false, strict: 'throw' }));
elemente.discriminator('standard', new MongooseSchema({
typ: { type: String, enum: standardTypen, required: true },
}, { _id: false, strict: 'throw' }));