diff --git a/src/composables/useWashDepartments.js b/src/composables/useWashDepartments.js index e6c6e5f4..d9e6cbfa 100644 --- a/src/composables/useWashDepartments.js +++ b/src/composables/useWashDepartments.js @@ -14,6 +14,9 @@ const isLaneSelfServeEnabled = (lane) => ) ); +const hasSelfServeEnabledLane = (department) => + department?.self_serve_enabled === true && (department.lanes || []).some(isLaneSelfServeEnabled); + const toDepartmentViewModel = (department, distance = null) => ({ id: department.id, distance, @@ -47,6 +50,7 @@ export function useWashDepartments(options = {}) { const isSearchingDepartments = ref(false); const lastDepartmentFetchTime = ref(null); const departmentFetchError = ref(null); + const departmentSelectionStrategy = ref(null); let refreshInterval = null; @@ -73,6 +77,12 @@ export function useWashDepartments(options = {}) { return nearestDepartment.value.self_serve_enabled === true; }); + const isDepartmentSelectionDistanceBased = computed(() => departmentSelectionStrategy.value === "distance"); + + const isDepartmentSelectionFallbackBased = computed(() => departmentSelectionStrategy.value === "fallback"); + + const hasLocationCoordinates = (locationValue = locations.location.value) => !!locationValue?.coords; + const buildGuestDepartmentParams = () => (includeLanes ? { include_lanes: true } : {}); const fetchDepartments = async () => { @@ -110,20 +120,24 @@ export function useWashDepartments(options = {}) { const forcedDepartment = getForcedDepartment(); if (forcedDepartment) { nearestDepartment.value = toDepartmentViewModel(forcedDepartment); + departmentSelectionStrategy.value = "forced"; return nearestDepartment.value; } } + if (!hasLocationCoordinates(locationValue)) { + const fallbackDepartment = guestDepartments.value.find(hasSelfServeEnabledLane); + nearestDepartment.value = fallbackDepartment ? toDepartmentViewModel(fallbackDepartment) : null; + departmentSelectionStrategy.value = fallbackDepartment ? "fallback" : null; + return nearestDepartment.value; + } + let currentNearestDepartment = { id: null, distance: Infinity, }; guestDepartments.value.forEach((department) => { - if (!locationValue?.coords) { - return; - } - const from = { latitude: locationValue.coords.latitude, longitude: locationValue.coords.longitude, @@ -139,9 +153,8 @@ export function useWashDepartments(options = {}) { } }); - if (currentNearestDepartment.id) { - nearestDepartment.value = currentNearestDepartment; - } + nearestDepartment.value = currentNearestDepartment.id ? currentNearestDepartment : null; + departmentSelectionStrategy.value = currentNearestDepartment.id ? "distance" : null; return nearestDepartment.value; }; @@ -167,7 +180,7 @@ export function useWashDepartments(options = {}) { }; const startDepartmentSearch = () => { - if (!canAccessSuperUser()) { + if (!canAccessSuperUser() && hasLocationCoordinates()) { return; } @@ -245,6 +258,9 @@ export function useWashDepartments(options = {}) { isSearchingDepartments, lastDepartmentFetchTime, departmentFetchError, + departmentSelectionStrategy, + isDepartmentSelectionDistanceBased, + isDepartmentSelectionFallbackBased, availableProductIds, doesCurrentDepartmentSelectionHaveSelfServeEnabled, fetchDepartments, diff --git a/src/views/dashboards/userDashboard/wash/MyWash.vue b/src/views/dashboards/userDashboard/wash/MyWash.vue index e8c453c8..204248f9 100644 --- a/src/views/dashboards/userDashboard/wash/MyWash.vue +++ b/src/views/dashboards/userDashboard/wash/MyWash.vue @@ -10,12 +10,15 @@ import { useWashDepartments } from "@/composables/useWashDepartments"; const { guestDepartments, nearestDepartment, + isDepartmentSelectionFallbackBased, fetchDepartments, evaluateLocationDepartments, orderDepartmentsByDistance, } = useWashDepartments(); const orderedDepartments = computed(() => orderDepartmentsByDistance(guestDepartments.value)); +const hasLocationCoordinates = computed(() => !!locations.location.value?.coords); +const shouldShowChooseDepartmentMessage = computed(() => !hasLocationCoordinates.value && !nearestDepartment.value); const getDepartmentDistance = (department) => { if (!locations.location.value?.coords) { @@ -46,16 +49,36 @@ onMounted(async () => {