module Mks module Rate class ServiceBundlesController < ApplicationController before_action :set_service_bundle, only: %i[show update services add_services] before_action :set_bundle_service, only: %i[add_services] def index @service_bundles = ServiceBundle.all render json: @service_bundles end def sdu_bundles result = ServiceBundle.where(service_delivery_unit_id: params[:id]) render json: result end def show render json: @service_bundle end def create @service_bundle = ServiceBundle.new(service_bundle_params) if @service_bundle.save render json: @service_bundle, status: :created, location: @service_bundle else render json: @service_bundle.errors, status: :unprocessable_entity end end def update if @service_bundle.update(service_bundle_params) render json: @service_bundle else render json: @service_bundle.errors, status: :unprocessable_entity end end def filter search = "%#{params[:search].downcase}%" bundles = ServiceBundle.where('lower(mks_rate_service_bundles.name) LIKE ?', search) render json: bundles end def services render json: @service_bundle.chargeable_services end def add_services result = @service.add_services(@service_bundle, add_services_params[:ids]) render json: { success: result } end private def set_service_bundle @service_bundle = ServiceBundle.find(params[:id]) end def set_bundle_service @service = ServiceBundleService.new end def add_services_params params.permit(ids: []) end def service_bundle_params params.require(:service_bundle).permit(:code, :name, :description, :percent_discount, :service_delivery_unit_id) end end end end