diff --git a/api/src/models/biohub-create.test.ts b/api/src/models/biohub-create.test.ts index 1d6351a2c4..4c1f8b8230 100644 --- a/api/src/models/biohub-create.test.ts +++ b/api/src/models/biohub-create.test.ts @@ -118,6 +118,7 @@ describe('PostSurveyToBiohubObject', () => { survey_obj, [observation_obj], { type: 'FeatureCollection', features: [] }, + [], [] ); }); @@ -206,6 +207,7 @@ describe('PostSurveySubmissionToBioHubObject', () => { observation_obj, survey_geometry, [], + [], submissionComment ); }); @@ -227,7 +229,7 @@ describe('PostSurveySubmissionToBioHubObject', () => { }); it('sets content', () => { - expect(data.content).to.eql(new PostSurveyToBiohubObject(survey_obj, observation_obj, survey_geometry, [])); + expect(data.content).to.eql(new PostSurveyToBiohubObject(survey_obj, observation_obj, survey_geometry, [], [])); }); }); }); diff --git a/api/src/repositories/attachment-repository.ts b/api/src/repositories/attachment-repository.ts index 06dec5539b..21ec9ee356 100644 --- a/api/src/repositories/attachment-repository.ts +++ b/api/src/repositories/attachment-repository.ts @@ -1,5 +1,6 @@ import { QueryResult } from 'pg'; import SQL from 'sql-template-strings'; +import { ATTACHMENT_TYPE } from '../constants/attachments'; import { getKnex } from '../database/db'; import { ApiExecuteSQLError } from '../errors/api-error'; import { PostReportAttachmentMetadata, PutReportAttachmentMetadata } from '../models/project-survey-attachments'; @@ -414,6 +415,46 @@ export class AttachmentRepository extends BaseRepository { return response.rows; } + /** + * Get all survey attachments for the given survey id, which are safe to publish to BioHub. + * + * Note: Not all attachment types are publishable to BioHub. This method filters out attachment types that should not + * be published. + * + * @param {number} surveyId + * @param {number[]} attachmentIds + * @return {*} {Promise} + * @memberof AttachmentRepository + */ + async getSurveyAttachmentsForBioHubSubmission(surveyId: number): Promise { + defaultLog.debug({ label: 'getSurveyAttachmentsForBioHubSubmission' }); + + const sqlStatement = SQL` + SELECT + survey_attachment_id, + uuid, + file_name, + file_type, + title, + description, + create_date, + update_date, + create_date, + file_size, + key + FROM + survey_attachment + WHERE + survey_id = ${surveyId} + AND + LOWER(file_type) != LOWER(${ATTACHMENT_TYPE.KEYX}); + `; + + const response = await this.connection.sql(sqlStatement); + + return response.rows; + } + /** * Query to return all survey report attachments belonging to the given survey. * @param {number} surveyId the ID of the survey diff --git a/api/src/services/attachment-service.ts b/api/src/services/attachment-service.ts index 8b51327cd4..c3f40f92ed 100644 --- a/api/src/services/attachment-service.ts +++ b/api/src/services/attachment-service.ts @@ -223,6 +223,20 @@ export class AttachmentService extends DBService { return this.attachmentRepository.getSurveyAttachmentsByIds(surveyId, attachmentIds); } + /** + * Get all survey attachments for the given survey ID, which are publishable to BioHub. + * + * Note: Not all attachment types are publishable to BioHub. This method filters out attachment types that should not + * be published. + * + * @param {number} surveyId the ID of the survey. + * @return {Promise} Promise resolving all survey publishable attachments. + * @memberof AttachmentService + */ + async getSurveyAttachmentsForBioHubSubmission(surveyId: number): Promise { + return this.attachmentRepository.getSurveyAttachmentsForBioHubSubmission(surveyId); + } + /** * Finds all of the survey report attachments for the given survey ID. * @param {number} surveyId the ID of the survey diff --git a/api/src/services/platform-service.test.ts b/api/src/services/platform-service.test.ts index b3da4edd40..1f0cb7e76a 100644 --- a/api/src/services/platform-service.test.ts +++ b/api/src/services/platform-service.test.ts @@ -48,7 +48,9 @@ describe('PlatformService', () => { .stub(KeycloakService.prototype, 'getKeycloakServiceToken') .resolves('token'); - sinon.stub(AttachmentService.prototype, 'getSurveyAttachments').resolves([]); + sinon.stub(AttachmentService.prototype, 'getSurveyAttachmentsForBioHubSubmission').resolves([]); + + sinon.stub(AttachmentService.prototype, 'getSurveyReportAttachments').resolves([]); const _generateSurveyDataPackageStub = sinon .stub(PlatformService.prototype, '_generateSurveyDataPackage') @@ -61,7 +63,7 @@ describe('PlatformService', () => { } catch (error) { expect((error as Error).message).to.equal('Failed to submit survey ID to Biohub'); expect(getKeycloakServiceTokenStub).to.have.been.calledOnce; - expect(_generateSurveyDataPackageStub).to.have.been.calledOnceWith(1, [], 'test'); + expect(_generateSurveyDataPackageStub).to.have.been.calledOnceWith(1, [], [], 'test'); } }); @@ -76,7 +78,9 @@ describe('PlatformService', () => { .stub(KeycloakService.prototype, 'getKeycloakServiceToken') .resolves('token'); - sinon.stub(AttachmentService.prototype, 'getSurveyAttachments').resolves([]); + sinon.stub(AttachmentService.prototype, 'getSurveyAttachmentsForBioHubSubmission').resolves([]); + + sinon.stub(AttachmentService.prototype, 'getSurveyReportAttachments').resolves([]); const _generateSurveyDataPackageStub = sinon .stub(PlatformService.prototype, '_generateSurveyDataPackage') @@ -88,6 +92,10 @@ describe('PlatformService', () => { .stub(PlatformService.prototype, '_submitSurveyAttachmentsToBioHub') .resolves(); + const _submitSurveyReportAttachmentsToBioHubStub = sinon + .stub(PlatformService.prototype, '_submitSurveyReportAttachmentsToBioHub') + .resolves(); + const insertSurveyMetadataPublishRecordStub = sinon .stub(HistoryPublishService.prototype, 'insertSurveyMetadataPublishRecord') .resolves(); @@ -95,8 +103,9 @@ describe('PlatformService', () => { const response = await platformService.submitSurveyToBioHub(1, { submissionComment: 'test' }); expect(getKeycloakServiceTokenStub).to.have.been.calledOnce; - expect(_generateSurveyDataPackageStub).to.have.been.calledOnceWith(1, [], 'test'); + expect(_generateSurveyDataPackageStub).to.have.been.calledOnceWith(1, [], [], 'test'); expect(_submitSurveyAttachmentsToBioHubStub).to.have.been.calledOnceWith('123-456-789', [], []); + expect(_submitSurveyReportAttachmentsToBioHubStub).to.have.been.calledOnceWith('123-456-789', [], []); expect(insertSurveyMetadataPublishRecordStub).to.have.been.calledOnceWith({ survey_id: 1, submission_uuid: '123-456-789' @@ -128,7 +137,7 @@ describe('PlatformService', () => { .stub(SurveyService.prototype, 'getSurveyLocationsData') .resolves([] as any); - const response = await platformService._generateSurveyDataPackage(1, [], 'a comment about the submission'); + const response = await platformService._generateSurveyDataPackage(1, [], [], 'a comment about the submission'); expect(getSurveyDataStub).to.have.been.calledOnceWith(1); expect(getSurveyPurposeAndMethodologyStub).to.have.been.calledOnceWith(1); diff --git a/api/src/services/platform-service.ts b/api/src/services/platform-service.ts index 575deee6f7..6243186eb8 100644 --- a/api/src/services/platform-service.ts +++ b/api/src/services/platform-service.ts @@ -84,7 +84,7 @@ export class PlatformService extends DBService { const backboneSurveyIntakeUrl = new URL(getBackboneSurveyIntakePath(), getBackboneApiHost()).href; // Get survey attachments - const surveyAttachments = await this.attachmentService.getSurveyAttachments(surveyId); + const surveyAttachments = await this.attachmentService.getSurveyAttachmentsForBioHubSubmission(surveyId); // Get survey report attachments const surveyReportAttachments = await this.attachmentService.getSurveyReportAttachments(surveyId);