diff --git a/extensions/personio/src/api/attendances.tsx b/extensions/personio/src/api/attendances.tsx index 3052c216137..1a3d757a0fb 100644 --- a/extensions/personio/src/api/attendances.tsx +++ b/extensions/personio/src/api/attendances.tsx @@ -27,6 +27,7 @@ export interface AttendancePeriod { date: string; start_time: string; end_time: string; + duration: number; break: number; comment: string; updated_at: string; @@ -40,6 +41,43 @@ function daysInMonth(year: number, month: number) { return new Date(year, month, 0).getDate(); } +function hoursBetween(time1: string, time2: string): number { + const date1 = Date.parse(`1970-01-01T${time1}Z`); + const date2 = Date.parse(`1970-01-01T${time2}Z`); + + const diffInMs = Math.abs(date2 - date1); + + return diffInMs / 1000 / 60 / 60; +} + +export function hoursToNiceString(hours: number): string { + const whole_hours = Math.floor(hours); + const remaining_time = hours - whole_hours; + const minutes = Math.round(60 * remaining_time); + if (minutes > 0 && hours > 0) { + return `${whole_hours} hours and ${minutes} minutes`; + } else if (hours > 0 && minutes == 0) { + return `${whole_hours} hours`; + } else if (hours == 0 && minutes > 0) { + return `${minutes} minutes`; + } else { + return "0 minutes"; + } +} + +export function uniqueDateFilter(arr: AttendancePeriod[]) { + const dateSet = new Set(); + + return arr.filter((obj) => { + if (dateSet.has(obj.date)) { + return false; + } else { + dateSet.add(obj.date); + return true; + } + }); +} + export async function getAttendancesAPI( employeeNumber: number, token: string, @@ -67,6 +105,7 @@ export async function getAttendancesAPI( date: a.attributes.date, start_time: a.attributes.start_time, end_time: a.attributes.end_time, + duration: hoursBetween(a.attributes.end_time, a.attributes.start_time), break: a.attributes.break, comment: a.attributes.comment, updated_at: a.attributes.updated_at, @@ -100,7 +139,7 @@ export async function getAttendances( return JSON.parse(attendances) as AttendancePeriod[]; } else { const attendances = await getAttendancesAPI(employeeNumber, token, currentYear, selectedMonth); - cache.set(key, JSON.stringify(attendances), 23 * 60); + cache.set(key, JSON.stringify(attendances), 30); return attendances; } } diff --git a/extensions/personio/src/api/cache.tsx b/extensions/personio/src/api/cache.tsx index ff45f44a11b..25119d6663c 100644 --- a/extensions/personio/src/api/cache.tsx +++ b/extensions/personio/src/api/cache.tsx @@ -33,10 +33,13 @@ export class MyCache { // Calculate whether the entry is expired (more living minutes than it should have) // Return the entry only when it exists and when is is not yet expired if (data) { + console.log(`Found ${key} in cache`); const cacheEntry = JSON.parse(data) as CacheEntry; const livingMinutes = getMinutesBetweenDates(new Date(cacheEntry.createdAt), new Date()); if (livingMinutes < cacheEntry.expiresInMinutes) { - console.log(`Found ${key} in cache`); + console.log( + `Cache is not yet expired, ${key} is expiring in ${cacheEntry.expiresInMinutes - livingMinutes} minutes.`, + ); return cacheEntry.data; } else { console.log(`${key} is expired`); diff --git a/extensions/personio/src/attendances.tsx b/extensions/personio/src/attendances.tsx index 128124d8faa..0b065e5d10c 100644 --- a/extensions/personio/src/attendances.tsx +++ b/extensions/personio/src/attendances.tsx @@ -1,7 +1,7 @@ -import { List, getPreferenceValues } from "@raycast/api"; +import { Action, ActionPanel, List, getPreferenceValues } from "@raycast/api"; import { getPersonioToken } from "./api/api"; import { useEffect, useState } from "react"; -import { AttendancePeriod, getAttendances } from "./api/attendances"; +import { AttendancePeriod, getAttendances, hoursToNiceString, uniqueDateFilter } from "./api/attendances"; const months = [ "January", @@ -25,6 +25,7 @@ export default function Attendances() { const [attendances, setAttendances] = useState([]); const [selectedMonth, setSelectedMonth] = useState(currentMonth); + const [rerunFetchTrigger, setRerunFetchTrigger] = useState(true); const paddedMonth = (selectedMonth + 1).toString().padStart(2, "0"); @@ -44,12 +45,27 @@ export default function Attendances() { setAttendances(sortedAttendances); } fetchAttendances(); - }, [selectedMonth]); + }, [selectedMonth, rerunFetchTrigger]); + + function computeTotalHours() { + let totalHours = 0; + for (const attendance of attendances) { + totalHours = totalHours + attendance.duration; + } + console.log(`Total hours this month: ${hoursToNiceString(totalHours)}`); + return totalHours; + } + + function computeAttendanceDays() { + const attendanceDays = uniqueDateFilter(attendances).length; + console.log(`Total working days this month: ${attendanceDays}`); + return attendanceDays; + } return ( + @@ -90,10 +110,24 @@ export default function Attendances() { )} + + + } /> } + actions={ + + setRerunFetchTrigger(!rerunFetchTrigger)} /> + + } /> ))}