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

wip: paid subscriptions system and articles #467

Open
wants to merge 84 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
01e20d1
wip: begin of migration creation
DioFun Jun 20, 2024
5a8e0df
feat: creating migration (work in progress)
benoitlx Jun 20, 2024
f76fb69
fix: relations that wasn't well defined, fixtures fix
DioFun Jun 21, 2024
fda8cd9
Remove user_id from subscription and get subscriptions through the sa…
DioFun Jul 1, 2024
9f884e5
fix: through association was define after a call to it
DioFun Jul 3, 2024
472b9a8
Add subscriptions modules routes
DioFun Jul 4, 2024
c82f46e
fix rubocop problems
DioFun Jul 4, 2024
8956ebc
feat: creation and deletion of articles
DioFun Jul 4, 2024
09e8c50
ref: change soft_delete! to soft_delete
DioFun Jul 4, 2024
a27935a
Deletion of subscriptions controller
DioFun Jul 4, 2024
97ffc49
comment unusefull tests & try to fix those that did not passed
DioFun Jul 4, 2024
adbc98c
Comment outdated tests, fix existing that should pass
DioFun Jul 5, 2024
ffbccc5
fix: add locals
DioFun Jul 5, 2024
6681798
fix: add locals
DioFun Jul 5, 2024
ca250bd
test: add some test and fix problems related
DioFun Jul 5, 2024
66907ce
test: add more tests
DioFun Jul 5, 2024
b64f9ec
fix: let rails issue errors when trying to destroy a dependant article
DioFun Jul 5, 2024
0e731a6
feat: manage subscription offers with tests impleted
DioFun Jul 5, 2024
6d236ec
fix: mistake
DioFun Jul 5, 2024
6ba8bd1
wip: sales implementation
DioFun Jul 5, 2024
85f996d
wip: dynamically add articles to a sale using StimulusJS
nymous Jul 5, 2024
4d14f17
wip: generate invoice pdf with hexapdf
D0gmaDev Jul 7, 2024
518d6c6
wip: add subscription to sale
DioFun Jul 8, 2024
152a8b5
fix(test): update fixtures regarding the column renaming of the last …
DioFun Jul 8, 2024
2d9083e
feat(paymentMethods): add, remove payment methods
DioFun Jul 10, 2024
5e49aef
wip: create sale, todo: implement invoice creation
DioFun Jul 10, 2024
63a2e1d
refactor(subscription_offer&article): make new params shorter
DioFun Jul 10, 2024
9692009
Merge branch 'master' into subscriptions
DioFun Jul 10, 2024
7c24259
fix: fix rubocop outages
DioFun Jul 10, 2024
defe95b
Lock file up to date
DioFun Jul 10, 2024
434a130
test(paymentMethod): add test for payment method controller and model
DioFun Jul 10, 2024
58dff5c
test: add redirection assertion to Articles, Subscription offers and …
DioFun Jul 10, 2024
b1997a0
fix(locals): specify locals variable in partials
DioFun Jul 10, 2024
48abb03
test(paymentMethod): fix should soft-delete payment_method
DioFun Jul 10, 2024
368a182
feat(generate_invoices): return output as stream
D0gmaDev Jul 10, 2024
28bd61c
Merge branch 'document_generator' into subscriptions
DioFun Jul 11, 2024
4590d97
wip: sales invoices
DioFun Jul 11, 2024
c956e56
feat(sales): finish sale feature (need to be refactored)
DioFun Jul 11, 2024
eb89235
fix(saleForm): add event on select change
DioFun Jul 15, 2024
a0d683b
refactor(sales): refactor sales_creation
DioFun Jul 15, 2024
b9db1cd
fix(settingsFixture): add suitable parameters
DioFun Jul 15, 2024
d2391e9
refactor(sales): add default_scope for article, subscription_offer an…
DioFun Jul 16, 2024
e112313
fix(tests): fix tests broke by last commit & advised change in review
DioFun Jul 16, 2024
4e2e840
fix(saleView): add min on duration field
DioFun Jul 16, 2024
fee752c
test: add a test for soft deletion & check deleted_at not nil for sof…
DioFun Jul 17, 2024
06ad62e
fix(sales): fix sales reation
DioFun Jul 19, 2024
12e32fd
refactor(invoiceSaleCreation): change name and place of the invoice h…
DioFun Jul 19, 2024
6cae8a0
feat(subscriptions): create subscription on sale creation
DioFun Jul 19, 2024
012f6bd
fix(invoices): multiple pages pdf
D0gmaDev Jul 19, 2024
5b1bff1
fix(setting): rename last_invoice_id key to next_invoice_id
DioFun Jul 19, 2024
8a5a714
refactor(sales): improve readability
D0gmaDev Jul 21, 2024
6d3196f
test: write some invoice tests
D0gmaDev Jul 21, 2024
74232b0
test(invoices): write generator service tests
D0gmaDev Jul 21, 2024
ef231da
test(articles_sales): create test for articles_sale model
DioFun Aug 2, 2024
a85b030
fix(sales): ensure duration is positive
D0gmaDev Aug 2, 2024
d7bbf2c
test(sales): start sales_controller tests
D0gmaDev Aug 2, 2024
604b731
test(sales_controller): test if sales is not processable and duration…
DioFun Aug 2, 2024
470c527
test(sales_controller): create test for invalid payment_method
DioFun Aug 3, 2024
1768f59
fix(subscription): comment out cancel method
DioFun Aug 3, 2024
e5b05ec
test(sale): add test for sale model
DioFun Aug 3, 2024
8894741
fix(subscription): comment out unuseful methods
DioFun Aug 3, 2024
720c7d2
fix(sale): generate_sales_subscription_offers now returns true
DioFun Aug 3, 2024
1a799a5
test(sale): test if subscription offers are not exhaustive
DioFun Aug 3, 2024
97308cb
refactor: rework the sale and invoice flow
nymous Nov 1, 2024
5a8745d
fix: add DB unique index to prevent duplicate ArticlesSales & show pr…
nymous Nov 2, 2024
62a1671
chore: use create_join_table instead of manual tables
nymous Jan 19, 2025
f7612cc
fix: make duration and price strictly positive
nymous Jan 19, 2025
1e45760
fix: remove total_price from sale/refund to compute it on the fly
nymous Jan 19, 2025
9a89c40
refactor: make destroy/soft-delete more readable
nymous Jan 19, 2025
30781cd
fix: improve sale subscription offer computation & docs
nymous Jan 19, 2025
5e1cdc2
chore: remove unused comments
nymous Jan 19, 2025
94969bd
ci: allow 'chore' commit type in overcommit
nymous Jan 19, 2025
a581ec2
fix: make Sale#total_price faster
nymous Jan 19, 2025
f32b650
docs: comment Sale.generate_sales_subscription_offers
nymous Jan 19, 2025
941e461
chore: add DejaVu font license
nymous Jan 19, 2025
26df468
fix: use number_to_currency instead of manual currency formatting
nymous Jan 19, 2025
c92c379
feat: protect Dashboard SQL queries with current user ability
nymous Jan 19, 2025
bc40946
feat: add frontend validation for article & subscription fields
nymous Jan 19, 2025
d786742
fix: automatically pluralize subscription 'months' duration
nymous Jan 19, 2025
05f125a
fix: make invoice JSON not nullable
nymous Jan 19, 2025
579bca7
test: make fixtures more realistic
nymous Jan 26, 2025
3d6d1ab
fix: fix Rails not being able to delete many-to-many associations
nymous Jan 26, 2025
a755954
test: fix realistic fixtures to only have 1 article
nymous Jan 26, 2025
e825b68
test: fix sale setup to have new virtual attributes
nymous Jan 26, 2025
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
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ gem 'cancancan', '~> 3.6'
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

# Generate pdf files [https://github.com/gettalong/hexapdf]
gem 'hexapdf', '~> 0.40.0'

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: [:mri, :mswin, :mswin64, :mingw, :x64_mingw]
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ GEM
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
childprocess (5.0.0)
cmdparse (3.0.7)
coderay (1.1.3)
concurrent-ruby (1.3.3)
connection_pool (2.4.1)
Expand All @@ -126,6 +127,7 @@ GEM
net-http
ffi (1.16.3)
formatador (1.1.0)
geom2d (0.4.1)
globalid (1.2.1)
activesupport (>= 6.1)
guard (2.18.1)
Expand All @@ -143,6 +145,10 @@ GEM
minitest (>= 3.0)
hashdiff (1.1.0)
hashie (5.0.0)
hexapdf (0.40.0)
cmdparse (~> 3.0, >= 3.0.3)
geom2d (~> 0.4, >= 0.4.1)
openssl (>= 2.2.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
importmap-rails (1.2.3)
Expand Down Expand Up @@ -232,6 +238,7 @@ GEM
tzinfo
validate_url
webfinger (~> 2.0)
openssl (3.2.0)
overcommit (0.63.0)
childprocess (>= 0.6.3, < 6)
iniparse (~> 1.4)
Expand Down Expand Up @@ -423,6 +430,7 @@ DEPENDENCIES
error_highlight (>= 0.4.0)
guard (~> 2.18)
guard-minitest (~> 2.4)
hexapdf (~> 0.40.0)
importmap-rails (~> 1.2)
ipaddress (~> 0.8.3)
jbuilder (~> 2.12)
Expand Down
Binary file added app/assets/fonts/DejaVuSans-Bold.ttf
nymous marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
Binary file added app/assets/fonts/DejaVuSans.ttf
Binary file not shown.
Binary file added app/assets/images/rezoleo_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions app/controllers/admin/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Admin
class ArticlesController < ApplicationController
def new
@article = Article.new
authorize! :new, @article
end

def create
@article = Article.new(article_params)
Copy link
Member

Choose a reason for hiding this comment

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

If we receive the price as a "human" number (5.00 €), we need to multiply it here to get cents or else the validation will not be happy with a float.

authorize! :create, @article
if @article.save
flash[:success] = "Article #{article_params[:name]} created!"
redirect_to admin_path
else
render 'new', status: :unprocessable_entity
end
end

def destroy
@article = Article.find(params[:id])
authorize! :destroy, @article
@article.soft_delete unless @article.destroy
nymous marked this conversation as resolved.
Show resolved Hide resolved
redirect_to admin_path
end

def article_params
params.require(:article).permit(:price, :name)
end
end
end
12 changes: 12 additions & 0 deletions app/controllers/admin/dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Admin
class DashboardController < ApplicationController
def index
authorize! :manage, :all
nymous marked this conversation as resolved.
Show resolved Hide resolved
@articles = Article.where(deleted_at: nil).order(created_at: :desc)
@subscription_offers = SubscriptionOffer.where(deleted_at: nil).order(created_at: :desc)
@payment_methods = PaymentMethod.where(deleted_at: nil).order(created_at: :desc)
DioFun marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
32 changes: 32 additions & 0 deletions app/controllers/admin/payment_methods_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Admin
class PaymentMethodsController < ApplicationController
def new
@payment_method = PaymentMethod.new
authorize! :new, @payment_method
end

def create
@payment_method = PaymentMethod.new(payment_method_params)
authorize! :create, @payment_method
if @payment_method.save
flash[:success] = "Payment method #{payment_method_params[:name]} created!"
redirect_to admin_path
else
render 'new', status: :unprocessable_entity
end
end

def destroy
@payment_method = PaymentMethod.find(params[:id])
authorize! :destroy, @payment_method
@payment_method.soft_delete unless @payment_method.destroy
nymous marked this conversation as resolved.
Show resolved Hide resolved
redirect_to admin_path
end

def payment_method_params
params.require(:payment_method).permit(:name, :auto_verify)
end
end
end
32 changes: 32 additions & 0 deletions app/controllers/admin/subscription_offers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Admin
class SubscriptionOffersController < ApplicationController
def new
@subscription_offer = SubscriptionOffer.new
authorize! :new, @subscription_offer
end

def create
@subscription_offer = SubscriptionOffer.new(subscription_offer_params)
authorize! :create, @subscription_offer
if @subscription_offer.save
flash[:success] = "Subscription offer for #{subscription_offer_params[:duration]} months created!"
redirect_to admin_path
else
render 'new', status: :unprocessable_entity
end
end

def destroy
@subscription_offer = SubscriptionOffer.find(params[:id])
authorize! :destroy, @subscription_offer
@subscription_offer.soft_delete unless @subscription_offer.destroy
nymous marked this conversation as resolved.
Show resolved Hide resolved
redirect_to admin_path
end

def subscription_offer_params
params.require(:subscription_offer).permit(:price, :duration)
end
end
end
4 changes: 4 additions & 0 deletions app/controllers/refunds_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class RefundsController < ApplicationController
end
81 changes: 81 additions & 0 deletions app/controllers/sales_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

class SalesController < ApplicationController
before_action :owner, only: [:new, :create]

def new
@sale = @owner.sales_as_client.new
@sale.articles_sales.new
@articles = Article.where(deleted_at: nil)
@subscription_offers = SubscriptionOffer.where(deleted_at: nil).order(duration: :desc)
@payment_methods = PaymentMethod.where(deleted_at: nil)
authorize! :new, @sale

Check warning on line 12 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L7-L12

Added lines #L7 - L12 were not covered by tests
end

# rubocop:disable Metrics/AbcSize
def create
@sale = @owner.sales_as_client.new(reformated_params)
@sale.update_total_price
nymous marked this conversation as resolved.
Show resolved Hide resolved
@sale.seller = current_user

Check warning on line 19 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L17-L19

Added lines #L17 - L19 were not covered by tests
if @sale.total_price.zero?
flash.now[:error] = "You can't process an empty sale!"
return render :new, status: :unprocessable_entity

Check warning on line 22 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L22

Added line #L22 was not covered by tests
end
@sale.verify if @sale.payment_method.auto_verify
@sale.gen_temp_invoice
authorize! :create, @sale
if @sale.save

Check warning on line 27 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L25-L27

Added lines #L25 - L27 were not covered by tests
name = @sale.generate_invoice_id
@sale.invoice.pdf.attach(io: @sale.invoice.generate, filename: name, content_type: 'application/pdf')
@sale.save!
DioFun marked this conversation as resolved.
Show resolved Hide resolved
flash[:success] = 'Sale was successfully created.'
redirect_to @owner

Check warning on line 32 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L29-L32

Added lines #L29 - L32 were not covered by tests
else
render :new, status: :unprocessable_entity
end
end
# rubocop:enable Metrics/AbcSize

private

def owner
@owner = User.find(params[:user_id])

Check warning on line 42 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L42

Added line #L42 was not covered by tests
end

def sales_params
params.require(:sale).permit(:duration, :payment_method_id, articles_sales_attributes: [:article_id, :quantity])

Check warning on line 46 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L46

Added line #L46 was not covered by tests
end

def reformated_params
nymous marked this conversation as resolved.
Show resolved Hide resolved
par = sales_params
par[:sales_subscription_offers_attributes] = duration_to_subscription_offers sales_params[:duration].to_i
par.delete(:duration)

Check warning on line 52 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L50-L52

Added lines #L50 - L52 were not covered by tests
par[:articles_sales_attributes]&.each do |rec|
par[:articles_sales_attributes].delete(rec[0]) if rec[1][:quantity].to_i.zero?
end
par

Check warning on line 56 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L56

Added line #L56 was not covered by tests
end

def duration_to_subscription_offers(duration)
tab = []
subscription_offers = SubscriptionOffer.where(deleted_at: nil).order(duration: :desc)

Check warning on line 61 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L60-L61

Added lines #L60 - L61 were not covered by tests
if subscription_offers.empty?
flash[:error] = 'There are no subscription offers registered!'
return false

Check warning on line 64 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L64

Added line #L64 was not covered by tests
end
subscription_offers.each do |offer|

Check warning on line 66 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L66

Added line #L66 was not covered by tests
break if duration.zero?

quantity = duration / offer.duration

Check warning on line 69 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L69

Added line #L69 was not covered by tests
if quantity.positive?
tab << { subscription_offer_id: offer.id, quantity: quantity }
duration -= quantity * offer.duration

Check warning on line 72 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L72

Added line #L72 was not covered by tests
end
end
unless duration.zero?
flash[:error] = 'Subscription offers are not exhaustive!'
return false

Check warning on line 77 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L77

Added line #L77 was not covered by tests
end
tab

Check warning on line 79 in app/controllers/sales_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/sales_controller.rb#L79

Added line #L79 was not covered by tests
end
end
38 changes: 0 additions & 38 deletions app/controllers/subscriptions_controller.rb

This file was deleted.

6 changes: 6 additions & 0 deletions app/helpers/admin/articles_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Admin
module ArticlesHelper
end
end
6 changes: 6 additions & 0 deletions app/helpers/admin/dashboard_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Admin
module DashboardHelper
end
end
6 changes: 6 additions & 0 deletions app/helpers/admin/payment_methods_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Admin
module PaymentMethodsHelper
end
end
6 changes: 6 additions & 0 deletions app/helpers/admin/subscription_offers_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Admin
module SubscriptionOffersHelper
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true

module SubscriptionsHelper
module RefundsHelper
end
4 changes: 4 additions & 0 deletions app/helpers/sales_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module SalesHelper
end
Loading