Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIMSBIOHUB 347/341 - Names from shapefiles #1146

Merged
merged 8 commits into from
Oct 31, 2023
13 changes: 8 additions & 5 deletions api/src/repositories/sample-location-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ export class SampleLocationRepository extends BaseRepository {
* @memberof SampleLocationRepository
*/
async insertSampleLocation(sample: InsertSampleLocationRecord): Promise<SampleLocationRecord> {
const name = sample.name
GrahamS-Quartech marked this conversation as resolved.
Show resolved Hide resolved
? SQL`${sample.name}`
: SQL`(SELECT concat('Sample Site ', (SELECT count(survey_sample_site_id) + 1 FROM survey_sample_site sss WHERE survey_id = ${sample.survey_id})))`;

const sqlStatement = SQL`
INSERT INTO survey_sample_site (
survey_id,
Expand All @@ -160,11 +164,10 @@ export class SampleLocationRepository extends BaseRepository {
geojson,
geography
) VALUES (
${sample.survey_id},
(SELECT concat('Sample Site ', (SELECT count(survey_sample_site_id) + 1 FROM survey_sample_site sss WHERE survey_id = ${sample.survey_id}))),
${sample.description},
${sample.geojson},
`;
${sample.survey_id},`.append(name).append(SQL`,
${sample.description},
${sample.geojson},
`);
const geometryCollectionSQL = generateGeometryCollectionSQL(sample.geojson);

sqlStatement.append(SQL`
Expand Down
21 changes: 16 additions & 5 deletions api/src/services/sample-location-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Feature } from '@turf/helpers';
import { Feature, Geometry, GeometryCollection, Properties } from '@turf/helpers';
import { IDBConnection } from '../database/db';
import {
SampleLocationRecord,
Expand Down Expand Up @@ -64,13 +64,24 @@ export class SampleLocationService extends DBService {
*/
async insertSampleLocations(sampleLocations: PostSampleLocations): Promise<SampleLocationRecord[]> {
const methodService = new SampleMethodService(this.connection);

const determineName = (geometry: Feature<Geometry | GeometryCollection, Properties>) => {
GrahamS-Quartech marked this conversation as resolved.
Show resolved Hide resolved
const nameKey = Object.keys(geometry.properties ?? {}).find(
(key) => key.toLowerCase() === 'name' || key.toLowerCase() === 'label'
);
return nameKey && geometry.properties ? geometry.properties[nameKey].substring(0, 50) : '';
};
const determineDesc = (geometry: Feature<Geometry | GeometryCollection, Properties>) => {
const descKey = Object.keys(geometry.properties ?? {}).find(
(key) => key.toLowerCase() === 'desc' || key.toLowerCase() === 'description'
GrahamS-Quartech marked this conversation as resolved.
Show resolved Hide resolved
);
return descKey && geometry.properties ? geometry.properties[descKey].substring(0, 250) : '';
GrahamS-Quartech marked this conversation as resolved.
Show resolved Hide resolved
};
// Create a sample location for each feature found
const promises = sampleLocations.survey_sample_sites.map((item, index) => {
const promises = sampleLocations.survey_sample_sites.map((item) => {
const sampleLocation = {
survey_id: sampleLocations.survey_id,
name: `Sample Site ${index + 1}`, // Business requirement to default the names to Sample Site # on creation
description: sampleLocations.description,
name: determineName(item), // Please note that additional logic for this also happens in insertSampleLocation below
GrahamS-Quartech marked this conversation as resolved.
Show resolved Hide resolved
description: determineDesc(item) || sampleLocations.description,
geojson: item
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface ISampleSiteEditFormProps {

export const samplingSiteYupSchema = yup.object({
sampleSite: yup.object({
name: yup.string().default(''),
name: yup.string().default('').max(50),
GrahamS-Quartech marked this conversation as resolved.
Show resolved Hide resolved
description: yup.string().default('').nullable(),
survey_sample_sites: yup
.array(yup.object())
Expand Down