-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmtoBatchWriter.ts
More file actions
98 lines (81 loc) · 2.43 KB
/
mtoBatchWriter.ts
File metadata and controls
98 lines (81 loc) · 2.43 KB
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
import { type DateString } from '@cityssm/utils-datetime'
import { NEWLINE, dateStringToYymmdd } from './utilities.js'
export interface MTOBatchWriterConfig {
authorizedUser: string
sentDate: DateString
includeLabels: boolean
}
export interface MTOBatchWriterEntry {
licencePlateNumber: string
ticketNumber: string
issueDate: DateString
}
export class MTOBatchWriter {
readonly #config: MTOBatchWriterConfig
readonly #batchEntries: MTOBatchWriterEntry[]
constructor(config: MTOBatchWriterConfig, batchEntries?: MTOBatchWriterEntry[]) {
this.#config = config
this.#batchEntries = batchEntries ?? []
}
addBatchEntry(batchEntry: MTOBatchWriterEntry): void {
this.#batchEntries.push(batchEntry)
}
getBatchSize(): number {
return this.#batchEntries.length
}
writeBatch(): string {
let output = ''
/*
* RECORD ROWS
* -----------
* Record Id | 4 characters | "PKTD"
* Plate Number | 10 characters | "XXXX111 "
* Issue Date | 6 numbers | YYMMDD
* Ticket Number | 23 characters | "XX00000 "
* Authorized User | 4 characters | "XX00"
*/
const authorizedUserPadded = this.#config.authorizedUser
.padStart(4)
.slice(0, 4)
for (const entry of this.#batchEntries) {
output +=
'PKTD' +
entry.licencePlateNumber.padEnd(10).slice(0, 10) +
dateStringToYymmdd(entry.issueDate) +
entry.ticketNumber.padEnd(23).slice(0, 23) +
authorizedUserPadded +
NEWLINE
}
const recordCountPadded = this.#batchEntries.length
.toString()
.padStart(6, '0')
.slice(-6)
/*
* HEADER ROW
* ----------
* Record Id | 4 characters | "PKTA"
* Unknown | 5 characters | " 1"
* Export Date | 6 numbers | YYMMDD
* Record Count | 6 numbers, zero padded | 000201
* RET-TAPE | 1 character, Y or N | Y
* CERT-LABEL | 1 character, Y or N | N
*/
output =
'PKTA' +
' 1' +
dateStringToYymmdd(this.#config.sentDate) +
recordCountPadded +
'Y' +
(this.#config.includeLabels ? 'Y' : 'N') +
NEWLINE +
output
/*
* FOOTER ROW
* ----------
* Record Id | 4 characters | "PKTZ"
* Record Count | 6 numbers, zero padded | 000201
*/
output += 'PKTZ' + recordCountPadded + NEWLINE
return output
}
}