From 95669754eb0f7ff5ec35cff2c599794129d9b0de Mon Sep 17 00:00:00 2001 From: sambokar Date: Fri, 11 Oct 2024 11:59:58 -0400 Subject: [PATCH] viewfulltable bug -- no data being returned. reworked backend SQL condition and verified function on local dev server. Added missing handling for quadrat form file processing -- forgot to add censusquadrat handling to that API --- .../components/processors/processcensus.tsx | 4 +- .../components/processors/processormacros.tsx | 4 +- .../components/processors/processquadrats.tsx | 45 +++++++++++++++++++ frontend/config/datamapper.ts | 4 +- frontend/config/sqlrdsdefinitions/views.ts | 6 --- frontend/config/sqlrdsdefinitions/zones.ts | 10 ++++- frontend/config/utils.ts | 30 ++++++++----- 7 files changed, 79 insertions(+), 24 deletions(-) create mode 100644 frontend/components/processors/processquadrats.tsx diff --git a/frontend/components/processors/processcensus.tsx b/frontend/components/processors/processcensus.tsx index 7ffccdbe..b56015be 100644 --- a/frontend/components/processors/processcensus.tsx +++ b/frontend/components/processors/processcensus.tsx @@ -2,7 +2,7 @@ import { runQuery, SpecialProcessingProps } from '@/components/processors/proces import moment from 'moment'; import { createError, fetchPrimaryKey, handleUpsert } from '@/config/utils'; import { SpeciesResult, StemResult, TreeResult } from '@/config/sqlrdsdefinitions/taxonomies'; -import { QuadratsResult } from '@/config/sqlrdsdefinitions/zones'; +import { QuadratResult } from '@/config/sqlrdsdefinitions/zones'; import { CMAttributesResult, CoreMeasurementsResult } from '@/config/sqlrdsdefinitions/core'; export async function processCensus(props: Readonly): Promise { @@ -21,7 +21,7 @@ export async function processCensus(props: Readonly): Pr const speciesID = await fetchPrimaryKey(schema, 'species', { SpeciesCode: spcode }, connection, 'SpeciesID'); // Fetch quadrat - const quadratID = await fetchPrimaryKey(schema, 'quadrats', { QuadratName: quadrat, PlotID: plotID }, connection, 'QuadratID'); + const quadratID = await fetchPrimaryKey(schema, 'quadrats', { QuadratName: quadrat, PlotID: plotID }, connection, 'QuadratID'); if (tag) { // Handle Tree Upsert diff --git a/frontend/components/processors/processormacros.tsx b/frontend/components/processors/processormacros.tsx index f6c2ac98..09a48104 100644 --- a/frontend/components/processors/processormacros.tsx +++ b/frontend/components/processors/processormacros.tsx @@ -4,6 +4,7 @@ import { processSpecies } from '@/components/processors/processspecies'; import { processCensus } from '@/components/processors/processcensus'; import { PoolMonitor } from '@/config/poolmonitor'; import { processPersonnel } from '@/components/processors/processpersonnel'; +import { processQuadrats } from '@/components/processors/processquadrats'; export interface SpecialProcessingProps { connection: PoolConnection; @@ -71,7 +72,8 @@ export const fileMappings: Record = { dimy: 'DimensionY', dimensionunit: 'DimensionUnits', quadratshape: 'QuadratShape' - } + }, + specialProcessing: processQuadrats }, // "subquadrats": "subquadrat, quadrat, dimx, dimy, xindex, yindex, unit, orderindex", subquadrats: { diff --git a/frontend/components/processors/processquadrats.tsx b/frontend/components/processors/processquadrats.tsx new file mode 100644 index 00000000..35cb0d8a --- /dev/null +++ b/frontend/components/processors/processquadrats.tsx @@ -0,0 +1,45 @@ +import { SpecialProcessingProps } from '@/components/processors/processormacros'; +import { createError, handleUpsert } from '@/config/utils'; +import { CensusQuadratResult, QuadratResult } from '@/config/sqlrdsdefinitions/zones'; + +export async function processQuadrats(props: Readonly) { + const { connection, rowData, schema, censusID } = props; + if (!censusID) throw createError('CensusID missing', { censusID }); + + const { quadrat, startx, starty, coordinateunit, dimx, dimy, dimensionunit, area, areaunit, quadratshape } = rowData; + + try { + await connection.beginTransaction(); + const quadratsData = { + QuadratName: quadrat, + StartX: startx, + StartY: starty, + CoordinateUnit: coordinateunit, + DimensionX: dimx, + DimensionY: dimy, + DimensionUnit: dimensionunit, + Area: area, + AreaUnit: areaunit, + QuadratShape: quadratshape + }; + + const quadratID = await handleUpsert(connection, schema, 'quadrats', quadratsData, 'QuadratID'); + if (!quadratID) throw createError('upsert failure for row: ', { quadratsData }); + + // need to update censusquadrat + + const cqData = { + CensusID: censusID, + QuadratID: quadratID + }; + const cqID = await handleUpsert(connection, schema, 'censusquadrat', cqData, 'CQID'); + if (!cqID) throw createError('upsert failure on censusquadrat for row: ', { cqData }); + + await connection.commit(); + return quadratID; + } catch (error: any) { + await connection.rollback(); + console.error('Upsert failed:', error.message); + throw createError('Upsert failed', { error }); + } +} diff --git a/frontend/config/datamapper.ts b/frontend/config/datamapper.ts index df3f7926..58b4b58b 100644 --- a/frontend/config/datamapper.ts +++ b/frontend/config/datamapper.ts @@ -2,7 +2,7 @@ import moment from 'moment'; import { bitToBoolean, booleanToBit } from './macros'; import { Common, ResultType, Unique } from '@/config/utils'; import { SpeciesLimitsRDS, SpeciesLimitsResult, SpeciesRDS, SpeciesResult, StemRDS, StemResult } from '@/config/sqlrdsdefinitions/taxonomies'; -import { PlotRDS, PlotsResult, QuadratRDS, QuadratsResult, SitesMapper } from '@/config/sqlrdsdefinitions/zones'; +import { PlotRDS, PlotsResult, QuadratRDS, QuadratResult, SitesMapper } from '@/config/sqlrdsdefinitions/zones'; import { AllTaxonomiesViewRDS, AllTaxonomiesViewResult, @@ -208,7 +208,7 @@ class MapperFactory { case 'quadratpersonnel': return new GenericMapper() as unknown as IDataMapper; case 'quadrats': - return new GenericMapper() as unknown as IDataMapper; + return new GenericMapper() as unknown as IDataMapper; case 'sites': return new SitesMapper() as any; case 'species': diff --git a/frontend/config/sqlrdsdefinitions/views.ts b/frontend/config/sqlrdsdefinitions/views.ts index 338c5484..b51b4bf7 100644 --- a/frontend/config/sqlrdsdefinitions/views.ts +++ b/frontend/config/sqlrdsdefinitions/views.ts @@ -134,12 +134,6 @@ export function getStemTaxonomiesViewHCs(): ColumnStates { }; } -/** - * materialized view --> do this before batch updates to make sure that refresh function isn't called for each row - * INSERT INTO batchprocessingflag (flag_status) VALUES ('STARTED') - ON DUPLICATE KEY UPDATE flag_status = 'STARTED'; - */ - export type ViewFullTableRDS = { // datagrid id?: number; diff --git a/frontend/config/sqlrdsdefinitions/zones.ts b/frontend/config/sqlrdsdefinitions/zones.ts index fa28f769..b6c00c8b 100644 --- a/frontend/config/sqlrdsdefinitions/zones.ts +++ b/frontend/config/sqlrdsdefinitions/zones.ts @@ -87,7 +87,7 @@ export type QuadratRDS = { areaUnits?: string; quadratShape?: string; }; -export type QuadratsResult = ResultType; +export type QuadratResult = ResultType; export type Quadrat = QuadratRDS | undefined; export const validateQuadratsRow: ValidationFunction = row => { const errors: RowValidationErrors = {}; @@ -112,6 +112,14 @@ export function getQuadratHCs(): ColumnStates { }; } +export type CensusQuadratRDS = { + id?: number; + cqID?: number; + quadratID?: number; + censusID?: number; +}; +export type CensusQuadratResult = ResultType; + export type SubquadratRDS = { id?: number; subquadratID?: number; diff --git a/frontend/config/utils.ts b/frontend/config/utils.ts index 713a90d2..12622b9e 100644 --- a/frontend/config/utils.ts +++ b/frontend/config/utils.ts @@ -30,21 +30,25 @@ export const toggleSidebar = () => { export type CapitalizeFirstLetter = T extends `${infer F}${infer R}` ? `${Uppercase}${R}` : T; // Utility type to transform 'dbh' and 'hom' to uppercase -export type TransformSpecialCases = T extends `${infer Prefix}dbh${infer Suffix}` - ? `${Prefix}DBH${Suffix}` - : T extends `${infer Prefix}Dbh${infer Suffix}` +export type TransformSpecialCases = T extends `${infer Prefix}CqID${infer Suffix}` + ? `${Prefix}CQID${Suffix}` + : T extends `${infer Prefix}dbh${infer Suffix}` ? `${Prefix}DBH${Suffix}` - : T extends `${infer Prefix}hom${infer Suffix}` - ? `${Prefix}HOM${Suffix}` - : T extends `${infer Prefix}Hom${infer Suffix}` + : T extends `${infer Prefix}Dbh${infer Suffix}` + ? `${Prefix}DBH${Suffix}` + : T extends `${infer Prefix}hom${infer Suffix}` ? `${Prefix}HOM${Suffix}` - : T extends `${infer Prefix}Id${infer Suffix}` - ? `${Prefix}ID${Suffix}` - : T extends `${infer Prefix}cma${infer Suffix}` - ? `${Prefix}CMA${Suffix}` - : T extends `${infer Prefix}Cma${infer Suffix}` + : T extends `${infer Prefix}Hom${infer Suffix}` + ? `${Prefix}HOM${Suffix}` + : T extends `${infer Prefix}Id${infer Suffix}` + ? `${Prefix}ID${Suffix}` + : T extends `${infer Prefix}cma${infer Suffix}` ? `${Prefix}CMA${Suffix}` - : T; + : T extends `${infer Prefix}Cma${infer Suffix}` + ? `${Prefix}CMA${Suffix}` + : T extends `${infer Prefix}cqID${infer Suffix}` + ? `${Prefix}CQID${Suffix}` + : T; // Utility type to omit specific keys export type OmitKey = Pick>; @@ -224,6 +228,8 @@ export function transformSpecialCases(field: string): string { return field.replace(/hom/gi, 'HOM'); } else if (/cma/i.test(field)) { return field.replace(/cma/gi, 'CMA'); + } else if (/cq/i.test(field)) { + return field.replace(/cq/gi, 'CQ'); } // General transformation for ID