-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmtoBatchParser.ts
More file actions
253 lines (197 loc) · 7.36 KB
/
mtoBatchParser.ts
File metadata and controls
253 lines (197 loc) · 7.36 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
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
import { fixSaultSteMarie } from '@cityssm/is-sault-ste-marie'
import { getFieldValueDescription } from '@cityssm/ncic-lookup'
import { type DateString, dateToString } from '@cityssm/utils-datetime'
import { NEWLINE, yyToYyyy, yymmddToDateString } from './utilities.js'
export interface MTOBatchResultEntry {
licencePlateNumber: string
issueDate: DateString
ticketNumber: string
driverLicenceNumber: string
ownerGenderKey: string
ownerName1: string
ownerName2: string
ownerAddress: string
ownerCity: string
ownerProvince: string
ownerPostalCode: string
vehicleMake: string
vehicleMakeDescription?: string
vehicleYear?: number
vehicleColor: string
vehicleColorDescription?: string
errorCode: string
errorMessage: string
licencePlateExpiryDate?: DateString
}
interface MTOBatchResultHeader {
sentDate: DateString
recordDate: DateString
}
function parsePKRA(rowData: string): undefined | MTOBatchResultHeader {
if (!rowData.startsWith('PKRA')) {
return undefined
}
/*
* PKRA RECORD
* -----------
* Record Id | 4 characters | "PKRA"
* DEST-CODE | 4 characters | " "
* BATCH-NO | 1 character | "1"
* Sent Date | 6 numbers | YYMMDD
* Record Count | 6 numbers, right justified | " 201"
* TAPE-NEEDED | 1 character, Y or N | Y
* LABEL-NEEDED | 1 character, Y or N | N
* ENTRY-DATE | 6 numbers | YYMMDD
* Record Date | 6 numbers | YYMMDD
* FILLER | 165 characters
*/
try {
const record: Partial<MTOBatchResultHeader> = {}
const rawSentDate = rowData.slice(9, 15).trim()
if (rawSentDate === '') {
return undefined
}
record.sentDate = yymmddToDateString(rawSentDate)
const rawRecordDate = rowData.slice(29, 35).trim()
if (rawRecordDate === '') {
return undefined
}
record.recordDate = yymmddToDateString(rawRecordDate)
return record as MTOBatchResultHeader
} catch {
return undefined
}
}
async function parsePKRD(
rowData: string
): Promise<undefined | MTOBatchResultEntry> {
if (!rowData.startsWith('PKRD')) {
return undefined
}
/*
* PKRD RECORD
* -----------
* Record Id | 4 characters | "PKRD"
* Licence Plate Number | 10 characters | "XXXX111 "
* Issue Date | 6 numbers | YYMMDD
* Ticket Number | 8 characters | "XX00000 "
* INPUT-MAKE | 4 characters |
* Driver's Licence Number | 15 characters | "A12345123451234"
* Birth Date | 6 numbers | YYMMDD
* Gender | 1 character | M
* Owner's Name | 50 characters, "/" separated | "DOE,JOHN/DOE,JANE "
* Address | 40 characters, including city | "1234 STREET RD,S STE MARIE "
* Postal Code | 6 characters | "A1A1A1"
* Vehicle Make | 4 characters | "CHEV"
* Vehicle Year | 2 numbers | "17"
* Odometer Reading | 8 numbers | "123456 "
* Odometer Unit | 2 characters | "KM"
* Vehicle Colour | 3 characters | "BLK"
* Error Code | 6 characters | "WRN262"
* Error Message | 29 characters | "PLATE WAS UNATTACHED "
* Licence Plate Expiry Date | 4 numbers | YYMM
* BRAND | 3 characters
* FILLER | 10 characters
*/
const record: Partial<MTOBatchResultEntry> = {}
record.licencePlateNumber = rowData.slice(4, 14).trim()
record.issueDate = yymmddToDateString(rowData.slice(14, 20))
record.ticketNumber = rowData.slice(20, 28).trim()
record.driverLicenceNumber = rowData.slice(32, 47).trim()
record.ownerGenderKey = rowData.slice(53, 54)
record.ownerName1 = rowData.slice(54, 104).replaceAll(',', ', ').trim()
if (record.ownerName1.includes('/')) {
const slashIndex = record.ownerName1.indexOf('/')
record.ownerName2 = record.ownerName1.slice(Math.max(0, slashIndex + 1))
record.ownerName1 = record.ownerName1.slice(0, Math.max(0, slashIndex))
}
record.ownerAddress = rowData.slice(104, 144).trim()
if (record.ownerAddress.includes(',')) {
const lastCommaIndex = record.ownerAddress.lastIndexOf(',')
record.ownerCity = record.ownerAddress.slice(
Math.max(0, lastCommaIndex + 1)
)
record.ownerAddress = record.ownerAddress.slice(
0,
Math.max(0, lastCommaIndex)
)
record.ownerCity = fixSaultSteMarie(record.ownerCity, 'SAULT STE. MARIE')
}
record.ownerPostalCode = rowData.slice(144, 150).trim()
record.vehicleMake = rowData.slice(150, 154).trim()
record.vehicleMakeDescription = await getFieldValueDescription(
'VMA',
record.vehicleMake as string
)
record.vehicleYear = yyToYyyy(Number.parseInt(rowData.slice(154, 156), 10))
record.vehicleColor = rowData.slice(166, 169).trim()
record.vehicleColorDescription = await getFieldValueDescription(
'VCO',
record.vehicleColor as string
)
record.errorCode = rowData.slice(169, 175).trim()
record.errorMessage = rowData.slice(175, 204).trim()
const expiryYear = yyToYyyy(Number.parseInt(rowData.slice(204, 206), 10))
const expiryDate = new Date(
expiryYear,
Number.parseInt(rowData.slice(206, 208), 10) - 1 + 1,
1
)
expiryDate.setDate(expiryDate.getDate() - 1)
record.licencePlateExpiryDate = dateToString(expiryDate) as DateString
if (record.errorCode !== '') {
delete record.vehicleYear
delete record.licencePlateExpiryDate
}
return record as MTOBatchResultEntry
}
export interface MTOBatchResults {
sentDate: DateString
recordDate: DateString
unparsedResultsCount: number
parsedResults: MTOBatchResultEntry[]
parsedResultsWithErrors: MTOBatchResultEntry[]
}
/**
* Parses the string data from an MTO results file.
* @param {string} resultData - A string of results data.
* @returns {MTOBatchResults} - The parsed results.
*/
export async function parseMTOBatchResult(
resultData: string
): Promise<MTOBatchResults> {
// Split the file into rows
const ownershipDataRows = resultData.split(NEWLINE)
if (ownershipDataRows.length === 0) {
throw new Error('The file contains zero data rows.')
}
const headerRow = parsePKRA(ownershipDataRows[0])
if (headerRow === undefined) {
throw new Error(
'An error occurred while trying to parse the first row of the file.'
)
}
const results: MTOBatchResults = {
sentDate: headerRow.sentDate,
recordDate: headerRow.recordDate,
unparsedResultsCount: 0,
parsedResults: [],
parsedResultsWithErrors: []
}
// Loop through record rows
for (const ownershipDataRow of ownershipDataRows) {
try {
const recordRow = await parsePKRD(ownershipDataRow)
if (recordRow !== undefined) {
if (recordRow.errorCode === '') {
results.parsedResults.push(recordRow)
} else {
results.parsedResultsWithErrors.push(recordRow)
}
}
} catch {
results.unparsedResultsCount += 1
}
}
return results
}