Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ Feat ] Cobranca assinatura premium #243

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/jobs/billing_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class BillingJob < ApplicationJob
queue_as :default

def perform(subscription)
subscription.billings.create(amount: 19.90)
end
end
13 changes: 13 additions & 0 deletions app/models/billing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Billing < ApplicationRecord
belongs_to :subscription

before_create :set_billing_date

private

def set_billing_date
return self.billing_date = subscription.start_date if subscription.start_date.day < 29

self.billing_date = subscription.start_date.next_month.beginning_of_month
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Galera, essa linha ficou sem cobertura de testes. Ela é realmente necessária pro código?

end
end
19 changes: 19 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
class Subscription < ApplicationRecord
SUBSCRIPTION_AMOUNT = 19.90
belongs_to :user
has_many :billings, dependent: :destroy

enum status: { inactive: 0, active: 10 }

after_update :create_billing, if: :active? && :saved_change_to_status?

def active!
self.start_date = Time.zone.now.to_date
super
Expand All @@ -11,4 +16,18 @@ def inactive!
self.start_date = nil
super
end

private

def create_billing
return if start_date.nil?

billing_date = if start_date.day < 29
start_date + 1.month
else
start_date.next_month.beginning_of_month + 1.month
end

billings.create(billing_date:, amount: SUBSCRIPTION_AMOUNT)
end
end
11 changes: 11 additions & 0 deletions db/migrate/20240214211350_create_billings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateBillings < ActiveRecord::Migration[7.1]
def change
create_table :billings do |t|
t.decimal :amount, precision: 8, scale: 2, null: false
t.references :subscription, null: false, foreign_key: true
t.date :billing_date, null: false

t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions spec/factories/billings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :billing do
amount { 19.901 }
subscription { nil }
billing_date { '2024-02-14' }
end
end
6 changes: 3 additions & 3 deletions spec/factories/subscriptions.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FactoryBot.define do
factory :subscription do
user
start_date { nil }
status { 0 }
user { nil }
start_date { '2024-02-14' }
status { 10 }
end
end
16 changes: 16 additions & 0 deletions spec/jobs/premium_user_receives_monthly_billing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'rails_helper'

RSpec.describe BillingJob, type: :job do
context 'usuário premium' do
it 'recebe cobrança de pagamento mensal' do
user = User.create!(full_name: 'nome completo', password: 'testes',
email: '[email protected]', citizen_id_number: '13266529073')
user.subscription.active!
user.subscription.update(start_date: '2024-02-10')

perform_enqueued_jobs

expect(Billing.count).to eq 1
end
end
end
5 changes: 5 additions & 0 deletions spec/models/billing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Billing, type: :model do
# pending "add some examples to (or delete) #{__FILE__}"
end
6 changes: 4 additions & 2 deletions spec/models/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
RSpec.describe Subscription, type: :model do
describe '#active!' do
it 'atualiza data de início automaticamente' do
subscription = create(:subscription, status: :inactive, start_date: nil)
user = create(:user, :free)
subscription = user.subscription

subscription.active!

Expand All @@ -14,7 +15,8 @@

describe '#active!' do
it 'atualiza data de início automaticamente' do
subscription = create(:subscription, status: :active, start_date: Time.zone.now)
user = create(:user, :free)
subscription = user.subscription

subscription.inactive!

Expand Down
Loading