diff --git a/app/assets/javascripts/email_filter.js b/app/assets/javascripts/email_filter.js
new file mode 100644
index 0000000..dee720f
--- /dev/null
+++ b/app/assets/javascripts/email_filter.js
@@ -0,0 +1,2 @@
+// Place all the behaviors and hooks related to the matching controller here.
+// All this logic will automatically be available in application.js.
diff --git a/app/controllers/dotations_controller.rb b/app/controllers/dotations_controller.rb
index 7be3372..417d3e9 100644
--- a/app/controllers/dotations_controller.rb
+++ b/app/controllers/dotations_controller.rb
@@ -37,6 +37,8 @@ class DotationsController < ApplicationController
false
end
@dotation.save
+
+ NotifyEmailsJob.perform_later(@dotation.id) if @dotation.active.eql?(true)
end
# POST /dotations or /dotations.json
diff --git a/app/controllers/email_filter_controller.rb b/app/controllers/email_filter_controller.rb
new file mode 100644
index 0000000..8cfdc3e
--- /dev/null
+++ b/app/controllers/email_filter_controller.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+# Emails
+class EmailFilterController < ApplicationController
+ layout 'home_layout'
+ def unsubscribe
+ @email_filter =
+ FilterForEmail.by_email(params[:i]).by_unique(params[:k]).first
+ @email_filter.destroy unless @email_filter.blank?
+ end
+end
diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb
index 0427664..c90e58c 100644
--- a/app/controllers/home_controller.rb
+++ b/app/controllers/home_controller.rb
@@ -5,13 +5,17 @@ class HomeController < ApplicationController
layout 'home_layout'
def index
+ require 'json'
prepare_filters
+ cookies[:filter] = JSON.generate(build_filter_hash)
@dotations = Dotation.extra_search(params[:search]).public_dot
.point_desc.end_date_asc.page(params[:page])
end
def search
+ require 'json'
prepare_filters
+ cookies[:filter] = JSON.generate(build_filter_hash)
@dotations = Dotation.search_with_filters(build_filter_hash).public_dot
.point_desc.end_date_asc.page(params[:page])
end
@@ -23,6 +27,16 @@ class HomeController < ApplicationController
@company_sizes = CompanySize.all
end
+ def emailfilter
+ require 'json'
+ @emailfilter = FilterForEmail.new(
+ email: params[:email_filter_inp],
+ filters: JSON.parse(cookies[:filter])
+ )
+ @emailfilter.save
+ SendNotifyEmailJob.perform_later(@emailfilter.id, 1)
+ end
+
private
def prepare_filters
diff --git a/app/helpers/email_filter_helper.rb b/app/helpers/email_filter_helper.rb
new file mode 100644
index 0000000..be546f8
--- /dev/null
+++ b/app/helpers/email_filter_helper.rb
@@ -0,0 +1,2 @@
+module EmailFilterHelper
+end
diff --git a/app/jobs/generate_pdf_job.rb b/app/jobs/generate_pdf_job.rb
new file mode 100644
index 0000000..df66749
--- /dev/null
+++ b/app/jobs/generate_pdf_job.rb
@@ -0,0 +1,7 @@
+class GeneratePdfJob < ApplicationJob
+ queue_as :default
+
+ def perform(*args)
+ # Do something later
+ end
+end
diff --git a/app/jobs/notify_emails_job.rb b/app/jobs/notify_emails_job.rb
new file mode 100644
index 0000000..12d138e
--- /dev/null
+++ b/app/jobs/notify_emails_job.rb
@@ -0,0 +1,10 @@
+# frozen_string_literal: true
+
+# Job for async email notifications
+class NotifyEmailsJob < ApplicationJob
+ queue_as :default
+
+ def perform(dotation_id)
+ NotifyEmailService.new(dotation_id).call
+ end
+end
diff --git a/app/jobs/send_notify_email_job.rb b/app/jobs/send_notify_email_job.rb
new file mode 100644
index 0000000..6256857
--- /dev/null
+++ b/app/jobs/send_notify_email_job.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class SendNotifyEmailJob < ApplicationJob
+ queue_as :default
+
+ def perform(email_id, mail_type)
+ email_filter = FilterForEmail.where(id: email_id).first
+ return unless email_filter
+
+ if mail_type == 1
+ NewDotationMailer.with(email_filter: email_filter.id).welcome_email
+ .deliver
+ elsif mail_type == 2
+ NewDotationMailer.with(email_filter: email_filter.id).notification_email
+ .deliver
+ end
+ end
+end
diff --git a/app/mailers/new_dotation_mailer.rb b/app/mailers/new_dotation_mailer.rb
new file mode 100644
index 0000000..b30106a
--- /dev/null
+++ b/app/mailers/new_dotation_mailer.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+# Mailer for dotations
+class NewDotationMailer < ApplicationMailer
+ default from: 'kalendarzdotacji@gmail.com'
+
+ def welcome_email
+ @email_filter = FilterForEmail.where(id: params[:email_filter]).first
+ @email_params = {
+ i: @email_filter.email,
+ k: @email_filter.unique_name
+ }
+ # @emailfilter = FilterForEmail.find(params[:emailfilter])
+ mail(to: @email_filter.email, subject: 'Dodano subskrypcję dla nowych dotacji')
+ end
+
+ def notification_email
+ @email_filter = FilterForEmail.where(id: params[:email_filter]).first
+ @dotation = Dotation.where(id: params[:dotation_id]).first
+ @email_params = {
+ i: @email_filter.email,
+ k: @email_filter.unique_name
+ }
+ mail(to: @email_filter.email, subject: 'Pojawiła się nowa dotacja')
+ end
+end
diff --git a/app/models/dotation.rb b/app/models/dotation.rb
index 597c6da..3a5b5d4 100644
--- a/app/models/dotation.rb
+++ b/app/models/dotation.rb
@@ -64,6 +64,7 @@ class Dotation < ApplicationRecord
scope :point_desc, -> { order(points: :desc) }
scope :end_date_asc, -> { order(end_date: :asc) }
scope :public_dot, -> { where(active: true) }
+ scope :by_id, ->(val) { where(id: val) }
# == Callbacks ============================================================
# == Class Methods ========================================================
@@ -90,4 +91,8 @@ class Dotation < ApplicationRecord
ret
end
# == Instance Methods =====================================================
+
+ def safe_id
+ id
+ end
end
diff --git a/app/models/filter_for_email.rb b/app/models/filter_for_email.rb
new file mode 100644
index 0000000..7c529e4
--- /dev/null
+++ b/app/models/filter_for_email.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+# Emails with filters for customers
+class FilterForEmail < ApplicationRecord
+ # == Constants ============================================================
+
+ # == Attributes ===========================================================
+ serialize :filters, Hash
+ # == Extensions ===========================================================
+
+ # == Relationships ========================================================
+ has_many :sent_email_filters, dependent: :destroy
+ # == Validations ==========================================================
+
+ # == Scopes ===============================================================
+ scope :by_email, ->(val) { where(email: val) }
+ scope :by_unique, ->(val) { where(unique_name: val) }
+ # == Callbacks ============================================================
+ after_create :a_create
+ # == Class Methods ========================================================
+
+ # == Instance Methods =====================================================
+ def a_create
+ o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten
+ self.unique_name = (0...50).map { o[rand(o.length)] }.join
+ save
+ end
+end
diff --git a/app/models/sent_email_filter.rb b/app/models/sent_email_filter.rb
new file mode 100644
index 0000000..253564d
--- /dev/null
+++ b/app/models/sent_email_filter.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+# sent emails
+class SentEmailFilter < ApplicationRecord
+ # == Constants ============================================================
+
+ # == Attributes ===========================================================
+
+ # == Extensions ===========================================================
+
+ # == Relationships ========================================================
+ belongs_to :dotation
+ belongs_to :filter_for_email
+ # == Validations ==========================================================
+
+ # == Scopes ===============================================================
+ scope :by_dotation, ->(val) { where(dotation_id: val) }
+ scope :by_filter, ->(val) { where(filter_for_email_id: val) }
+ # == Callbacks ===================== =======================================
+
+ # == Class Methods ========================================================
+
+ # == Instance Methods =====================================================
+end
diff --git a/app/services/notify_email_service.rb b/app/services/notify_email_service.rb
new file mode 100644
index 0000000..a636385
--- /dev/null
+++ b/app/services/notify_email_service.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+# Popup service
+class NotifyEmailService
+
+ def initialize(dotation_id)
+ @dotation_id = dotation_id
+ end
+
+ def call
+ check_filters
+ end
+
+ private
+
+ def check_filters
+ filter_for_emails = FilterForEmail.all
+ filter_for_emails.each do |filter_for_email|
+ hsh = filter_for_email.filters.transform_keys(&:to_sym)
+ dot = Dotation.search_with_filters(hsh)
+ .by_id(@dotation_id).public_dot.first
+ next if dot.blank?
+
+ sef =
+ SentEmailFilter.by_dotation(dot.id).by_filter(filter_for_email.id).first
+
+ next unless sef.blank?
+
+ SentEmailFilter.create(filter_for_email_id: filter_for_email.id,
+ dotation_id: dot.id)
+ NewDotationMailer.with(dotation_id: dot.id,
+ email_filter: filter_for_email.id)
+ .notification_email.deliver
+ end
+ end
+end
diff --git a/app/views/email_filter/unsubscribe.html.erb b/app/views/email_filter/unsubscribe.html.erb
new file mode 100644
index 0000000..523422b
--- /dev/null
+++ b/app/views/email_filter/unsubscribe.html.erb
@@ -0,0 +1,38 @@
+Rezygnacja z subskrypcji
+ Subskrybcje
+ Informacja
+ Subskrybcja została usunięta
+
| + |
|
+ Witaj, <%= @email_filter.email %> + Pojawiła się nowa dotacja, która spełnia warunki Twojego wyszukiwania. + Aby zobaczyć tą dotację kliknij <%= link_to 'pokaż dotację', "#{APP_CONFIG['mail_link']}/dotacja/#{@dotation.safe_id}" %> + |
+
| + |
| Jeśli to nie Ty poprosiłeś o dodanie Ciebie do listy, możesz usunąć tą prośbę klikając w link <%= link_to 'Zrezygnuj', "#{APP_CONFIG['mail_link']}/zrezygnuj?#{@email_params.to_query}" %> + | +
| + |
| + |
| UWAGA! Prosimy nie odpowiadać na tą wiadomość, została ona automatycznie wygenerowana przez system | +
| + |
| Witaj, <%= @email_filter.email %> przyjęliśmy Twoją prośbę o informowanie Ciebie gdy pojawią się nowe dotacje. |
+
| + |
| Jeśli to nie Ty poprosiłeś o dodanie Ciebie do listy, możesz usunąć tą prośbę klikając w link <%= link_to 'Zrezygnuj', "#{APP_CONFIG['mail_link']}/zrezygnuj?#{@email_params.to_query}" %> + | +
| + |
| + |
| UWAGA! Prosimy nie odpowiadać na tą wiadomość, została ona automatycznie wygenerowana przez system | +