import { BadRequestException } from '@nestjs/common'; import { parseMotorRequest } from './motor.dto'; import type { EndschalterDirection } from '../graphics/elements/types'; export function parseEndschalterRequest(input: unknown) { if (!input || typeof input !== 'object' || Array.isArray(input)) { throw new BadRequestException('Erwartet werden config und optional options'); } const request = input as Record; const options = request.options === undefined ? {} : request.options; if (typeof options !== 'object' || options === null || Array.isArray(options)) { throw new BadRequestException('Endschalter-options muss ein Objekt sein'); } const { direction = 'unten', ...colors } = options as Record; if (typeof direction !== 'string' || !['oben', 'unten', 'rechts', 'links'].includes(direction)) { throw new BadRequestException('direction muss oben, unten, rechts oder links sein'); } const parsed = parseMotorRequest({ ...request, options: colors }); return { config: parsed.config, options: { ...parsed.options, direction: direction as EndschalterDirection } }; }