import express from 'express';
import { getRepository } from 'typeorm';
import { VehicleEntity } from '../../../../entities/vehicle.entity';

export async function VehicleReloadParameter(req: express.Request, res: express.Response): Promise<express.Response<any, Record<string, any>>> {
    if (!req.body.vehicleId) {
        res.status(400).send('Missing parameter');
    }

    if (!Number(req.body.vehicleId)) {
        res.status(500).send('Unable to parse given parameter');
    }

    const vehicleEntity = await getRepository(VehicleEntity).findOne(Number(req.body.vehicleId));
    if (vehicleEntity) {
        const vehicle = mp.vehicles.toArray().find((v) => v.vehicleEntity?.id === Number(req.body.vehicleId));
        if (vehicle) {
            // @ts-ignore
            vehicle.vehicleEntity![req.body.parameter] = vehicleEntity[req.body.parameter];

            vehicle.numberPlate = vehicle.vehicleEntity!.plate || '';
        }
    }

    return res.sendStatus(200);
}
