-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Enabled the download of a CSV file that represents a batch and its invoice details. - Each row represents awards added to one invoice and its distribution accounts. - Created a new role, `aest-cas-invoicing`, and added it globally to the CAS invoice controller and UI links. ### Report Download Benchmark Data for an outputted report with 1122 invoice details. - File size output: 287Kb. - Report generation total: 456.248ms - Data retrieval: 394.036ms (SQL query and typeorm objects creation). - Report generation: 53.758ms (Typeorm objects to 2D objects for CSV generation). - Report CSV generation: 5.309ms (2D objects to CSV format). ## Sample report for one invoice with two awards (color manually added)  ## Minor refactor Method `streamFile` moved to a utils file to be shared between reports, CAS invoice reports, and others in the future.
- Loading branch information
1 parent
129f923
commit b9c9ee0
Showing
17 changed files
with
362 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
sources/packages/backend/apps/api/src/route-controllers/utils/file-download-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { appendByteOrderMark } from "@sims/utilities"; | ||
import { Response } from "express"; | ||
import { Readable } from "stream"; | ||
|
||
/** | ||
* Streams a file to the response. | ||
* @param response express response. | ||
* @param fileName file name that will be presented to the user for download. | ||
* @param options optional parameters. | ||
* - `fileContent` file content to be downloaded. | ||
* - `contentType - MIME type of the file. Defaults to 'text/csv'. | ||
*/ | ||
export function streamFile( | ||
response: Response, | ||
fileName: string, | ||
options?: { fileContent?: string; contentType?: string }, | ||
): void { | ||
const contentType = options?.contentType ?? "text/csv"; | ||
// Adding byte order mark characters to the original file content as applications | ||
// like excel would look for BOM characters to view the file as UTF8 encoded. | ||
// Append byte order mark characters only if the file content is not empty. | ||
const responseBuffer = options?.fileContent | ||
? appendByteOrderMark(options.fileContent) | ||
: Buffer.from(""); | ||
response.setHeader("Content-Disposition", `attachment; filename=${fileName}`); | ||
response.setHeader("Content-Type", contentType); | ||
response.setHeader("Content-Length", responseBuffer.byteLength); | ||
const stream = new Readable(); | ||
stream.push(responseBuffer); | ||
stream.push(null); | ||
stream.pipe(response); | ||
} |
3 changes: 3 additions & 0 deletions
3
sources/packages/backend/apps/api/src/route-controllers/utils/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./file-download-utils"; | ||
export * from "./address-utils"; | ||
export * from "./custom-validation-pipe"; |
Oops, something went wrong.