Rephrase toggl scraping in term of attendances

This commit is contained in:
2023-09-29 21:19:43 +01:00
parent be4f3f75c3
commit 2934d21f5f
8 changed files with 72 additions and 8 deletions
+18 -6
View File
@@ -12,7 +12,8 @@ class ScrapeTogglJob < ApplicationJob
# @param [Course] course
def scrape_course(course)
toggl_project_id = course.toggl_project
lectures = course.lectures.order(:start_time).where(associated_toggl_entry: nil)
lectures = course.lectures.order(:start_time)
return if lectures.empty?
entries_data = Toggl::entries_for_project(
@@ -24,13 +25,24 @@ class ScrapeTogglJob < ApplicationJob
)
lectures.each do |lecture|
entries_data.find do |entry|
associated_entry = entry['time_entries'].find do |inner_entry|
(Time.new(inner_entry['start']) - lecture.start_time) < 15.seconds
entries_data.each do |entry|
concurrent_time_entry = entry['time_entries'].find do |inner_entry|
(Time.new(inner_entry['start']) - lecture.start_time) < 10.minutes
end
if associated_entry.present?
lecture.update!(associated_toggl_entry: associated_entry['id'])
if concurrent_time_entry.present?
Attendance.create!(
lecture:,
associated_toggl_entry: concurrent_time_entry['id'],
kind: :concurrent
)
elsif entry['description'] == lecture.nice_title
# If the title matches but it wasn't concurrent, then it was a catchup
Attendance.create!(
lecture:,
associated_toggl_entry: entry['time_entries'][0]['id'],
kind: :catchup
)
end
end
end
+8
View File
@@ -0,0 +1,8 @@
class Attendance < ApplicationRecord
belongs_to :lecture
enum :kind, [
:concurrent,
:catchup
]
end
+1
View File
@@ -1,5 +1,6 @@
class Lecture < ApplicationRecord
belongs_to :course
has_one :attendance
enum :status, [
:undetermined,