diff --git a/frontend/src/lib/utils/processing.js b/frontend/src/lib/utils/processing.js index dd621e5..2769e80 100644 --- a/frontend/src/lib/utils/processing.js +++ b/frontend/src/lib/utils/processing.js @@ -3,10 +3,10 @@ import { toRFC3339 } from './helpers'; /** * Converts FormData to a nested object structure * @param {FormData} formData - The FormData object to convert - * @returns {{ object: Partial | Partial, confirm_password: string }} Nested object representation of the form data + * @returns {{ object: Partial | Partial | Partial, confirm_password: string }} Nested object representation of the form data */ export function formDataToObject(formData) { - /** @type { Partial | Partial } */ + /** @type { Partial | Partial | Partial } */ const object = {}; let confirm_password = ''; @@ -142,8 +142,8 @@ export function processUserFormData(rawData) { } /** - * Processes the raw form data into the expected user data structure - * @param {{ object: Partial} } rawData - The raw form data object + * Processes the raw form data into the expected subscription data structure + * @param {{ object: Partial, confirm_password: string }} rawData - The raw form data object * @returns {{ subscription: Partial }} Processed user data */ export function processSubscriptionFormData(rawData) { @@ -166,3 +166,39 @@ export function processSubscriptionFormData(rawData) { console.dir(clean); return clean; } + +/** + * Processes the raw form data into the expected car data structure + * @param {{ object: Partial, confirm_password: string }} rawData - The raw form data object + * @returns {{ car: Partial }} Processed user data + */ +export function processCarFormData(rawData) { + /** @type {{ car: Partial }} */ + let processedData = { + car: { + id: Number(rawData.object.id) || 0, + name: String(rawData.object.name) || '', + status: Number(rawData.object.status) || 0, + brand: String(rawData.object.brand) || '', + model: String(rawData.object.model) || '', + price: Number(rawData.object.price) || 0, + rate: Number(rawData.object.rate) || 0, + licence_plate: String(rawData.object.licence_plate) || '', + start_date: toRFC3339(String(rawData.object.start_date)) || '', + end_date: toRFC3339(String(rawData.object.end_date)) || '', + color: String(rawData.object.color) || '', + notes: String(rawData.object.notes) || '', + location: { + latitude: Number(rawData.object.location?.latitude) || 0, + longitude: Number(rawData.object.location?.longitude) || 0 + }, + damages: rawData.object.damages || [], + insurances: rawData.object.insurances || [] + } + }; + const clean = JSON.parse(JSON.stringify(processedData), (key, value) => + value !== null && value !== '' ? value : undefined + ); + console.dir(clean); + return clean; +}