Skip to content

Commit

Permalink
penalty (#100)
Browse files Browse the repository at this point in the history
* rails generate sidekiq:job Penalty

* jquery datepicker

* fix method

* rubocop

* disable

* - test
  • Loading branch information
adnjoo authored Oct 24, 2024
1 parent 376888e commit 73c4c67
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ def set_note
end

def note_params
params.require(:note).permit(:title, :content)
params.require(:note).permit(:title, :content, :deadline)
end
end
12 changes: 11 additions & 1 deletion app/models/note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ class Note < ApplicationRecord

belongs_to :user

# After saving the note, schedule the penalty check if the deadline is changed
after_save :schedule_penalty_check

validates :title, presence: true
# content validation removed, allowing empty content

def schedule_penalty_check
if self.saved_change_to_attribute?(:deadline)
puts("Scheduling penalty check for note #{self.id}")
# Schedule PenaltyJob to run 1 minute after the deadline
PenaltyJob.perform_at(self.deadline + 1.minute, self.id)
end
end
end
41 changes: 41 additions & 0 deletions app/sidekiq/penalty_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# app/jobs/penalty_job.rb
class PenaltyJob
include Sidekiq::Job

def perform(note_id)
# Find the note
note = Note.find(note_id)
user = note.user

# Check if the note is incomplete and the deadline has passed
if !note.completed && note.deadline < Time.current
Rails.logger.info "PenaltyJob: Charging user #{user.id} for not completing the task #{note.id}"
# Charge the user for not completing the task
# charge_user(user, note)
end
end

private

def charge_user(user, note)
amount = 100 # Penalty fee in cents ($1.00)

# Retrieve the user's default payment method from Stripe
payment_method_id = Stripe::Customer.retrieve(user.stripe_id).invoice_settings.default_payment_method

if payment_method_id
# Create a payment intent to charge the user
Stripe::PaymentIntent.create({
amount: amount,
currency: "usd",
customer: user.stripe_id,
payment_method: payment_method_id,
off_session: true, # Charge without active user session
confirm: true,
description: "Penalty for not completing the task: #{note.title}"
})
else
Rails.logger.error "No payment method found for user #{user.id}"
end
end
end
8 changes: 7 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
<%= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<!-- jQuery UI -->
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
</head>

<body>
Expand Down
21 changes: 20 additions & 1 deletion app/views/notes/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,36 @@
Edit Note
</h1>
<%= form_with model: @note, local: true, class: "space-y-6" do |form| %>

<div class="flex flex-col">
<%= form.label :title, class: "text-lg font-semibold text-black uppercase tracking-wide" %>
<%= form.text_field :title, class: "border-2 border-black px-4 py-2 bg-white text-black focus:outline-none focus:border-gray-700" %>
</div>

<div class="flex flex-col">
<%= form.label :content, class: "text-lg font-semibold text-black uppercase tracking-wide" %>
<%= form.text_area :content, class: "border-2 border-black px-4 py-2 bg-white text-black focus:outline-none focus:border-gray-700 h-48" %>
</div>

<div class="flex flex-col max-w-xs">
<%= form.label :deadline, class: "text-lg font-semibold text-black uppercase tracking-wide" %>
<%= form.text_field :deadline, id: "datepicker", class: "border-2 border-black text-black bg-white focus:outline-none focus:border-gray-700 px-4 py-2" %>
</div>

<div>
<%= form.submit 'Update Note', class: "bg-black text-white px-6 py-2 uppercase font-bold tracking-wider hover:bg-gray-800 transition-all cursor-pointer" %>
</div>

<% end %>

<%= link_to 'Back to Notes', notes_path, class: "text-black underline hover:text-gray-800 mt-6 inline-block" %>
</div>
</div>

<script>
$(document).ready(function() {
$("#datepicker").datepicker({
dateFormat: 'yy-mm-dd',
minDate: 1
});
});
</script>

0 comments on commit 73c4c67

Please sign in to comment.