diff --git a/app/dashboards/lecture_dashboard.rb b/app/dashboards/lecture_dashboard.rb index 536f502..7aad9d5 100644 --- a/app/dashboards/lecture_dashboard.rb +++ b/app/dashboards/lecture_dashboard.rb @@ -77,6 +77,6 @@ class LectureDashboard < Administrate::BaseDashboard # across all pages of the admin dashboard. # def display_resource(lecture) - lecture.title + "#{lecture.title} (#{lecture.course.title})" end end diff --git a/app/jobs/scrape_toggl_job.rb b/app/jobs/scrape_toggl_job.rb index 159334c..a95d75a 100644 --- a/app/jobs/scrape_toggl_job.rb +++ b/app/jobs/scrape_toggl_job.rb @@ -12,7 +12,7 @@ class ScrapeTogglJob < ApplicationJob # @param [Course] course def scrape_course(course) toggl_project_id = course.toggl_project - lectures = course.lectures.order(:start_time) + lectures = course.lectures.order(:start_time).includes(:attendance) return if lectures.empty? @@ -24,26 +24,30 @@ class ScrapeTogglJob < ApplicationJob end_time: Time.new('2024-01-01') ) + return if entries_data.empty? + lectures.each do |lecture| return if lecture.attendance.present? 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 + (Time.new(inner_entry['start']) - lecture.start_time).abs < 10.minutes end if concurrent_time_entry.present? Attendance.create!( lecture:, associated_toggl_entry: concurrent_time_entry['id'], - kind: :concurrent + kind: :concurrent, + toggl_data: entry ) - elsif entry['description'] == lecture.nice_title + elsif entry['description'] == lecture.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 + kind: :catchup, + toggl_data: entry ) end end diff --git a/app/lib/toggl.rb b/app/lib/toggl.rb index 54dfa09..b991db4 100644 --- a/app/lib/toggl.rb +++ b/app/lib/toggl.rb @@ -1,6 +1,6 @@ module Toggl def self.entries_for_project(toggl_project_id, start_time:, end_time:) - HTTParty.post( + JSON.parse(HTTParty.post( "http://localhost:3005/report", body: { "start_date": start_time.to_date.to_fs(), @@ -8,6 +8,6 @@ module Toggl "project_ids": [toggl_project_id] }.to_json, headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' } - ) + ).body) end end diff --git a/app/models/course.rb b/app/models/course.rb index 9caa9c9..68942fd 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -1,7 +1,7 @@ class Course < ApplicationRecord - has_many :lectures + has_many :lectures, dependent: :destroy # A course has a standalone connection to its recordings. To be shown they must be associated with a lecture but we # track those not associated with a lecture to avoid duplication. - has_many :recordings + has_many :recordings, dependent: :destroy end diff --git a/app/views/attendance_tracker/index.html.erb b/app/views/attendance_tracker/index.html.erb index 4d99b92..62a866d 100644 --- a/app/views/attendance_tracker/index.html.erb +++ b/app/views/attendance_tracker/index.html.erb @@ -27,7 +27,8 @@ <% lectures.each do |lecture| %>