4 changed files with 77 additions and 61 deletions
@ -1,27 +1,57 @@ |
|||||||
module Imports |
module Imports |
||||||
class ImportReportService |
class ImportReportService |
||||||
def initialize(storage_service, old_organisation_ids, logger = Rails.logger) |
def initialize(storage_service, institutions_csv, logger = Rails.logger) |
||||||
@storage_service = storage_service |
@storage_service = storage_service |
||||||
@logger = logger |
@logger = logger |
||||||
@old_organisation_ids = old_organisation_ids |
@institutions_csv = institutions_csv |
||||||
end |
end |
||||||
|
|
||||||
BYTE_ORDER_MARK = "\uFEFF".freeze # Required to ensure Excel always reads CSV as UTF-8 |
BYTE_ORDER_MARK = "\uFEFF".freeze # Required to ensure Excel always reads CSV as UTF-8 |
||||||
|
|
||||||
def create_report(report_suffix) |
def create_report(report_suffix) |
||||||
generate_missing_data_coordinators_report(report_suffix) |
generate_missing_data_coordinators_report(report_suffix) |
||||||
|
generate_logs_report(report_suffix) |
||||||
end |
end |
||||||
|
|
||||||
def generate_missing_data_coordinators_report(report_suffix) |
def generate_missing_data_coordinators_report(report_suffix) |
||||||
csv_string = "Organisation ID,Old Organisation ID,Organisation Name\n" |
report_csv = "Organisation ID,Old Organisation ID,Organisation Name\n" |
||||||
@old_organisation_ids.each do |old_organisation_id| |
organisations = @institutions_csv.map { |row| Organisation.find_by(name: row[0]) }.compact |
||||||
organisation = Organisation.find_by(old_visible_id: old_organisation_id) |
organisations.each do |organisation| |
||||||
if organisation.users.none?(&:data_coordinator?) |
if organisation.users.none?(&:data_coordinator?) |
||||||
csv_string += "#{organisation.id},#{old_organisation_id},#{organisation.name}\n" |
report_csv += "#{organisation.id},#{organisation.old_visible_id},#{organisation.name}\n" |
||||||
end |
end |
||||||
end |
end |
||||||
|
|
||||||
@storage_service.write_file("OrganisationsWithoutDataCoordinators_#{report_suffix}.csv", BYTE_ORDER_MARK + csv_string) |
report_name = "OrganisationsWithoutDataCoordinators_#{report_suffix}.csv" |
||||||
|
@storage_service.write_file(report_name, BYTE_ORDER_MARK + report_csv) |
||||||
|
|
||||||
|
@logger.info("Missing data coordinators report available in s3 import bucket at #{report_name}") |
||||||
|
end |
||||||
|
|
||||||
|
def generate_logs_report(report_suffix) |
||||||
|
Rails.logger.info("Generating migrated logs report") |
||||||
|
|
||||||
|
rep = CSV.generate do |report| |
||||||
|
headers = ["Institution name", "Id", "Old Completed lettings logs", "Old In progress lettings logs", "Old Completed sales logs", "Old In progress sales logs", "New Completed lettings logs", "New In Progress lettings logs", "New Completed sales logs", "New In Progress sales logs"] |
||||||
|
report << headers |
||||||
|
|
||||||
|
@institutions_csv.each do |row| |
||||||
|
name = row[0] |
||||||
|
organisation = Organisation.find_by(name:) |
||||||
|
next unless organisation |
||||||
|
|
||||||
|
completed_sales_logs = organisation.owned_sales_logs.where(status: "completed").count |
||||||
|
in_progress_sales_logs = organisation.owned_sales_logs.where(status: "in_progress").count |
||||||
|
completed_lettings_logs = organisation.owned_lettings_logs.where(status: "completed").count |
||||||
|
in_progress_lettings_logs = organisation.owned_lettings_logs.where(status: "in_progress").count |
||||||
|
report << row.push(completed_lettings_logs, in_progress_lettings_logs, completed_sales_logs, in_progress_sales_logs) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
report_name = "MigratedLogsReport_#{report_suffix}.csv" |
||||||
|
@storage_service.write_file(report_name, BYTE_ORDER_MARK + rep) |
||||||
|
|
||||||
|
@logger.info("Logs report available in s3 import bucket at #{report_name}") |
||||||
end |
end |
||||||
end |
end |
||||||
end |
end |
||||||
|
|||||||
Loading…
Reference in new issue