diff --git a/api/src/paths/project/create.test.ts b/api/src/paths/project/create.test.ts index 61e75615c3..75fd185988 100644 --- a/api/src/paths/project/create.test.ts +++ b/api/src/paths/project/create.test.ts @@ -30,7 +30,7 @@ describe('create', () => { sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); - sinon.stub(ProjectService.prototype, 'createProject').resolves(1); + sinon.stub(ProjectService.prototype, 'createProjectAndUploadToBiohub').resolves(1); const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); @@ -51,7 +51,7 @@ describe('create', () => { sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); - sinon.stub(ProjectService.prototype, 'createProject').rejects(new Error('a test error')); + sinon.stub(ProjectService.prototype, 'createProjectAndUploadToBiohub').rejects(new Error('a test error')); const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); diff --git a/api/src/paths/project/create.ts b/api/src/paths/project/create.ts index c0f00174d2..252af6464f 100644 --- a/api/src/paths/project/create.ts +++ b/api/src/paths/project/create.ts @@ -87,7 +87,7 @@ export function createProject(): RequestHandler { const projectService = new ProjectService(connection); - const projectId = await projectService.createProject(sanitizedProjectPostData); + const projectId = await projectService.createProjectAndUploadToBiohub(sanitizedProjectPostData); await connection.commit(); diff --git a/api/src/paths/project/{projectId}/survey/create.test.ts b/api/src/paths/project/{projectId}/survey/create.test.ts index 9db1df370d..3ee6c7371e 100644 --- a/api/src/paths/project/{projectId}/survey/create.test.ts +++ b/api/src/paths/project/{projectId}/survey/create.test.ts @@ -21,7 +21,7 @@ describe('survey/create', () => { sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); - sinon.stub(SurveyService.prototype, 'createSurvey').resolves(2); + sinon.stub(SurveyService.prototype, 'createSurveyAndUploadToBiohub').resolves(2); const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); @@ -45,7 +45,7 @@ describe('survey/create', () => { sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); - sinon.stub(SurveyService.prototype, 'createSurvey').rejects(new Error('a test error')); + sinon.stub(SurveyService.prototype, 'createSurveyAndUploadToBiohub').rejects(new Error('a test error')); const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); diff --git a/api/src/paths/project/{projectId}/survey/create.ts b/api/src/paths/project/{projectId}/survey/create.ts index 6b0708d002..279fe09483 100644 --- a/api/src/paths/project/{projectId}/survey/create.ts +++ b/api/src/paths/project/{projectId}/survey/create.ts @@ -252,7 +252,7 @@ export function createSurvey(): RequestHandler { const surveyService = new SurveyService(connection); - const surveyId = await surveyService.createSurvey(projectId, sanitizedPostSurveyData); + const surveyId = await surveyService.createSurveyAndUploadToBiohub(projectId, sanitizedPostSurveyData); await connection.commit(); diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/update.test.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/update.test.ts index fc80acd387..99d55e9c7c 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/update.test.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/update.test.ts @@ -20,7 +20,7 @@ describe('updateSurvey', () => { sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); - sinon.stub(SurveyService.prototype, 'updateSurvey').resolves(); + sinon.stub(SurveyService.prototype, 'updateSurveyAndUploadToBiohub').resolves(); const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); @@ -48,7 +48,7 @@ describe('updateSurvey', () => { sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); - sinon.stub(SurveyService.prototype, 'updateSurvey').rejects(new Error('a test error')); + sinon.stub(SurveyService.prototype, 'updateSurveyAndUploadToBiohub').rejects(new Error('a test error')); const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts index 4553d9e8af..a5a9813266 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts @@ -289,7 +289,7 @@ export function updateSurvey(): RequestHandler { const surveyService = new SurveyService(connection); - await surveyService.updateSurvey(surveyId, sanitizedPutSurveyData); + await surveyService.updateSurveyAndUploadToBiohub(surveyId, sanitizedPutSurveyData); await connection.commit(); diff --git a/api/src/paths/project/{projectId}/update.test.ts b/api/src/paths/project/{projectId}/update.test.ts index 69f50c069f..009ce0aaea 100644 --- a/api/src/paths/project/{projectId}/update.test.ts +++ b/api/src/paths/project/{projectId}/update.test.ts @@ -154,7 +154,7 @@ describe('update', () => { sinon.stub(db, 'getDBConnection').returns(dbConnectionObj); - sinon.stub(ProjectService.prototype, 'updateProject').resolves(); + sinon.stub(ProjectService.prototype, 'updateProjectAndUploadToBiohub').resolves(); const requestHandler = update.updateProject(); diff --git a/api/src/paths/project/{projectId}/update.ts b/api/src/paths/project/{projectId}/update.ts index 8ab60dc3bf..21e1c4d78e 100644 --- a/api/src/paths/project/{projectId}/update.ts +++ b/api/src/paths/project/{projectId}/update.ts @@ -444,7 +444,7 @@ export function updateProject(): RequestHandler { const projectService = new ProjectService(connection); - await projectService.updateProject(projectId, entities); + await projectService.updateProjectAndUploadToBiohub(projectId, entities); await connection.commit(); diff --git a/api/src/services/project-service.test.ts b/api/src/services/project-service.test.ts index a440d6d4a9..6ee2e64693 100644 --- a/api/src/services/project-service.test.ts +++ b/api/src/services/project-service.test.ts @@ -2,6 +2,7 @@ import chai, { expect } from 'chai'; import { describe } from 'mocha'; import sinon from 'sinon'; import sinonChai from 'sinon-chai'; +import { PostProjectObject } from '../models/project-create'; import { GetCoordinatorData, GetFundingData, @@ -13,11 +14,15 @@ import { } from '../models/project-view'; import { ProjectRepository } from '../repositories/project-repository'; import { getMockDBConnection } from '../__mocks__/db'; +import { PlatformService } from './platform-service'; import { ProjectService } from './project-service'; chai.use(sinonChai); describe('ProjectService', () => { + afterEach(() => { + sinon.restore(); + }); describe('ensureProjectParticipant', () => { afterEach(() => { sinon.restore(); @@ -116,6 +121,38 @@ describe('ProjectService', () => { expect(repoStub).to.be.calledOnce; expect(response).to.eql(undefined); }); + + describe('createProjectAndUploadToBiohub', () => { + it('returns projectId on success', async () => { + const dbConnection = getMockDBConnection(); + const service = new ProjectService(dbConnection); + + const repoStub1 = sinon.stub(ProjectService.prototype, 'createProject').resolves(1); + const repoStub2 = sinon.stub(PlatformService.prototype, 'submitAndPublishDwcAMetadata').resolves(); + + const response = await service.createProjectAndUploadToBiohub((null as unknown) as PostProjectObject); + + expect(repoStub1).to.be.calledOnce; + expect(repoStub2).to.be.calledOnce; + expect(response).to.eql(1); + }); + }); + + describe('updateProjectAndUploadToBiohub', () => { + it('successfully updates project', async () => { + const dbConnection = getMockDBConnection(); + const service = new ProjectService(dbConnection); + + const repoStub1 = sinon.stub(ProjectService.prototype, 'updateProject').resolves(); + const repoStub2 = sinon.stub(PlatformService.prototype, 'submitAndPublishDwcAMetadata').resolves(); + + const response = await service.updateProjectAndUploadToBiohub(1, (null as unknown) as PostProjectObject); + + expect(repoStub1).to.be.calledOnce; + expect(repoStub2).to.be.calledOnce; + expect(response).to.eql(undefined); + }); + }); }); describe('getProjectList', () => { diff --git a/api/src/services/project-service.ts b/api/src/services/project-service.ts index 498b416f2a..2bb6e4f9de 100644 --- a/api/src/services/project-service.ts +++ b/api/src/services/project-service.ts @@ -286,6 +286,29 @@ export class ProjectService extends DBService { return this.projectRepository.getReportAttachmentsData(projectId); } + /** + * + * + * @param {PostProjectObject} postProjectData + * @return {*} {Promise} + * @memberof ProjectService + */ + async createProjectAndUploadToBiohub(postProjectData: PostProjectObject): Promise { + const projectId = await this.createProject(postProjectData); + + //Submit Eml to biohub and publish record + await this.platformService.submitAndPublishDwcAMetadata(projectId); + + return projectId; + } + + /** + * + * + * @param {PostProjectObject} postProjectData + * @return {*} {Promise} + * @memberof ProjectService + */ async createProject(postProjectData: PostProjectObject): Promise { const projectId = await this.insertProject(postProjectData); @@ -341,9 +364,6 @@ export class ProjectService extends DBService { // The user that creates a project is automatically assigned a project lead role, for this project await this.insertParticipantRole(projectId, PROJECT_ROLE.PROJECT_LEAD); - //Submit Eml to biohub and publish record - await this.platformService.submitAndPublishDwcAMetadata(projectId); - return projectId; } @@ -375,6 +395,28 @@ export class ProjectService extends DBService { return this.projectRepository.insertParticipantRole(projectId, projectParticipantRole); } + /** + * Updates the project and uploads to Biohub + * + * @param {number} projectId + * @param {IUpdateProject} entities + * @return {*} + * @memberof ProjectService + */ + async updateProjectAndUploadToBiohub(projectId: number, entities: IUpdateProject) { + await this.updateProject(projectId, entities); + + // Update Eml to biohub and publish record + return await this.platformService.submitAndPublishDwcAMetadata(projectId); + } + + /** + * Updates the project + * + * @param {number} projectId + * @param {IUpdateProject} entities + * @memberof ProjectService + */ async updateProject(projectId: number, entities: IUpdateProject) { const promises: Promise[] = []; @@ -395,9 +437,6 @@ export class ProjectService extends DBService { } await Promise.all(promises); - - // Update Eml to biohub and publish record - return this.platformService.submitAndPublishDwcAMetadata(projectId); } async updateIUCNData(projectId: number, entities: IUpdateProject): Promise { diff --git a/api/src/services/survey-service.test.ts b/api/src/services/survey-service.test.ts index b725e72f51..4d0ca15dcb 100644 --- a/api/src/services/survey-service.test.ts +++ b/api/src/services/survey-service.test.ts @@ -122,7 +122,7 @@ describe('SurveyService', () => { const surveyId = 2; const putSurveyData = new PutSurveyObject(null); - await surveyService.updateSurvey(surveyId, putSurveyData); + await surveyService.updateSurveyAndUploadToBiohub(surveyId, putSurveyData); expect(updateSurveyDetailsDataStub).not.to.have.been.called; expect(updateSurveyVantageCodesDataStub).not.to.have.been.called; @@ -166,7 +166,7 @@ describe('SurveyService', () => { location: {} }); - await surveyService.updateSurvey(surveyId, putSurveyData); + await surveyService.updateSurveyAndUploadToBiohub(surveyId, putSurveyData); expect(updateSurveyDetailsDataStub).to.have.been.calledOnce; expect(updateSurveyVantageCodesDataStub).to.have.been.calledOnce; @@ -1080,4 +1080,45 @@ describe('SurveyService', () => { } }); }); + + describe('createSurveyAndUploadToBiohub', () => { + it('returns projectId on success', async () => { + const dbConnection = getMockDBConnection(); + const service = new SurveyService(dbConnection); + + const repoStub1 = sinon.stub(SurveyService.prototype, 'createSurvey').resolves(1); + const repoStub2 = sinon.stub(PlatformService.prototype, 'submitAndPublishDwcAMetadata').resolves(); + + const response = await service.createSurveyAndUploadToBiohub(1, (null as unknown) as PostSurveyObject); + + expect(repoStub1).to.be.calledOnce; + expect(repoStub2).to.be.calledOnce; + expect(response).to.eql(1); + }); + }); + + describe('updateProjectAndUploadToBiohub', () => { + it('successfully updates project', async () => { + const dbConnection = getMockDBConnection(); + const service = new SurveyService(dbConnection); + + const repoStub1 = sinon.stub(SurveyService.prototype, 'updateSurvey').resolves(({ + survey_details: { + survey_name: 'my survey', + start_date: '2020-10-10', + end_date: '2021-10-10', + biologist_last_name: 'henry', + biologist_first_name: 'erin', + revision_count: 1 + } + } as unknown) as SurveyObject); + const repoStub2 = sinon.stub(PlatformService.prototype, 'submitAndPublishDwcAMetadata').resolves(); + + const response = await service.updateSurveyAndUploadToBiohub(1, (null as unknown) as PutSurveyObject); + + expect(repoStub1).to.be.calledOnce; + expect(repoStub2).to.be.calledOnce; + expect(response).to.eql(undefined); + }); + }); }); diff --git a/api/src/services/survey-service.ts b/api/src/services/survey-service.ts index c0b4c6189c..cca6ba8a54 100644 --- a/api/src/services/survey-service.ts +++ b/api/src/services/survey-service.ts @@ -295,11 +295,28 @@ export class SurveyService extends DBService { } /** - * Creates a new survey for a project and returns survey ID + * Creates a survey and uploads the metadata to Biohub * * @param {number} projectId * @param {PostSurveyObject} postSurveyData - * @returns {*} {Promise} + * @return {*} {Promise} + * @memberof SurveyService + */ + async createSurveyAndUploadToBiohub(projectId: number, postSurveyData: PostSurveyObject): Promise { + const surveyId = await this.createSurvey(projectId, postSurveyData); + + //Update Eml to biohub and publish record + await this.platformService.submitAndPublishDwcAMetadata(projectId, surveyId); + + return surveyId; + } + + /** + * Creates the survey + * + * @param {number} projectId + * @param {PostSurveyObject} postSurveyData + * @return {*} {Promise} * @memberof SurveyService */ async createSurvey(projectId: number, postSurveyData: PostSurveyObject): Promise { @@ -354,9 +371,6 @@ export class SurveyService extends DBService { await Promise.all(promises); - //Update Eml to biohub and publish record - await this.platformService.submitAndPublishDwcAMetadata(projectId, surveyId); - return surveyId; } @@ -487,7 +501,22 @@ export class SurveyService extends DBService { * @returns {*} {Promise} * @memberof SurveyService */ - async updateSurvey(surveyId: number, putSurveyData: PutSurveyObject): Promise { + async updateSurveyAndUploadToBiohub(surveyId: number, putSurveyData: PutSurveyObject): Promise { + const surveyData = await this.updateSurvey(surveyId, putSurveyData); + + // Update Eml to biohub and publish record + return await this.platformService.submitAndPublishDwcAMetadata(surveyData.survey_details.project_id, surveyId); + } + + /** + * Updates provided survey information and submits to BioHub + * + * @param {number} surveyId + * @param {PutSurveyObject} putSurveyData + * @returns {*} {Promise} + * @memberof SurveyService + */ + async updateSurvey(surveyId: number, putSurveyData: PutSurveyObject): Promise { const promises: Promise[] = []; if (putSurveyData?.survey_details || putSurveyData?.purpose_and_methodology || putSurveyData?.location) { @@ -516,10 +545,7 @@ export class SurveyService extends DBService { await Promise.all(promises); - const surveyData = await this.getSurveyById(surveyId); - - // Update Eml to biohub and publish record - return this.platformService.submitAndPublishDwcAMetadata(surveyData.survey_details.project_id, surveyId); + return await this.getSurveyById(surveyId); } /**