Skip to content

Commit

Permalink
Merge pull request #89 from uwcirg/feature/webpush-actions
Browse files Browse the repository at this point in the history
Feature/webpush actions
  • Loading branch information
kylegoodwin committed Dec 14, 2021
2 parents ebcdb08 + d252cb6 commit c8165cc
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 40 deletions.
34 changes: 19 additions & 15 deletions app/controllers/v2/daily_report_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ class V2::DailyReportController < UserController

def index
todays_report = @current_user.daily_reports.find_by(find_daily_report_params)
if(todays_report.nil?)
render(json: {error: "No report found for #{params[:date]}"}, status: :not_found)
if (todays_report.nil?)
render(json: { error: "No report found for #{params[:date]}" }, status: :not_found)
else
render(json: todays_report, status: :ok )
render(json: todays_report, status: :ok)
end
end

def create

if (create_params[:no_issues] == "true")
daily_report = nil
ActiveRecord::Base.transaction do
daily_report = DailyReport.create_if_not_exists(@current_user.id, create_params["date"])
medication_report = @current_user.medication_reports.create!(date: create_params["date"], medication_was_taken: true)
symptom_report = @current_user.symptom_reports.create!(date: create_params["date"])
daily_report.update!(was_one_step: true, doing_okay: true, medication_report: medication_report, symptom_report: symptom_report)
end

render(json: daily_report, status: :created)
else
render(json: { error: "This route only supports reports with no_issues=true, to report issues use individual routes ie. /v2/medication_reports", code: 400 }, status: 400)
end
end

private
Expand All @@ -22,17 +34,9 @@ def find_daily_report_params
params.permit(:date)
end

def create_daily_report_params
def create_params
params.require(:date)
params.permit( :date,
:nausea,:nausea_rating,:redness,
:hives, :fever, :appetite_loss,
:blurred_vision, :sore_belly,
:yellow_coloration, :difficulty_breathing,
:facial_swelling, :dizziness, :headache,
:other, :photo_url, :captured_at, :doing_okay, :doing_okay_reason,
:datetime_taken,:medication_was_taken,:why_medication_not_taken )

params.require(:no_issues)
params.permit(:date, :no_issues)
end

end
10 changes: 10 additions & 0 deletions app/controllers/v2/test_medication_reminder_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class V2::TestMedicationReminderController < UserController

def create
selected_patient = Patient.find(params[:patient_id])
authorize selected_patient, :update?
selected_patient.send_medication_reminder
render(json: {message: "Notification sent successfully"}, status: :ok)
end

end
6 changes: 4 additions & 2 deletions app/lib/push_notification_sender.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require "webpush"

class PushNotificationSender
def initialize(user, title, body, app_url, type)
def initialize(user, title, body, app_url, type, actions=nil)
@user = user
@title = title
@body = body
@app_url = app_url
@type = type
@actions = actions
@status = PushNotificationStatus.create!(user: @user, notification_type: type)
end

Expand Down Expand Up @@ -34,7 +35,8 @@ def message_body
title: "#{@title}",
body: "#{@body}",
url: "#{ENV["URL_CLIENT"]}",
data: { url: @app_url, id: @status.id, type: @status.notification_type }
data: { url: @app_url, id: @status.id, type: @status.notification_type },
actions: @actions
)
end

Expand Down
16 changes: 7 additions & 9 deletions app/models/daily_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class DailyReport < ApplicationRecord
scope :unresolved_symptoms, -> { active_patient.joins(:resolutions).where(:symptom_report => SymptomReport.has_symptom, "resolutions.id": Resolution.last_symptom_from_user).where("daily_reports.updated_at > resolutions.resolved_at") }
scope :since_last_missing_resolution, -> { active_patient.joins(:resolutions).where("resolutions.id": Resolution.last_medication_from_user).where("daily_reports.date > resolutions.resolved_at") }
scope :has_symptoms, -> { active_patient.joins(:symptom_report).where(:symptom_report => SymptomReport.has_symptom) }
scope :has_severe_symptoms, -> { active_patient.joins(:symptom_report).where(:symptom_report => SymptomReport.high_alert)}
scope :has_severe_symptoms, -> { active_patient.joins(:symptom_report).where(:symptom_report => SymptomReport.high_alert) }
scope :has_photo, -> { joins(:photo_report).where("photo_reports.photo_url IS NOT NULL") }
scope :unresolved_support_request, -> { active_patient.joins(:resolutions).where("resolutions.id": Resolution.last_support_request, "daily_reports.doing_okay": false).where("daily_reports.created_at > resolutions.resolved_at") }
scope :photo_missing, -> {joins(:photo_report).where("photo_reports.id = ?", nil)}
scope :photo_missing, -> { joins(:photo_report).where("photo_reports.id = ?", nil) }
scope :medication_was_not_taken, -> { joins(:medication_report).where(medication_reports: { medication_was_taken: false }).distinct }

def limit_one_report_per_day
Expand All @@ -33,14 +33,13 @@ def limit_one_report_per_day
end
end

def self.create_if_not_exists(patient_id,date)
report = DailyReport.where(user_id: patient_id,date: date)
def self.create_if_not_exists(patient_id, date)
report = DailyReport.where(user_id: patient_id, date: date)
if report.exists?
return report.first
end

return DailyReport.create!(user_id: patient_id,date: date)

return DailyReport.create!(user_id: patient_id, date: date, was_one_step: true)
end

def self.user_missed_days(user_id)
Expand Down Expand Up @@ -79,10 +78,10 @@ def symptom_summary
end

def medication_was_taken
if(medication_report.nil?)
if (medication_report.nil?)
return false
end

return medication_report.medication_was_taken
end

Expand All @@ -104,5 +103,4 @@ def update_reminders_since_last_report
def update_patient_stats
self.patient.update_stats_in_background
end

end
21 changes: 18 additions & 3 deletions app/models/patient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,24 @@ def last_photo_request_status
end

return {
date_of_request: request.nil? ? 0 : request.date,
photo_was_submitted: !submitted.nil? && submitted.has_photo?
}
date_of_request: request.nil? ? 0 : request.date,
photo_was_submitted: !submitted.nil? && submitted.has_photo?,
}
end

def send_medication_reminder
I18n.with_locale(self.locale) do
self.send_push_to_user(I18n.t("medication_reminder"), I18n.t("medication_reminder_body"), "/home", "MedicationReminder", [
{
action: "good",
title: "👍 Si",
},
{
action: "issue",
title: "💬 No",
},
])
end
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def unsubscribe_push
self.update(push_p256dh: nil, push_auth: nil, push_url: nil)
end

def send_push_to_user(title, body, app_url = "/", type = 0)
PushNotificationSender.new(self, title, body, app_url, type).send_notification
def send_push_to_user(title, body, app_url = "/", type = 0,actions=nil)
PushNotificationSender.new(self, title, body, app_url, type,actions).send_notification
end

def update_last_message_seen(channel_id, number)
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/daily_report_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class DailyReportSerializer < ActiveModel::Serializer
attributes :id, :date, :user_id, :photo_url, :medication_was_taken,
attributes :id, :date, :user_id, :was_one_step, :photo_url, :medication_was_taken,
:symptoms, :taken_at, :updated_at, :doing_okay, :photo_was_required,
:created_offline, :status, :photo_was_skipped, :why_photo_was_skipped,
:number_of_days_after_request, :created_at
Expand Down
7 changes: 4 additions & 3 deletions app/workers/notification_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ def perform()
if(notifications.length > 0)
notifications.each do |notification|
user = notification.user
I18n.with_locale(user.locale) do
user.send_push_to_user(I18n.t('medication_reminder'), I18n.t('medication_reminder_body'), "/home/report/0", "MedicationReminder")
end
# I18n.with_locale(user.locale) do
# user.send_push_to_user(I18n.t('medication_reminder'), I18n.t('medication_reminder_body'), "/", "MedicationReminder")
# end
user.send_medication_reminder
puts("Sent notification to #{user.id} at #{Time.now.strftime("%H:%M")} with #{user.locale} locale")
end
else
Expand Down
4 changes: 3 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ en:
welcome: "Welcome!"
new_messages: "New Messages"
medication_reminder: "Medication Reminder"
medication_reminder_body: "Please take your medication and report it in the Treatment Companion app"
medication_reminder_body: "Have you taken your medication today, and are you feeling okay?"
yes: "Yes"
no : "No"
user_settings:
password_mismatch: "Passwords Do Not Match"
current_password_incorrect: "Current Password Incorrect"
Expand Down
6 changes: 4 additions & 2 deletions config/locales/es-ar.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
es-AR:
welcome: "Bienvenidos"
new_messages: "Nuevas Mensajes"
medication_reminder: "Por favor toma tu medicina"
medication_reminder_body: "toca aquí para informarlo en la aplicación"
medication_reminder: "Recuerde de reportar cada dia"
medication_reminder_body: "Hoy tome mis medicamentos, no tengo nada para reportar y estoy bien. Seleccione su repuesta"
yes: ""
no : "No"
user_settings:
password_mismatch: "Nuevas contraseñas no coinciden"
current_password_incorrect: "Contraseña Actual Incorrecta"
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace "v2" do
resource :vapid_public_key, only: [:show]
resources :time_summary, only: [:index]
resources :daily_report, only: [:index]
resources :daily_report, only: [:index, :create]
resources :medication_reports, only: [:create]
resources :symptom_reports, only: [:create]
resources :photo_reports, only: [:create, :index]
Expand All @@ -17,6 +17,7 @@
resources :activation, only: [:create]
resources :contact_tracing_surveys, only: [:index, :create]
resources :treatment_outcome, only: [:create]
resource :test_medication_reminder, only: [:create] , controller: "test_medication_reminder"
end

resources :patients, only: [:index], controller: "patient"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddOneStepStatusToDailyReport < ActiveRecord::Migration[6.0]
def change
add_column :daily_reports, :was_one_step, :boolean, null: true
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_08_18_164327) do
ActiveRecord::Schema.define(version: 2021_12_10_151609) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -58,6 +58,7 @@
t.boolean "doing_okay"
t.text "doing_okay_reason"
t.boolean "created_offline", default: false
t.boolean "was_one_step"
t.index ["user_id", "date"], name: "index_daily_reports_on_user_id_and_date", unique: true
t.index ["user_id"], name: "index_daily_reports_on_user_id"
end
Expand Down

0 comments on commit c8165cc

Please sign in to comment.