summaryrefslogtreecommitdiff
path: root/app/controllers/admin/broadcast_messages_controller.rb
blob: d641a26c9fb1fca43ce5f561b1f5cd81c8f7b5de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# frozen_string_literal: true

module Admin
  class BroadcastMessagesController < ApplicationController
    include BroadcastMessagesHelper

    before_action :find_broadcast_message, only: [:edit, :update, :destroy]
    before_action :find_broadcast_messages, only: [:index, :create]
    before_action :push_features, only: [:index, :edit]

    feature_category :onboarding
    urgency :low

    def index
      @broadcast_message = BroadcastMessage.new
    end

    def edit
    end

    def create
      @broadcast_message = BroadcastMessage.new(broadcast_message_params)
      success = @broadcast_message.save

      respond_to do |format|
        format.json do
          if success
            render json: @broadcast_message, status: :ok
          else
            render json: { errors: @broadcast_message.errors.full_messages }, status: :bad_request
          end
        end
        format.html do
          if success
            redirect_to admin_broadcast_messages_path, notice: _('Broadcast Message was successfully created.')
          else
            render :index
          end
        end
      end
    end

    def update
      success = @broadcast_message.update(broadcast_message_params)

      respond_to do |format|
        format.json do
          if success
            render json: @broadcast_message, status: :ok
          else
            render json: { errors: @broadcast_message.errors.full_messages }, status: :bad_request
          end
        end
        format.html do
          if success
            redirect_to admin_broadcast_messages_path, notice: _('Broadcast Message was successfully updated.')
          else
            render :edit
          end
        end
      end
    end

    def destroy
      @broadcast_message.destroy

      respond_to do |format|
        format.html { redirect_back_or_default(default: { action: 'index' }) }
        format.js { head :ok }
      end
    end

    def preview
      @broadcast_message = BroadcastMessage.new(broadcast_message_params)
      render partial: 'admin/broadcast_messages/preview'
    end

    protected

    def find_broadcast_message
      @broadcast_message = BroadcastMessage.find(params[:id])
    end

    def find_broadcast_messages
      @broadcast_messages = BroadcastMessage.order(ends_at: :desc).page(params[:page]) # rubocop: disable CodeReuse/ActiveRecord
    end

    def broadcast_message_params
      params.require(:broadcast_message)
        .permit(%i[
                  theme
                  ends_at
                  message
                  starts_at
                  target_path
                  broadcast_type
                  dismissable
                ], target_access_levels: []).reverse_merge!(target_access_levels: [])
    end

    def push_features
      push_frontend_feature_flag(:role_targeted_broadcast_messages, current_user)
    end
  end
end