-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.rb
126 lines (96 loc) · 3.39 KB
/
web.rb
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require 'combine_pdf'
require 'facets'
require_relative 'lib/visit-report'
require_relative 'lib/intervention-report'
require_relative 'lib/offer-document'
require_relative 'lib/order-document'
require_relative 'lib/delivery-note'
require_relative 'lib/production-ticket'
require_relative 'lib/invoice-document'
require_relative 'lib/deposit-invoice-document'
configure :development do
class WickedPdf # monkey patch useful for debugging
def in_development_mode?
true
end
def print_command(cmd)
puts cmd
end
end
end
before do
session = session_id_header request
@user = fetch_user_for_session session
end
post '/requests/:id/documents' do
generator = DocumentGenerator::VisitReport.new id: params['id'], user: @user
path = generator.generate
send_file path
end
post '/interventions/:id/documents' do
generator = DocumentGenerator::InterventionReport.new id: params['id'], user: @user
path = generator.generate
send_file path
end
post '/offers/:id/documents' do
data = @json_body['data']
language = data['attributes']['language']
generator = DocumentGenerator::OfferDocument.new id: params['id'], language: language, user: @user
path = generator.generate
send_file path
end
post '/orders/:id/documents' do
data = @json_body['data']
language = data['attributes']['language']
generator = DocumentGenerator::OrderDocument.new id: params['id'], language: language, user: @user
path = generator.generate
send_file path
end
post '/orders/:id/delivery-notes' do
data = @json_body['data']
language = data['attributes']['language']
generator = DocumentGenerator::DeliveryNote.new id: params['id'], language: language, user: @user
path = generator.generate
send_file path
end
post '/deposit-invoices/:id/documents' do
data = @json_body['data']
language = data['attributes']['language']
generator = DocumentGenerator::DepositInvoiceDocument.new id: params['id'], language: language, user: @user
path = generator.generate(data)
send_file path
end
post '/invoices/:id/documents' do
data = @json_body['data']
language = data['attributes']['language']
generator = DocumentGenerator::InvoiceDocument.new id: params['id'], language: language, user: @user
path = generator.generate(data)
send_file path
end
post '/cases/:id/production-ticket-templates' do
generator = DocumentGenerator::ProductionTicket.new id: params['id'], user: @user
path = generator.generate
send_file path
end
post '/cases/:id/watermarked-production-tickets' do
if params['file']
uploaded_file = params['file'][:tempfile]
random = (rand * 100000000).to_i
production_ticket_path = "/tmp/#{random}-production-ticket.pdf"
FileUtils.copy(uploaded_file.path, production_ticket_path)
watermark_path = ENV['PRODUCTION_TICKET_WATERMARK_NL'] || '/watermarks/productiebon-nl.pdf'
watermark = CombinePDF.load(watermark_path).pages[0]
begin
pdf = CombinePDF.load production_ticket_path, allow_optional_content: true
pdf.pages.each { |page| page << watermark }
path = "/tmp/#{random}-production-ticket-watermark.pdf"
pdf.save path
send_file path
rescue CombinePDF::ParsingError
# log.warn "Unable to parse incoming production ticket PDF and add a watermark. Just returning the original production ticket instead."
send_file production_ticket_path
end
else
halt 400, { title: 'File is required' }
end
end