Feature: Mass award badge (#8694)

* UI: Mass grant a badge from the admin ui

* Send the uploaded CSV and badge ID to the backend

* Read the CSV and grant badge in batches

* UX: Communicate the result to the user

* Don't award if badge is disabled

* Create a 'send_notification' method to remove duplicated code, slightly shrink badge image. Replace router transition with href.

* Dynamically discover current route
This commit is contained in:
Roman Rizzi
2020-01-13 11:20:26 -03:00
committed by GitHub
parent eb105ba79d
commit d69c5eebcf
15 changed files with 291 additions and 34 deletions

View File

@@ -1,5 +1,7 @@
# frozen_string_literal: true
require 'csv'
class Admin::BadgesController < Admin::AdminController
def index
@@ -33,6 +35,38 @@ class Admin::BadgesController < Admin::AdminController
def show
end
def award
end
def mass_award
csv_file = params.permit(:file).fetch(:file, nil)
badge = Badge.find_by(id: params[:badge_id])
raise Discourse::InvalidParameters if csv_file.try(:tempfile).nil? || badge.nil?
batch_number = 1
batch = []
File.open(csv_file) do |csv|
csv.each_line do |email_line|
batch.concat CSV.parse_line(email_line)
# Split the emails in batches of 200 elements.
full_batch = csv.lineno % (BadgeGranter::MAX_ITEMS_FOR_DELTA * batch_number) == 0
last_batch_item = full_batch || csv.eof?
if last_batch_item
Jobs.enqueue(:mass_award_badge, user_emails: batch, badge_id: badge.id)
batch = []
batch_number += 1
end
end
end
head :ok
rescue CSV::MalformedCSVError
raise Discourse::InvalidParameters
end
def badge_types
badge_types = BadgeType.all.to_a
render_serialized(badge_types, BadgeTypeSerializer, root: "badge_types")