Created
June 11, 2015 21:02
-
-
Save macalinao/7816a67783028790c4c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Developers::Api::DynamicMailTemplatesController < Developers::Api::BaseController | |
model_class DynamicMailTemplate | |
before_filter :find_email_context, only: [:preview, :send_email] | |
# GET /developers/dynamic_mail_templates | |
# GET /developers/dynamic_mail_templates.json | |
def index | |
render json: DynamicMailTemplate.all.map { |d| api_serialize(d) } | |
end | |
# GET /developers/dynamic_mail_templates/preview | |
def preview | |
render json: { count: @users.length } | |
end | |
# POST /developers/dynamic_mail_templates/send | |
def send_email | |
DynamicMailerWorker::add_to_queue_sliced(@template.slug, @users, idempotent: false) | |
render json: { count: @users.length } | |
end | |
protected | |
def find_email_context | |
return if find_channel.blank? or find_template.blank? | |
target = params[:target] | |
@users = user_ids_for_target(target) | |
if @users.nil? | |
render json: { error: "Invalid target" } and return | |
end | |
end | |
def find_channel | |
channel_slug = params[:channel] | |
@channel = Channel.find_by_module_name(channel_slug) | |
if @channel.blank? | |
render json: { error: "Channel #{channel_slug} missing" } and return | |
end | |
@channel | |
end | |
def find_template | |
template_slug = params[:slug] | |
@template = DynamicMailTemplate.find_by_slug(template_slug) | |
if @template.blank? | |
render json: { error: "Template #{template_slug} missing" } and return | |
end | |
@template | |
end | |
def user_ids_for_target(target) | |
case @target | |
when "admins" | |
@channel.admin_ids | |
when "users" | |
@channel.live_channels.map(&:user_id) | |
else | |
nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment