From 0f493f059143b57a17b740c3e2b26d560bee9f07 Mon Sep 17 00:00:00 2001 From: Kjartan Date: Fri, 17 Feb 2023 11:23:41 -0800 Subject: [PATCH] main functionality of ticket functioning --- api/src/models/survey-view.ts | 2 + api/src/paths/project/create.test.ts | 3 -- api/src/paths/project/create.ts | 9 ---- .../project/{projectId}/survey/create.test.ts | 3 -- .../project/{projectId}/survey/create.ts | 9 ---- .../survey/{surveyId}/update.test.ts | 3 -- .../{projectId}/survey/{surveyId}/update.ts | 10 ----- .../paths/project/{projectId}/update.test.ts | 3 -- api/src/paths/project/{projectId}/update.ts | 9 ---- api/src/services/platform-service.ts | 41 +++++++++++-------- api/src/services/project-service.ts | 9 ++++ api/src/services/survey-service.test.ts | 21 +++++++++- api/src/services/survey-service.ts | 9 ++++ 13 files changed, 63 insertions(+), 68 deletions(-) diff --git a/api/src/models/survey-view.ts b/api/src/models/survey-view.ts index 9abc85d87d..8e10e4e5d3 100644 --- a/api/src/models/survey-view.ts +++ b/api/src/models/survey-view.ts @@ -13,6 +13,7 @@ export type SurveyObject = { export class GetSurveyData { id: number; + project_id: number; uuid: string; survey_name: string; start_date: string; @@ -25,6 +26,7 @@ export class GetSurveyData { constructor(obj?: any) { this.id = obj?.survey_id || null; + this.project_id = obj?.project_id || null; this.uuid = obj?.uuid || null; this.survey_name = obj?.name || ''; this.start_date = obj?.start_date || null; diff --git a/api/src/paths/project/create.test.ts b/api/src/paths/project/create.test.ts index 8a6f33a0b1..61e75615c3 100644 --- a/api/src/paths/project/create.test.ts +++ b/api/src/paths/project/create.test.ts @@ -5,7 +5,6 @@ import sinon from 'sinon'; import sinonChai from 'sinon-chai'; import * as db from '../../database/db'; import { HTTPError } from '../../errors/http-error'; -import { PlatformService } from '../../services/platform-service'; import { ProjectService } from '../../services/project-service'; import { getMockDBConnection, getRequestHandlerMocks } from '../../__mocks__/db'; import { createProject, POST } from './create'; @@ -33,8 +32,6 @@ describe('create', () => { sinon.stub(ProjectService.prototype, 'createProject').resolves(1); - sinon.stub(PlatformService.prototype, 'submitDwCAMetadataPackage').resolves(); - const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); try { diff --git a/api/src/paths/project/create.ts b/api/src/paths/project/create.ts index bb363f8d79..c0f00174d2 100644 --- a/api/src/paths/project/create.ts +++ b/api/src/paths/project/create.ts @@ -5,7 +5,6 @@ import { getDBConnection } from '../../database/db'; import { PostProjectObject } from '../../models/project-create'; import { projectCreatePostRequestObject, projectIdResponseObject } from '../../openapi/schemas/project'; import { authorizeRequestHandler } from '../../request-handlers/security/authorization'; -import { PlatformService } from '../../services/platform-service'; import { ProjectService } from '../../services/project-service'; import { getLogger } from '../../utils/logger'; @@ -90,14 +89,6 @@ export function createProject(): RequestHandler { const projectId = await projectService.createProject(sanitizedProjectPostData); - try { - const platformService = new PlatformService(connection); - await platformService.submitDwCAMetadataPackage(projectId); - } catch (error) { - // Don't fail the rest of the endpoint if submitting metadata fails - defaultLog.error({ label: 'createProject->submitDwCAMetadataPackage', message: 'error', error }); - } - await connection.commit(); return res.status(200).json({ id: projectId }); diff --git a/api/src/paths/project/{projectId}/survey/create.test.ts b/api/src/paths/project/{projectId}/survey/create.test.ts index d21ca4f184..9db1df370d 100644 --- a/api/src/paths/project/{projectId}/survey/create.test.ts +++ b/api/src/paths/project/{projectId}/survey/create.test.ts @@ -4,7 +4,6 @@ import sinon from 'sinon'; import sinonChai from 'sinon-chai'; import * as db from '../../../../database/db'; import { HTTPError } from '../../../../errors/http-error'; -import { PlatformService } from '../../../../services/platform-service'; import { SurveyService } from '../../../../services/survey-service'; import { getMockDBConnection, getRequestHandlerMocks } from '../../../../__mocks__/db'; import { createSurvey } from './create'; @@ -24,8 +23,6 @@ describe('survey/create', () => { sinon.stub(SurveyService.prototype, 'createSurvey').resolves(2); - sinon.stub(PlatformService.prototype, 'submitDwCAMetadataPackage').resolves(); - const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); mockReq.params = { projectId: '1' }; diff --git a/api/src/paths/project/{projectId}/survey/create.ts b/api/src/paths/project/{projectId}/survey/create.ts index 67fccee3f8..24950c3753 100644 --- a/api/src/paths/project/{projectId}/survey/create.ts +++ b/api/src/paths/project/{projectId}/survey/create.ts @@ -5,7 +5,6 @@ import { getDBConnection } from '../../../../database/db'; import { PostSurveyObject } from '../../../../models/survey-create'; import { geoJsonFeature } from '../../../../openapi/schemas/geoJson'; import { authorizeRequestHandler } from '../../../../request-handlers/security/authorization'; -import { PlatformService } from '../../../../services/platform-service'; import { SurveyService } from '../../../../services/survey-service'; import { getLogger } from '../../../../utils/logger'; @@ -251,14 +250,6 @@ export function createSurvey(): RequestHandler { const surveyId = await surveyService.createSurvey(projectId, sanitizedPostSurveyData); - try { - const platformService = new PlatformService(connection); - await platformService.submitDwCAMetadataPackage(projectId); - } catch (error) { - // Don't fail the rest of the endpoint if submitting metadata fails - defaultLog.error({ label: 'createSurvey->submitDwCAMetadataPackage', message: 'error', error }); - } - await connection.commit(); return res.status(200).json({ id: surveyId }); 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 86d143b8f7..fc80acd387 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/update.test.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/update.test.ts @@ -4,7 +4,6 @@ import sinon from 'sinon'; import sinonChai from 'sinon-chai'; import * as db from '../../../../../database/db'; import { HTTPError } from '../../../../../errors/http-error'; -import { PlatformService } from '../../../../../services/platform-service'; import { SurveyService } from '../../../../../services/survey-service'; import { getMockDBConnection, getRequestHandlerMocks } from '../../../../../__mocks__/db'; import { updateSurvey } from './update'; @@ -23,8 +22,6 @@ describe('updateSurvey', () => { sinon.stub(SurveyService.prototype, 'updateSurvey').resolves(); - sinon.stub(PlatformService.prototype, 'submitDwCAMetadataPackage').resolves(); - const { mockReq, mockRes, mockNext } = getRequestHandlerMocks(); mockReq.params = { diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts index 289a4539fd..e1a4f942d0 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts @@ -5,7 +5,6 @@ import { getDBConnection } from '../../../../../database/db'; import { PutSurveyObject } from '../../../../../models/survey-update'; import { geoJsonFeature } from '../../../../../openapi/schemas/geoJson'; import { authorizeRequestHandler } from '../../../../../request-handlers/security/authorization'; -import { PlatformService } from '../../../../../services/platform-service'; import { SurveyService } from '../../../../../services/survey-service'; import { getLogger } from '../../../../../utils/logger'; @@ -275,7 +274,6 @@ PUT.apiDoc = { export function updateSurvey(): RequestHandler { return async (req, res) => { - const projectId = Number(req.params.projectId); const surveyId = Number(req.params.surveyId); const sanitizedPutSurveyData = new PutSurveyObject(req.body); @@ -289,14 +287,6 @@ export function updateSurvey(): RequestHandler { await surveyService.updateSurvey(surveyId, sanitizedPutSurveyData); - try { - const platformService = new PlatformService(connection); - await platformService.submitDwCAMetadataPackage(projectId); - } catch (error) { - // Don't fail the rest of the endpoint if submitting metadata fails - defaultLog.error({ label: 'updateSurvey->submitDwCAMetadataPackage', message: 'error', error }); - } - await connection.commit(); return res.status(200).json({ id: surveyId }); diff --git a/api/src/paths/project/{projectId}/update.test.ts b/api/src/paths/project/{projectId}/update.test.ts index 091bc841ff..69f50c069f 100644 --- a/api/src/paths/project/{projectId}/update.test.ts +++ b/api/src/paths/project/{projectId}/update.test.ts @@ -4,7 +4,6 @@ import sinon from 'sinon'; import sinonChai from 'sinon-chai'; import * as db from '../../../database/db'; import { HTTPError } from '../../../errors/http-error'; -import { PlatformService } from '../../../services/platform-service'; import { ProjectService } from '../../../services/project-service'; import { getMockDBConnection, getRequestHandlerMocks } from '../../../__mocks__/db'; import * as update from './update'; @@ -157,8 +156,6 @@ describe('update', () => { sinon.stub(ProjectService.prototype, 'updateProject').resolves(); - sinon.stub(PlatformService.prototype, 'submitDwCAMetadataPackage').resolves(); - const requestHandler = update.updateProject(); await requestHandler(mockReq, mockRes, mockNext); diff --git a/api/src/paths/project/{projectId}/update.ts b/api/src/paths/project/{projectId}/update.ts index 279a5f0388..ef723398fe 100644 --- a/api/src/paths/project/{projectId}/update.ts +++ b/api/src/paths/project/{projectId}/update.ts @@ -6,7 +6,6 @@ import { HTTP400 } from '../../../errors/http-error'; import { geoJsonFeature } from '../../../openapi/schemas/geoJson'; import { projectIdResponseObject, projectUpdatePutRequestObject } from '../../../openapi/schemas/project'; import { authorizeRequestHandler } from '../../../request-handlers/security/authorization'; -import { PlatformService } from '../../../services/platform-service'; import { ProjectService } from '../../../services/project-service'; import { getLogger } from '../../../utils/logger'; @@ -439,14 +438,6 @@ export function updateProject(): RequestHandler { await projectService.updateProject(projectId, entities); - try { - const platformService = new PlatformService(connection); - await platformService.submitDwCAMetadataPackage(projectId); - } catch (error) { - // Don't fail the rest of the endpoint if submitting metadata fails - defaultLog.error({ label: 'updateProject->submitDwCAMetadataPackage', message: 'error', error }); - } - await connection.commit(); return res.status(200).json({ id: projectId }); diff --git a/api/src/services/platform-service.ts b/api/src/services/platform-service.ts index 4f9fcd6088..e5b8ae3f9e 100644 --- a/api/src/services/platform-service.ts +++ b/api/src/services/platform-service.ts @@ -4,6 +4,7 @@ import FormData from 'form-data'; import { URL } from 'url'; import { HTTP400 } from '../errors/http-error'; import { getFileFromS3 } from '../utils/file-utils'; +import { getLogger } from '../utils/logger'; import { DBService } from './db-service'; import { EmlService } from './eml-service'; import { KeycloakService } from './keycloak-service'; @@ -33,7 +34,7 @@ export interface IDwCADataset { export class PlatformService extends DBService { BACKBONE_INTAKE_ENABLED = process.env.BACKBONE_INTAKE_ENABLED === 'true' || false; BACKBONE_API_HOST = process.env.BACKBONE_API_HOST; - BACKBONE_INTAKE_PATH = process.env.BACKBONE_INTAKE_PATH || '/api/dwc/submission/intake'; + BACKBONE_INTAKE_PATH = process.env.BACKBONE_INTAKE_PATH || '/api/dwc/submission/queue'; /** * Submit a Darwin Core Archive (DwCA) data package, that only contains the project/survey metadata, to the BioHub @@ -50,27 +51,33 @@ export class PlatformService extends DBService { * @memberof PlatformService */ async submitDwCAMetadataPackage(projectId: number) { - if (!this.BACKBONE_INTAKE_ENABLED) { - return; - } + try { + if (!this.BACKBONE_INTAKE_ENABLED) { + return; + } - const emlService = new EmlService({ projectId: projectId }, this.connection); + const emlService = new EmlService({ projectId: projectId }, this.connection); - const emlString = await emlService.buildProjectEml(); + const emlString = await emlService.buildProjectEml(); - const dwcArchiveZip = new AdmZip(); - dwcArchiveZip.addFile('eml.xml', Buffer.from(emlString)); + const dwcArchiveZip = new AdmZip(); + dwcArchiveZip.addFile('eml.xml', Buffer.from(emlString)); - const dwCADataset = { - archiveFile: { - data: dwcArchiveZip.toBuffer(), - fileName: 'DwCA.zip', - mimeType: 'application/zip' - }, - dataPackageId: emlService.packageId - }; + const dwCADataset = { + archiveFile: { + data: dwcArchiveZip.toBuffer(), + fileName: 'DwCA.zip', + mimeType: 'application/zip' + }, + dataPackageId: emlService.packageId + }; - return this._submitDwCADatasetToBioHubBackbone(dwCADataset); + return this._submitDwCADatasetToBioHubBackbone(dwCADataset); + } catch (error) { + const defaultLog = getLogger('platformService->submitDwCAMetadataPackage'); + // Don't fail the rest of the endpoint if submitting metadata fails + defaultLog.error({ label: 'platformService->submitDwCAMetadataPackage', message: 'error', error }); + } } /** diff --git a/api/src/services/project-service.ts b/api/src/services/project-service.ts index 84f00728ac..e45a9a1f02 100644 --- a/api/src/services/project-service.ts +++ b/api/src/services/project-service.ts @@ -32,16 +32,19 @@ import { ProjectRepository } from '../repositories/project-repository'; import { deleteFileFromS3 } from '../utils/file-utils'; import { AttachmentService } from './attachment-service'; import { DBService } from './db-service'; +import { PlatformService } from './platform-service'; import { SurveyService } from './survey-service'; export class ProjectService extends DBService { attachmentService: AttachmentService; projectRepository: ProjectRepository; + platformService: PlatformService; constructor(connection: IDBConnection) { super(connection); this.attachmentService = new AttachmentService(connection); this.projectRepository = new ProjectRepository(connection); + this.platformService = new PlatformService(connection); } /** @@ -338,6 +341,9 @@ 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 + await this.platformService.submitDwCAMetadataPackage(projectId); + return projectId; } @@ -389,6 +395,9 @@ export class ProjectService extends DBService { } await Promise.all(promises); + + //Update Eml to biohub + await this.platformService.submitDwCAMetadataPackage(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 648c0bf2e6..b725e72f51 100644 --- a/api/src/services/survey-service.test.ts +++ b/api/src/services/survey-service.test.ts @@ -16,7 +16,8 @@ import { GetSurveyFundingSources, GetSurveyLocationData, GetSurveyProprietorData, - GetSurveyPurposeAndMethodologyData + GetSurveyPurposeAndMethodologyData, + SurveyObject } from '../models/survey-view'; import { IPermitModel } from '../repositories/permit-repository'; import { @@ -26,6 +27,7 @@ import { } from '../repositories/survey-repository'; import { getMockDBConnection } from '../__mocks__/db'; import { PermitService } from './permit-service'; +import { PlatformService } from './platform-service'; import { SurveyService } from './survey-service'; import { TaxonomyService } from './taxonomy-service'; @@ -108,6 +110,12 @@ describe('SurveyService', () => { const updateSurveyProprietorDataStub = sinon .stub(SurveyService.prototype, 'updateSurveyProprietorData') .resolves(); + const getSurveyByIdStub = sinon + .stub(SurveyService.prototype, 'getSurveyById') + .resolves(({ survey_details: { project_id: 1 } } as unknown) as SurveyObject); + const submitDwCAMetadataPackageStub = sinon + .stub(PlatformService.prototype, 'submitDwCAMetadataPackage') + .resolves(); const surveyService = new SurveyService(dbConnectionObj); @@ -122,6 +130,8 @@ describe('SurveyService', () => { expect(updateSurveyPermitDataStub).not.to.have.been.called; expect(updateSurveyFundingDataStub).not.to.have.been.called; expect(updateSurveyProprietorDataStub).not.to.have.been.called; + expect(getSurveyByIdStub).to.have.been.called; + expect(submitDwCAMetadataPackageStub).to.have.been.called; }); it('updates everything when all data provided', async () => { @@ -137,7 +147,12 @@ describe('SurveyService', () => { const updateSurveyProprietorDataStub = sinon .stub(SurveyService.prototype, 'updateSurveyProprietorData') .resolves(); - + const getSurveyByIdStub = sinon + .stub(SurveyService.prototype, 'getSurveyById') + .resolves(({ survey_details: { project_id: 1 } } as unknown) as SurveyObject); + const submitDwCAMetadataPackageStub = sinon + .stub(PlatformService.prototype, 'submitDwCAMetadataPackage') + .resolves(); const surveyService = new SurveyService(dbConnectionObj); const surveyId = 2; @@ -159,6 +174,8 @@ describe('SurveyService', () => { expect(updateSurveyPermitDataStub).to.have.been.calledOnce; expect(updateSurveyFundingDataStub).to.have.been.calledOnce; expect(updateSurveyProprietorDataStub).to.have.been.calledOnce; + expect(getSurveyByIdStub).to.have.been.called; + expect(submitDwCAMetadataPackageStub).to.have.been.called; }); }); diff --git a/api/src/services/survey-service.ts b/api/src/services/survey-service.ts index 620ad5f98f..bfa4c12c79 100644 --- a/api/src/services/survey-service.ts +++ b/api/src/services/survey-service.ts @@ -27,6 +27,7 @@ import { import { getLogger } from '../utils/logger'; import { DBService } from './db-service'; import { PermitService } from './permit-service'; +import { PlatformService } from './platform-service'; import { TaxonomyService } from './taxonomy-service'; const defaultLog = getLogger('services/survey-service'); @@ -41,12 +42,14 @@ export interface IMessageTypeGroup { export class SurveyService extends DBService { attachmentRepository: AttachmentRepository; surveyRepository: SurveyRepository; + platformService: PlatformService; constructor(connection: IDBConnection) { super(connection); this.attachmentRepository = new AttachmentRepository(connection); this.surveyRepository = new SurveyRepository(connection); + this.platformService = new PlatformService(connection); } async getSurveyIdsByProjectId(projectId: number): Promise<{ id: number }[]> { @@ -246,6 +249,8 @@ export class SurveyService extends DBService { await Promise.all(promises); + await this.platformService.submitDwCAMetadataPackage(projectId); + return surveyId; } @@ -323,6 +328,10 @@ export class SurveyService extends DBService { } await Promise.all(promises); + + const surveyData = await this.getSurveyById(surveyId); + + await this.platformService.submitDwCAMetadataPackage(surveyData.survey_details.project_id); } async updateSurveyDetailsData(surveyId: number, surveyData: PutSurveyObject) {