114 lines
4.5 KiB
TypeScript
114 lines
4.5 KiB
TypeScript
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
|
import { HydratedDocument, Schema as MongooseSchema, Types } from 'mongoose';
|
|
import { VisuAggregate } from './visu-aggregate';
|
|
import type { DrawDataDto } from '../../_dto/draw.dto';
|
|
|
|
export interface GrafikGeneratorDaten {
|
|
grafikTyp: 'motor' | 'bandOben' | 'warnung';
|
|
config: Record<string, unknown>;
|
|
options: Record<string, unknown>;
|
|
ergebnis?: DrawDataDto;
|
|
}
|
|
|
|
export interface XamlVariante {
|
|
zustand: string;
|
|
svg?: string;
|
|
xaml?: 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;
|
|
manuell?: boolean;
|
|
}
|
|
|
|
export type ProConViewElement =
|
|
| (ViewElementBasis & { darstellung: 'xaml'; typ: string; varianten: XamlVariante[]; generator?: GrafikGeneratorDaten })
|
|
| (ViewElementBasis & { darstellung: 'standard'; typ: 'anfahrzeit' | 'betriebsart' | 'text' | 'motor' });
|
|
|
|
const standardTypen = ['anfahrzeit', 'betriebsart', 'text', 'motor'];
|
|
const dateiTeil = /^[a-zA-Z0-9_-]+$/;
|
|
const endlich = { validator: Number.isFinite, message: 'Wert muss eine endliche Zahl sein' };
|
|
|
|
const VarianteSchema = new MongooseSchema<XamlVariante>({
|
|
svg: { type: String },
|
|
xaml: { type: String },
|
|
zustand: { type: String, required: true, match: dateiTeil },
|
|
xamlBase64: { type: String, default: '' },
|
|
}, { _id: false, strict: 'throw', minimize: false });
|
|
|
|
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 },
|
|
manuell: { type: Boolean, default: false },
|
|
}, { discriminatorKey: 'darstellung', strict: 'throw', minimize: false });
|
|
|
|
@Schema({ collection: 'proConViewAggregate', timestamps: true, strict: 'throw', minimize: false })
|
|
export class ProConViewAggregate {
|
|
_id!: Types.ObjectId;
|
|
|
|
@Prop({ type: String, default: null })
|
|
positionsMasterId!: string | null;
|
|
|
|
@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({
|
|
generator: {
|
|
type: new MongooseSchema({
|
|
grafikTyp: { type: String, enum: ['motor', 'bandOben', 'warnung'], required: true },
|
|
config: { type: MongooseSchema.Types.Mixed, required: true },
|
|
options: { type: MongooseSchema.Types.Mixed, default: () => ({}) },
|
|
ergebnis: { type: MongooseSchema.Types.Mixed },
|
|
}, { _id: false, strict: 'throw', minimize: false }),
|
|
default: undefined,
|
|
},
|
|
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', minimize: false }));
|
|
elemente.discriminator('standard', new MongooseSchema({
|
|
typ: { type: String, enum: standardTypen, required: true },
|
|
}, { _id: false, strict: 'throw', minimize: false }));
|
|
|