Skip to content

Instantly share code, notes, and snippets.

@zeeshangulzar
Created July 19, 2020 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeeshangulzar/9d7ec87906bc209956b9b6326b821457 to your computer and use it in GitHub Desktop.
Save zeeshangulzar/9d7ec87906bc209956b9b6326b821457 to your computer and use it in GitHub Desktop.
accounts_controller.rb
class Api::AccountsController < ApiAuthenticationController
LOG_TAG = 'API_ACCOUNTS'
INVALID_PRODUCT = 'INVALID_PRODUCT'
CREATING = 'creating'
OPEN = 'open'
DISCARDED = 'discarded'
CLOSED = 'closed'
CLOSED_STATES = %w(settled)
DISCARDED_STATES = %w(deleted rejected)
CREATING_STATES = %w(pending_disbursement disbursing funds_disbursed)
OPEN_STATES = %w(disbursed striking dealing_with_unsettled_loan churned)
before_filter :validate_and_set_product
before_filter :log_request
before_filter :validate_member
before_filter :get_account
skip_before_filter :verify_authenticity_token
skip_before_filter :ensure_logged_in
def status
respond_to do |format|
format.json {render json: response_hash(@loan), status: 200}
end
end
private
def validate_and_set_product
product_url = params['product']
msisdn = params['msisdn']
@product = Product.url_product_map[product_url]
return if @product.present?
log(LOG_TAG, INVALID_PRODUCT, product: product_url, msisdn: msisdn)
head 400
end
def validate_member
msisdn = params['msisdn']
@member = MobileMember.locate(msisdn, partner.partner_code)
return if @member.present?
log(LOG_TAG, 'MEMBER_NOT_FOUND', msisdn: msisdn, product: @product.code)
head 404
end
def get_account
account_id = params['account_id']
@loan = MobileLoan.find_by_id_and_partner(account_id, partner.partner_code)
@loan = @member.current_loan if account_id.blank?
return if @loan.present?
log(LOG_TAG, 'ACCOUNT_NOT_EXIST', account_id: account_id, product: @product.code)
head 404
end
def log_request
NewRelic::Agent.add_custom_attributes(msisdn: params['msisdn'], product: @product.code)
log_params = params.reject { |param| %w(controller api_accounts).include?(param) }.symbolize_keys
log(LOG_TAG, **log_params)
end
def loan_status(status)
return CREATING if status.in? CREATING_STATES
return OPEN if status.in? OPEN_STATES
return CLOSED if status.in? CLOSED_STATES
return DISCARDED if status.in? DISCARDED_STATES
end
def response_hash(loan)
loan_due_date = loan.due_date || loan.initial_due_date
{
'status' => loan_status(loan.status),
'loan_amount' => loan.amount,
'created' => loan.created_at,
'due_date' => loan_due_date,
'term' => loan.duration,
}
end
# @return [Partner]
def partner
@product.partner
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment