Skip to content

Commit

Permalink
Tech debt: decouple upload to Biohub from create/update project/survey (
Browse files Browse the repository at this point in the history
  • Loading branch information
anissa-agahchen authored Mar 14, 2023
1 parent b54b603 commit dfef452
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 29 deletions.
4 changes: 2 additions & 2 deletions api/src/paths/project/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion api/src/paths/project/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions api/src/paths/project/{projectId}/survey/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion api/src/paths/project/{projectId}/survey/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion api/src/paths/project/{projectId}/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion api/src/paths/project/{projectId}/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
37 changes: 37 additions & 0 deletions api/src/services/project-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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', () => {
Expand Down
51 changes: 45 additions & 6 deletions api/src/services/project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,29 @@ export class ProjectService extends DBService {
return this.projectRepository.getReportAttachmentsData(projectId);
}

/**
*
*
* @param {PostProjectObject} postProjectData
* @return {*} {Promise<number>}
* @memberof ProjectService
*/
async createProjectAndUploadToBiohub(postProjectData: PostProjectObject): Promise<number> {
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<number>}
* @memberof ProjectService
*/
async createProject(postProjectData: PostProjectObject): Promise<number> {
const projectId = await this.insertProject(postProjectData);

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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<any>[] = [];

Expand All @@ -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<void> {
Expand Down
45 changes: 43 additions & 2 deletions api/src/services/survey-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
});
});
46 changes: 36 additions & 10 deletions api/src/services/survey-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>}
* @return {*} {Promise<number>}
* @memberof SurveyService
*/
async createSurveyAndUploadToBiohub(projectId: number, postSurveyData: PostSurveyObject): Promise<number> {
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<number>}
* @memberof SurveyService
*/
async createSurvey(projectId: number, postSurveyData: PostSurveyObject): Promise<number> {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -487,7 +501,22 @@ export class SurveyService extends DBService {
* @returns {*} {Promise<void>}
* @memberof SurveyService
*/
async updateSurvey(surveyId: number, putSurveyData: PutSurveyObject): Promise<void> {
async updateSurveyAndUploadToBiohub(surveyId: number, putSurveyData: PutSurveyObject): Promise<void> {
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<void>}
* @memberof SurveyService
*/
async updateSurvey(surveyId: number, putSurveyData: PutSurveyObject): Promise<SurveyObject> {
const promises: Promise<any>[] = [];

if (putSurveyData?.survey_details || putSurveyData?.purpose_and_methodology || putSurveyData?.location) {
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit dfef452

Please sign in to comment.