@@ -11,24 +11,20 @@ export async function processCensus(props: Readonly<SpecialProcessingProps>): Pr
11
11
console . error ( 'Missing required parameters: plotID or censusID' ) ;
12
12
throw new Error ( 'Process Census: Missing plotID or censusID' ) ;
13
13
}
14
-
15
14
const { tag, stemtag, spcode, quadrat, lx, ly, coordinateunit, dbh, dbhunit, hom, homunit, date, codes } = rowData ;
16
15
17
16
try {
18
17
await connection . beginTransaction ( ) ;
19
-
20
18
// Fetch species
21
19
const speciesID = await fetchPrimaryKey < SpeciesResult > ( schema , 'species' , { SpeciesCode : spcode } , connection , 'SpeciesID' ) ;
22
-
23
20
// Fetch quadrat
24
21
const quadratID = await fetchPrimaryKey < QuadratResult > ( schema , 'quadrats' , { QuadratName : quadrat , PlotID : plotID } , connection , 'QuadratID' ) ;
25
22
26
23
if ( tag ) {
27
24
// Handle Tree Upsert
28
25
const treeID = await handleUpsert < TreeResult > ( connection , schema , 'trees' , { TreeTag : tag , SpeciesID : speciesID } , 'TreeID' ) ;
29
26
30
- if ( stemtag && lx && ly ) {
31
- console . log ( 'Processing stem with StemTag:' , stemtag ) ;
27
+ if ( stemtag || lx || ly ) {
32
28
// Handle Stem Upsert
33
29
const stemID = await handleUpsert < StemResult > (
34
30
connection ,
@@ -38,46 +34,47 @@ export async function processCensus(props: Readonly<SpecialProcessingProps>): Pr
38
34
'StemID'
39
35
) ;
40
36
41
- if ( dbh && hom && date ) {
42
- // Handle Core Measurement Upsert
43
- const coreMeasurementID = await handleUpsert < CoreMeasurementsResult > (
44
- connection ,
45
- schema ,
46
- 'coremeasurements' ,
47
- {
48
- CensusID : censusID ,
49
- StemID : stemID ,
50
- IsValidated : null ,
51
- MeasurementDate : moment ( date ) . format ( 'YYYY-MM-DD' ) ,
52
- MeasuredDBH : dbh ,
53
- DBHUnit : dbhunit ,
54
- MeasuredHOM : hom ,
55
- HOMUnit : homunit
56
- } ,
57
- 'CoreMeasurementID'
58
- ) ;
37
+ // Handle Core Measurement Upsert
38
+ const coreMeasurementID = await handleUpsert < CoreMeasurementsResult > (
39
+ connection ,
40
+ schema ,
41
+ 'coremeasurements' ,
42
+ {
43
+ CensusID : censusID ,
44
+ StemID : stemID ,
45
+ IsValidated : null ,
46
+ MeasurementDate : date && moment ( date ) . isValid ( ) ? moment . utc ( date ) . format ( 'YYYY-MM-DD' ) : null ,
47
+ MeasuredDBH : dbh ? parseFloat ( dbh ) : null ,
48
+ DBHUnit : dbhunit ,
49
+ MeasuredHOM : hom ? parseFloat ( hom ) : null ,
50
+ HOMUnit : homunit ,
51
+ Description : null ,
52
+ UserDefinedFields : null
53
+ } ,
54
+ 'CoreMeasurementID'
55
+ ) ;
59
56
60
- // Handle CM Attributes Upsert
61
- if ( codes ) {
62
- const parsedCodes = codes
63
- . split ( ';' )
64
- . map ( code => code . trim ( ) )
65
- . filter ( Boolean ) ;
66
- if ( parsedCodes . length === 0 ) {
67
- console . error ( 'No valid attribute codes found:' , codes ) ;
68
- } else {
69
- for ( const code of parsedCodes ) {
70
- const attributeRows = await runQuery ( connection , `SELECT COUNT(*) as count FROM ${ schema } .attributes WHERE Code = ?` , [ code ] ) ;
71
- if ( ! attributeRows || attributeRows . length === 0 || ! attributeRows [ 0 ] . count ) {
72
- throw createError ( `Attribute code ${ code } not found or query failed.` , { code } ) ;
73
- }
74
- await handleUpsert < CMAttributesResult > ( connection , schema , 'cmattributes' , { CoreMeasurementID : coreMeasurementID , Code : code } , 'CMAID' ) ;
57
+ // Handle CM Attributes Upsert
58
+ if ( codes ) {
59
+ const parsedCodes = codes
60
+ . split ( ';' )
61
+ . map ( code => code . trim ( ) )
62
+ . filter ( Boolean ) ;
63
+ if ( parsedCodes . length === 0 ) {
64
+ console . error ( 'No valid attribute codes found:' , codes ) ;
65
+ } else {
66
+ for ( const code of parsedCodes ) {
67
+ const attributeRows = await runQuery ( connection , `SELECT COUNT(*) as count FROM ${ schema } .attributes WHERE Code = ?` , [ code ] ) ;
68
+ if ( ! attributeRows || attributeRows . length === 0 || ! attributeRows [ 0 ] . count ) {
69
+ throw createError ( `Attribute code ${ code } not found or query failed.` , { code } ) ;
75
70
}
71
+ await handleUpsert < CMAttributesResult > ( connection , schema , 'cmattributes' , { CoreMeasurementID : coreMeasurementID , Code : code } , 'CMAID' ) ;
76
72
}
77
73
}
74
+ }
78
75
79
- // Update Census Start/End Dates
80
- const combinedQuery = `
76
+ // Update Census Start/End Dates
77
+ const combinedQuery = `
81
78
UPDATE ${ schema } .census c
82
79
JOIN (
83
80
SELECT CensusID, MIN(MeasurementDate) AS FirstMeasurementDate, MAX(MeasurementDate) AS LastMeasurementDate
@@ -88,11 +85,10 @@ export async function processCensus(props: Readonly<SpecialProcessingProps>): Pr
88
85
SET c.StartDate = m.FirstMeasurementDate, c.EndDate = m.LastMeasurementDate
89
86
WHERE c.CensusID = ${ censusID } ;` ;
90
87
91
- await runQuery ( connection , combinedQuery ) ;
92
- await connection . commit ( ) ;
93
- console . log ( 'Upsert successful. CoreMeasurement ID generated:' , coreMeasurementID ) ;
94
- return coreMeasurementID ;
95
- }
88
+ await runQuery ( connection , combinedQuery ) ;
89
+ await connection . commit ( ) ;
90
+ console . log ( 'Upsert successful. CoreMeasurement ID generated:' , coreMeasurementID ) ;
91
+ return coreMeasurementID ;
96
92
}
97
93
}
98
94
} catch ( error : any ) {
0 commit comments