Skip to content

Commit

Permalink
BHBC-1363 (#599)
Browse files Browse the repository at this point in the history
Support project/survey report attachments with meta
  • Loading branch information
anissa-agahchen authored Oct 27, 2021
1 parent 475b338 commit 2b239d6
Show file tree
Hide file tree
Showing 49 changed files with 2,477 additions and 580 deletions.
2 changes: 1 addition & 1 deletion .sonarcloud.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sonar.projectName=BioHubBC
sonar.projectVersion=Autoscan

# Path to sources
sonar.sources=app/src,api/src,database
sonar.sources=app/src,api/src
sonar.exclusions=**/*.test.*,**/*.spec.*,**/__tests__/**,**/__snapshots__/**,**/constants/**,**/shared-api-docs.ts
#sonar.inclusions=

Expand Down
4 changes: 4 additions & 0 deletions api/src/constants/attachments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum ATTACHMENT_TYPE {
REPORT = 'Report',
OTHER = 'Other'
}
29 changes: 29 additions & 0 deletions api/src/models/project-survey-attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,32 @@ export class GetAttachmentsData {
[];
}
}

export interface ReportAttachmentAuthor {
first_name: string;
last_name: string;
}

export class PostReportAttachmentMetadata {
title: string;
year_published: string;
authors: ReportAttachmentAuthor[];
description: string;

constructor(obj?: any) {
this.title = (obj && obj?.title) || null;
this.year_published = (obj && obj?.year_published) || null;
this.authors = (obj?.authors?.length && obj.authors) || [];
this.description = (obj && obj?.description) || null;
}
}

export class PutReportAttachmentMetadata extends PostReportAttachmentMetadata {
revision_count: number;

constructor(obj?: any) {
super(obj);

this.revision_count = (obj && obj?.revision_count) || null;
}
}
65 changes: 1 addition & 64 deletions api/src/paths/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,8 @@ import {
PostProjectObject
} from '../models/project-create';
import { projectCreatePostRequestObject, projectIdResponseObject } from '../openapi/schemas/project';
import { associatePermitToProjectSQL } from '../queries/permit/permit-update-queries';
import { postProjectPermitSQL } from '../queries/permit/permit-create-queries';
import {
getProjectAttachmentByFileNameSQL,
postProjectAttachmentSQL,
postProjectReportAttachmentSQL,
putProjectAttachmentSQL,
putProjectReportAttachmentSQL
} from '../queries/project/project-attachments-queries';
import { associatePermitToProjectSQL } from '../queries/permit/permit-update-queries';
import {
postProjectActivitySQL,
postProjectFundingSourceSQL,
Expand All @@ -28,7 +21,6 @@ import {
postProjectSQL,
postProjectStakeholderPartnershipSQL
} from '../queries/project/project-create-queries';
import { generateS3FileKey } from '../utils/file-utils';
import { getLogger } from '../utils/logger';
import { logRequest } from '../utils/path-utils';

Expand Down Expand Up @@ -363,58 +355,3 @@ export const insertProjectActivity = async (

return result.id;
};

export const upsertProjectAttachment = async (
file: Express.Multer.File,
projectId: number,
attachmentType: string,
connection: IDBConnection
): Promise<number> => {
const getSqlStatement = getProjectAttachmentByFileNameSQL(projectId, file.originalname);

if (!getSqlStatement) {
throw new HTTP400('Failed to build SQL get statement');
}

const getResponse = await connection.query(getSqlStatement.text, getSqlStatement.values);

if (getResponse && getResponse.rowCount > 0) {
const updateSqlStatement =
attachmentType === 'Report'
? putProjectReportAttachmentSQL(projectId, file.originalname)
: putProjectAttachmentSQL(projectId, file.originalname, attachmentType);

if (!updateSqlStatement) {
throw new HTTP400('Failed to build SQL update statement');
}

const updateResponse = await connection.query(updateSqlStatement.text, updateSqlStatement.values);
const updateResult = (updateResponse && updateResponse.rowCount) || null;

if (!updateResult) {
throw new HTTP400('Failed to update project attachment data');
}

return updateResult;
}

const key = generateS3FileKey({ projectId: projectId, fileName: file.originalname });

const insertSqlStatement =
attachmentType === 'Report'
? postProjectReportAttachmentSQL(file.originalname, file.size, projectId, key)
: postProjectAttachmentSQL(file.originalname, file.size, attachmentType, projectId, key);

if (!insertSqlStatement) {
throw new HTTP400('Failed to build SQL insert statement');
}

const insertResponse = await connection.query(insertSqlStatement.text, insertSqlStatement.values);
const insertResult = (insertResponse && insertResponse.rows && insertResponse.rows[0]) || null;

if (!insertResult || !insertResult.id) {
throw new HTTP400('Failed to insert project attachment data');
}

return insertResult.id;
};
9 changes: 4 additions & 5 deletions api/src/paths/project/{projectId}/attachments/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { describe } from 'mocha';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import * as upload from './upload';
import * as project from '../../../project';
import * as db from '../../../../database/db';
import * as file_utils from '../../../../utils/file-utils';
import { getMockDBConnection } from '../../../../__mocks__/db';
Expand Down Expand Up @@ -129,7 +128,7 @@ describe('uploadMedia', () => {
});

sinon.stub(file_utils, 'uploadFileToS3').resolves({ Key: '1/1/test.txt' } as any);
sinon.stub(project, 'upsertProjectAttachment').resolves(1);
sinon.stub(upload, 'upsertProjectAttachment').resolves({ id: 1, revision_count: 0, key: 'key' });
sinon.stub(file_utils, 'scanFileForVirus').resolves(false);

try {
Expand All @@ -143,7 +142,7 @@ describe('uploadMedia', () => {
}
});

it('should return file key on success (with username and email)', async () => {
it('should return id and revision_count on success (with username and email)', async () => {
sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
Expand All @@ -153,12 +152,12 @@ describe('uploadMedia', () => {

sinon.stub(file_utils, 'scanFileForVirus').resolves(true);
sinon.stub(file_utils, 'uploadFileToS3').resolves({ Key: '1/1/test.txt' } as any);
sinon.stub(project, 'upsertProjectAttachment').resolves(1);
sinon.stub(upload, 'upsertProjectAttachment').resolves({ id: 1, revision_count: 0, key: 'key' });

const result = upload.uploadMedia();

await result(sampleReq, sampleRes as any, (null as unknown) as any);

expect(actualResult).to.eql('1/1/test.txt');
expect(actualResult).to.eql({ attachmentId: 1, revision_count: 0 });
});
});
Loading

0 comments on commit 2b239d6

Please sign in to comment.