Skip to content

Commit

Permalink
feat: split coordinates field into lat, long and set geolocation on v…
Browse files Browse the repository at this point in the history
…alidate
  • Loading branch information
ruchamahabal committed May 29, 2024
1 parent ea6649a commit 239e1a4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
28 changes: 9 additions & 19 deletions frontend/src/components/CheckInPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
:initial-breakpoint="1"
:breakpoints="[0, 1]"
>
<div
class="h-120 w-full flex flex-col items-center justify-center gap-5 p-4 mb-5"
>
<div class="h-120 w-full flex flex-col items-center justify-center gap-5 p-4 mb-5">
<div class="flex flex-col gap-1.5 mt-2 items-center justify-center">
<div class="font-bold text-xl">
{{ dayjs(checkinTimestamp).format("hh:mm:ss a") }}
Expand All @@ -50,9 +48,7 @@
{{ locationStatus }}
</span>

<div
class="rounded border-4 translate-z-0 block overflow-hidden w-full h-170"
>
<div class="rounded border-4 translate-z-0 block overflow-hidden w-full h-170">
<iframe
width="100%"
height="170"
Expand All @@ -67,11 +63,7 @@
</div>
</template>

<Button
variant="solid"
class="w-full py-5 text-sm"
@click="submitLog(nextAction.action)"
>
<Button variant="solid" class="w-full py-5 text-sm" @click="submitLog(nextAction.action)">
Confirm {{ nextAction.label }}
</Button>
</div>
Expand All @@ -90,8 +82,8 @@ const socket = inject("$socket")
const employee = inject("$employee")
const dayjs = inject("$dayjs")
const checkinTimestamp = ref(null)
const latitude = ref("")
const longitude = ref("")
const latitude = ref(0)
const longitude = ref(0)
const locationStatus = ref("")

const checkins = createListResource({
Expand Down Expand Up @@ -148,14 +140,10 @@ function handleLocationError(error) {

const fetchLocation = () => {
if (!navigator.geolocation) {
locationStatus.value =
"Geolocation is not supported by your current browser"
locationStatus.value = "Geolocation is not supported by your current browser"
} else {
locationStatus.value = "Locating..."
navigator.geolocation.getCurrentPosition(
handleLocationSuccess,
handleLocationError
)
navigator.geolocation.getCurrentPosition(handleLocationSuccess, handleLocationError)
}
}

Expand All @@ -175,6 +163,8 @@ const submitLog = (logType) => {
employee: employee.data.name,
log_type: logType,
time: checkinTimestamp.value,
latitude: latitude.value,
longitude: longitude.value,
},
{
onSuccess() {
Expand Down
36 changes: 28 additions & 8 deletions hrms/hr/doctype/employee_checkin/employee_checkin.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
"skip_auto_attendance",
"attendance",
"location_section",
"coordinates",
"latitude",
"column_break_yqpi",
"longitude",
"section_break_ksbo",
"geolocation",
"shift_timings_section",
"shift_start",
Expand Down Expand Up @@ -118,12 +121,6 @@
"fieldtype": "Section Break",
"label": "Location"
},
{
"fieldname": "coordinates",
"fieldtype": "Data",
"label": "Coordinates",
"read_only": 1
},
{
"fieldname": "geolocation",
"fieldtype": "Geolocation",
Expand All @@ -137,10 +134,33 @@
{
"fieldname": "column_break_vyyt",
"fieldtype": "Column Break"
},
{
"fieldname": "latitude",
"fieldtype": "Float",
"label": "Latitude",
"precision": "7",
"read_only": 1
},
{
"fieldname": "longitude",
"fieldtype": "Float",
"label": "Longitude",
"precision": "7",
"read_only": 1
},
{
"fieldname": "column_break_yqpi",
"fieldtype": "Column Break"
},
{
"fieldname": "section_break_ksbo",
"fieldtype": "Section Break",
"hide_border": 1
}
],
"links": [],
"modified": "2024-04-03 06:36:07.640893",
"modified": "2024-05-29 20:09:19.489203",
"modified_by": "Administrator",
"module": "HR",
"name": "Employee Checkin",
Expand Down
27 changes: 16 additions & 11 deletions hrms/hr/doctype/employee_checkin/employee_checkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def validate(self):
validate_active_employee(self.employee)
self.validate_duplicate_log()
self.fetch_shift()
self.set_geolocation_coordinates()
self.set_geolocation_from_coordinates()

def validate_duplicate_log(self):
doc = frappe.db.exists(
Expand Down Expand Up @@ -61,18 +61,23 @@ def fetch_shift(self):
else:
self.shift = None

def set_geolocation_coordinates(self):
if not self.geolocation:
def set_geolocation_from_coordinates(self):
if not (self.latitude and self.longitude):
return

try:
self.coordinates = str(get_coordinates_from_geolocation(self.geolocation))
except Exception:
frappe.log_error("Error parsing geolocation field")


def get_coordinates_from_geolocation(geolocation: str):
return frappe.parse_json(geolocation)["features"][0]["geometry"]["coordinates"]
self.geolocation = frappe.json.dumps(
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
# geojson needs coordinates in reverse order: long, lat instead of lat, long
"geometry": {"type": "Point", "coordinates": [self.longitude, self.latitude]},
}
],
}
)


@frappe.whitelist()
Expand Down

0 comments on commit 239e1a4

Please sign in to comment.