-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdoc-builder.js
356 lines (334 loc) · 13 KB
/
doc-builder.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
const docx = require('docx');
const placeholders = require('./placeholderTextStrings');
// This could use type definitions but I'm bad at JSDoc and that takes time
/**
* data looks like:
* {
* category1: { totalExpenditure: 1234, projects: [ProjectData] },
* category2: { totalExpenditure: 2345, projects: [ProjectData] },
* ...etc
* }
*/
class ArpaDocumentBuilder {
/*
This giant class takes in data in roughly the shape above and turns it into a formatted
word document for return at the annualReporter route
*/
constructor(data) {
this.data = data;
this.sortedCategories = Object.keys(data).sort();
this.dollarFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
}
static buildSectionHeader(text) {
return new docx.Paragraph({
children: [
new docx.TextRun({
size: 24,
text,
}),
],
spacing: { before: 200, after: 0 },
});
}
// Helper cuz the api for this package is kinda rough
static buildTableHeaderCell(value) {
return new docx.TableCell({
children: [
new docx.Paragraph({
text: value,
spacing: { line: 200 },
}),
],
verticalAlign: docx.VerticalAlign.CENTER,
});
}
static buildTableCell(value, alignRight = false) {
return new docx.TableCell({
children: [
new docx.Paragraph({
text: value,
spacing: { line: 200 },
alignment: alignRight ? docx.AlignmentType.RIGHT : docx.AlignmentType.LEFT,
}),
],
verticalAlign: docx.VerticalAlign.CENTER,
});
}
static buildPlaceholderParagraph(arrayOfStrings) {
const children = [];
arrayOfStrings.forEach((text) => {
const run = new docx.TextRun({
size: 24,
color: 'ADADAD',
break: 1,
text,
});
children.push(run);
});
return new docx.Paragraph({
spacing: { after: 400 },
children,
});
}
static buildTableHeaderRow() {
return new docx.TableRow({
children: [
ArpaDocumentBuilder.buildTableHeaderCell('Category'),
// The two below will be the same this year
ArpaDocumentBuilder.buildTableHeaderCell('Cumulative Expenditures to Date ($)'),
ArpaDocumentBuilder.buildTableHeaderCell('Amount since last recovery plan'),
],
height: { value: 600 },
});
}
static buildPageHeader(text) {
return new docx.Paragraph({
heading: docx.HeadingLevel.HEADING_1,
text,
spacing: { after: 200 },
});
}
static buildUnderlinedSubHeader(text) {
return new docx.Paragraph({
children: [
new docx.TextRun({
size: 26,
underline: {},
text,
}),
],
spacing: { before: 200, after: 100 },
});
}
static buildDocumentIntro() {
const size = 28;
return new docx.Paragraph({
children: [
new docx.TextRun({
text: 'The structure of this document is taken directly from US Treasury\'s ',
break: 1,
size,
}),
new docx.ExternalHyperlink({
children: [new docx.TextRun({
text: 'Annual Recovery Plan Template. ',
style: 'Hyperlink',
size,
})],
link: 'https://home.treasury.gov/system/files/136/SLFRF-Recovery-Plan-Performance-Report-Template.docx',
}),
new docx.TextRun({
text: 'Gray text ',
color: 'ADADAD',
break: 2,
size,
}),
new docx.TextRun({
text: 'indicates guidance that should be deleted as you fill out the section. ',
size,
}),
new docx.TextRun({
text: 'USDR has pre-populated the Table of Expenses and Project Inventory '
+ 'sections based on your uploaded workbooks.',
break: 2,
size,
}),
new docx.TextRun({
text: 'Please delete this page before submitting the report.',
break: 3,
bold: true,
size,
}),
],
});
}
formatExpenditureValue(val) {
return this.dollarFormatter.format(val);
}
buildSummaryTable() {
const tableRows = [ArpaDocumentBuilder.buildTableHeaderRow()];
this.sortedCategories.forEach((cat) => {
const categoryData = this.data[cat];
const amountString = this.formatExpenditureValue(categoryData.totalExpenditure);
const newRow = new docx.TableRow({
children: [
ArpaDocumentBuilder.buildTableCell(cat),
ArpaDocumentBuilder.buildTableCell(amountString, true),
ArpaDocumentBuilder.buildTableCell(amountString, true),
],
height: { value: 800 },
});
tableRows.push(newRow);
});
return new docx.Table({
columnWidths: ['30pc', '30pc', '30pc'],
rows: tableRows,
});
}
buildProjectInventory() {
const sectionHeader = ArpaDocumentBuilder.buildPageHeader('Project Inventory');
// All the child elements will go in here
const inventory = [sectionHeader];
// these size values are in half-values
// so size 28 is actually 14 in the doc
const headerFontSize = 28;
this.sortedCategories.forEach((cat) => {
const categoryData = this.data[cat];
categoryData.projects.forEach((project) => {
const amountSpent = this.formatExpenditureValue(project.amountSpent);
const paragraphs = [
new docx.Paragraph({
children: [
new docx.TextRun({
text: `Project Name: ${project.name}`,
size: headerFontSize,
}),
],
spacing: { before: 500 },
}),
// Spread syntax because for some reason '!this.isTulsa &&' has
// collateral damage
new docx.Paragraph({
children: [
new docx.TextRun({
text: `Expenditure Category: ${project.category}`,
size: headerFontSize,
}),
],
}),
new docx.Paragraph({
children: [
new docx.TextRun({
text: `Recipient: ${project.recipient}`,
size: headerFontSize,
}),
],
}),
new docx.Paragraph({
children: [
new docx.TextRun({
text: `Amount Spent: ${amountSpent}`,
size: headerFontSize,
}),
],
}),
ArpaDocumentBuilder.buildUnderlinedSubHeader('Project Impact'),
new docx.Paragraph({
children: [
new docx.TextRun({ text: 'Website: ', size: 24 }),
new docx.ExternalHyperlink({
children: [new docx.TextRun({
text: project.website || 'www.project.com',
style: 'Hyperlink',
size: 24,
})],
link: project.website || 'www.project.com',
}),
],
bullet: { level: 0 },
}),
new docx.Paragraph({
children: [
new docx.TextRun({
text: project.description || 'A brief description of the project',
size: 24,
}),
],
spacing: { before: 100 },
bullet: { level: 0 },
}),
ArpaDocumentBuilder.buildUnderlinedSubHeader('Use of Evidence'),
new docx.Paragraph({
children: [
new docx.TextRun({ text: project.impactStatement, size: 24 }),
],
spacing: { before: 100 },
bullet: { level: 0 },
}),
];
inventory.push(...paragraphs);
});
});
return inventory;
}
buildReportDocument() {
return new docx.Document({
sections: [
{
children: [
ArpaDocumentBuilder.buildPageHeader('Instructions for this template'),
ArpaDocumentBuilder.buildDocumentIntro(),
],
},
{
properties: {
type: docx.SectionType.NEXT_PAGE,
},
children: [
ArpaDocumentBuilder.buildPageHeader('General Overview'),
ArpaDocumentBuilder.buildSectionHeader('Executive Summary'),
ArpaDocumentBuilder.buildPlaceholderParagraph(
placeholders.EXECUTIVE_SUMMARY,
),
ArpaDocumentBuilder.buildSectionHeader('Uses of Funds'),
ArpaDocumentBuilder.buildPlaceholderParagraph(placeholders.USES_OF_FUNDS),
ArpaDocumentBuilder.buildSectionHeader('Promoting Equitable Outcomes'),
ArpaDocumentBuilder.buildPlaceholderParagraph(
placeholders.PROMOTING_EQUITABLE_OUTCOMES,
),
ArpaDocumentBuilder.buildSectionHeader('Community Engagement'),
ArpaDocumentBuilder.buildPlaceholderParagraph(
placeholders.COMMUNITY_ENGAGEMENT,
),
ArpaDocumentBuilder.buildSectionHeader('Labor Practices'),
ArpaDocumentBuilder.buildPlaceholderParagraph(
placeholders.LABOR_PRACTICES,
),
ArpaDocumentBuilder.buildSectionHeader('Use of Evidence'),
ArpaDocumentBuilder.buildPlaceholderParagraph(
placeholders.USE_OF_EVIDENCE,
),
],
},
{
properties: {
type: docx.SectionType.NEXT_PAGE,
},
children: [
ArpaDocumentBuilder.buildPageHeader(
'Table of Expenses by Expenditure Category',
),
ArpaDocumentBuilder.buildPlaceholderParagraph(
placeholders.TABLE_OF_EXPENSES,
),
this.buildSummaryTable(),
],
},
{
properties: {
type: docx.SectionType.NEXT_PAGE,
},
children: this.buildProjectInventory(),
},
{
properties: {
type: docx.SectionType.NEXT_PAGE,
},
children: [
ArpaDocumentBuilder.buildPageHeader('Performance Report'),
ArpaDocumentBuilder.buildPlaceholderParagraph(
placeholders.PERFORMANCE_REPORT,
),
],
},
],
});
}
static async documentToBuffer(document) {
return docx.Packer.toBuffer(document);
}
}
module.exports = ArpaDocumentBuilder;