mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-30 21:27:17 +00:00
Add Service and remove Job
This commit is contained in:
@@ -4,40 +4,28 @@ module Spree
|
||||
respond_to :json
|
||||
|
||||
def create
|
||||
Delayed::Job.enqueue BulkInvoiceJob.new(params[:order_ids], directory, filename)
|
||||
invoice_service = BulkInvoiceService.new
|
||||
invoice_service.create_bulk_invoice(params[:order_ids])
|
||||
|
||||
render text: filename, status: :ok
|
||||
render json: invoice_service.id, status: :ok
|
||||
end
|
||||
|
||||
def show
|
||||
invoice_id = params[:id]
|
||||
invoice_pdf = BulkInvoiceService.new.filepath(invoice_id)
|
||||
|
||||
send_file(filepath(invoice_id), type: 'application/pdf', disposition: :inline)
|
||||
send_file(invoice_pdf, type: 'application/pdf', disposition: :inline)
|
||||
end
|
||||
|
||||
def poll
|
||||
invoice_id = params[:invoice_id]
|
||||
|
||||
if File.exist? filepath(invoice_id)
|
||||
if BulkInvoiceService.new.invoice_created? invoice_id
|
||||
render json: { created: true }, status: :ok
|
||||
else
|
||||
render json: { created: false }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def filename
|
||||
@filename ||= Time.zone.now.to_i.to_s
|
||||
end
|
||||
|
||||
def directory
|
||||
'tmp/invoices'
|
||||
end
|
||||
|
||||
def filepath(invoice_id)
|
||||
@filepath ||= "#{directory}/#{invoice_id}.pdf"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
class BulkInvoiceJob
|
||||
class BulkInvoiceService
|
||||
include WickedPdf::PdfHelper
|
||||
attr_reader :id
|
||||
|
||||
def initialize(order_ids, directory, filename)
|
||||
@order_ids = order_ids
|
||||
@directory = directory
|
||||
@filename = filename
|
||||
def initialize
|
||||
@id = new_invoice_id
|
||||
end
|
||||
|
||||
def perform
|
||||
def create_bulk_invoice(order_ids)
|
||||
pdf = CombinePDF.new
|
||||
orders = Spree::Order.where(id: @order_ids)
|
||||
orders = Spree::Order.where(id: order_ids)
|
||||
|
||||
orders.each do |order|
|
||||
invoice = renderer.render_to_string pdf: "invoice-#{order.number}.pdf",
|
||||
@@ -20,11 +19,28 @@ class BulkInvoiceJob
|
||||
pdf << CombinePDF.parse(invoice)
|
||||
end
|
||||
|
||||
pdf.save "#{file_directory}/#{@filename}.pdf"
|
||||
pdf.save "#{file_directory}/#{@id}.pdf"
|
||||
end
|
||||
handle_asynchronously :create_bulk_invoice
|
||||
|
||||
def invoice_created?(invoice_id)
|
||||
File.exist? filepath(invoice_id)
|
||||
end
|
||||
|
||||
def filepath(invoice_id)
|
||||
"#{directory}/#{invoice_id}.pdf"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def new_invoice_id
|
||||
Time.zone.now.to_i.to_s
|
||||
end
|
||||
|
||||
def directory
|
||||
'tmp/invoices'
|
||||
end
|
||||
|
||||
def renderer
|
||||
ApplicationController.new
|
||||
end
|
||||
@@ -34,8 +50,7 @@ class BulkInvoiceJob
|
||||
end
|
||||
|
||||
def file_directory
|
||||
dir = @directory
|
||||
Dir.mkdir(dir) unless File.exist?(dir)
|
||||
dir
|
||||
Dir.mkdir(directory) unless File.exist?(directory)
|
||||
directory
|
||||
end
|
||||
end
|
||||
@@ -2,16 +2,19 @@ require 'spec_helper'
|
||||
|
||||
describe Spree::Admin::InvoicesController, type: :controller do
|
||||
let(:order) { create(:order_with_totals_and_distribution) }
|
||||
let(:user) { create(:admin_user) }
|
||||
|
||||
before do
|
||||
controller.stub spree_current_user: create(:admin_user)
|
||||
allow(controller).to receive(:spree_current_user) { user }
|
||||
end
|
||||
|
||||
describe "#create" do
|
||||
it "enqueues a job to create a bulk invoice and returns the filename" do
|
||||
expect do
|
||||
spree_post :create, order_ids: [order.id]
|
||||
end.to enqueue_job BulkInvoiceJob
|
||||
end.to enqueue_job Delayed::PerformableMethod
|
||||
|
||||
expect(Delayed::Job.last.payload_object.method_name).to eq :create_bulk_invoice_without_delay
|
||||
end
|
||||
end
|
||||
|
||||
@@ -37,4 +40,4 @@ describe Spree::Admin::InvoicesController, type: :controller do
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
40
spec/services/bulk_invoice_service_spec.rb
Normal file
40
spec/services/bulk_invoice_service_spec.rb
Normal file
@@ -0,0 +1,40 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe BulkInvoiceService do
|
||||
let(:service) { BulkInvoiceService.new }
|
||||
|
||||
describe "#start_pdf_job" do
|
||||
it "starts a background process to create a pdf with multiple invoices" do
|
||||
expect do
|
||||
service.start_pdf_job [1, 2]
|
||||
end.to enqueue_job Delayed::PerformableMethod
|
||||
|
||||
expect(Delayed::Job.last.payload_object.method_name).to eq :start_pdf_job_without_delay
|
||||
end
|
||||
end
|
||||
|
||||
describe "#invoice_created?" do
|
||||
context "when the invoice has been created" do
|
||||
it "returns true" do
|
||||
allow(File).to receive(:exist?).and_return(true)
|
||||
|
||||
created = service.invoice_created? '45891723'
|
||||
expect(created).to be_truthy
|
||||
end
|
||||
end
|
||||
|
||||
context "when the invoice has not been created" do
|
||||
it "returns false" do
|
||||
created = service.invoice_created? '1234567'
|
||||
expect(created).to_not be_truthy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#filepath" do
|
||||
it "returns the filepath of a given invoice" do
|
||||
filepath = service.filepath '1234567'
|
||||
expect(filepath).to eq 'tmp/invoices/1234567.pdf'
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user