Skip to content

Commit b223796

Browse files
committed
fix/merge conflicts
1 parent 6f1d776 commit b223796

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

app/models/subscription.rb

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
class Subscription < ApplicationRecord
2+
SUBSCRIPTION_AMOUNT = 19.90
23
belongs_to :user
34
has_many :billings, dependent: :destroy
45

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

7-
after_update :enqueue_billing_job, if: :active? && :saved_change_to_status?
8+
after_update :create_billing, if: :active? && :saved_change_to_status?
89

910
def active!
10-
self.start_date = Time.zone.now
11+
self.start_date = Time.zone.now.to_date
1112
super
1213
end
1314

@@ -18,8 +19,15 @@ def inactive!
1819

1920
private
2021

21-
def enqueue_billing_job
22-
billing_date = start_date.to_datetime + 1.month
23-
BillingJob.set(wait_until: billing_date).perform_later(self)
22+
def create_billing
23+
return if start_date.nil?
24+
25+
billing_date = if start_date.day < 29
26+
start_date + 1.month
27+
else
28+
start_date.next_month.beginning_of_month + 1.month
29+
end
30+
31+
billings.create(billing_date:, amount: SUBSCRIPTION_AMOUNT)
2432
end
2533
end

spec/factories/billings.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FactoryBot.define do
22
factory :billing do
3-
amount { "9.99" }
3+
amount { 19.901 }
44
subscription { nil }
5-
billing_date { "2024-02-14" }
5+
billing_date { '2024-02-14' }
66
end
77
end

spec/jobs/premium_user_receives_monthly_billing_spec.rb

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
perform_enqueued_jobs
1212

13-
debugger
14-
1513
expect(Billing.count).to eq 1
1614
end
1715
end

spec/models/billing_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require 'rails_helper'
22

33
RSpec.describe Billing, type: :model do
4-
pending "add some examples to (or delete) #{__FILE__}"
4+
# pending "add some examples to (or delete) #{__FILE__}"
55
end

spec/models/subscription_spec.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
RSpec.describe Subscription, type: :model do
44
describe '#active!' do
55
it 'atualiza data de início automaticamente' do
6-
subscription = create(:subscription, status: :inactive, start_date: nil)
6+
user = create(:user, :free)
7+
subscription = user.subscription
78

89
subscription.active!
910

@@ -14,7 +15,8 @@
1415

1516
describe '#active!' do
1617
it 'atualiza data de início automaticamente' do
17-
subscription = create(:subscription, status: :active, start_date: Time.zone.now)
18+
user = create(:user, :free)
19+
subscription = user.subscription
1820

1921
subscription.inactive!
2022

0 commit comments

Comments
 (0)