module Mks module Rate class RateChangeRequestsController < ApplicationController before_action :set_rate_change_request, only: %i[show update approve] before_action :set_change_request_service, only: %i[create approve] def index @rate_change_requests = RateChangeRequest.all render json: @rate_change_requests end def show render json: @rate_change_request end def create rate_change_request, success = @service.create(rate_change_request_params, current_user) if success render json: Mks::Common::MethodResponse.success_response(rate_change_request), status: :created else render json: Mks::Common::MethodResponse.failure_response(rate_change_request), status: :unprocessable_entity end end def update if @rate_change_request.update(rate_change_request_params) render json: Mks::Common::MethodResponse.success_response(@rate_change_request) else render json: Mks::Common::MethodResponse.failure_response(@rate_change_request), status: :unprocessable_entity end end def approve if @service.approve(@rate_change_request, current_user) render json: Mks::Common::MethodResponse.success_response(@rate_change_request, 'Rate change request approved!') else render json: Mks::Common::MethodResponse.failure_response(@rate_change_request) end end private def set_change_request_service @service = RateChangeRequestService.new end def set_rate_change_request @rate_change_request = RateChangeRequest.find(params[:id]) end def rate_change_request_params params.require(:rate_change_request).permit(:reference_number, :authorized_by, :effective_on, :authorized_on, :status, :created_by_id, :approved_by_id, :remark, rates: %i[chargeable_service_id chargeable_service_name equipment_item_id equipment_item_name base_rate utilization_rate], ) end end end end