-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from TreinaDev/feat/manda-cobranca-assinatura
[ FEAT ] Cobrança mensal da assinatura Premium
- Loading branch information
Showing
23 changed files
with
250 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class NotifyBillingJob < ApplicationJob | ||
queue_as :default | ||
|
||
before_perform do |job| | ||
current_billing = job.arguments.first | ||
next_billing = current_billing.subscription.billings.create!( | ||
billing_date: current_billing.billing_date + 1.month, amount: 19.90 | ||
) | ||
self.class.set(wait_until: next_billing.billing_date.to_datetime).perform_later(next_billing) | ||
end | ||
|
||
def perform(billing) | ||
BillingsMailer.with(billing:).notify_billing.deliver | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class BillingsMailer < ApplicationMailer | ||
default from: '[email protected]' | ||
|
||
def notify_billing | ||
@billing = params[:billing] | ||
@user = @billing.subscription.user | ||
|
||
mail(subject: default_i18n_subject, to: @user.email) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Billing < ApplicationRecord | ||
belongs_to :subscription | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h3><%= t('.greeting', recipient: @user.full_name)%></h3> | ||
|
||
<p><%= t('.reminder')%></p> | ||
|
||
<p><%= t('.call_to_action')%></p> | ||
|
||
<p><%= t('.due_date', due_date: I18n.l(@billing.billing_date)) %></p> | ||
|
||
<p><%= t('.amount', amount: number_to_currency(@billing.amount)) %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pt-BR: | ||
activerecord: | ||
models: | ||
billings: | ||
one: Cobrança | ||
other: Cobranças | ||
attributes: | ||
billings: | ||
billing_date: Data de Cobrança | ||
amount: Valor | ||
billings_mailer: | ||
notify_billing: | ||
subject: Porfoliorrr - Assinatura mensal | ||
greeting: Olá, %{recipient}! | ||
reminder: Sua assinatura mensal do Porfoliorrr Premium vence hoje | ||
call_to_action: Efetue o pagamento para continuar usando os benefícios da sua assinatura | ||
due_date: "Vencimento: %{due_date}" | ||
amount: "Valor: %{amount}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.references :subscription, null: false, foreign_key: true | ||
t.date :billing_date, null: false | ||
t.decimal :amount, precision: 8, scale: 2, null: false | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
FactoryBot.define do | ||
factory :subscription do | ||
user | ||
start_date { nil } | ||
start_date { Time.zone.now.to_date } | ||
status { 0 } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe NotifyBillingJob, type: :job do | ||
context 'envia email de cobrança ' do | ||
it 'para usuário com assinatura premium' do | ||
user = create(:user, :paid, full_name: 'Marcos Madeira', email: '[email protected]') | ||
billing = user.subscription.billings.first | ||
|
||
mail = double('mail', deliver: true) | ||
mailer = double('BillingsMailer', notifify_billing: mail) | ||
|
||
allow(BillingsMailer).to receive(:with).and_return(mailer) | ||
allow(mailer).to receive(:notify_billing).and_return(mail) | ||
|
||
NotifyBillingJob.perform_now(billing) | ||
|
||
expect(mail).to have_received(:deliver).once | ||
end | ||
|
||
it 'A cobrança é recriada para ser enviada via email no próximo mês' do | ||
user = create(:user, :paid, full_name: 'Marcos Madeira', email: '[email protected]') | ||
billing = user.subscription.billings.first | ||
|
||
expect { NotifyBillingJob.perform_now(billing) }.to have_enqueued_job(NotifyBillingJob).on_queue('default') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe BillingsMailer, type: :mailer do | ||
context '#notify_billing' do | ||
it 'envia e-mail de acordo com data de cobrança da assinatura' do | ||
user = create(:user, :paid, full_name: 'Marcos Madeira', email: '[email protected]') | ||
|
||
billing = user.subscription.billings.first | ||
|
||
mail = BillingsMailer.with(billing:).notify_billing | ||
|
||
expect(mail.subject).to eq 'Porfoliorrr - Assinatura mensal' | ||
expect(mail.to).to eq ['[email protected]'] | ||
expect(mail.from).to eq ['[email protected]'] | ||
expect(mail.body).to include 'Olá, Marcos Madeira!' | ||
expect(mail.body).to include 'Sua assinatura mensal do Porfoliorrr Premium vence hoje' | ||
expect(mail.body).to include 'Efetue o pagamento para continuar usando os benefícios da sua assinatura' | ||
expect(mail.body).to include "Vencimento: #{I18n.l(billing.billing_date)}" | ||
expect(mail.body).to include 'Valor: R$ 19,90' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,74 @@ | ||
require 'rails_helper' | ||
|
||
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) | ||
context 'assinatura de usuário premium' do | ||
it 'cria cobrança antes do dia 29' do | ||
user = create(:user, :free) | ||
|
||
subscription.active! | ||
travel_to Date.parse('2024-02-10') do | ||
user.subscription.active! | ||
end | ||
|
||
expect(subscription.start_date).to eq Time.zone.now.to_date | ||
expect(subscription).to be_active | ||
billing = Billing.first | ||
expect(Billing.count).to eq 1 | ||
expect(billing.billing_date).to eq Date.parse '2024-03-10' | ||
expect(billing.amount).to eq 19.90 | ||
end | ||
|
||
context 'recebe cobrança no primeiro dia do mês seguinte' do | ||
it 'quando assinatura for no dia 29' do | ||
user = create(:user, :free) | ||
|
||
travel_to Date.parse('2024-03-29') do | ||
user.subscription.active! | ||
end | ||
|
||
expect(Billing.count).to eq 1 | ||
expect(Billing.first.billing_date).to eq Date.parse '2024-05-01' | ||
end | ||
|
||
it 'quando assinatura for no dia 30' do | ||
user = create(:user, :free) | ||
|
||
travel_to Date.parse('2024-06-30') do | ||
user.subscription.active! | ||
end | ||
|
||
expect(Billing.count).to eq 1 | ||
expect(Billing.first.billing_date).to eq Date.parse '2024-08-01' | ||
end | ||
|
||
it 'quando assinatura for no dia 31' do | ||
user = create(:user, :free) | ||
|
||
travel_to Date.parse('2024-10-31') do | ||
user.subscription.active! | ||
end | ||
|
||
expect(Billing.count).to eq 1 | ||
expect(Billing.first.billing_date).to eq Date.parse '2024-12-01' | ||
end | ||
end | ||
describe '#active!' do | ||
it 'atualiza data de início automaticamente' do | ||
subscription = create(:subscription, status: :inactive, start_date: nil) | ||
|
||
subscription.active! | ||
|
||
expect(subscription.start_date).to eq Time.zone.now.to_date | ||
expect(subscription).to be_active | ||
end | ||
end | ||
end | ||
|
||
describe '#active!' do | ||
it 'atualiza data de início automaticamente' do | ||
subscription = create(:subscription, status: :active, start_date: Time.zone.now) | ||
describe '#active!' do | ||
it 'atualiza data de início automaticamente' do | ||
subscription = create(:subscription, status: :active, start_date: Time.zone.now) | ||
|
||
subscription.inactive! | ||
subscription.inactive! | ||
|
||
expect(subscription.start_date).to be_nil | ||
expect(subscription).to be_inactive | ||
expect(subscription.start_date).to be_nil | ||
expect(subscription).to be_inactive | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.