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

Bug Fix: Misc #1185

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/src/database/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export const getDBConnection = function (keycloakToken: KeycloakUserInformation)
const zodEnd = Date.now();

defaultLog.silly({
label: '_sq + zod',
label: '_sql + zod',
message: sqlStatement.text,
queryExecutionTime: queryEnd - queryStart,
zodExecutionTime: zodEnd - zodStart
Expand Down
90 changes: 70 additions & 20 deletions api/src/models/biohub-create.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { expect } from 'chai';
import { FeatureCollection } from 'geojson';
import { describe } from 'mocha';
import { ObservationRecord } from '../repositories/observation-repository';
import { PostSurveyObservationToBiohubObject, PostSurveyToBiohubObject } from './biohub-create';
import {
PostSurveyObservationToBiohubObject,
PostSurveySubmissionToBioHubObject,
PostSurveyToBiohubObject
} from './biohub-create';
import { GetSurveyData } from './survey-view';

describe('PostSurveyObservationToBiohubObject', () => {
Expand Down Expand Up @@ -127,25 +132,70 @@ describe('PostSurveyToBiohubObject', () => {
});

it('sets features', () => {
expect(data.features).to.eql([
{
id: '1',
type: 'observation',
properties: {
survey_id: 1,
taxonomy: 1,
survey_sample_site_id: 1,
survey_sample_method_id: 1,
survey_sample_period_id: 1,
latitude: 1,
longitude: 1,
count: 1,
observation_time: 'observation_time',
observation_date: 'observation_date'
},
features: []
}
]);
expect(data.features).to.eql([new PostSurveyObservationToBiohubObject(observation_obj)]);
});
});
});

describe('PostSurveySubmissionToBioHubObject', () => {
describe('All values provided', () => {
let data: PostSurveySubmissionToBioHubObject;

const observation_obj: ObservationRecord[] = [
{
survey_observation_id: 1,
survey_id: 1,
wldtaxonomic_units_id: 1,
survey_sample_site_id: 1,
survey_sample_method_id: 1,
survey_sample_period_id: 1,
latitude: 1,
longitude: 1,
count: 1,
observation_time: 'observation_time',
observation_date: 'observation_date',
create_date: 'create_date',
create_user: 1,
update_date: 'update_date',
update_user: 1,
revision_count: 1
}
];

const survey_obj: GetSurveyData = {
id: 1,
uuid: '1',
project_id: 1,
survey_name: 'survey_name',
start_date: 'start_date',
end_date: 'end_date',
survey_types: [9],
revision_count: 1
};

const survey_geometry: FeatureCollection = {
type: 'FeatureCollection',
features: []
};

before(() => {
data = new PostSurveySubmissionToBioHubObject(survey_obj, observation_obj, survey_geometry);
});

it('sets id', () => {
expect(data.id).to.equal('1');
});

it('sets name', () => {
expect(data.name).to.equal('survey_name');
});

it('sets description', () => {
expect(data.description).to.equal('A Temp Description');
});

it('sets features', () => {
expect(data.features).to.eql([new PostSurveyToBiohubObject(survey_obj, observation_obj, survey_geometry)]);
});
});
});
42 changes: 37 additions & 5 deletions api/src/models/biohub-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ import { GetSurveyData } from './survey-view';

const defaultLog = getLogger('models/biohub-create');

export interface BioHubSubmissionFeature {
id: string;
type: string;
properties: Record<string, any>;
features: BioHubSubmissionFeature[];
}

/**
* Object to be sent to Biohub API for creating an observation.
*
* @export
* @class PostSurveyObservationToBiohubObject
* @implements {BioHubSubmissionFeature}
*/
export class PostSurveyObservationToBiohubObject {
export class PostSurveyObservationToBiohubObject implements BioHubSubmissionFeature {
id: string;
type: string;
properties: object;
features: [];
properties: Record<string, any>;
features: BioHubSubmissionFeature[];

constructor(observationRecord: ObservationRecord) {
defaultLog.debug({ label: 'PostSurveyObservationToBiohubObject', message: 'params', observationRecord });
Expand Down Expand Up @@ -43,11 +51,12 @@ export class PostSurveyObservationToBiohubObject {
*
* @export
* @class PostSurveyToBiohubObject
* @implements {BioHubSubmissionFeature}
*/
export class PostSurveyToBiohubObject {
export class PostSurveyToBiohubObject implements BioHubSubmissionFeature {
id: string;
type: string;
properties: object;
properties: Record<string, any>;
features: PostSurveyObservationToBiohubObject[];

constructor(
Expand Down Expand Up @@ -75,6 +84,29 @@ export class PostSurveyToBiohubObject {
}
}

export class PostSurveySubmissionToBioHubObject {
id: string;
name: string;
description: string;
features: BioHubSubmissionFeature[];

constructor(
surveyData: GetSurveyData,
observationRecords: ObservationRecord[],
surveyGeometry: FeatureCollection,
additionalInformation?: string
) {
defaultLog.debug({ label: 'PostSurveySubmissionToBioHubObject' });

this.id = surveyData.uuid;
this.name = surveyData.survey_name;
this.description = 'A Temp Description'; // TODO replace with data entered in survey publish dialog
this.features = [
new PostSurveyToBiohubObject(surveyData, observationRecords, surveyGeometry, additionalInformation)
];
}
}

export enum BiohubFeatureType {
DATASET = 'dataset',
OBSERVATION = 'observation'
Expand Down
2 changes: 1 addition & 1 deletion api/src/repositories/survey-location-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const SurveyLocationRecord = z.object({
description: z.string(),
geometry: z.record(z.any()).nullable(),
geography: z.string(),
geojson: GeoJSONFeatureZodSchema,
geojson: z.array(GeoJSONFeatureZodSchema),
revision_count: z.number()
});

Expand Down
61 changes: 34 additions & 27 deletions api/src/services/platform-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,38 +351,45 @@ describe('PlatformService', () => {
expect(getSurveyLocationsDataStub).to.have.been.calledOnceWith(1);
expect(response).to.eql({
id: '1',
type: 'dataset',
properties: {
additional_information: 'test',
survey_id: undefined,
project_id: undefined,
name: undefined,
start_date: undefined,
end_date: undefined,
survey_types: undefined,
revision_count: undefined,
geometry: {
type: 'FeatureCollection',
features: []
}
},
name: undefined,
description: 'A Temp Description',
features: [
{
id: '2',
type: 'observation',
id: '1',
type: 'dataset',
properties: {
additional_information: 'test',
survey_id: undefined,
taxonomy: undefined,
survey_sample_site_id: null,
survey_sample_method_id: null,
survey_sample_period_id: null,
latitude: undefined,
longitude: undefined,
count: undefined,
observation_time: undefined,
observation_date: undefined
project_id: undefined,
name: undefined,
start_date: undefined,
end_date: undefined,
survey_types: undefined,
revision_count: undefined,
geometry: {
type: 'FeatureCollection',
features: []
}
},
features: []
features: [
{
id: '2',
type: 'observation',
properties: {
survey_id: undefined,
taxonomy: undefined,
survey_sample_site_id: null,
survey_sample_method_id: null,
survey_sample_period_id: null,
latitude: undefined,
longitude: undefined,
count: undefined,
observation_time: undefined,
observation_date: undefined
},
features: []
}
]
}
]
});
Expand Down
16 changes: 9 additions & 7 deletions api/src/services/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { v4 as uuidv4 } from 'uuid';
import { IDBConnection } from '../database/db';
import { ApiError, ApiErrorType, ApiGeneralError } from '../errors/api-error';
import { PostSurveyToBiohubObject } from '../models/biohub-create';
import { PostSurveySubmissionToBioHubObject } from '../models/biohub-create';
import {
IProjectAttachment,
IProjectReportAttachment,
Expand Down Expand Up @@ -264,10 +264,9 @@
/**
* Submit survey ID to BioHub.
*
* @param {string} surveyUUID
* @param {number} surveyId
* @param {{ additionalInformation: string }} data
* @return {*} {Promise<{ queue_id: number }>}
* @return {*} {Promise<{ submission_id: number }>}
* @memberof PlatformService
*/
async submitSurveyToBioHub(
Expand Down Expand Up @@ -316,10 +315,13 @@
*
* @param {number} surveyId
* @param {string} additionalInformation
* @return {*} {Promise<ISubmitSurvey>}
* @return {*} {Promise<PostSurveySubmissionToBioHubObject>}
* @memberof PlatformService
*/
async generateSurveyDataPackage(surveyId: number, additionalInformation: string): Promise<PostSurveyToBiohubObject> {
async generateSurveyDataPackage(
surveyId: number,
additionalInformation: string
): Promise<PostSurveySubmissionToBioHubObject> {
const observationService = new ObservationService(this.connection);
const surveyService = new SurveyService(this.connection);

Expand All @@ -329,10 +331,10 @@

const geometryFeatureCollection: FeatureCollection = {
type: 'FeatureCollection',
features: surveyLocation.flatMap((location) => location.geojson as Feature)
features: surveyLocation.flatMap((location) => location.geojson as Feature[])

Check warning on line 334 in api/src/services/platform-service.ts

View check run for this annotation

Codecov / codecov/patch

api/src/services/platform-service.ts#L334

Added line #L334 was not covered by tests
};

const surveyDataPackage = new PostSurveyToBiohubObject(
const surveyDataPackage = new PostSurveySubmissionToBioHubObject(
survey,
surveyObservations,
geometryFeatureCollection,
Expand Down
8 changes: 4 additions & 4 deletions api/src/zod-schema/geoJsonZodSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ export const GeoJSONMultiPolygonZodSchema = z.object({

export const GeoJSONGeometryCollectionZodSchema = z.object({
type: z.enum(['GeometryCollection']),
geometries: z.array(z.object({})),
geometries: z.array(z.record(z.string(), z.any())),
bbox: z.array(z.number()).min(4).optional()
});

export const GeoJSONFeatureZodSchema = z.object({
type: z.enum(['Feature']),
id: z.union([z.number(), z.string()]).optional(),
properties: z.object({}),
geometry: z.object({}),
properties: z.record(z.string(), z.any()),
geometry: z.record(z.string(), z.any()),
bbox: z.array(z.number()).min(4).optional()
});

export const GeoJSONFeatureCollectionZodSchema = z.object({
type: z.enum(['FeatureCollection']),
features: z.array(z.object({})),
features: z.array(z.record(z.string(), z.any())),
bbox: z.array(z.number()).min(4).optional()
});
Loading