Skip to content

Commit 3102398

Browse files
Updated doc and node:fs on csv files
1 parent 29208a2 commit 3102398

4 files changed

Lines changed: 22 additions & 4 deletions

File tree

scripts/nft-event-generator/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ It supports two event types, each invoked as a subcommand:
77
- **`supplier-api-letter-event`** – generates `SupplierApiLetterEvent` events (mirrors the `LetterEvent` consumed by `print-status-handler`)
88
- **`paper-letter-opt-out-event`** – generates `PaperLetterOptedOut` channel status events, reading input from a CSV file
99

10+
To execute the commands you need to:
11+
12+
- Be located in the scripts/nft-event-generator directory
13+
- Have an active `aws sso login` in the account you want (e.g. `NHS Notify Suppliers Nonprod`)
14+
1015
## Common features
1116

1217
- Custom environments (e.g. `pr293`, `main`, `nft`)
@@ -88,13 +93,13 @@ The `messageReference` field in each generated event is built as `<senderId>_<me
8893
Send opt-out events from a CSV file to the `nft` environment:
8994

9095
```shell
91-
npm start -- paper-letter-opt-out-event --environment nft --csvFile ./opt-outs.csv
96+
npm start -- paper-letter-opt-out-event --environment nft --csvFile /home/notify/git/nhs-notify-digital-letters/scripts/nft-event-generator/opt-outs.csv
9297
```
9398

9499
Send to a PR environment with a custom batch interval:
95100

96101
```shell
97-
npm start -- paper-letter-opt-out-event --environment pr293 --csvFile ./opt-outs.csv --interval 500
102+
npm start -- paper-letter-opt-out-event --environment pr293 --csvFile /home/notify/git/nhs-notify-digital-letters/scripts/nft-event-generator/opt-outs.csv --interval 500
98103
```
99104

100105
---

scripts/nft-event-generator/src/__tests__/utils/csv-reader.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { readCsvFile } from 'utils/csv-reader';
44
const SAMPLE_CSV = path.join(__dirname, '../fixtures', 'sample-opt-outs.csv');
55

66
describe('readCsvFile', () => {
7+
it('should throw for a non-csv path', () => {
8+
expect(() => readCsvFile('/etc/passwd')).toThrow(
9+
'Invalid file path: must be a .csv file',
10+
);
11+
});
12+
713
it('should parse a CSV file into CsvRow objects', () => {
814
const rows = readCsvFile(SAMPLE_CSV);
915

scripts/nft-event-generator/src/utils/csv-reader.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import { parse } from 'csv-parse/sync';
22
import { readFileSync } from 'node:fs';
3+
import path from 'node:path';
34

45
export type PaperLetterOptOutRow = {
56
messageReference: string;
67
senderId: string;
78
};
89

910
export function readCsvFile(filePath: string): PaperLetterOptOutRow[] {
10-
const fileContent = readFileSync(filePath, 'utf8');
11+
const resolvedPath = path.resolve(filePath);
12+
13+
if (!resolvedPath.endsWith('.csv')) {
14+
throw new Error(`Invalid file path: must be a .csv file`);
15+
}
16+
17+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- path is validated above
18+
const fileContent = readFileSync(resolvedPath, 'utf8');
1119

1220
return parse(fileContent, {
1321
columns: ['messageReference', 'senderId'],

scripts/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"lint": "eslint nft-event-generator/",
2525
"lint:fix": "eslint nft-event-generator/ --fix",
2626
"start": "tsx nft-event-generator/src/cli.ts",
27-
"start:nft": "npm start -- supplier-api-letter-event --environment nft --numberOfEvents 2 --interval 2000",
2827
"test:unit": "jest",
2928
"typecheck": "tsc --noEmit"
3029
},

0 commit comments

Comments
 (0)