Class: GfreshPoint::Usecase::ConsumePointUsecase

Inherits:
BaseUsecase
  • Object
show all
Defined in:
lib/gfresh_point/usecase/consume_point_usecase.rb

Instance Attribute Summary

Attributes inherited from BaseUsecase

#repo

Instance Method Summary collapse

Methods inherited from BaseUsecase

#call, #generate_response, #initialize

Constructor Details

This class inherits a constructor from GfreshPoint::Usecase::BaseUsecase

Instance Method Details

#execute(request) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/gfresh_point/usecase/consume_point_usecase.rb', line 4

def execute(request)
  last_balance = repo.get_user_last_balance(request.app_id, request.user_id).try(:balance).to_i
  if request.force
    raise "not enough point" if request.point > last_balance
    consumed_point = request.point
  else
    consumed_point = [request.point, last_balance].min
  end
  # save balance and rule snapshot
  if consumed_point > 0
    repo.create_balance(request.app_id, request.user_id, consumed_point * -1, last_balance - consumed_point, request.event_name, request.origin_id, request.comment)
  end
  consumed_point
end