Skip to content

Commit

Permalink
sync payeeorgan
Browse files Browse the repository at this point in the history
  • Loading branch information
qinmingyuan committed Nov 22, 2024
1 parent 156c791 commit 2797487
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/models/wechat/model/payee_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Model::PayeeApp
attribute :appid, :string, index: true
attribute :enabled, :boolean, default: true

belongs_to :organ, class_name: 'Org::Organ', optional: true
belongs_to :app, foreign_key: :appid, primary_key: :appid, optional: true
belongs_to :payee, foreign_key: :mch_id, primary_key: :mch_id, optional: true

Expand All @@ -16,9 +17,14 @@ module Model::PayeeApp

validates :appid, uniqueness: { scope: :mch_id }

before_validation :sync_organ, if: -> { mch_id_changed? }
after_update :set_enabled, if: -> { enabled? && saved_change_to_enabled? }
end

def sync_organ
self.organ_id = payee.organ_id
end

def set_enabled
self.class.where.not(id: self.id).where(appid: self.appid).update_all(enabled: false)
end
Expand Down

0 comments on commit 2797487

Please sign in to comment.