Skip to content

Instantly share code, notes, and snippets.

@eqbal eqbal/tbd.diff
Created Sep 24, 2018

Embed
What would you like to do?
diff --git a/app/concepts/affiliate_commission/contract/create_offsetting_commission.rb b/app/concepts/affiliate_commission/contract/create_offsetting_commission.rb
new file mode 100644
index 0000000000..f3104037ee
--- /dev/null
+++ b/app/concepts/affiliate_commission/contract/create_offsetting_commission.rb
@@ -0,0 +1,44 @@
+require "reform/form/dry"
+
+module AffiliateCommission::Contract
+ class CreateOffsettingCommission < Reform::Form
+
+ VALID_STATUS = ["canceled", "paid and reversed"]
+
+ property :affiliate
+ property :commission_plan_schedule
+ property :status
+ property :amount
+ property :due_at
+ property :purchase
+ property :original_commission
+
+ validation do
+ configure do
+ def greater_than_zero?(value)
+ value > 0
+ end
+ end
+
+ required(:amount).filled(:greater_than_zero?)
+
+ required(:affiliate).filled
+ required(:commission_plan_schedule).filled
+ end
+
+ #validate :affiliate, presence: true
+ #validate :commission_plan_schedule, presence: true
+
+ #validate :amount, numericality: { greater_than: 0 }
+
+ #validate :status do
+ #return if VALID_STATUS.include?(status)
+ #errors.add(:status, "Not a valid status")
+ #end
+
+ #validate :status_changed do
+ #return unless status_changed?
+ #errors.add(:status, "Status has not been changed")
+ #end
+ end
+end
diff --git a/app/concepts/affiliate_commission/create_offsetting_commission.rb b/app/concepts/affiliate_commission/create_offsetting_commission.rb
index b4d4380d86..315ab8fbb5 100644
--- a/app/concepts/affiliate_commission/create_offsetting_commission.rb
+++ b/app/concepts/affiliate_commission/create_offsetting_commission.rb
@@ -1,38 +1,33 @@
-class AffiliateCommission::CreateOffsettingCommission
- VALID_STATUS = ["canceled", "paid and reversed"]
+class AffiliateCommission::CreateOffsettingCommission < Trailblazer::Operation
+ step Model(AffiliateCommission, :new)
- def initialize(model)
- @model = model
- end
+ step Contract::Build(
+ constant: AffiliateCommission::Contract::CreateOffsettingCommission
+ )
- def call
- return unless valid_amount?
- return unless valid_status?
+ step :update_status
+ step :update_amount
+ step :link_original_affliate_commission_model
+ step :update_due_at_time
- create_offsetting_commission_record
- rescue => e
- Bugsnag.notify(e)
- end
+ step Contract::Validate()
+ step Contract::Persist()
private
- attr_accessor :model
-
- def create_offsetting_commission_record
- offsetting_commission = model.dup
- offsetting_commission.status = 'pending'
- offsetting_commission.amount = offsetting_commission.amount * -1
- offsetting_commission.original_commission_id = model.id
- offsetting_commission.due_at = Time.now.in_time_zone
- offsetting_commission.save
- offsetting_commission
+
+ def update_status
+ options["model"].status = :pending
end
- def valid_amount?
- model.amount > 0
+ def update_amount
+ options["model"].amount = options["amount"].to_i * -1
end
- def valid_status?
- model.status_changed? && VALID_STATUS.include?(model.status)
+ def link_original_affliate_commission_model
+ options["model"].original_commission_id = options["id"]
end
-end
+ def update_due_at_time
+ options["model"].due_at = Time.now.in_time_zone
+ end
+end
diff --git a/app/models/affiliate_commission.rb b/app/models/affiliate_commission.rb
index 37a8afad45..4d464220c9 100644
--- a/app/models/affiliate_commission.rb
+++ b/app/models/affiliate_commission.rb
@@ -158,7 +158,12 @@ class AffiliateCommission < ActiveRecord::Base
end
def create_offsetting_commission
- offsetting_commission = CreateOffsettingCommission.new(self).call
+
+ puts "-------------"
+ puts attributes.inspect
+
+ offsetting_commission = CreateOffsettingCommission.call(params: attributes)
+
return unless offsetting_commission.present?
if status == 'canceled'
diff --git a/spec/app/concepts/affiliate_commission/contract/create_offsetting_commission.rb b/spec/app/concepts/affiliate_commission/contract/create_offsetting_commission.rb
new file mode 100644
index 0000000000..67980fa250
--- /dev/null
+++ b/spec/app/concepts/affiliate_commission/contract/create_offsetting_commission.rb
@@ -0,0 +1,30 @@
+module AffiliateCommission::Contract
+ class CreateOffsettingCommission < Reform::Form
+
+ VALID_STATUS = ["canceled", "paid and reversed"]
+
+ property :affiliate
+ property :commission_plan_schedule
+ property :status
+ property :amount
+ property :due_at
+ property :purchase
+ property :original_commission
+
+
+ validates :affiliate, presence: true
+ validates :commission_plan_schedule, presence: true
+
+ validate :amount, numericality: { greater_than: 0 }
+
+ validate :status do
+ return if VALID_STATUS.include?(status)
+ errors.add(:status, "Not valid status")
+ end
+
+ validate :status_changed do
+ return unless status_changed?
+ errors.add(:status, "Status has not been changed")
+ end
+ end
+end
diff --git a/spec/app/concepts/affiliate_commission/contract/create_offsetting_commission_spec.rb b/spec/app/concepts/affiliate_commission/contract/create_offsetting_commission_spec.rb
new file mode 100644
index 0000000000..8b13789179
--- /dev/null
+++ b/spec/app/concepts/affiliate_commission/contract/create_offsetting_commission_spec.rb
@@ -0,0 +1 @@
+
@emaglio

This comment has been minimized.

Copy link

emaglio commented Sep 25, 2018

Opening this with my phone but this looks good.
The validation in the contract needs an improvement, there is a build-in predicated to validate numerically greater than zero and also the status validations needs to follow the dry-v validation.
Jason and Sean can help you with that and the web docs is not too bad.

It doesn’t look that more readable because it’s not used a TRB context so it’s just called in a model and by itself seems not that clear which for me it actually not that true. You can just read the steps to see what in high level the operation does and understand way easier the logic instead of chain of method calls with if/else or returns.
Also ones the whole logic in the model is translated in TRB you can nest few operation and easily debugging it using the trace feature.
I might be able to open this with a computer and leave a more technical comment.
Good job so for

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.