"Refactor vehicle and booking components: streamline booking application logic, update metadata fields (e.g., booking_id, lastOrderId), enhance vehicle selection with duplicate prevention, and improve consistency in pending bookings handling."

This commit is contained in:
Jeppe Bundgaard
2025-11-24 13:02:15 +01:00
parent 85f0901cc2
commit db996f093e
6 changed files with 34 additions and 22 deletions
@@ -40,10 +40,10 @@ const pendingBookings = ref([]);
// Get the department booking list
const loadPendingBookings = () => {
SessionUser.request(
SessionUser.objects.bookings.meta.endpoint,
SessionUser.objects.order_bookings.meta.endpoint,
'GET',
{
filters: 'department:' + department_id.value + ',status:pending',
filters: 'department:' + department_id.value + ',order_id:is null',
page: 1,
limit: 100,
}
@@ -72,12 +72,12 @@ const getVehiclePlateBooking = (vehiclePlate) => {
return null;
}
// Find the booking for the given vehicle plate
return pendingBookings.value.find(booking => booking.regNrTraekker === vehiclePlate || booking.regNrTrailer === vehiclePlate);
return pendingBookings.value.find(booking => booking.reg_1 === vehiclePlate || booking.reg_2 === vehiclePlate);
};
const doesVehiclePlateHaveBooking = (vehiclePlate) => {
// Check if the vehicle has a booking in the pending bookings list
return pendingBookings.value.some(booking => booking.regNrTraekker === vehiclePlate || booking.regNrTrailer === vehiclePlate);
return pendingBookings.value.some(booking => booking.reg_1 === vehiclePlate || booking.reg_2 === vehiclePlate);
};
@@ -172,6 +172,7 @@ const onAutomaticSelection = (object: PosSearchResult | null) => {
reference: object?.reference || null,
last_order_id: object?.lastOrderId || null,
wash_subscription: object?.washSubscription,
booking_id: object?.bookingId || null,
} as PosVehicle;
switch (vehicles.activeVehicleIndex.value) {
case 1:
@@ -138,8 +138,6 @@ const applyPendingBookingFromSelection = async () => {
try {
await SessionUser.objects.orders.set.po(order_id.value, booking.po);
console.warn('Applied PO from booking:', booking.po);
// If the primary product is set, do not show the vehicle selection view
vehicleSelection.value = false;
} catch (e) {
console.error('Failed to set PO on order from booking', e);
}
@@ -190,10 +188,15 @@ const applyPendingBookingFromSelection = async () => {
if (transactionItems.primaryItem.value) {
// Keep any existing addons that may have been set previously only if they are different.
transactionItems.primaryItem.value.addons = preparedAddons as any;
primaryProduct.addons = preparedAddons as any;
}
lastAppliedBookingId.value = booking.id;
console.warn('Applied pending booking to cart (primary + addons):', booking.id);
//console.warn('Applied pending booking to cart (primary + addons):', booking.id, primaryProduct, preparedAddons);
//console.warn('Current transaction items after applying booking:', transactionItems.primaryItem.value);
lastFetchedPrimaryItemProduct.value = primaryProduct; // Update last fetched primary item
// Hide the vehicle selection as booking has been applied
vehicleSelection.value = false;
} catch (e) {
console.error('Failed to apply pending booking to Step 2', e);
}
@@ -53,7 +53,7 @@ const registrationNumber3 = ref("");
const label3 = ref("Reg 3");
// Function to set a value if it is not null
function setIfNotNull(variable: string, value: { registrationNumber: string, customerStatus: any, customerId?: number } | null) {
function setIfNotNull(variable: string, value: { registrationNumber: string, customerStatus: any, customerId?: number, lastOrderId?: number } | null) {
//console.warn("setIfNotNull", variable, value);
if (value !== null) {
switch (variable) {
@@ -65,6 +65,7 @@ function setIfNotNull(variable: string, value: { registrationNumber: string, cus
reg: value.registrationNumber,
status: value.customerStatus,
customer_id: value.customerId,
last_order_id: value.lastOrderId,
}
)
registrationNumber1.value = value.registrationNumber;
@@ -129,7 +129,7 @@ const applyBookingAutomatically = (booking: any) => {
const onSelect = (result: PosSearchResult) => {
//console.warn("onSelect:", result);
// Check if the vehicle has a booking
if (doesVehiclePlateHaveBooking(result.registrationNumber)) {
if (result?.bookingId) {
//console.warn("Vehicle has a booking:", getVehiclePlateBooking(result.registrationNumber));
applyBookingAutomatically(getVehiclePlateBooking(result.registrationNumber));
}
@@ -207,6 +207,9 @@ watch(isSearching, (newValue) => {
watch(() => props.searchQuery, (newValue) => {
if (newValue) {
performSearch(newValue);
if (props.automaticallySelect) {
automaticallySelect();
}
}
});
@@ -232,6 +235,7 @@ watch(() => vehicles_matching.value, (newValue) => {
reference: vehicle?.reference || null,
lastOrderId: vehicle?.last_order_id || null,
washSubscription: vehicle?.wash_subscription || null,
bookingId: vehicle?.booking_id || null,
} as PosSearchResult;
});
//console.warn('Transformed PosSearchResult:', result);
@@ -244,24 +248,26 @@ watch(() => vehicles_matching.value, (newValue) => {
});
const automaticallySelect = () => {
if (props.automaticallySelect && props.searchQuery !== '' && vehicles_matching.value.length > 0) {
const withoutDuplicates = preventDuplicates(searchResults.value);
if (props.automaticallySelect && props.searchQuery !== '' && withoutDuplicates.length > 0) {
//console.warn("Automatically selecting vehicle for reg:", props.searchQuery, "from vehicles_matching:", vehicles_matching.value);
// Check if the vehicle is found in the results.
// If the vehicle is found.
if (vehicles_matching.value.find(v => v.reg.toUpperCase() === props.searchQuery.toUpperCase())) {
const match = vehicles_matching.value.find(v => v.reg.toUpperCase() === props.searchQuery.toUpperCase());
//console.warn("Automatically selecting vehicle:", match);
if (withoutDuplicates.find(v => v.registrationNumber.toUpperCase() === props.searchQuery.toUpperCase())) {
const match = withoutDuplicates.find(v => v.registrationNumber.toUpperCase() === props.searchQuery.toUpperCase());
console.warn("Automatically selecting vehicle:", match);
// Emit the selected vehicle object
onSelect({
registrationNumber: props.searchQuery.toUpperCase(),
customerName: match?.customer_name || "Unknown Customer",
customerId: match?.customer_id || 0,
customerStatus: determineCustomerStatus(match),
customerName: match?.customerName || "Unknown Customer",
customerId: match?.customerId || 0,
customerStatus: match ? match.customerStatus : determineCustomerStatus({reg: props.searchQuery} as any),
type: match?.type || null,
barred: match?.barred,
reference: match?.reference || null,
lastOrderId: match?.last_order_id || null,
washSubscription: match?.wash_subscription,
lastOrderId: match?.lastOrderId || null,
washSubscription: match?.washSubscription,
bookingId: match?.bookingId || null,
});
} else {
console.warn("No exact match found for automatic selection.");
@@ -272,19 +278,19 @@ const automaticallySelect = () => {
const determineCustomerStatus = (vehicle: any): VehicleStatusKey => {
//console.warn('Determine customer status for vehicle:', vehicle);
// Determine the customer status based on the vehicle object
if (doesVehiclePlateHaveBooking(vehicle.reg)) {
if (vehicle?.booking_id) {
//console.warn('Vehicle has a booking:', getVehiclePlateBooking(vehicle.reg));
return 'booked';
} else if (vehicle?.status === 'known') {
//console.warn('Vehicle is known:', vehicle);
return 'known'
} else if (!vehicle.customer_id && !vehicle.customer_name) {
} else if (vehicle?.status === 'unknown') {
//console.warn('Vehicle is unknown:', vehicle);
return 'unknown';
} else if (vehicle.barred) {
} else if (vehicle?.status === 'card' || vehicle?.barred === true) {
//console.warn('Vehicle is barred:', vehicle);
return 'card';
} else if (vehicle.status === 'verified') {
} else if (vehicle?.status === 'verified') {
//console.warn('Vehicle is verified:', vehicle);
return 'verified';
} else {
@@ -1067,6 +1067,7 @@ export const searchVehicle = async ( inputValue, search_id ) => {
'GET',
{
search: inputValue,
...(department_id.value ? {department: department_id.value} : {}),
page: 1,
limit: 10,
}