@@ -114,60 +114,10 @@ class CoreDataSource {
114
114
*/
115
115
async getAllRecordData ( ) {
116
116
try {
117
- let currentPage = 1 ;
118
- let totalPages = 1 ;
119
- let mineRecords = [ ] ;
120
-
121
- // The Core API can not return all data in a single call. Must fetch data in batches.
122
- do {
123
- const queryParams = { per_page : CORE_API_BATCH_SIZE , page : currentPage } ;
124
- const url = this . getIntegrationUrl ( CORE_API_HOST , CORE_API_PATH_MINES , queryParams ) ;
125
-
126
- // Get Core records
127
- const data = await integrationUtils . getRecords ( url , this . getAuthHeader ( ) ) ;
128
- // Get records from response and add to total.
129
- const newRecords = data && data . mines || [ ] ;
130
- mineRecords = [ ...mineRecords , ...newRecords ] ;
131
-
132
- currentPage ++ ;
133
- totalPages = data . total_pages ;
117
+ const verifiedMines = await this . getVerifiedMines ( ) ;
118
+ const minesWithDetails = await this . addMinesDetails ( verifiedMines ) ;
134
119
135
- defaultLog . info ( `Fetched page ${ currentPage - 1 } out of ${ totalPages } ` ) ;
136
- } while ( currentPage <= totalPages )
137
-
138
- // Filter to only verified mines. There is some discrepancy with data, so check that there is a verified mine ID to be sure.
139
- const verifiedRecords = mineRecords . filter ( record => record . verified_status && record . verified_status . mine_guid ) ;
140
-
141
- // Get additional info for each mine.
142
- const promises = verifiedRecords . map ( ( mine , index ) => {
143
- const partyQueryParams = {
144
- mine_guid : mine . mine_guid ,
145
- relationships : 'party'
146
- } ;
147
-
148
- const partyUrl = this . getIntegrationUrl ( CORE_API_HOST , CORE_API_PATH_PARTIES , partyQueryParams ) ;
149
- const mineDetailsPath = `${ CORE_API_PATH_MINES } /${ mine . mine_guid } ` ;
150
- const mineDetailsUrl = this . getIntegrationUrl ( CORE_API_HOST , mineDetailsPath ) ;
151
-
152
- return new Promise ( async ( resolve ) => {
153
- const [ parties , mineDetails ] = await Promise . all ( [
154
- integrationUtils . getRecords ( partyUrl , this . getAuthHeader ( ) ) ,
155
- integrationUtils . getRecords ( mineDetailsUrl , this . getAuthHeader ( ) )
156
- ] ) ;
157
-
158
- const latitude = mineDetails . mine_location && mineDetails . mine_location . latitude || 0.00 ;
159
- const longitude = mineDetails . mine_location && mineDetails . mine_location . longitude || 0.00 ;
160
-
161
- // Add the location and parties to the mine record.
162
- verifiedRecords [ index ] . coordinates = [ latitude , longitude ] ;
163
- verifiedRecords [ index ] . parties = parties ;
164
- resolve ( ) ;
165
- } ) ;
166
- } ) ;
167
-
168
- await Promise . all ( promises ) ;
169
-
170
- return verifiedRecords ;
120
+ return minesWithDetails ;
171
121
} catch ( error ) {
172
122
defaultLog . error ( `getAllRecordData - unexpected error: ${ error . message } ` ) ;
173
123
throw ( error ) ;
@@ -235,6 +185,11 @@ class CoreDataSource {
235
185
// Get the valid permit.
236
186
const permit = await this . getMinePermit ( coreRecord ) ;
237
187
188
+ // A mine must have a valid permit.
189
+ if ( ! permit ) {
190
+ throw new Error ( `processRecord - no valid record found for ${ coreRecord . mine_guid } ` ) ;
191
+ }
192
+
238
193
// Perform any data transformations necessary to convert core record to NRPTI record
239
194
const nrptiRecord = await recordTypeUtils . transformRecord ( coreRecord , commodityTypes , permit ) ;
240
195
@@ -355,7 +310,6 @@ class CoreDataSource {
355
310
const url = this . getIntegrationUrl ( CORE_API_HOST , `/api/mines/${ coreRecord . mine_guid } /permits` ) ;
356
311
const { records : permits } = await integrationUtils . getRecords ( url , this . getAuthHeader ( ) ) ;
357
312
358
-
359
313
// First, any mines with 'X' as their second character are considered exploratory. Remove them.
360
314
const nonExploratoryPermits = permits . filter ( permit => permit . permit_no [ 1 ] . toLowerCase ( ) !== 'x' ) ;
361
315
@@ -367,18 +321,19 @@ class CoreDataSource {
367
321
if ( permit . permit_amendments . length && ! permit . permit_amendments [ 0 ] . authorization_end_date ) {
368
322
// There should only be a single record. If there is more then we do not want to continue processing.
369
323
if ( validPermit ) {
370
- throw new Error ( ' getMinePermit - more than one valid permit found' )
324
+ throw new Error ( ` getMinePermit - more than one valid permit found for mine ${ coreRecord . mine_guid } ` ) ;
371
325
}
372
326
373
327
validPermit = permit ;
374
328
}
375
329
else {
376
330
// If it is not '9999' it is considered valid.
377
331
const authDate = new Date ( permit . permit_amendments [ 0 ] . authorization_end_date ) ;
378
- if ( authDate . getFullYear !== 9999 ) {
332
+
333
+ if ( authDate . getFullYear ( ) !== 9999 ) {
379
334
// There should only be a single record. If there is more then we do not want to continue processing.
380
335
if ( validPermit ) {
381
- throw new Error ( ' getMinePermit - more than one valid permit found' )
336
+ throw new Error ( ` getMinePermit - more than one valid permit found for mine ${ coreRecord . mine_guid } ` ) ;
382
337
}
383
338
384
339
validPermit = permit ;
@@ -392,6 +347,91 @@ class CoreDataSource {
392
347
throw ( error ) ;
393
348
}
394
349
}
350
+
351
+ /**
352
+ * Gets all verified mines from Core.
353
+ *
354
+ * @returns {object[] } Verified Core mines.
355
+ * @memberof CoreDataSource
356
+ */
357
+ async getVerifiedMines ( ) {
358
+ try {
359
+ let currentPage = 1 ;
360
+ let totalPages = 1 ;
361
+ let mineRecords = [ ] ;
362
+
363
+ // The Core API can not return all data in a single call. Must fetch data in batches.
364
+ do {
365
+ const queryParams = {
366
+ per_page : CORE_API_BATCH_SIZE ,
367
+ page : currentPage ,
368
+ major : true
369
+ } ;
370
+ const url = this . getIntegrationUrl ( CORE_API_HOST , CORE_API_PATH_MINES , queryParams ) ;
371
+
372
+ // Get Core records
373
+ const data = await integrationUtils . getRecords ( url , this . getAuthHeader ( ) ) ;
374
+ // Get records from response and add to total.
375
+ const newRecords = data && data . mines || [ ] ;
376
+ mineRecords = [ ...mineRecords , ...newRecords ] ;
377
+
378
+ currentPage ++ ;
379
+ totalPages = data . total_pages ;
380
+
381
+ defaultLog . info ( `Fetched page ${ currentPage - 1 } out of ${ totalPages } ` ) ;
382
+ } while ( currentPage <= totalPages )
383
+
384
+ // Filter to only verified mines. There is some discrepancy with data, so check that there is a verified mine ID to be sure.
385
+ const verifiedRecords = mineRecords . filter ( record => record . verified_status && record . verified_status . mine_guid ) ;
386
+
387
+ return verifiedRecords
388
+ } catch ( error ) {
389
+ defaultLog . error ( `getVerifiedMines - unexpected error: ${ error . message } ` ) ;
390
+ throw ( error ) ;
391
+ }
392
+ }
393
+
394
+ /**
395
+ * Adds party and lat/long details to each Core record.
396
+ *
397
+ * @param {object[] } coreRecords Records from Core API that have not been transformed.
398
+ * @returns {obejct[] } Records with details added.
399
+ * @memberof CoreDataSource
400
+ */
401
+ async addMinesDetails ( coreRecords ) {
402
+ const completeRecords = [ ] ;
403
+
404
+ for ( let i = 0 ; i < coreRecords . length ; i ++ ) {
405
+ const partyQueryParams = {
406
+ mine_guid : coreRecords [ i ] . mine_guid ,
407
+ relationships : 'party'
408
+ } ;
409
+
410
+ const partyUrl = this . getIntegrationUrl ( CORE_API_HOST , CORE_API_PATH_PARTIES , partyQueryParams ) ;
411
+ const mineDetailsPath = `${ CORE_API_PATH_MINES } /${ coreRecords [ i ] . mine_guid } ` ;
412
+ const mineDetailsUrl = this . getIntegrationUrl ( CORE_API_HOST , mineDetailsPath ) ;
413
+
414
+ try {
415
+ const [ parties , mineDetails ] = await Promise . all ( [
416
+ integrationUtils . getRecords ( partyUrl , this . getAuthHeader ( ) ) ,
417
+ integrationUtils . getRecords ( mineDetailsUrl , this . getAuthHeader ( ) )
418
+ ] ) ;
419
+
420
+ const latitude = mineDetails && mineDetails . mine_location && mineDetails . mine_location . latitude || 0.00 ;
421
+ const longitude = mineDetails && mineDetails . mine_location && mineDetails . mine_location . longitude || 0.00 ;
422
+
423
+ completeRecords . push ( {
424
+ ...coreRecords [ i ] ,
425
+ coordinates : [ latitude , longitude ] ,
426
+ parties
427
+ } ) ;
428
+ } catch ( error ) {
429
+ defaultLog . error ( `coreRecords - error getting details for Core record '${ coreRecords [ i ] . mine_guid } ' ...skipping` ) ;
430
+ }
431
+ }
432
+
433
+ return completeRecords ;
434
+ }
395
435
}
396
436
397
437
module . exports = CoreDataSource ;
0 commit comments