Files
rk-backend/src/_dto/endschalter.dto.ts
T
2026-11-05 15:16:54 +01:00

21 lines
1.1 KiB
TypeScript

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<string, unknown>;
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<string, unknown>;
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 } };
}